ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ilTermsOfServiceCriterionFormGUITest.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
9 {
14  {
15  $document = $this
16  ->getMockBuilder(\ilTermsOfServiceDocument::class)
17  ->disableOriginalConstructor()
18  ->getMock();
19 
20  $criterionAssignment = $this
21  ->getMockBuilder(\ilTermsOfServiceDocumentCriterionAssignment::class)
22  ->disableOriginalConstructor()
23  ->setMethods(['getId', 'getCriterionId'])
24  ->getMock();
25 
26  $criterionAssignment
27  ->expects($this->any())
28  ->method('getId')
29  ->willReturn(0);
30 
31  $criterionAssignment
32  ->expects($this->any())
33  ->method('getCriterionId')
34  ->willReturn('');
35 
36  $criterionTypeFactory = $this
37  ->getMockBuilder(\ilTermsOfServiceCriterionTypeFactoryInterface::class)
38  ->getMock();
39 
40  $criterionType1 = $this
41  ->getMockBuilder(\ilTermsOfServiceCriterionType::class)
42  ->getMock();
43 
44  $criterionType1
45  ->expects($this->any())
46  ->method('getTypeIdent')
47  ->willReturn('dummy1');
48 
49  $criterionType1
50  ->expects($this->any())
51  ->method('ui')
52  ->willReturn($this->getMockBuilder(\ilTermsOfServiceCriterionTypeGUI::class)->getMock());
53 
54  $criterionType2 = $this
55  ->getMockBuilder(\ilTermsOfServiceCriterionType::class)
56  ->getMock();
57 
58  $criterionType2
59  ->expects($this->any())
60  ->method('getTypeIdent')
61  ->willReturn('dummy2');
62 
63  $criterionType2
64  ->expects($this->any())
65  ->method('ui')
66  ->willReturn($this->getMockBuilder(\ilTermsOfServiceCriterionTypeGUI::class)->getMock());
67 
68  $criterionTypeFactory
69  ->expects($this->once())
70  ->method('getTypesByIdentMap')
71  ->willReturn([
72  $criterionType1, $criterionType2
73  ]);
74 
75  $user = $this
76  ->getMockBuilder(\ilObjUser::class)
77  ->disableOriginalConstructor()
78  ->getMock();
79 
80  $form = new \ilTermsOfServiceCriterionFormGUI(
81  $document,
82  $criterionAssignment,
83  $criterionTypeFactory,
84  $user,
85  '',
86  'save',
87  'cancel'
88  );
89 
90  $this->assertEquals($criterionType1->getTypeIdent(), $form->getItemByPostVar('criterion')->getValue());
91  }
92 
97  {
98  $document = $this
99  ->getMockBuilder(\ilTermsOfServiceDocument::class)
100  ->disableOriginalConstructor()
101  ->getMock();
102 
103  $criterionAssignment = $this
104  ->getMockBuilder(\ilTermsOfServiceDocumentCriterionAssignment::class)
105  ->disableOriginalConstructor()
106  ->setMethods(['getId', 'getCriterionId', 'getCriterionValue'])
107  ->getMock();
108 
109  $criterionAssignment
110  ->expects($this->any())
111  ->method('getId')
112  ->willReturn(1);
113 
114  $criterionAssignment
115  ->expects($this->any())
116  ->method('getCriterionId')
117  ->willReturn('dummy2');
118 
119  $criterionAssignment
120  ->expects($this->any())
121  ->method('getCriterionValue')
122  ->willReturn(new \ilTermsOfServiceCriterionConfig([]));
123 
124  $criterionTypeFactory = $this
125  ->getMockBuilder(\ilTermsOfServiceCriterionTypeFactoryInterface::class)
126  ->getMock();
127 
128  $criterionType1 = $this
129  ->getMockBuilder(\ilTermsOfServiceCriterionType::class)
130  ->getMock();
131 
132  $criterionType1
133  ->expects($this->any())
134  ->method('getTypeIdent')
135  ->willReturn('dummy1');
136 
137  $criterionType1
138  ->expects($this->any())
139  ->method('ui')
140  ->willReturn($this->getMockBuilder(\ilTermsOfServiceCriterionTypeGUI::class)->getMock());
141 
142  $criterionType2 = $this
143  ->getMockBuilder(\ilTermsOfServiceCriterionType::class)
144  ->getMock();
145 
146  $criterionType2
147  ->expects($this->any())
148  ->method('getTypeIdent')
149  ->willReturn('dummy2');
150 
151  $criterionType2
152  ->expects($this->any())
153  ->method('ui')
154  ->willReturn($this->getMockBuilder(\ilTermsOfServiceCriterionTypeGUI::class)->getMock());
155 
156  $criterionTypeFactory
157  ->expects($this->once())
158  ->method('getTypesByIdentMap')
159  ->willReturn([
160  $criterionType1, $criterionType2
161  ]);
162 
163  $user = $this
164  ->getMockBuilder(\ilObjUser::class)
165  ->disableOriginalConstructor()
166  ->getMock();
167 
168  $form = new \ilTermsOfServiceCriterionFormGUI(
169  $document,
170  $criterionAssignment,
171  $criterionTypeFactory,
172  $user,
173  '',
174  'save',
175  'cancel'
176  );
177 
178  $this->assertEquals($criterionType2->getTypeIdent(), $form->getItemByPostVar('criterion')->getValue());
179  }
180 
185  {
186  $document = $this
187  ->getMockBuilder(\ilTermsOfServiceDocument::class)
188  ->disableOriginalConstructor()
189  ->getMock();
190 
191  $document
192  ->expects($this->once())
193  ->method('save');
194 
195  $document
196  ->expects($this->once())
197  ->method('attachCriterion');
198 
199  $criterionAssignment = $this
200  ->getMockBuilder(\ilTermsOfServiceDocumentCriterionAssignment::class)
201  ->disableOriginalConstructor()
202  ->setMethods(['getId', 'getCriterionId', 'getCriterionValue'])
203  ->getMock();
204 
205  $criterionAssignment
206  ->expects($this->any())
207  ->method('getId')
208  ->willReturn(0);
209 
210  $criterionAssignment
211  ->expects($this->any())
212  ->method('getCriterionId')
213  ->willReturn('');
214 
215  $criterionTypeFactory = $this
216  ->getMockBuilder(\ilTermsOfServiceCriterionTypeFactoryInterface::class)
217  ->getMock();
218 
219  $criterionType1 = $this
220  ->getMockBuilder(\ilTermsOfServiceCriterionType::class)
221  ->getMock();
222 
223  $criterionType1
224  ->expects($this->any())
225  ->method('getTypeIdent')
226  ->willReturn('dummy1');
227 
228  $criterionType1
229  ->expects($this->any())
230  ->method('ui')
231  ->willReturn($this->getMockBuilder(\ilTermsOfServiceCriterionTypeGUI::class)->getMock());
232 
233  $criterionType2 = $this
234  ->getMockBuilder(\ilTermsOfServiceCriterionType::class)
235  ->getMock();
236 
237  $criterionType2
238  ->expects($this->any())
239  ->method('getTypeIdent')
240  ->willReturn('dummy2');
241 
242  $criterionType2
243  ->expects($this->any())
244  ->method('ui')
245  ->willReturn($this->getMockBuilder(\ilTermsOfServiceCriterionTypeGUI::class)->getMock());
246 
247  $criterionTypeFactory
248  ->expects($this->once())
249  ->method('getTypesByIdentMap')
250  ->willReturn([
251  $criterionType1, $criterionType2
252  ]);
253 
254  $criterionTypeFactory
255  ->expects($this->once())
256  ->method('findByTypeIdent')
257  ->willReturn($criterionType1);
258 
259  $user = $this
260  ->getMockBuilder(\ilObjUser::class)
261  ->disableOriginalConstructor()
262  ->setMethods(['getId'])
263  ->getMock();
264 
265  $user
266  ->expects($this->any())
267  ->method('getId')
268  ->willReturn(6);
269 
270  $form = $this->getMockBuilder(\ilTermsOfServiceCriterionFormGUI::class)
271  ->setConstructorArgs([
272  $document, $criterionAssignment, $criterionTypeFactory, $user,
273  'action', 'save', 'cancel'
274  ])
275  ->setMethods(['checkInput'])
276  ->getMock();
277 
278  $form
279  ->expects($this->once())
280  ->method('checkInput')
281  ->willReturn(true);
282 
283  $_POST = [
284  'criterion' => $criterionType1->getTypeIdent()
285  ];
286 
287  $form->setCheckInputCalled(true);
288 
289  $this->assertTrue($form->saveObject());
290  $this->assertFalse($form->hasTranslatedError());
291  $this->assertEmpty($form->getTranslatedError());
292  }
293 
298  {
299  $lng = $this->getLanguageMock();
300 
301  $lng
302  ->expects($this->any())
303  ->method('txt')
304  ->willReturn('translation');
305 
306  $this->setGlobalVariable('lng', $lng);
307 
308  $document = $this
309  ->getMockBuilder(\ilTermsOfServiceDocument::class)
310  ->disableOriginalConstructor()
311  ->getMock();
312 
313  $document
314  ->expects($this->never())
315  ->method('save');
316 
317  $document
318  ->expects($this->never())
319  ->method('attachCriterion');
320 
321  $criterionAssignment = $this
322  ->getMockBuilder(\ilTermsOfServiceDocumentCriterionAssignment::class)
323  ->disableOriginalConstructor()
324  ->setMethods(['getId', 'getCriterionId', 'getCriterionValue'])
325  ->getMock();
326 
327  $criterionAssignment
328  ->expects($this->any())
329  ->method('getId')
330  ->willReturn(1);
331 
332  $criterionAssignment
333  ->expects($this->any())
334  ->method('getCriterionId')
335  ->willReturn('usr_global_role');
336 
337  $criterionAssignment
338  ->expects($this->any())
339  ->method('getCriterionValue')
340  ->willReturn(new \ilTermsOfServiceCriterionConfig(['role_id' => 4]));
341 
342  $criterionTypeFactory = $this
343  ->getMockBuilder(\ilTermsOfServiceCriterionTypeFactoryInterface::class)
344  ->getMock();
345 
346  $criterionType1 = $this
347  ->getMockBuilder(\ilTermsOfServiceCriterionType::class)
348  ->getMock();
349 
350  $criterionType1
351  ->expects($this->any())
352  ->method('getTypeIdent')
353  ->willReturn('dummy1');
354 
355  $criterionType1
356  ->expects($this->any())
357  ->method('ui')
358  ->willReturn($this->getMockBuilder(\ilTermsOfServiceCriterionTypeGUI::class)->getMock());
359 
360  $criterionType2 = $this
361  ->getMockBuilder(\ilTermsOfServiceCriterionType::class)
362  ->getMock();
363 
364  $criterionType2
365  ->expects($this->any())
366  ->method('getTypeIdent')
367  ->willReturn('dummy2');
368 
369  $criterionTypeGui2 = $this->getMockBuilder(\ilTermsOfServiceCriterionTypeGUI::class)->getMock();
370 
371  $criterionTypeGui2
372  ->expects($this->any())
373  ->method('getConfigByForm')
374  ->willReturn($criterionAssignment->getCriterionValue());
375 
376  $criterionType2
377  ->expects($this->any())
378  ->method('ui')
379  ->willReturn($criterionTypeGui2);
380 
381  $criterionTypeFactory
382  ->expects($this->once())
383  ->method('getTypesByIdentMap')
384  ->willReturn([
385  $criterionType1, $criterionType2
386  ]);
387 
388  $criterionTypeFactory
389  ->expects($this->exactly(2))
390  ->method('findByTypeIdent')
391  ->willReturnOnConsecutiveCalls(
392  $this->throwException(new \ilTermsOfServiceCriterionTypeNotFoundException('')),
393  $criterionType1
394  );
395 
396  $anotherCriterionAssignment = $this
397  ->getMockBuilder(\ilTermsOfServiceDocumentCriterionAssignment::class)
398  ->disableOriginalConstructor()
399  ->setMethods(['getId', 'getCriterionId', 'getCriterionValue'])
400  ->getMock();
401 
402  $anotherCriterionAssignment
403  ->expects($this->any())
404  ->method('getId')
405  ->willReturn(2);
406 
407  $anotherCriterionAssignment
408  ->expects($this->any())
409  ->method('getCriterionId')
410  ->willReturn('usr_global_role');
411 
412  $anotherCriterionAssignment
413  ->expects($this->any())
414  ->method('getCriterionValue')
415  ->willReturn(new \ilTermsOfServiceCriterionConfig(['role_id' => 4]));
416 
417  $document
418  ->expects($this->once())
419  ->method('criteria')
420  ->willReturn([$anotherCriterionAssignment]);
421 
422  $user = $this
423  ->getMockBuilder(\ilObjUser::class)
424  ->disableOriginalConstructor()
425  ->setMethods(['getId'])
426  ->getMock();
427 
428  $user
429  ->expects($this->any())
430  ->method('getId')
431  ->willReturn(6);
432 
433  $form = $this->getMockBuilder(\ilTermsOfServiceCriterionFormGUI::class)
434  ->setConstructorArgs([
435  $document, $criterionAssignment, $criterionTypeFactory, $user,
436  'action', 'save', 'cancel'
437  ])
438  ->setMethods(['checkInput'])
439  ->getMock();
440 
441  $form
442  ->expects($this->exactly(2))
443  ->method('checkInput')
444  ->willReturn(true);
445 
446  $_POST = [
447  'criterion' => $criterionType1->getTypeIdent()
448  ];
449 
450  $form->setCheckInputCalled(true);
451 
452  $this->assertFalse(
453  $form->saveObject(),
454  'Failed asserting form cannot be saved selected criterion type was not found'
455  );
456  $this->assertTrue($form->hasTranslatedError());
457  $this->assertNotEmpty($form->getTranslatedError());
458 
459  $this->assertFalse(
460  $form->saveObject(),
461  'Failed asserting form cannot be saved selected criterion type was already assigned to document'
462  );
463  $this->assertTrue($form->hasTranslatedError());
464  $this->assertNotEmpty($form->getTranslatedError());
465  }
466 }
setGlobalVariable(string $name, $value)
Class ilTermsOfServiceCriterionConfig.
Class ilTermsOfServiceBaseTest.
if(isset($_POST['submit'])) $form
Class ilTermsOfServiceCriterionFormGUITest.
$lng
once($eventName, callable $callBack, $priority=100)
Subscribe to an event exactly once.
$user
Definition: migrateto20.php:57
$_POST["username"]