ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
ilTermsOfServiceUserHasGlobalRoleCriterionTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
24 
30 {
34  protected ilLanguage $lng;
35  protected int $expectedInitialValue = 2;
36  protected int $expectedAfterFormSubmitValue = 4;
37  protected string $userRoleTitle = 'User';
38  protected string $adminRoleTitle = 'Administrator';
39 
40  protected function setUp(): void
41  {
42  parent::setUp();
43 
44  $this->lng = $this->getLanguageMock();
45 
46  $this->lng
47  ->method('txt')
48  ->willReturn('dummy');
49  }
50 
55  {
56  $this->rbacReview = $this->getRbacReviewMock();
57 
59  $this->rbacReview,
60  $this->getObjectDataCacheMock()
61  );
62 
63  return $criterion;
64  }
65 
70  {
71  $criterion = $this->getInstance();
72 
73  $this->assertSame('usr_global_role', $criterion->getTypeIdent());
74  $this->assertFalse($criterion->hasUniqueNature());
75 
76  return $criterion;
77  }
78 
84  protected function buildForm(
86  string $httpCriterionSelectionBodyParameter
88  $form = $this->getFormMock();
89 
90  $radioGroup = $this->getRadioGroupMock();
91 
92  $radioGroup
93  ->method('getPostVar')
94  ->willReturn($httpCriterionSelectionBodyParameter);
95 
96  $form->addItem($radioGroup);
97 
98  $gui->appendOption(
99  $radioGroup,
100  new ilTermsOfServiceCriterionConfig(['role_id' => $this->expectedInitialValue])
101  );
102 
103  return $form;
104  }
105 
114  $httpCriterionSelectionBodyParameter = 'criterion';
115  $httpCriterionConfigBodyParameter = $criterion->getTypeIdent() . '_role_id';
116 
117  $gui = $criterion->ui($this->lng);
118 
119  $this->assertInstanceOf(ilTermsOfServiceUserHasGlobalRoleCriterionGUI::class, $gui);
120 
121  $form = $this->buildForm($gui, $httpCriterionSelectionBodyParameter);
122 
123  $roleSelection = $form->getItemByPostVar($httpCriterionConfigBodyParameter);
124  $this->assertInstanceOf(ilSelectInputGUI::class, $roleSelection);
125  $this->assertEquals($roleSelection->getValue(), (string) $this->expectedInitialValue);
126 
127  return $criterion;
128  }
129 
136  ): void {
137  $httpCriterionSelectionBodyParameter = 'criterion';
138  $httpCriterionConfigBodyParameter = $criterion->getTypeIdent() . '_role_id';
139 
140  $gui = $criterion->ui($this->lng);
141 
142  $form = $this->buildForm($gui, $httpCriterionSelectionBodyParameter);
143 
144  $form
145  ->expects($this->once())
146  ->method('getInput')
147  ->with($httpCriterionConfigBodyParameter)
148  ->willReturnCallback(function () {
150  });
151 
152  $value = $gui->getConfigByForm($form);
153 
154  $this->assertInstanceOf(ilTermsOfServiceCriterionConfig::class, $value);
155  $this->assertSame($this->expectedAfterFormSubmitValue, $value['role_id']);
156  $this->assertEquals($this->getCriterionConfig(['role_id' => $this->expectedAfterFormSubmitValue]), $value);
157  }
158 
165  ): void {
166  $gui = $criterion->ui($this->lng);
167 
168  $actual = $gui->getIdentPresentation();
169 
170  $this->assertIsString($actual);
171  $this->assertNotEmpty($actual);
172  }
173 
174  public function objectCacheProvider(): array
175  {
176  return [
177  'Administrator Role Id' => [$this->expectedInitialValue, $this->adminRoleTitle],
179  'Invalid Role Id' => [-1, ''],
180  ];
181  }
182 
188  public function testValuePresentationMatchesExpectation(int $roleId, string $roleTitle): void
189  {
190  $rbacReview = $this->getRbacReviewMock();
191  $objectDataCache = $this->getObjectDataCacheMock();
192 
193  $objectDataCache
194  ->method('lookupTitle')
195  ->with($roleId)
196  ->willReturn($roleTitle);
197 
198  $criterion = new ilTermsOfServiceUserHasGlobalRoleCriterion($rbacReview, $objectDataCache);
199  $gui = $criterion->ui($this->lng);
200 
202  $actual = $gui->getValuePresentation(
203  $this->getCriterionConfig(['role_id' => $roleId]),
204  $this->getUiFactoryMock()
205  );
206 
207  $this->assertInstanceOf(Component::class, $actual);
208  $this->assertInstanceOf(Legacy::class, $actual);
209  $this->assertSame($roleTitle, $actual->getContent());
210  }
211 
212  public function failingConfigProvider(): array
213  {
214  $criterion = $this->getInstance();
215 
216  return [
217  'Array' => [$criterion, $this->getCriterionConfig(['role_id' => []])],
218  'Object' => [$criterion, $this->getCriterionConfig(['role_id' => new stdClass()])],
219  'Double' => [$criterion, $this->getCriterionConfig(['role_id' => 1.424])],
220  'String' => [$criterion, $this->getCriterionConfig(['role_id' => 'phpunit'])],
221  'Wrong Key Provided for Extracting Role' => [
222  $criterion,
223  $this->getCriterionConfig(['another_config_key' => true])
224  ],
225  'Empty Configuration' => [$criterion, $this->getCriterionConfig()],
226  ];
227  }
228 
237  ): void {
238  $user = $this->getUserMock();
239 
240  $this->assertFalse($criterion->evaluate($user, $config));
241  }
242 
244  {
245  $user = $this->getUserMock();
246  $criterion = $this->getInstance();
247 
248  $this->rbacReview
249  ->expects($this->once())
250  ->method('isGlobalRole')
251  ->willReturn(false);
252 
253  $this->assertFalse(
254  $criterion->evaluate($user, $this->getCriterionConfig(['role_id' => $this->expectedAfterFormSubmitValue]))
255  );
256  }
257 
259  {
260  $user = $this->getUserMock();
261  $criterion = $this->getInstance();
262 
263  $this->rbacReview
264  ->expects($this->once())
265  ->method('isGlobalRole')
266  ->willReturn(true);
267 
268  $this->rbacReview
269  ->expects($this->once())
270  ->method('isAssigned')
271  ->willReturn(false);
272 
273  $this->assertFalse(
274  $criterion->evaluate($user, $this->getCriterionConfig(['role_id' => $this->expectedAfterFormSubmitValue]))
275  );
276  }
277 
279  {
280  $user = $this->getUserMock();
281  $criterion = $this->getInstance();
282 
283  $this->rbacReview
284  ->expects($this->once())
285  ->method('isGlobalRole')
286  ->willReturn(true);
287 
288  $this->rbacReview
289  ->expects($this->once())
290  ->method('isAssigned')
291  ->willReturn(true);
292 
293  $this->assertTrue(
294  $criterion->evaluate($user, $this->getCriterionConfig(['role_id' => $this->expectedAfterFormSubmitValue]))
295  );
296  }
297 }
Interface ilTermsOfServiceCriterionTypeGUI.
testEvaluationFailsIfConfiguredRoleDoesNotMatchTheExpectedFormat(ilTermsOfServiceUserHasGlobalRoleCriterion $criterion, ilTermsOfServiceCriterionConfig $config)
Class ilTermsOfServiceCriterionBaseTest.
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:85
buildForm(ilTermsOfServiceCriterionTypeGUI $gui, string $httpCriterionSelectionBodyParameter)
Class ilTermsOfServiceUserHasGlobalRoleCriterionTest.
testFormUserInterfaceElementsAreProperlyBuilt(ilTermsOfServiceUserHasGlobalRoleCriterion $criterion)
Class ilTermsOfServiceCriterionConfig.
testTypeIdentPresentationIsANonEmptyString(ilTermsOfServiceUserHasGlobalRoleCriterion $criterion)
testFormUserInterfaceElementsAreProperlyBuilt
evaluate(ilObjUser $user, ilTermsOfServiceCriterionConfig $config)
appendOption(ilRadioGroupInputGUI $group, ilTermsOfServiceCriterionConfig $config)
testValuesFromFormUserInterfaceElementsCanBeRetrieved(ilTermsOfServiceUserHasGlobalRoleCriterion $criterion)
testFormUserInterfaceElementsAreProperlyBuilt