ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ilTermsOfServiceDocumentCriterionAssignmentConstraintTest.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
5 
11 {
16  {
17  $criterionTypeFactory = $this
18  ->getMockBuilder(\ilTermsOfServiceCriterionTypeFactoryInterface::class)
19  ->getMock();
20 
21  return $criterionTypeFactory;
22  }
23 
28  protected function getCriterionTypeMock(string $typeIdent) : \ilTermsOfServiceCriterionType
29  {
30  $criterionType = $this
31  ->getMockBuilder(\ilTermsOfServiceCriterionType::class)
32  ->getMock();
33 
34  $criterionType
35  ->expects($this->any())
36  ->method('getTypeIdent')
37  ->willReturn($typeIdent);
38 
39  return $criterionType;
40  }
41 
46  {
47  $criterionTypeFactory = $this->getCriterionTypeFactoryMock();
48 
49  $criterionType1 = $this->getCriterionTypeMock('dummy');
50 
51  $criterionType1
52  ->expects($this->any())
53  ->method('hasUniqueNature')
54  ->willReturn(false);
55 
56  $criterionTypeFactory
57  ->expects($this->any())
58  ->method('getTypesByIdentMap')
59  ->willReturn([
60  $criterionType1->getTypeIdent() => $criterionType1,
61  ]);
62 
63  $criterionTypeFactory
64  ->expects($this->any())
65  ->method('findByTypeIdent')
66  ->willReturn($criterionType1);
67 
68  return $criterionTypeFactory;
69  }
70 
74  public function criteriaAssignmentProvider() : array
75  {
76  $criterionAssignment1 = $this
77  ->getMockBuilder(\ilTermsOfServiceDocumentCriterionAssignment::class)
78  ->disableOriginalConstructor()
79  ->setMethods(['getId', 'getCriterionValue', 'getCriterionId'])
80  ->getMock();
81 
82  $criterionAssignment1
83  ->expects($this->any())
84  ->method('getId')
85  ->willReturn(1);
86 
87  $criterionAssignment1
88  ->expects($this->any())
89  ->method('getCriterionId')
90  ->willReturn('usr_global_role');
91 
92  $criterionAssignment1
93  ->expects($this->any())
94  ->method('getCriterionValue')
95  ->willReturn($this->getCriterionConfig(['role_id' => 4]));
96 
97  $criterionAssignment2 = $this
98  ->getMockBuilder(\ilTermsOfServiceDocumentCriterionAssignment::class)
99  ->disableOriginalConstructor()
100  ->setMethods(['getId', 'getCriterionValue', 'getCriterionId'])
101  ->getMock();
102 
103  $criterionAssignment2
104  ->expects($this->any())
105  ->method('getId')
106  ->willReturn(1);
107 
108  $criterionAssignment2
109  ->expects($this->any())
110  ->method('getCriterionId')
111  ->willReturn('usr_language');
112 
113  $criterionAssignment2
114  ->expects($this->any())
115  ->method('getCriterionValue')
116  ->willReturn($this->getCriterionConfig(['lng' => 'de']));
117 
118  $criterionAssignment3 = $this
119  ->getMockBuilder(\ilTermsOfServiceDocumentCriterionAssignment::class)
120  ->setMethods(['getId', 'getCriterionValue', 'getCriterionId'])
121  ->disableOriginalConstructor()
122  ->getMock();
123 
124  $criterionAssignment3
125  ->expects($this->any())
126  ->method('getId')
127  ->willReturn(0);
128 
129  $criterionAssignment3
130  ->expects($this->any())
131  ->method('getCriterionId')
132  ->willReturn('usr_language');
133 
134  $criterionAssignment3
135  ->expects($this->any())
136  ->method('getCriterionValue')
137  ->willReturn($this->getCriterionConfig(['lng' => 'de']));
138 
139  $criterionAssignment4 = $this
140  ->getMockBuilder(\ilTermsOfServiceDocumentCriterionAssignment::class)
141  ->disableOriginalConstructor()
142  ->setMethods(['getId', 'getCriterionValue', 'getCriterionId'])
143  ->getMock();
144 
145  $criterionAssignment4
146  ->expects($this->any())
147  ->method('getId')
148  ->willReturn(0);
149 
150  $criterionAssignment4
151  ->expects($this->any())
152  ->method('getCriterionId')
153  ->willReturn('usr_global_role');
154 
155  $criterionAssignment4
156  ->expects($this->any())
157  ->method('getCriterionValue')
158  ->willReturn($this->getCriterionConfig(['role_id' => 6]));
159 
160  return [
161  [$criterionAssignment1, $criterionAssignment2, $criterionAssignment3, $criterionAssignment4]
162  ];
163  }
164 
173  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment1,
174  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment2,
175  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment3,
176  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment4
177  ) {
178  $document1 = $this
179  ->getMockBuilder(\ilTermsOfServiceDocument::class)
180  ->disableOriginalConstructor()
181  ->setMethods(['criteria'])
182  ->getMock();
183 
184  $document1
185  ->expects($this->any())
186  ->method('criteria')
187  ->willReturn([$criterionAssignment1, $criterionAssignment2]);
188 
189  $constraint = new \ilTermsOfServiceDocumentCriterionAssignmentConstraint(
190  $this->getTypeMockForConstraint(),
191  $document1,
192  new Factory(),
193  $this->getLanguageMock()
194  );
195 
196  $this->assertTrue($constraint->accepts($criterionAssignment1));
197  $this->assertTrue($constraint->accepts($criterionAssignment2));
198  $this->assertFalse($constraint->accepts($criterionAssignment3));
199  $this->assertTrue($constraint->accepts($criterionAssignment4));
200  }
201 
210  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment1,
211  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment2,
212  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment3,
213  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment4
214  ) {
215  $document1 = $this
216  ->getMockBuilder(\ilTermsOfServiceDocument::class)
217  ->disableOriginalConstructor()
218  ->setMethods(['criteria'])
219  ->getMock();
220 
221  $document1
222  ->expects($this->any())
223  ->method('criteria')
224  ->willReturn([$criterionAssignment1, $criterionAssignment2]);
225 
226  $constraint = new \ilTermsOfServiceDocumentCriterionAssignmentConstraint(
227  $this->getTypeMockForConstraint(),
228  $document1,
229  new Factory(),
230  $this->getLanguageMock()
231  );
232 
233  $raised = false;
234 
235  try {
236  $constraint->check($criterionAssignment4);
237  } catch (\UnexpectedValueException $e) {
238  $raised = true;
239  }
240 
241  $this->assertFalse($raised);
242 
243  try {
244  $constraint->check($criterionAssignment3);
245  $raised = false;
246  } catch (\UnexpectedValueException $e) {
247  $this->assertEquals('The passed assignment must be unique for the document!', $e->getMessage());
248  $raised = true;
249  }
250 
251  $this->assertTrue($raised);
252  }
253 
262  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment1,
263  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment2,
264  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment3,
265  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment4
266  ) {
267  $document1 = $this
268  ->getMockBuilder(\ilTermsOfServiceDocument::class)
269  ->disableOriginalConstructor()
270  ->setMethods(['criteria'])
271  ->getMock();
272 
273  $document1
274  ->expects($this->any())
275  ->method('criteria')
276  ->willReturn([$criterionAssignment1, $criterionAssignment2]);
277 
278  $constraint = new \ilTermsOfServiceDocumentCriterionAssignmentConstraint(
279  $this->getTypeMockForConstraint(),
280  $document1,
281  new Factory(),
282  $this->getLanguageMock()
283  );
284 
285  $this->assertNull($constraint->problemWith($criterionAssignment1));
286  $this->assertNull($constraint->problemWith($criterionAssignment2));
287  $this->assertNull($constraint->problemWith($criterionAssignment4));
288  $this->assertInternalType('string', $constraint->problemWith($criterionAssignment3));
289  }
290 
299  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment1,
300  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment2,
301  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment3,
302  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment4
303  ) {
304  $document1 = $this
305  ->getMockBuilder(\ilTermsOfServiceDocument::class)
306  ->disableOriginalConstructor()
307  ->setMethods(['criteria'])
308  ->getMock();
309 
310  $document1
311  ->expects($this->any())
312  ->method('criteria')
313  ->willReturn([$criterionAssignment1, $criterionAssignment2]);
314 
315  $dataFavtgory = new Factory();
316 
317  $constraint = new \ilTermsOfServiceDocumentCriterionAssignmentConstraint(
318  $this->getTypeMockForConstraint(),
319  $document1,
320  $dataFavtgory,
321  $this->getLanguageMock()
322  );
323 
324  $ok = $dataFavtgory->ok($criterionAssignment1);
325  $ok2 = $dataFavtgory->ok($criterionAssignment3);
326  $error = $dataFavtgory->error('An error occurred');
327 
328  $result = $constraint->restrict($ok);
329  $this->assertTrue($result->isOk());
330 
331  $result = $constraint->restrict($ok2);
332  $this->assertTrue($result->isError());
333 
334  $result = $constraint->restrict($error);
335  $this->assertSame($error, $result);
336  }
337 
346  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment1,
347  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment2,
348  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment3,
349  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment4
350  ) {
351  $document1 = $this
352  ->getMockBuilder(\ilTermsOfServiceDocument::class)
353  ->disableOriginalConstructor()
354  ->setMethods(['criteria'])
355  ->getMock();
356 
357  $document1
358  ->expects($this->any())
359  ->method('criteria')
360  ->willReturn([$criterionAssignment1, $criterionAssignment2]);
361 
362  $constraint = new \ilTermsOfServiceDocumentCriterionAssignmentConstraint(
363  $this->getTypeMockForConstraint(),
364  $document1,
365  new Factory(),
366  $this->getLanguageMock()
367  );
368 
369  $newConstraint = $constraint->withProblemBuilder(function () {
370  return 'phpunit';
371  });
372  $this->assertEquals('phpunit', $newConstraint->problemWith($criterionAssignment3));
373  }
374 
383  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment1,
384  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment2,
385  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment3,
386  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment4
387  ) {
388  $document1 = $this
389  ->getMockBuilder(\ilTermsOfServiceDocument::class)
390  ->disableOriginalConstructor()
391  ->setMethods(['criteria'])
392  ->getMock();
393 
394  $document1
395  ->expects($this->any())
396  ->method('criteria')
397  ->willReturn([$criterionAssignment1, $criterionAssignment2]);
398 
399  $constraint = new \ilTermsOfServiceDocumentCriterionAssignmentConstraint(
400  $this->getTypeMockForConstraint(),
401  $document1,
402  new Factory(),
403  $this->getLanguageMock()
404  );
405 
406  $criterionAssignment5 = clone $criterionAssignment3;
407 
408  $constraint->accepts($criterionAssignment1);
409  $constraint->accepts($criterionAssignment2);
410  $constraint->accepts($criterionAssignment3);
411  $constraint->accepts($criterionAssignment4);
412  $constraint->accepts($criterionAssignment5);
413 
414  $this->assertEquals('The passed assignment must be unique for the document!', $constraint->problemWith($criterionAssignment3));
415  $this->assertEquals('The passed assignment must be unique for the document!', $constraint->problemWith($criterionAssignment5));
416  }
417 
426  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment1,
427  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment2,
428  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment3,
429  \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment4
430  ) {
431  $document = $this
432  ->getMockBuilder(\ilTermsOfServiceDocument::class)
433  ->disableOriginalConstructor()
434  ->setMethods(['criteria'])
435  ->getMock();
436 
437  $document
438  ->expects($this->any())
439  ->method('criteria')
440  ->willReturn([$criterionAssignment1, $criterionAssignment2]);
441 
442  $criterionTypeFactory = $this->getCriterionTypeFactoryMock();
443 
444  $criterionType1 = $this->getCriterionTypeMock('usr_global_role');
445  $criterionType2 = $this->getCriterionTypeMock('usr_language');
446 
447  $criterionType1
448  ->expects($this->any())
449  ->method('hasUniqueNature')
450  ->willReturn(false);
451 
452  $criterionType2
453  ->expects($this->any())
454  ->method('hasUniqueNature')
455  ->willReturn(true);
456 
457  $criterionTypeFactory
458  ->expects($this->any())
459  ->method('findByTypeIdent')
460  ->willReturn($criterionType2);
461 
462  $constraint = new \ilTermsOfServiceDocumentCriterionAssignmentConstraint(
463  $criterionTypeFactory,
464  $document,
465  new Factory(),
466  $this->getLanguageMock()
467  );
468 
469  $criterionWithSameNature = $this
470  ->getMockBuilder(\ilTermsOfServiceDocumentCriterionAssignment::class)
471  ->setMethods(['getId', 'getCriterionValue', 'getCriterionId'])
472  ->disableOriginalConstructor()
473  ->getMock();
474 
475  $criterionWithSameNature
476  ->expects($this->any())
477  ->method('getId')
478  ->willReturn(0);
479 
480  $criterionWithSameNature
481  ->expects($this->any())
482  ->method('getCriterionId')
483  ->willReturn('usr_language');
484 
485  $criterionWithSameNature
486  ->expects($this->any())
487  ->method('getCriterionValue')
488  ->willReturn($this->getCriterionConfig(['lng' => 'ru']));
489 
490  $this->assertFalse($constraint->accepts($criterionWithSameNature));
491  }
492 }
testConstraintExposesCorrectErrorMessagesAfterMultiAccept(\ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment1, \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment2, \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment3, \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment4)
criteriaAssignmentProvider
$result
Interface ilTermsOfServiceCriterionType.
Class ilTermsOfServiceCriterionBaseTest.
testConstraintProblemDetectionWorksAsExpected(\ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment1, \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment2, \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment3, \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment4)
criteriaAssignmentProvider
testConstraintRestrictionWorksAsExpected(\ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment1, \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment2, \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment3, \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment4)
criteriaAssignmentProvider
Builds data types.
Definition: Factory.php:14
testConstraintCheckWorksAsExpected(\ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment1, \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment2, \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment3, \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment4)
criteriaAssignmentProvider
testConstraintAcceptanceWorksAsExpected(\ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment1, \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment2, \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment3, \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment4)
criteriaAssignmentProvider
testConstraintProblemBuilderWorksAsExpected(\ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment1, \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment2, \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment3, \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment4)
criteriaAssignmentProvider
testCriterionWithSameNatureIsNotAcceptedWhenAlreadyAssigned(\ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment1, \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment2, \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment3, \ilTermsOfServiceDocumentCriterionAssignment $criterionAssignment4)
criteriaAssignmentProvider