Skip to content
Open
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
26 changes: 26 additions & 0 deletions src/Glpi/Asset/AssetDefinitionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,32 @@ public function getCapacity(string $classname): ?CapacityInterface
return $this->capacities[$classname] ?? null;
}

public function getAssetClassNameFromLegacyClassName(string $legacy): ?string
{
/** @var \DBmysql $DB */
global $DB;

// Try to load glpi_plugin_genericobject_types row for this legacy item
if (!$DB->tableExists('glpi_plugin_genericobject_types')) {
return null;
}
$rows = $DB->request([
'FROM' => 'glpi_plugin_genericobject_types',
'WHERE' => ['itemtype' => $legacy],
]);
if (count($rows) !== 1) {
return null;
}

$row = $rows->current();
$name = $row['name'];
return AssetDefinition::getCustomObjectNamespace()
. "\\"
. $name
. AssetDefinition::getCustomObjectClassSuffix()
;
}

private function loadConcreteClass(AssetDefinition $definition): void
{
$rightname = $definition->getCustomObjectRightname();
Expand Down
10 changes: 9 additions & 1 deletion src/Glpi/Form/QuestionType/QuestionTypeItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use DbUtils;
use Dropdown;
use Glpi\Application\View\TemplateRenderer;
use Glpi\Asset\AssetDefinitionManager;
use Glpi\DBAL\JsonFieldInterface;
use Glpi\Form\Condition\ConditionHandler\ItemAsTextConditionHandler;
use Glpi\Form\Condition\ConditionHandler\ItemConditionHandler;
Expand Down Expand Up @@ -132,8 +133,15 @@ public function convertExtraData(array $rawData): mixed
$selectable_tree_root = (bool) $values['selectable_tree_root'];
}

$itemtype = $rawData['itemtype'] ?? null;
// Replace generic object name with asset name if migrated
if (str_starts_with($itemtype, 'PluginGenericobject')) {
$manager = AssetDefinitionManager::getInstance();
$itemtype = $manager->getAssetClassNameFromLegacyClassName($itemtype);
}

return (new QuestionTypeItemExtraDataConfig(
itemtype: $rawData['itemtype'] ?? null,
itemtype: $itemtype,
root_items_id: $root_items_id,
subtree_depth: $subtree_depth,
selectable_tree_root: $selectable_tree_root
Expand Down
49 changes: 49 additions & 0 deletions tests/functional/Glpi/Form/Migration/FormMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
use Glpi\Form\QuestionType\QuestionTypeUrgency;
use Glpi\Form\Section;
use Glpi\Message\MessageType;
use Glpi\Migration\GenericobjectPluginMigration;
use Glpi\Migration\PluginMigrationResult;
use Glpi\Tests\FormTesterTrait;
use GlpiPlugin\Tester\Form\QuestionTypeIpConverter;
Expand All @@ -111,6 +112,13 @@ public static function setUpBeforeClass(): void
foreach ($queries as $query) {
$DB->doQuery($query);
}

// Some tests in this file also require generic objects migration so
// we also load its tables.
$queries = $DB->getQueriesFromFile(sprintf('%s/tests/fixtures/genericobject-migration/genericobject-db.sql', GLPI_ROOT));
foreach ($queries as $query) {
$DB->doQuery($query);
}
}

public static function tearDownAfterClass(): void
Expand All @@ -122,6 +130,11 @@ public static function tearDownAfterClass(): void
$DB->dropTable($table['TABLE_NAME']);
}

$tables = $DB->listTables('glpi\_plugin\_genericobject\_%');
foreach ($tables as $table) {
$DB->dropTable($table['TABLE_NAME']);
}

parent::tearDownAfterClass();
}

Expand Down Expand Up @@ -3453,6 +3466,42 @@ public function testFormWithConditionOnCategories(): void
], $condition_data->getValue());
}

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

// Arrange: create a form with a reference to generic object assets
$this->createSimpleFormcreatorForm("With generic object", [
[
'name' => 'Generic object',
'fieldtype' => 'glpiselect',
'itemtype' => "PluginGenericobjectSmartphone",
],
]);
// Migrated asset definition
$asset_migrations = new GenericobjectPluginMigration($DB);
$asset_migrations->execute();

// Act: try to import the form
$migration = new FormMigration($DB, FormAccessControlManager::getInstance());
$migration->execute();

// Assert: make sure the question was imported with the correct type
$form = getItemByTypeName(Form::class, "With generic object");
$question_id = $this->getQuestionId($form, "Generic object");
$question = Question::getById($question_id);

$config = $question->getExtraDataConfig();
if (!$config instanceof QuestionTypeItemExtraDataConfig) {
$this->fail("Unexpected config class");
}
$this->assertEquals(
"Glpi\CustomAsset\smartphoneAsset",
$config->getItemtype()
);
}

protected function createSimpleFormcreatorForm(
string $name,
array $questions,
Expand Down