ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
ilTermsOfServiceUserHasGlobalRoleCriterionTest.php
Go to the documentation of this file.
1 <?php declare(strict_types=1);
2 /* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
7 
13 {
15  protected $rbacReview;
16 
18  protected $lng;
19 
21  protected $expectedInitialValue = 2;
22 
25 
27  protected $userRoleTitle = 'User';
28 
30  protected $adminRoleTitle = 'Administrator';
31 
35  public function setUp() : void
36  {
37  parent::setUp();
38 
39  $this->lng = $this->getLanguageMock();
40 
41  $this->lng
42  ->expects($this->any())
43  ->method('txt')
44  ->willReturn('dummy');
45  }
46 
52  {
53  $this->rbacReview = $this->getRbacReviewMock();
54 
56  $this->rbacReview,
57  $this->getObjectDataCacheMock()
58  );
59 
60  return $criterion;
61  }
62 
68  {
69  $criterion = $this->getInstance();
70 
71  $this->assertEquals('usr_global_role', $criterion->getTypeIdent());
72  $this->assertEquals(false, $criterion->hasUniqueNature());
73 
74  return $criterion;
75  }
76 
83  protected function buildForm(
85  string $httpCriterionSelectionBodyParameter
86  ) : ilPropertyFormGUI {
87  $form = $this->getFormMock();
88 
89  $radioGroup = $this->getRadioGroupMock();
90 
91  $radioGroup
92  ->expects($this->any())
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 
115  $httpCriterionSelectionBodyParameter = 'criterion';
116  $httpCriterionConfigBodyParameter = $criterion->getTypeIdent() . '_role_id';
117 
118  $gui = $criterion->ui($this->lng);
119 
120  $this->assertInstanceOf(ilTermsOfServiceUserHasGlobalRoleCriterionGUI::class, $gui);
121 
122  $form = $this->buildForm($gui, $httpCriterionSelectionBodyParameter);
123 
124  $roleSelection = $form->getItemByPostVar($httpCriterionConfigBodyParameter);
125  $this->assertInstanceOf(ilSelectInputGUI::class, $roleSelection);
126  $this->assertEquals($roleSelection->getValue(), $this->expectedInitialValue);
127 
128  return $criterion;
129  }
130 
138  ) : void {
139  $httpCriterionSelectionBodyParameter = 'criterion';
140  $httpCriterionConfigBodyParameter = $criterion->getTypeIdent() . '_role_id';
141 
142  $gui = $criterion->ui($this->lng);
143 
144  $form = $this->buildForm($gui, $httpCriterionSelectionBodyParameter);
145 
146  $form
147  ->expects($this->once())
148  ->method('getInput')
149  ->with($httpCriterionConfigBodyParameter)
150  ->will($this->returnCallback(function () {
152  }));
153 
154  $value = $gui->getConfigByForm($form);
155 
156  $this->assertInstanceOf(ilTermsOfServiceCriterionConfig::class, $value);
157  $this->assertEquals($this->expectedAfterFormSubmitValue, $value['role_id']);
158  $this->assertEquals($this->getCriterionConfig(['role_id' => $this->expectedAfterFormSubmitValue]), $value);
159  }
160 
167  ) : void {
168  $gui = $criterion->ui($this->lng);
169 
170  $actual = $gui->getIdentPresentation();
171 
172  $this->assertIsString($actual);
173  $this->assertNotEmpty($actual);
174  }
175 
179  public function objectCacheProvider() : array
180  {
181  return [
182  'Administrator Role Id' => [$this->expectedInitialValue, $this->adminRoleTitle],
184  'Invalid Role Id' => [-1, ''],
185  ];
186  }
187 
194  public function testValuePresentationMatchesExpectation(int $roleId, string $roleTitle) : void
195  {
196  $rbacReview = $this->getRbacReviewMock();
197  $objectDataCache = $this->getObjectDataCacheMock();
198 
199  $objectDataCache
200  ->expects($this->any())
201  ->method('lookupTitle')
202  ->with($roleId)
203  ->willReturn($roleTitle);
204 
205  $criterion = new ilTermsOfServiceUserHasGlobalRoleCriterion($rbacReview, $objectDataCache);
206  $gui = $criterion->ui($this->lng);
207 
209  $actual = $gui->getValuePresentation(
210  $this->getCriterionConfig(['role_id' => $roleId]),
211  $this->getUiFactoryMock()
212  );
213 
214  $this->assertInstanceOf(Component::class, $actual);
215  $this->assertInstanceOf(Legacy::class, $actual);
216  $this->assertEquals($roleTitle, $actual->getContent());
217  }
218 
223  public function failingConfigProvider() : array
224  {
225  $criterion = $this->getInstance();
226 
227  return [
228  'Array' => [$criterion, $this->getCriterionConfig(['role_id' => []])],
229  'Object' => [$criterion, $this->getCriterionConfig(['role_id' => new stdClass()])],
230  'Double' => [$criterion, $this->getCriterionConfig(['role_id' => 1.424])],
231  'String' => [$criterion, $this->getCriterionConfig(['role_id' => 'phpunit'])],
232  'Wrong Key Provided for Extracting Role' => [
233  $criterion,
234  $this->getCriterionConfig(['another_config_key' => true])
235  ],
236  'Empty Configuration' => [$criterion, $this->getCriterionConfig()],
237  ];
238  }
239 
249  ) : void {
250  $user = $this->getUserMock();
251 
252  $this->assertFalse($criterion->evaluate($user, $config));
253  }
254 
259  {
260  $user = $this->getUserMock();
261  $criterion = $this->getInstance();
262 
263  $this->rbacReview
264  ->expects($this->once())
265  ->method('isGlobalRole')
266  ->willReturn(false);
267 
268  $this->assertFalse(
269  $criterion->evaluate($user, $this->getCriterionConfig(['role_id' => $this->expectedAfterFormSubmitValue]))
270  );
271  }
272 
277  {
278  $user = $this->getUserMock();
279  $criterion = $this->getInstance();
280 
281  $this->rbacReview
282  ->expects($this->once())
283  ->method('isGlobalRole')
284  ->willReturn(true);
285 
286  $this->rbacReview
287  ->expects($this->once())
288  ->method('isAssigned')
289  ->willReturn(false);
290 
291  $this->assertFalse(
292  $criterion->evaluate($user, $this->getCriterionConfig(['role_id' => $this->expectedAfterFormSubmitValue]))
293  );
294  }
295 
300  {
301  $user = $this->getUserMock();
302  $criterion = $this->getInstance();
303 
304  $this->rbacReview
305  ->expects($this->once())
306  ->method('isGlobalRole')
307  ->willReturn(true);
308 
309  $this->rbacReview
310  ->expects($this->once())
311  ->method('isAssigned')
312  ->willReturn(true);
313 
314  $this->assertTrue(
315  $criterion->evaluate($user, $this->getCriterionConfig(['role_id' => $this->expectedAfterFormSubmitValue]))
316  );
317  }
318 }
getTypeIdent()
Returns a unique id of the criterion type.string
Interface ilTermsOfServiceCriterionTypeGUI.
testEvaluationFailsIfConfiguredRoleDoesNotMatchTheExpectedFormat(ilTermsOfServiceUserHasGlobalRoleCriterion $criterion, ilTermsOfServiceCriterionConfig $config)
This class represents a property form user interface.
Class ilTermsOfServiceCriterionBaseTest.
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:68
buildForm(ilTermsOfServiceCriterionTypeGUI $gui, string $httpCriterionSelectionBodyParameter)
Class ilTermsOfServiceUserHasGlobalRoleCriterionTest.
testFormUserInterfaceElementsAreProperlyBuilt(ilTermsOfServiceUserHasGlobalRoleCriterion $criterion)
Class ilTermsOfServiceCriterionConfig.
testTypeIdentPresentationIsANonEmptyString(ilTermsOfServiceUserHasGlobalRoleCriterion $criterion)
testFormUserInterfaceElementsAreProperlyBuilt
evaluate(ilObjUser $user, ilTermsOfServiceCriterionConfig $config)
bool
testValuesFromFormUserInterfaceElementsCanBeRetrieved(ilTermsOfServiceUserHasGlobalRoleCriterion $criterion)
testFormUserInterfaceElementsAreProperlyBuilt
appendOption(ilRadioGroupInputGUI $option, ilTermsOfServiceCriterionConfig $config)