Skip to content

Commit 7b46cab

Browse files
AdrienClairembaulttrasher
authored andcommitted
Prevent invalid associated item to be selected for predefined fields
1 parent 5329e56 commit 7b46cab

5 files changed

+258
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/**
4+
* ---------------------------------------------------------------------
5+
*
6+
* GLPI - Gestionnaire Libre de Parc Informatique
7+
*
8+
* http://glpi-project.org
9+
*
10+
* @copyright 2015-2025 Teclib' and contributors.
11+
* @licence https://www.gnu.org/licenses/gpl-3.0.html
12+
*
13+
* ---------------------------------------------------------------------
14+
*
15+
* LICENSE
16+
*
17+
* This file is part of GLPI.
18+
*
19+
* This program is free software: you can redistribute it and/or modify
20+
* it under the terms of the GNU General Public License as published by
21+
* the Free Software Foundation, either version 3 of the License, or
22+
* (at your option) any later version.
23+
*
24+
* This program is distributed in the hope that it will be useful,
25+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
26+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27+
* GNU General Public License for more details.
28+
*
29+
* You should have received a copy of the GNU General Public License
30+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
31+
*
32+
* ---------------------------------------------------------------------
33+
*/
34+
35+
namespace tests\units;
36+
37+
use ChangeTemplatePredefinedField;
38+
use Glpi\Tests\AbstractITILTemplatePredefinedFieldTest;
39+
use ITILTemplatePredefinedField;
40+
41+
final class ChangeTemplatePredefinedFieldTest extends AbstractITILTemplatePredefinedFieldTest
42+
{
43+
public function getConcreteClass(): ITILTemplatePredefinedField
44+
{
45+
return new ChangeTemplatePredefinedField();
46+
}
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/**
4+
* ---------------------------------------------------------------------
5+
*
6+
* GLPI - Gestionnaire Libre de Parc Informatique
7+
*
8+
* http://glpi-project.org
9+
*
10+
* @copyright 2015-2025 Teclib' and contributors.
11+
* @licence https://www.gnu.org/licenses/gpl-3.0.html
12+
*
13+
* ---------------------------------------------------------------------
14+
*
15+
* LICENSE
16+
*
17+
* This file is part of GLPI.
18+
*
19+
* This program is free software: you can redistribute it and/or modify
20+
* it under the terms of the GNU General Public License as published by
21+
* the Free Software Foundation, either version 3 of the License, or
22+
* (at your option) any later version.
23+
*
24+
* This program is distributed in the hope that it will be useful,
25+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
26+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27+
* GNU General Public License for more details.
28+
*
29+
* You should have received a copy of the GNU General Public License
30+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
31+
*
32+
* ---------------------------------------------------------------------
33+
*/
34+
35+
namespace tests\units;
36+
37+
use Glpi\Tests\AbstractITILTemplatePredefinedFieldTest;
38+
use ITILTemplatePredefinedField;
39+
use ProblemTemplatePredefinedField;
40+
41+
final class ProblemTemplatePredefinedFieldTest extends AbstractITILTemplatePredefinedFieldTest
42+
{
43+
public function getConcreteClass(): ITILTemplatePredefinedField
44+
{
45+
return new ProblemTemplatePredefinedField();
46+
}
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/**
4+
* ---------------------------------------------------------------------
5+
*
6+
* GLPI - Gestionnaire Libre de Parc Informatique
7+
*
8+
* http://glpi-project.org
9+
*
10+
* @copyright 2015-2025 Teclib' and contributors.
11+
* @licence https://www.gnu.org/licenses/gpl-3.0.html
12+
*
13+
* ---------------------------------------------------------------------
14+
*
15+
* LICENSE
16+
*
17+
* This file is part of GLPI.
18+
*
19+
* This program is free software: you can redistribute it and/or modify
20+
* it under the terms of the GNU General Public License as published by
21+
* the Free Software Foundation, either version 3 of the License, or
22+
* (at your option) any later version.
23+
*
24+
* This program is distributed in the hope that it will be useful,
25+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
26+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27+
* GNU General Public License for more details.
28+
*
29+
* You should have received a copy of the GNU General Public License
30+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
31+
*
32+
* ---------------------------------------------------------------------
33+
*/
34+
35+
namespace tests\units;
36+
37+
use Glpi\Tests\AbstractITILTemplatePredefinedFieldTest;
38+
use ITILTemplatePredefinedField;
39+
use TicketTemplatePredefinedField;
40+
41+
final class TicketTemplatePredefinedFieldTest extends AbstractITILTemplatePredefinedFieldTest
42+
{
43+
public function getConcreteClass(): ITILTemplatePredefinedField
44+
{
45+
return new TicketTemplatePredefinedField();
46+
}
47+
}

src/ITILTemplatePredefinedField.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public function prepareInputForAdd($input)
7575
}
7676

7777
if ((int) $input['num'] === 13) { // 13 - Search option ID for Associated Items for CommonITILObject types
78+
// An itemtype must be selected
7879
if ((string) $input['value'] === '0') {
7980
Session::addMessageAfterRedirect(
8081
__('You must select an associated item'),
@@ -83,6 +84,16 @@ public function prepareInputForAdd($input)
8384
);
8485
return false;
8586
}
87+
88+
// An item must be selected
89+
if (isset($input['add_items_id']) && $input['add_items_id'] == 0) {
90+
Session::addMessageAfterRedirect(
91+
__s('You must select an associated item'),
92+
true,
93+
ERROR
94+
);
95+
return false;
96+
}
8697
}
8798

8899
return parent::prepareInputForAdd($input);
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
/**
4+
* ---------------------------------------------------------------------
5+
*
6+
* GLPI - Gestionnaire Libre de Parc Informatique
7+
*
8+
* http://glpi-project.org
9+
*
10+
* @copyright 2015-2025 Teclib' and contributors.
11+
* @copyright 2003-2014 by the INDEPNET Development Team.
12+
* @licence https://www.gnu.org/licenses/gpl-3.0.html
13+
*
14+
* ---------------------------------------------------------------------
15+
*
16+
* LICENSE
17+
*
18+
* This file is part of GLPI.
19+
*
20+
* This program is free software: you can redistribute it and/or modify
21+
* it under the terms of the GNU General Public License as published by
22+
* the Free Software Foundation, either version 3 of the License, or
23+
* (at your option) any later version.
24+
*
25+
* This program is distributed in the hope that it will be useful,
26+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
27+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28+
* GNU General Public License for more details.
29+
*
30+
* You should have received a copy of the GNU General Public License
31+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
32+
*
33+
* ---------------------------------------------------------------------
34+
*/
35+
36+
namespace Glpi\Tests;
37+
38+
use DbTestCase;
39+
use ITILTemplatePredefinedField;
40+
41+
abstract class AbstractITILTemplatePredefinedFieldTest extends DbTestCase
42+
{
43+
abstract public function getConcreteClass(): ITILTemplatePredefinedField;
44+
45+
public static function getAssociatedItemsInputs(): iterable
46+
{
47+
yield [
48+
'input' => [
49+
'num' => 13,
50+
'value' => "Computer_1",
51+
'add_items_id' => 1,
52+
'entities_id' => 0,
53+
'is_recursive' => false,
54+
],
55+
'expected_result' => true,
56+
];
57+
yield [
58+
'input' => [
59+
'num' => 13,
60+
'value' => 0,
61+
'entities_id' => 0,
62+
'is_recursive' => false,
63+
],
64+
'expected_result' => false,
65+
'expected_errors' => ["You must select an associated item"],
66+
];
67+
yield [
68+
'input' => [
69+
'num' => 13,
70+
'value' => "Computer_0",
71+
'add_items_id' => 0,
72+
'entities_id' => 0,
73+
'is_recursive' => false,
74+
],
75+
'expected_result' => false,
76+
'expected_errors' => ["You must select an associated item"],
77+
];
78+
}
79+
80+
/**
81+
* @dataProvider getAssociatedItemsInputs
82+
*/
83+
public function testValidAssociatedItemsInput(
84+
array $input,
85+
bool $expected_result,
86+
array $expected_errors = []
87+
): void {
88+
// Arrange: create a valid template and insert it into the input
89+
$tpl_class = $this->getConcreteClass()::$itemtype;
90+
$tpl = $this->createItem(
91+
$tpl_class,
92+
['name' => 'my template']
93+
);
94+
$input[$tpl_class::getForeignKeyField()] = $tpl->getID();
95+
96+
// Act: login and prepare input
97+
$this->login('glpi', 'glpi');
98+
$result = $this->getConcreteClass()->prepareInputForAdd($input);
99+
100+
// Assert: check validity and error message
101+
$this->assertEquals($expected_result, (bool) $result);
102+
if ($expected_errors) {
103+
$this->hasSessionMessages(ERROR, $expected_errors);
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)