diff --git a/CHANGELOG.md b/CHANGELOG.md index ef6d5ba5..d7c0ee13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed +- Fix a container creation bug that was allowing to link incompatible container types and object types - Fix missing right checks on some ajax config endpoints and escape default value and URL field output. - Fix item creation with null value for mandatory fields - Fix search crash when two containers share a dropdown field with the same name. diff --git a/inc/container.class.php b/inc/container.class.php index ea8d3ac8..ae574963 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -706,6 +706,15 @@ public function prepareInputForAdd($input) } } } + + $accepted_itemtypes = array_keys(array_merge(...array_values(self::getItemtypes(true)))); + foreach ($input['itemtypes'] as $itemtype) { + if (!in_array($itemtype, $accepted_itemtypes)) { + Session::AddMessageAfterRedirect(__("At least one selected object cannot be linked with type 'Insertion in the form of a specific tab'.", 'fields'), false, ERROR); + + return false; + } + } } $input = PluginFieldsToolbox::prepareLabel($input); @@ -1046,6 +1055,7 @@ public function showForm($ID, $options = []) self::showFormItemtype([ 'rand' => $rand, 'subtype' => $this->fields['subtype'], + 'type' => $this->fields['type'], ]); echo ''; } diff --git a/rector.php b/rector.php index 3cdf1519..0b6ade4b 100644 --- a/rector.php +++ b/rector.php @@ -1,61 +1,30 @@ . - * ------------------------------------------------------------------------- - * @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 - * ------------------------------------------------------------------------- - */ +use Rector\Configuration\RectorConfigBuilder; declare(strict_types=1); require_once __DIR__ . '/../../src/Plugin.php'; -use Rector\Caching\ValueObject\Storage\FileCacheStorage; -use Rector\Config\RectorConfig; -use Rector\ValueObject\PhpVersion; +$baseline_file = __DIR__ . '/../../PluginsRector.php'; +if (!file_exists($baseline_file)) { + throw new RuntimeException( + sprintf( + 'Unable to find "%s". Running rector on a plugin requires a GLPI development checkout that ships PluginsRector.php.', + $baseline_file, + ), + ); +} -return RectorConfig::configure() - ->withPaths([ - __DIR__ . '/ajax', - __DIR__ . '/front', - __DIR__ . '/inc', - __DIR__ . '/src', - __DIR__ . '/tests', - ]) - ->withPhpVersion(PhpVersion::PHP_82) - ->withCache( - cacheDirectory: __DIR__ . '/var/rector', - cacheClass: FileCacheStorage::class, - ) - ->withRootFiles() - ->withParallel(timeoutSeconds: 300) - ->withImportNames(removeUnusedImports: true) - ->withPreparedSets( - deadCode: true, - codeQuality: true, - codingStyle: true, - ) - ->withPhpSets(php82: true) // apply PHP sets up to PHP 8.2 -; +$baseline = require $baseline_file; + +/** @var RectorConfigBuilder $config */ +$config = $baseline([ + __DIR__ . '/ajax', + __DIR__ . '/front', + __DIR__ . '/inc', + __DIR__ . '/src', + __DIR__ . '/tests', +]); + +return $config; diff --git a/tests/Units/ContainerItemUpdateTest.php b/tests/Units/ContainerItemUpdateTest.php index b7a456de..587a13be 100644 --- a/tests/Units/ContainerItemUpdateTest.php +++ b/tests/Units/ContainerItemUpdateTest.php @@ -35,6 +35,8 @@ use GlpiPlugin\Field\Tests\FieldTestTrait; use PluginFieldsContainer; use Ticket; +use Entity; +use Notification; require_once __DIR__ . '/../FieldTestCase.php'; @@ -392,8 +394,8 @@ public function testCreateIsNotBlockedWhenMandatoryTabOrDomtabFieldIsMissing(): $domtab_container = $this->createFieldContainer([ 'label' => 'Mandatory Domtab Container', 'type' => 'domtab', - 'subtype' => Ticket::class . '$1', - 'itemtypes' => [Ticket::class], + 'subtype' => Notification::class . '$1', + 'itemtypes' => [Entity::class], 'is_active' => 1, 'entities_id' => 0, 'is_recursive' => 1, diff --git a/tests/Units/ContainerTest.php b/tests/Units/ContainerTest.php index 74cdc500..9d7c2cd1 100644 --- a/tests/Units/ContainerTest.php +++ b/tests/Units/ContainerTest.php @@ -38,6 +38,7 @@ use GlpiPlugin\Field\Tests\FieldTestTrait; use PHPUnit\Framework\Attributes\DataProvider; use PluginFieldsContainer; +use Ticket; require_once __DIR__ . '/../FieldTestCase.php'; @@ -108,4 +109,19 @@ public function testAddWithValidItemtypesSucceeds(): void $this->assertGreaterThan(0, $container->getID()); } + + public function testAddDomtabWithIncompatibleItemtypeIsRejected(): void + { + $container = new PluginFieldsContainer(); + $result = $container->add([ + 'label' => 'Domtab with invalid item type', + 'type' => 'domtab', + 'subtype' => '', + 'itemtypes' => [Ticket::class], + 'is_active' => 1, + 'entities_id' => 0, + 'is_recursive' => 1, + ]); + $this->assertFalse($result); + } }