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