diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e33be7f..b96bb6d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [Unreleased] + +### Fixed + +- Fix models left in a deleted entity: declare their entity relation so it is checked and replaced on entity deletion, and move models already left in a deleted entity to the root entity + ## [2.15.13] - 2026-11-13 ### Fixed diff --git a/hook.php b/hook.php index 7002b863..8b4f793e 100644 --- a/hook.php +++ b/hook.php @@ -28,6 +28,8 @@ * ------------------------------------------------------------------------- */ +use Glpi\DBAL\QuerySubQuery; + use function Safe\mkdir; function plugin_datainjection_install() @@ -55,6 +57,7 @@ function plugin_datainjection_install() plugin_datainjection_migration_2141_2150($migration); plugin_datainjection_migration_2158_2159($migration); plugin_datainjection_migration_21512_21513($migration); + plugin_datainjection_migration_21513_21514($migration); break; case 0: @@ -180,6 +183,7 @@ function plugin_datainjection_install() plugin_datainjection_migration_2141_2150($migration); plugin_datainjection_migration_2158_2159($migration); plugin_datainjection_migration_21512_21513($migration); + plugin_datainjection_migration_21513_21514($migration); break; default: @@ -223,6 +227,28 @@ function plugin_datainjection_uninstall() return true; } +function plugin_datainjection_migration_21513_21514(Migration $migration) +{ + /** @var DBmysql $DB */ + global $DB; + + // Models left in a deleted entity can no longer be opened: move them to the root entity + $DB->update( + 'glpi_plugin_datainjection_models', + ['entities_id' => 0], + [ + 'NOT' => [ + 'entities_id' => new QuerySubQuery([ + 'SELECT' => 'id', + 'FROM' => Entity::getTable(), + ]), + ], + ], + ); + + $migration->executeMigration(); +} + function plugin_datainjection_migration_21512_21513(Migration $migration) { // Models are always read by `models_id`: without index, every read scans the whole table @@ -2024,3 +2050,17 @@ function plugin_datainjection_addDefaultWhere($itemtype) return null; } + +/** + * Declare foreign keys to core tables, so that core usage checks and replacements take them into account + * + * @return array + */ +function plugin_datainjection_getDatabaseRelations() +{ + return [ + 'glpi_entities' => [ + 'glpi_plugin_datainjection_models' => 'entities_id', + ], + ]; +} diff --git a/tests/unit/ModelEntityRelationTest.php b/tests/unit/ModelEntityRelationTest.php new file mode 100644 index 00000000..741479b5 --- /dev/null +++ b/tests/unit/ModelEntityRelationTest.php @@ -0,0 +1,103 @@ +. + * ------------------------------------------------------------------------- + * @copyright Copyright (C) 2007-2023 by DataInjection plugin team. + * @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html + * @link https://github.com/pluginsGLPI/datainjection + * ------------------------------------------------------------------------- + */ + +namespace GlpiPlugin\Datainjection\Tests\Unit; + +use DBmysql; +use Computer; +use Entity; +use Migration; +use Glpi\Tests\DbTestCase; +use PluginDatainjectionModel; +use Session; + +final class ModelEntityRelationTest extends DbTestCase +{ + public function testModelIsMovedWhenItsEntityIsReplaced(): void + { + $this->login(); + $root_id = getItemByTypeName(Entity::class, '_test_root_entity', true); + + $entity = $this->createItem(Entity::class, [ + 'name' => __FUNCTION__, + 'entities_id' => $root_id, + ]); + $model = $this->createItem(PluginDatainjectionModel::class, [ + 'name' => __FUNCTION__, + 'itemtype' => Computer::class, + 'filetype' => 'csv', + 'behavior_add' => 1, + 'entities_id' => $entity->getID(), + 'is_recursive' => 0, + 'is_private' => 0, + 'users_id' => Session::getLoginUserID(), + ]); + + $this->assertTrue($entity->isUsed()); + + $this->assertTrue($entity->delete(['id' => $entity->getID(), '_replace_by' => $root_id], true)); + $this->assertTrue($model->getFromDB($model->getID())); + $this->assertSame($root_id, $model->fields['entities_id']); + } + + public function testMigrationMovesOrphanModelsToRootEntity(): void + { + /** @var DBmysql $DB */ + global $DB; + + $this->login(); + $entity_id = getItemByTypeName(Entity::class, '_test_child_1', true); + + $orphan = $this->createItem(PluginDatainjectionModel::class, [ + 'name' => __FUNCTION__ . '_orphan', + 'itemtype' => Computer::class, + 'filetype' => 'csv', + 'behavior_add' => 1, + 'entities_id' => $entity_id, + 'users_id' => Session::getLoginUserID(), + ]); + $valid = $this->createItem(PluginDatainjectionModel::class, [ + 'name' => __FUNCTION__ . '_valid', + 'itemtype' => Computer::class, + 'filetype' => 'csv', + 'behavior_add' => 1, + 'entities_id' => $entity_id, + 'users_id' => Session::getLoginUserID(), + ]); + $DB->update(PluginDatainjectionModel::getTable(), ['entities_id' => 999999], ['id' => $orphan->getID()]); + + plugin_datainjection_migration_21513_21514(new Migration(PLUGIN_DATAINJECTION_VERSION)); + + $this->assertTrue($orphan->getFromDB($orphan->getID())); + $this->assertSame(0, $orphan->fields['entities_id']); + $this->assertTrue($valid->getFromDB($valid->getID())); + $this->assertSame($entity_id, $valid->fields['entities_id']); + } +}