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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
* -------------------------------------------------------------------------
*/

use Glpi\DBAL\QuerySubQuery;

use function Safe\mkdir;

function plugin_datainjection_install()
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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',
],
];
}
103 changes: 103 additions & 0 deletions tests/unit/ModelEntityRelationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

/**
* -------------------------------------------------------------------------
* DataInjection plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of DataInjection.
*
* DataInjection 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.
*
* DataInjection 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 DataInjection. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @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']);
}
}