ILIAS  release_8 Revision v8.24
ilTermsOfServiceUserHasGlobalRoleCriterionTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23use PHPUnit\Framework\MockObject\MockObject;
24
30{
34 protected ilLanguage $lng;
35 protected int $expectedInitialValue = 2;
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,
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 () {
149 return $this->expectedAfterFormSubmitValue;
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],
178 'User Role Id' => [$this->expectedAfterFormSubmitValue, $this->userRoleTitle],
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}
language handling
This class represents a property form user interface.
class ilRbacReview Contains Review functions of core Rbac.
Class ilTermsOfServiceCriterionBaseTest.
Class ilTermsOfServiceCriterionConfig.
testTypeIdentPresentationIsANonEmptyString(ilTermsOfServiceUserHasGlobalRoleCriterion $criterion)
@depends testFormUserInterfaceElementsAreProperlyBuilt
buildForm(ilTermsOfServiceCriterionTypeGUI $gui, string $httpCriterionSelectionBodyParameter)
testFormUserInterfaceElementsAreProperlyBuilt(ilTermsOfServiceUserHasGlobalRoleCriterion $criterion)
testEvaluationFailsIfConfiguredRoleDoesNotMatchTheExpectedFormat(ilTermsOfServiceUserHasGlobalRoleCriterion $criterion, ilTermsOfServiceCriterionConfig $config)
testValuesFromFormUserInterfaceElementsCanBeRetrieved(ilTermsOfServiceUserHasGlobalRoleCriterion $criterion)
@depends testFormUserInterfaceElementsAreProperlyBuilt
evaluate(ilObjUser $user, ilTermsOfServiceCriterionConfig $config)
A component is the most general form of an entity in the UI.
Definition: Component.php:28
Interface ilTermsOfServiceCriterionTypeGUI.
appendOption(ilRadioGroupInputGUI $group, ilTermsOfServiceCriterionConfig $config)
getConfigByForm(ilPropertyFormGUI $form)
getValuePresentation(ilTermsOfServiceCriterionConfig $config, Factory $uiFactory)
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:85