ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ilTermsOfServiceUserHasGlobalRoleCriterionTest.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
6 
12 {
14  protected $rbacReview;
15 
17  protected $lng;
18 
20  protected $expectedInitialValue = 2;
21 
24 
26  protected $userRoleTitle = 'User';
27 
29  protected $adminRoleTitle = 'Administrator';
30 
34  public function setUp()
35  {
36  parent::setUp();
37 
38  $this->lng = $this->getLanguageMock();
39 
40  $this->lng
41  ->expects($this->any())
42  ->method('txt')
43  ->willReturn('dummy');
44  }
45 
50  {
51  $this->rbacReview = $this->getRbacReviewMock();
52 
53  $criterion = new \ilTermsOfServiceUserHasGlobalRoleCriterion(
54  $this->rbacReview,
55  $this->getObjectDataCacheMock()
56  );
57 
58  return $criterion;
59  }
60 
65  {
66  $criterion = $this->getInstance();
67 
68  $this->assertEquals('usr_global_role', $criterion->getTypeIdent());
69  $this->assertEquals(false, $criterion->hasUniqueNature());
70 
71  return $criterion;
72  }
73 
79  protected function buildForm(
81  string $httpCriterionSelectionBodyParameter
82  ) : \ilPropertyFormGUI {
83  $form = $this->getFormMock();
84 
85  $radioGroup = $this->getRadioGroupMock();
86 
87  $radioGroup
88  ->expects($this->any())
89  ->method('getPostVar')
90  ->willReturn($httpCriterionSelectionBodyParameter);
91 
92  $form->addItem($radioGroup);
93 
94  $gui->appendOption(
95  $radioGroup,
96  new \ilTermsOfServiceCriterionConfig(['role_id' => $this->expectedInitialValue])
97  );
98 
99  return $form;
100  }
101 
109  ) {
110  $httpCriterionSelectionBodyParameter = 'criterion';
111  $httpCriterionConfigBodyParameter = $criterion->getTypeIdent() . '_role_id';
112 
113  $gui = $criterion->ui($this->lng);
114 
115  $this->assertInstanceOf(\ilTermsOfServiceUserHasGlobalRoleCriterionGUI::class, $gui);
116 
117  $form = $this->buildForm($gui, $httpCriterionSelectionBodyParameter);
118 
119  $roleSelection = $form->getItemByPostVar($httpCriterionConfigBodyParameter);
120  $this->assertInstanceOf(\ilSelectInputGUI::class, $roleSelection);
121  $this->assertEquals($roleSelection->getValue(), $this->expectedInitialValue);
122 
123  return $criterion;
124  }
125 
132  ) {
133  $httpCriterionSelectionBodyParameter = 'criterion';
134  $httpCriterionConfigBodyParameter = $criterion->getTypeIdent() . '_role_id';
135 
136  $gui = $criterion->ui($this->lng);
137 
138  $form = $this->buildForm($gui, $httpCriterionSelectionBodyParameter);
139 
140  $form
141  ->expects($this->once())
142  ->method('getInput')
143  ->with($httpCriterionConfigBodyParameter)
144  ->will($this->returnCallback(function () {
146  }));
147 
148  $value = $gui->getConfigByForm($form);
149 
150  $this->assertInstanceOf(\ilTermsOfServiceCriterionConfig::class, $value);
151  $this->assertEquals($this->expectedAfterFormSubmitValue, $value['role_id']);
152  $this->assertEquals($this->getCriterionConfig(['role_id' => $this->expectedAfterFormSubmitValue]), $value);
153  }
154 
160  {
161  $gui = $criterion->ui($this->lng);
162 
163  $actual = $gui->getIdentPresentation();
164 
165  $this->assertInternalType('string', $actual);
166  $this->assertNotEmpty($actual);
167  }
168 
172  public function objectCacheProvider() : array
173  {
174  return [
177  [-1, ''],
178  ];
179  }
180 
186  public function testValuePresentationMatchesExpectation(int $roleId, string $roleTitle)
187  {
188  $rbacReview = $this->getRbacReviewMock();
189  $objectDataCache = $this->getObjectDataCacheMock();
190 
191  $objectDataCache
192  ->expects($this->any())
193  ->method('lookupTitle')
194  ->with($roleId)
195  ->willReturn($roleTitle);
196 
197  $criterion = new \ilTermsOfServiceUserHasGlobalRoleCriterion($rbacReview, $objectDataCache);
198  $gui = $criterion->ui($this->lng);
199 
201  $actual = $gui->getValuePresentation(
202  $this->getCriterionConfig(['role_id' => $roleId]),
203  $this->dic->ui()->factory()
204  );
205 
206  $this->assertInstanceOf(Component::class, $actual);
207  $this->assertInstanceOf(Legacy::class, $actual);
208  $this->assertEquals($roleTitle, $actual->getContent());
209  }
210 
214  public function failingConfigProvider() : array
215  {
216  $criterion = $this->getInstance();
217 
218  return [
219  [$criterion, $this->getCriterionConfig(['role_id' => []])],
220  [$criterion, $this->getCriterionConfig(['role_id' => new stdClass()])],
221  [$criterion, $this->getCriterionConfig(['role_id' => 1.424])],
222  [$criterion, $this->getCriterionConfig(['role_id' => 'phpunit'])],
223  [$criterion, $this->getCriterionConfig(['another_config_key' => true])],
224  [$criterion, $this->getCriterionConfig()],
225  ];
226  }
227 
236  ) {
237  $user = $this->getUserMock();
238 
239  $this->assertFalse($criterion->evaluate($user, $config));
240  }
241 
246  {
247  $user = $this->getUserMock();
248  $criterion = $this->getInstance();
249 
250  $this->rbacReview
251  ->expects($this->once())
252  ->method('isGlobalRole')
253  ->willReturn(false);
254 
255  $this->assertFalse(
256  $criterion->evaluate($user, $this->getCriterionConfig(['role_id' => $this->expectedAfterFormSubmitValue]))
257  );
258  }
259 
264  {
265  $user = $this->getUserMock();
266  $criterion = $this->getInstance();
267 
268  $this->rbacReview
269  ->expects($this->once())
270  ->method('isGlobalRole')
271  ->willReturn(true);
272 
273  $this->rbacReview
274  ->expects($this->once())
275  ->method('isAssigned')
276  ->willReturn(false);
277 
278  $this->assertFalse(
279  $criterion->evaluate($user, $this->getCriterionConfig(['role_id' => $this->expectedAfterFormSubmitValue]))
280  );
281  }
282 
287  {
288  $user = $this->getUserMock();
289  $criterion = $this->getInstance();
290 
291  $this->rbacReview
292  ->expects($this->once())
293  ->method('isGlobalRole')
294  ->willReturn(true);
295 
296  $this->rbacReview
297  ->expects($this->once())
298  ->method('isAssigned')
299  ->willReturn(true);
300 
301  $this->assertTrue(
302  $criterion->evaluate($user, $this->getCriterionConfig(['role_id' => $this->expectedAfterFormSubmitValue]))
303  );
304  }
305 }
getTypeIdent()
Returns a unique id of the criterion type.string
Interface ilTermsOfServiceCriterionTypeGUI.
testValuesFromFormUserInterfaceElementsCanBeRetrieved(\ilTermsOfServiceUserHasGlobalRoleCriterion $criterion)
testFormUserInterfaceElementsAreProperlyBuilt
$config
Definition: bootstrap.php:15
This class represents a property form user interface.
Class ilTermsOfServiceCriterionBaseTest.
Class ilTermsOfServiceUserHasGlobalRoleCriterionTest.
testFormUserInterfaceElementsAreProperlyBuilt(\ilTermsOfServiceUserHasGlobalRoleCriterion $criterion)
Class ilTermsOfServiceCriterionConfig.
evaluate(\ilObjUser $user, \ilTermsOfServiceCriterionConfig $config)
bool
if(isset($_POST['submit'])) $form
buildForm(\ilTermsOfServiceCriterionTypeGUI $gui, string $httpCriterionSelectionBodyParameter)
once($eventName, callable $callBack, $priority=100)
Subscribe to an event exactly once.
$user
Definition: migrateto20.php:57
appendOption(\ilRadioGroupInputGUI $option, \ilTermsOfServiceCriterionConfig $config)
testTypeIdentPresentationIsANonEmptyString(\ilTermsOfServiceUserHasGlobalRoleCriterion $criterion)
testFormUserInterfaceElementsAreProperlyBuilt
testEvaluationFailsIfConfiguredRoleDoesNotMatchTheExpectedFormat(\ilTermsOfServiceUserHasGlobalRoleCriterion $criterion, \ilTermsOfServiceCriterionConfig $config)