Skip to content

Commit 2d58ba2

Browse files
committed
test(formanswer): add tests
1 parent a9451d9 commit 2d58ba2

3 files changed

Lines changed: 133 additions & 13 deletions

File tree

tests/3-unit/PluginFormcreatorFormAnswer.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
use PluginFormcreatorTargetProblem;
5151
use Problem;
5252
use Session;
53+
use PluginFormcreatorForm_Validator;
5354
use Ticket;
5455
use TicketValidation;
5556
use Toolbox;
@@ -946,4 +947,112 @@ public function testParseTags($instance, $template, $expected) {
946947
$output = $instance->parseTags($template, $ticket, true);
947948
$this->string($output)->isEqualTo($expected);
948949
}
950+
951+
public function providerCanViewItem() {
952+
$this->login('glpi', 'glpi');
953+
$form = $this->getForm();
954+
$formAnswer = $this->getFormAnswer([
955+
'plugin_formcreator_forms_id' => $form->getID(),
956+
]);
957+
$this->logout();
958+
959+
yield 'Not authenticated' => [
960+
'formAnswer' => $formAnswer,
961+
'expected' => false,
962+
];
963+
964+
$this->login('glpi', 'glpi');
965+
966+
yield 'User granted to edit forms' => [
967+
'formAnswer' => $formAnswer,
968+
'expected' => true,
969+
];
970+
971+
$this->login('normal', 'normal');
972+
$formAnswer = $this->getFormAnswer([
973+
'plugin_formcreator_forms_id' => $form->getID(),
974+
]);
975+
976+
yield 'User is the requester' => [
977+
'formAnswer' => $formAnswer,
978+
'expected' => true,
979+
];
980+
981+
$this->login('tech', 'tech');
982+
983+
yield 'User is not the requester' => [
984+
'formAnswer' => $formAnswer,
985+
'expected' => false,
986+
];
987+
988+
$form->update([
989+
'id' => $form->getID(),
990+
'validation_required' => PluginFormcreatorForm_Validator::VALIDATION_USER,
991+
'_validator_users' => [
992+
User::getIdByName('tech'),
993+
],
994+
]);
995+
$formAnswer = $this->getFormAnswer([
996+
'plugin_formcreator_forms_id' => $form->getID(),
997+
]);
998+
999+
yield 'User is the validator' => [
1000+
'formAnswer' => $formAnswer,
1001+
'expected' => true,
1002+
];
1003+
1004+
$this->login('normal', 'normal');
1005+
1006+
yield 'User is not the validator' => [
1007+
'formAnswer' => $formAnswer,
1008+
'expected' => false,
1009+
];
1010+
1011+
$group = $this->getGlpiCoreItem(Group::class, [
1012+
'name' => 'group' . $this->getUniqueString()
1013+
]);
1014+
$user = $this->getGlpiCoreItem(User::class, [
1015+
'name' => 'user' . $this->getUniqueString(),
1016+
'password' => 'password',
1017+
'password2' => 'password',
1018+
]);
1019+
1020+
$form->update([
1021+
'id' => $form->getID(),
1022+
'validation_required' => PluginFormcreatorForm_Validator::VALIDATION_GROUP,
1023+
'_validator_groups' => [
1024+
$group->getID(),
1025+
],
1026+
]);
1027+
$this->login('normal', 'normal');
1028+
$formAnswer = $this->getFormAnswer([
1029+
'plugin_formcreator_forms_id' => $form->getID(),
1030+
]);
1031+
$this->login($user->fields['name'], 'password');
1032+
1033+
yield 'User is not a member of validator group' => [
1034+
'formAnswer' => $formAnswer,
1035+
'expected' => false,
1036+
];
1037+
1038+
$groupUser = new Group_User();
1039+
$groupUser->add([
1040+
'groups_id' => $group->getID(),
1041+
'users_id' => $user->getID(),
1042+
]);
1043+
1044+
yield 'User is a member of validator group' => [
1045+
'formAnswer' => $formAnswer,
1046+
'expected' => true,
1047+
];
1048+
}
1049+
1050+
/**
1051+
* @dataProvider providerCanViewItem
1052+
*/
1053+
public function testCanViewItem($formAnswer, bool $expected) {
1054+
/** @var \PluginFormcreatorFormAnswer $formAnswer */
1055+
$output = $formAnswer->canViewItem();
1056+
$this->boolean($output)->isEqualTo($expected);
1057+
}
9491058
}

tests/3-unit/PluginFormcreatorForm_Validator.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
*/
3131
namespace tests\units;
3232
use GlpiPlugin\Formcreator\Tests\CommonTestCase;
33+
use Group;
34+
use User;
35+
use Group_User;
36+
use PluginFormcreatorForm;
37+
use PluginFormcreatorLinker;
3338

3439
class PluginFormcreatorForm_Validator extends CommonTestCase {
3540
public function testPrepareInputForAdd() {
@@ -104,9 +109,9 @@ public function testExport() {
104109
}
105110

106111
public function testImport() {
107-
$linker = new \PluginFormcreatorLinker();
112+
$linker = new PluginFormcreatorLinker();
108113
$input = [
109-
'itemtype' => \User::class,
114+
'itemtype' => User::class,
110115
'_item' => 'normal',
111116
'uuid' => plugin_formcreator_getUuid(),
112117
];
@@ -124,32 +129,32 @@ public function testGetValidatorsForForm() {
124129
$output = $formValidator->getValidatorsForForm($form, UnknownItemtype::class);
125130
$this->array($output)->hasSize(0);
126131

127-
$groupA = $this->getGlpiCoreItem(\group::class, [
132+
$groupA = $this->getGlpiCoreItem(Group::class, [
128133
'name' => 'group A' . $this->getUniqueString()
129134
]);
130-
$groupB = $this->getGlpiCoreItem(\group::class, [
135+
$groupB = $this->getGlpiCoreItem(Group::class, [
131136
'name' => 'group B' . $this->getUniqueString()
132137
]);
133138

134-
$userA = $this->getGlpiCoreItem(\User::class, [
139+
$userA = $this->getGlpiCoreItem(User::class, [
135140
'name' => 'user A' . $this->getUniqueString(),
136141
]);
137-
$userB = $this->getGlpiCoreItem(\User::class, [
142+
$userB = $this->getGlpiCoreItem(User::class, [
138143
'name' => 'user B' . $this->getUniqueString(),
139144
]);
140-
$userC = $this->getGlpiCoreItem(\User::class, [
145+
$userC = $this->getGlpiCoreItem(User::class, [
141146
'name' => 'user C' . $this->getUniqueString(),
142147
]);
143-
$userD = $this->getGlpiCoreItem(\User::class, [
148+
$userD = $this->getGlpiCoreItem(User::class, [
144149
'name' => 'user D' . $this->getUniqueString(),
145150
]);
146151

147152
// Add users into groups
148-
$this->getGlpiCoreItem(\Group_User::class, [
153+
$this->getGlpiCoreItem(Group_User::class, [
149154
'users_id' => $userA->getID(),
150155
'groups_id' => $groupA->getID(),
151156
]);
152-
$this->getGlpiCoreItem(\Group_User::class, [
157+
$this->getGlpiCoreItem(Group_User::class, [
153158
'users_id' => $userB->getID(),
154159
'groups_id' => $groupB->getID(),
155160
]);
@@ -178,7 +183,7 @@ public function testGetValidatorsForForm() {
178183
// Test when form has users as validators
179184
$form->update([
180185
'id' => $form->getID(),
181-
'validation_required' => \PluginFormcreatorForm::VALIDATION_USER,
186+
'validation_required' => PluginFormcreatorForm::VALIDATION_USER,
182187
'_validator_users' => [
183188
$userC->getID(),
184189
$userD->getID(),
@@ -195,7 +200,7 @@ public function testGetValidatorsForForm() {
195200
// Test when form has groups as validators
196201
$form->update([
197202
'id' => $form->getID(),
198-
'validation_required' => \PluginFormcreatorForm::VALIDATION_GROUP,
203+
'validation_required' => PluginFormcreatorForm::VALIDATION_GROUP,
199204
'_validator_groups' => [
200205
$groupA->getID(),
201206
$groupB->getID(),
@@ -212,7 +217,7 @@ public function testGetValidatorsForForm() {
212217
// Test when form has no validation
213218
$form->update([
214219
'id' => $form->getID(),
215-
'validation_required' => \PluginFormcreatorForm::VALIDATION_NONE,
220+
'validation_required' => PluginFormcreatorForm::VALIDATION_NONE,
216221
]);
217222
$output = $formValidator->getValidatorsForForm($form);
218223
$this->array($output)

tests/src/CommonTestCase.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ protected function login($name, $password, $noauto = false) {
7070
return $result;
7171
}
7272

73+
protected function logout() {
74+
Session::destroy();
75+
session_regenerate_id();
76+
Session::start();
77+
}
78+
7379
protected function disableDebug() {
7480
$this->debugMode = Session::DEBUG_MODE;
7581
if (isset($_SESSION['glpi_use_mode'])) {

0 commit comments

Comments
 (0)