Skip to content

Commit 613e6e3

Browse files
committed
feat(formanswer): do not valdiate if requester is validator
1 parent bb32843 commit 613e6e3

File tree

3 files changed

+84
-26
lines changed

3 files changed

+84
-26
lines changed

inc/formanswer.class.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,6 +1732,10 @@ protected function setValidator(array $input): array {
17321732
break;
17331733
}
17341734
$usersIdValidator = (int) $validatorItem[1];
1735+
if (Session::getLoginUserID(true) == $usersIdValidator) {
1736+
// The requester is the validator. No need to validate
1737+
break;
1738+
}
17351739
$input['status'] = self::STATUS_WAITING;
17361740
break;
17371741

@@ -1741,6 +1745,10 @@ protected function setValidator(array $input): array {
17411745
break;
17421746
}
17431747
$groupIdValidator = (int) $validatorItem[1];
1748+
if (Session::getLoginUserID(true) !== false && in_array($groupIdValidator, $_SESSION['glpigroups'])) {
1749+
// The requester is a member of the validator group
1750+
break;
1751+
}
17441752
$input['status'] = self::STATUS_WAITING;
17451753
break;
17461754
}

tests/3-unit/PluginFormcreatorForm.php

Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,25 @@ public function beforeTestMethod($method) {
4646
case 'testCountAvailableForm':
4747
$this->login('glpi', 'glpi');
4848
}
49+
50+
switch ($method) {
51+
case 'testCreateValidationNotification':
52+
\Config::setConfigurationValues(
53+
'core',
54+
['use_notifications' => 1, 'notifications_mailing' => 1]
55+
);
56+
}
57+
}
58+
59+
public function afterTestMethod($method) {
60+
parent::afterTestMethod($method);
61+
switch ($method) {
62+
case 'testCreateValidationNotification':
63+
\Config::setConfigurationValues(
64+
'core',
65+
['use_notifications' => 0, 'notifications_mailing' => 0]
66+
);
67+
}
4968
}
5069

5170
public function providerGetTypeName() {
@@ -390,50 +409,81 @@ public function testIncreateUsageCount() {
390409
$this->integer((int) $form->fields['usage_count'])->isEqualTo(1);
391410
}
392411

393-
public function testCreateValidationNotification() {
394-
global $DB, $CFG_GLPI;
412+
public function providerCreateValidationNotification() {
413+
// give email address to users
414+
$validator = new \User();
415+
$validator->getFromDBbyName('tech');
416+
$useremail = new \UserEmail();
417+
$useremail->deleteByCriteria([
418+
'users_id' => $validator->getID(),
419+
]);
420+
$validator->update([
421+
'id' => $validator->getID(),
422+
'_useremails' => [
423+
'tech@localhost.com',
424+
]
425+
]);
395426

396-
// Enable notifications in GLPI
397-
\Config::setConfigurationValues(
398-
'core',
399-
['use_notifications' => 1, 'notifications_mailing' => 1]
400-
);
401-
$CFG_GLPI['use_notifications'] = 1;
402-
$CFG_GLPI['notifications_mailing'] = 1;
403-
$user = new \User();
404-
$user->getFromDBbyName('glpi');
405-
$_SESSION['glpiID'] = $user->getID();
427+
$requester = new \User();
428+
$requester->getFromDBbyName('normal');
406429
$useremail = new \UserEmail();
407430
$useremail->deleteByCriteria([
408-
'users_id' => $user->getID(),
431+
'users_id' => $requester->getID(),
409432
]);
410-
$user->update([
411-
'id' => $_SESSION['glpiID'],
433+
$requester->update([
434+
'id' => $requester->getID(),
412435
'_useremails' => [
413-
'glpi@localhost.com',
436+
'normal@localhost.com',
414437
]
415438
]);
439+
440+
yield [
441+
$requester,
442+
$validator,
443+
2
444+
];
445+
446+
yield [
447+
$requester,
448+
$requester,
449+
1
450+
];
451+
}
452+
453+
/**
454+
* @dataProvider providerCreateValidationNotification
455+
*/
456+
public function testCreateValidationNotification(\User $requester, \User $validator, $expectedNotificationCount) {
457+
global $DB, $CFG_GLPI;
458+
459+
// Enable notifications in GLPI
460+
\Config::setConfigurationValues(
461+
'core',
462+
['use_notifications' => 1, 'notifications_mailing' => 1]
463+
);
464+
// $CFG_GLPI['use_notifications'] = 1;
465+
// $CFG_GLPI['notifications_mailing'] = 1;
466+
416467
$form = $this->getForm([
417468
'name' => 'validation notification',
418469
'validation_required' => \PluginFormcreatorForm_Validator::VALIDATION_USER,
419-
'_validator_users' => [$_SESSION['glpiID']],
470+
'_validator_users' => [$validator->getID()],
420471
]);
421472
$this->getSection([
422473
\PluginFormcreatorForm::getForeignKeyField() => $form->getID(),
423474
'name' => 'section',
424475
]);
425476

426477
$formAnswer = new \PluginFormcreatorFormAnswer();
478+
$this->login('normal', 'normal');
427479
$this->disableDebug();
428480
$formAnswerId = $formAnswer->add([
429481
'plugin_formcreator_forms_id' => $form->getID(),
430-
'formcreator_validator' => \User::class . '_' . $_SESSION['glpiID'],
482+
'formcreator_validator' => $validator->getType() . '_' . $validator->getID(),
431483
]);
432484
$this->restoreDebug();
433485
$this->boolean($formAnswer->isNewItem())->isFalse();
434486

435-
// 1 notification to the validator
436-
// 1 notification to the requester
437487
$foundNotifications = $DB->request([
438488
'COUNT' => 'cpt',
439489
'FROM' => \QueuedNotification::getTable(),
@@ -442,7 +492,7 @@ public function testCreateValidationNotification() {
442492
'items_id' => $formAnswerId,
443493
]
444494
])->current();
445-
$this->integer((int) $foundNotifications['cpt'])->isEqualTo(2);
495+
$this->integer((int) $foundNotifications['cpt'])->isEqualTo($expectedNotificationCount);
446496
}
447497

448498
public function testExport() {

tests/3-unit/PluginFormcreatorFormAnswer.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,8 @@
3131
namespace tests\units;
3232

3333
use CommonITILObject;
34-
use Glpi\Agent\Communication\Headers\Common;
3534
use GlpiPlugin\Formcreator\Tests\CommonTestCase;
3635
use PluginFormcreatorForm;
37-
use PluginFormcreatorFormAnswer as GlobalPluginFormcreatorFormAnswer;
3836
use PluginFormcreatorTargetTicket;
3937
use PluginFormcreatorTargetChange;
4038
use PluginFormcreatorTargetProblem;
@@ -98,10 +96,12 @@ public function providerPrepareInputForAdd() {
9896
$form = new PluginFormcreatorForm();
9997
$form = \PluginFormcreatorForm::getByItem($question);
10098
$this->boolean($form->isNewItem())->isFalse();
99+
$user = new \User();
100+
$user->getFromDBbyName('tech');
101101
$success = $form->update([
102102
'id' => $form->getID(),
103103
'validation_required' => \PluginFormcreatorForm::VALIDATION_USER,
104-
'_validator_users' => [2] // glpi
104+
'_validator_users' => [$user->getID()] // glpi
105105
]);
106106
$this->boolean($success)->isTrue();
107107

@@ -117,8 +117,8 @@ public function providerPrepareInputForAdd() {
117117
'entities_id' => \Session::getActiveEntity(),
118118
'is_recursive' => 0,
119119
'requester_id' => \Session::getLoginUserID(),
120-
'formcreator_validator' => 'User_2',
121-
'users_id_validator' => 2,
120+
'formcreator_validator' => $user::getType() . '_' . $user->getID(),
121+
'users_id_validator' => $user->getID(),
122122
'groups_id_validator' => 0,
123123
'status' => \PluginFormcreatorFormAnswer::STATUS_WAITING,
124124
'request_date' => $_SESSION['glpi_currenttime'],

0 commit comments

Comments
 (0)