Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Fix fields values updates not applying the expected checks in some cases
- Fix ticket observers being able to edit and save additional fields they are not allowed to modify
- Fix `Yes/No` field values returned as strings instead of integers in search results
- Fix fatal error during plugin update caused by outdated generated dropdown classes

## [1.25.0] - 2026-10-06

Expand Down
4 changes: 4 additions & 0 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ function plugin_fields_install()

$version = $plugin_fields->fields['version'];

// Remove generated classes before migration, they may be incompatible with current GLPI version
// and would trigger a fatal error if autoloaded before being regenerated.
// They will be regenerated by `installUserData()` methods.
array_map(unlink(...), glob(PLUGINFIELDS_CLASS_PATH . '/*.class.php') ?: []);

$migration = new Migration($version);
if (isCommandLine()) {
Expand Down
117 changes: 117 additions & 0 deletions tests/Units/InstallOutdatedGeneratedFilesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

/**
* -------------------------------------------------------------------------
* Fields plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of Fields.
*
* Fields is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Fields is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Fields. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2013-2023 by Fields plugin team.
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
* @link https://github.com/pluginsGLPI/fields
* -------------------------------------------------------------------------
*/

declare(strict_types=1);

namespace GlpiPlugin\Field\Tests\Units;

use Computer;
use DBmysql;
use Glpi\Tests\DbTestCase;
use Glpi\Tests\GLPITestCase;
use GlpiPlugin\Field\Tests\FieldTestTrait;
use PluginFieldsContainer;
use PluginFieldsField;

require_once __DIR__ . '/../FieldTestCase.php';
require_once __DIR__ . '/../../hook.php';

final class InstallOutdatedGeneratedFilesTest extends DbTestCase
{
use FieldTestTrait;

public function setUp(): void
{
GLPITestCase::setUp();
$this->login();
}

public function tearDown(): void
{
$this->tearDownFieldTest();
GLPITestCase::tearDown();
}

public function testInstallDoesNotLoadOutdatedGeneratedDropdownClass(): void
{
/** @var DBmysql $DB */
global $DB;

$container = $this->createFieldContainer([
'label' => 'Outdated files container',
'type' => 'tab',
'itemtypes' => [Computer::class],
'is_active' => 1,
'entities_id' => 0,
]);

$field = $this->createField([
'label' => 'Outdated dropdown',
'type' => 'dropdown',
PluginFieldsContainer::getForeignKeyField() => $container->getID(),
'ranking' => 1,
'is_active' => 1,
'is_readonly' => 0,
]);
$this->assertSame('outdateddropdownfield', $field->fields['name']);

// Simulate a dropdown generated by a previous plugin version, with a system name that does not match
// its label anymore (it will be renamed by the migration) and a class that has not been loaded yet.
$old_name = 'notloadedyetfield';
$container_classname = PluginFieldsContainer::getClassname(Computer::class, $container->fields['name']);
$container_table = getTableForItemType($container_classname);
$container_class_file = PLUGINFIELDS_CLASS_PATH . '/' . strtolower(substr($container_classname, strlen('PluginFields'))) . '.class.php';
$DB->update(PluginFieldsField::getTable(), ['name' => $old_name], ['id' => $field->getID()]);
$DB->doQuery('RENAME TABLE `glpi_plugin_fields_outdateddropdownfielddropdowns` TO `glpi_plugin_fields_notloadedyetfielddropdowns`');
$DB->doQuery(
"ALTER TABLE `{$container_table}` CHANGE `plugin_fields_outdateddropdownfielddropdowns_id` `plugin_fields_notloadedyetfielddropdowns_id` INT UNSIGNED DEFAULT NULL",
);

// Outdated class file, that would trigger a fatal error if loaded (e.g. incompatible property declaration).
$outdated_file = PLUGINFIELDS_CLASS_PATH . '/' . $old_name . 'dropdown.class.php';
file_put_contents($outdated_file, '<?php throw new \RuntimeException("Outdated generated class has been loaded.");');
$this->assertFileExists($container_class_file);

ob_start();
$result = plugin_fields_install();
ob_end_clean();
$DB->clearSchemaCache();

$this->assertTrue($result);

// Outdated file has been removed, dropdown has been renamed and its files regenerated
$this->assertFileDoesNotExist($outdated_file);
$this->assertTrue($field->getFromDB($field->getID()));
$this->assertSame('outdateddropdownfield', $field->fields['name']);
$this->assertTrue($DB->tableExists('glpi_plugin_fields_outdateddropdownfielddropdowns'));
$this->assertFileExists(PLUGINFIELDS_CLASS_PATH . '/outdateddropdownfielddropdown.class.php');
$this->assertFileExists($container_class_file);
}
}
Loading