Skip to content

Commit c220b99

Browse files
[Fix]: A bug that was giving the possibility to link incompatibles container types and objects types (#1231)
* fix: A bug that make the user able to link a container type and a object that are not normaly linkable * chore: update Changelog * Apply suggestion from @Rom1-B Co-authored-by: Romain B. <8530352+Rom1-B@users.noreply.github.com> * Update inc/container.class.php Co-authored-by: Romain B. <8530352+Rom1-B@users.noreply.github.com> * chore: Add unit test following code review * chore: update rector * Apply suggestion from @Rom1-B * Apply suggestion from @Rom1-B --------- Co-authored-by: Romain B. <8530352+Rom1-B@users.noreply.github.com>
1 parent acf23dd commit c220b99

5 files changed

Lines changed: 53 additions & 29 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Fixed
1111

12+
- Fix a container creation bug that was allowing to link incompatible container types and object types
1213
- Fix missing right checks on some ajax config endpoints and escape default value and URL field output.
1314
- Fix item creation with null value for mandatory fields
1415
- Fix search crash when two containers share a dropdown field with the same name.

inc/container.class.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,15 @@ public function prepareInputForAdd($input)
675675
}
676676
}
677677
}
678+
679+
$accepted_itemtypes = array_keys(array_merge(...array_values(self::getItemtypes(true))));
680+
foreach ($input['itemtypes'] as $itemtype) {
681+
if (!in_array($itemtype, $accepted_itemtypes)) {
682+
Session::AddMessageAfterRedirect(__("At least one selected object cannot be linked with type 'Insertion in the form of a specific tab'.", 'fields'), false, ERROR);
683+
684+
return false;
685+
}
686+
}
678687
}
679688

680689
$input = PluginFieldsToolbox::prepareLabel($input);
@@ -1015,6 +1024,7 @@ public function showForm($ID, $options = [])
10151024
self::showFormItemtype([
10161025
'rand' => $rand,
10171026
'subtype' => $this->fields['subtype'],
1027+
'type' => $this->fields['type'],
10181028
]);
10191029
echo '</span>';
10201030
}

rector.php

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,29 @@
2828
* -------------------------------------------------------------------------
2929
*/
3030

31-
declare(strict_types=1);
31+
use Rector\Configuration\RectorConfigBuilder;
3232

3333
require_once __DIR__ . '/../../src/Plugin.php';
3434

35-
use Rector\Caching\ValueObject\Storage\FileCacheStorage;
36-
use Rector\Config\RectorConfig;
37-
use Rector\ValueObject\PhpVersion;
35+
$baseline_file = __DIR__ . '/../../PluginsRector.php';
36+
if (!file_exists($baseline_file)) {
37+
throw new RuntimeException(
38+
sprintf(
39+
'Unable to find "%s". Running rector on a plugin requires a GLPI development checkout that ships PluginsRector.php.',
40+
$baseline_file,
41+
),
42+
);
43+
}
3844

39-
return RectorConfig::configure()
40-
->withPaths([
41-
__DIR__ . '/ajax',
42-
__DIR__ . '/front',
43-
__DIR__ . '/inc',
44-
__DIR__ . '/src',
45-
__DIR__ . '/tests',
46-
])
47-
->withPhpVersion(PhpVersion::PHP_82)
48-
->withCache(
49-
cacheDirectory: __DIR__ . '/var/rector',
50-
cacheClass: FileCacheStorage::class,
51-
)
52-
->withRootFiles()
53-
->withParallel(timeoutSeconds: 300)
54-
->withImportNames(removeUnusedImports: true)
55-
->withPreparedSets(
56-
deadCode: true,
57-
codeQuality: true,
58-
codingStyle: true,
59-
)
60-
->withPhpSets(php82: true) // apply PHP sets up to PHP 8.2
61-
;
45+
$baseline = require $baseline_file;
46+
47+
/** @var RectorConfigBuilder $config */
48+
$config = $baseline([
49+
__DIR__ . '/ajax',
50+
__DIR__ . '/front',
51+
__DIR__ . '/inc',
52+
__DIR__ . '/src',
53+
__DIR__ . '/tests',
54+
]);
55+
56+
return $config;

tests/Units/ContainerItemUpdateTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
use GlpiPlugin\Field\Tests\FieldTestTrait;
3636
use PluginFieldsContainer;
3737
use Ticket;
38+
use Entity;
39+
use Notification;
3840

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

@@ -392,8 +394,8 @@ public function testCreateIsNotBlockedWhenMandatoryTabOrDomtabFieldIsMissing():
392394
$domtab_container = $this->createFieldContainer([
393395
'label' => 'Mandatory Domtab Container',
394396
'type' => 'domtab',
395-
'subtype' => Ticket::class . '$1',
396-
'itemtypes' => [Ticket::class],
397+
'subtype' => Notification::class . '$1',
398+
'itemtypes' => [Entity::class],
397399
'is_active' => 1,
398400
'entities_id' => 0,
399401
'is_recursive' => 1,

tests/Units/ContainerTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
use GlpiPlugin\Field\Tests\FieldTestTrait;
3939
use PHPUnit\Framework\Attributes\DataProvider;
4040
use PluginFieldsContainer;
41+
use Ticket;
4142

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

@@ -108,4 +109,19 @@ public function testAddWithValidItemtypesSucceeds(): void
108109

109110
$this->assertGreaterThan(0, $container->getID());
110111
}
112+
113+
public function testAddDomtabWithIncompatibleItemtypeIsRejected(): void
114+
{
115+
$container = new PluginFieldsContainer();
116+
$result = $container->add([
117+
'label' => 'Domtab with invalid item type',
118+
'type' => 'domtab',
119+
'subtype' => '',
120+
'itemtypes' => [Ticket::class],
121+
'is_active' => 1,
122+
'entities_id' => 0,
123+
'is_recursive' => 1,
124+
]);
125+
$this->assertFalse($result);
126+
}
111127
}

0 commit comments

Comments
 (0)