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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Fix `Some mandatory fields are empty` error on a filled dropdown field when its block table contains a leftover column with the same name
- Fix GenericObject migration error message to name the required 3.0.x version
- Fix sort on number fields: values were sorted as text (`1000`, `12345`, `1500`, `999`)
- Fix blocks and dropdown values left in a deleted entity: declare their entity relation so it is checked and replaced on entity deletion

## [1.24.7] - 2026-10-28

Expand Down
30 changes: 30 additions & 0 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,36 @@ function plugin_fields_getDropdown()
return $dropdowns;
}

/**
* Declare foreign keys to core tables, so that core usage checks and replacements take them into account.
* Values tables are not declared: their entity is a copy of the parent item one, kept in sync by the transfer hook.
*
* @return array
*/
function plugin_fields_getDatabaseRelations()
{
/** @var DBmysql $DB */
global $DB;

if (!$DB->tableExists(PluginFieldsField::getTable())) {
return [];
}

$entity_relations = [
PluginFieldsContainer::getTable() => 'entities_id',
];
foreach ((new PluginFieldsField())->find(['type' => 'dropdown']) as $field) {
// Core warns on every relations lookup when a declared table has no loadable class
$dropdown_class = PluginFieldsDropdown::getClassname($field['name']);
$dropdown_table = getTableForItemType($dropdown_class);
if (class_exists($dropdown_class) && $DB->tableExists($dropdown_table)) {
$entity_relations[$dropdown_table] = 'entities_id';
}
}

return ['glpi_entities' => $entity_relations];
}


/**** MASSIVE ACTIONS ****/

Expand Down
101 changes: 101 additions & 0 deletions tests/Units/EntityRelationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?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
* -------------------------------------------------------------------------
*/

namespace GlpiPlugin\Field\Tests\Units;

use Computer;
use Entity;
use Glpi\Tests\DbTestCase;
use Glpi\Tests\GLPITestCase;
use GlpiPlugin\Field\Tests\FieldTestTrait;
use PluginFieldsContainer;
use PluginFieldsDropdown;

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

final class EntityRelationTest extends DbTestCase
{
use FieldTestTrait;

public function setUp(): void
{
GLPITestCase::setUp();
}

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

public function testContainerAndDropdownValuesAreMovedWhenTheirEntityIsReplaced(): void
{
$this->login();
$root_id = getItemByTypeName(Entity::class, '_test_root_entity', true);

$entity = $this->createItem(Entity::class, [
'name' => __FUNCTION__ . ' ' . $this->getUniqueString(),
'entities_id' => $root_id,
]);

// Short label: the values table name is derived from it and limited to 64 chars
$container = $this->createFieldContainer([
'label' => 'Entity relation ' . $this->getUniqueString(),
'type' => 'tab',
'itemtypes' => [Computer::class],
'is_active' => 1,
'entities_id' => $entity->getID(),
'is_recursive' => 1,
]);
$field = $this->createField([
'label' => 'Entity relation dropdown',
'type' => 'dropdown',
PluginFieldsContainer::getForeignKeyField() => $container->getID(),
'ranking' => 1,
'is_active' => 1,
'is_readonly' => 0,
], ['default_value']);

$dropdown_class = PluginFieldsDropdown::getClassname($field->fields['name']);
$dropdown_value = $this->createItem($dropdown_class, [
'name' => __FUNCTION__,
'entities_id' => $entity->getID(),
]);

$this->assertTrue($entity->isUsed());

$this->assertTrue($entity->delete(['id' => $entity->getID(), '_replace_by' => $root_id], true));

$this->assertTrue($container->getFromDB($container->getID()));
$this->assertSame($root_id, $container->fields['entities_id']);
$this->assertTrue($dropdown_value->getFromDB($dropdown_value->getID()));
$this->assertSame($root_id, $dropdown_value->fields['entities_id']);
}
}