ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
ilTermsOfServiceDocumentFormGUITest.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 
10 
16 {
21  {
22  $document = $this
23  ->getMockBuilder(ilTermsOfServiceDocument::class)
24  ->disableOriginalConstructor()
25  ->setMethods(['getId'])
26  ->getMock();
27 
28  $purifier = $this
29  ->getMockBuilder(ilHtmlPurifierInterface::class)
30  ->getMock();
31 
32  $user = $this
33  ->getMockBuilder(ilObjUser::class)
34  ->disableOriginalConstructor()
35  ->getMock();
36 
37  $fs = $this
38  ->getMockBuilder(Filesystem::class)
39  ->getMock();
40 
41  $fu = $this
42  ->getMockBuilder(FileUpload::class)
43  ->getMock();
44 
46  $document,
47  $purifier,
48  $user,
49  $fs,
50  $fu,
51  'action',
52  'save',
53  'cancel',
54  true
55  );
56 
57  $this->assertTrue(
58  $form->getItemByPostVar('document')->getRequired(),
59  'Failed asserting document upload is required for new documents'
60  );
61 
62  $this->assertCount(
63  2,
64  $form->getCommandButtons(),
65  'Failed asserting save and cancel buttons are given if form is editable'
66  );
67  $this->assertArrayHasKey(
68  0,
69  $form->getCommandButtons(),
70  'Failed asserting save and cancel buttons are given if form is editable'
71  );
72  $this->assertArrayHasKey(
73  1,
74  $form->getCommandButtons(),
75  'Failed asserting save and cancel buttons are given if form is editable'
76  );
77  $this->assertEquals(
78  'save',
79  $form->getCommandButtons()[0]['cmd'],
80  'Failed asserting save and cancel buttons are given if form is editable'
81  );
82  $this->assertEquals(
83  'cancel',
84  $form->getCommandButtons()[1]['cmd'],
85  'Failed asserting save and cancel buttons are given if form is editable'
86  );
87 
89  $document,
90  $purifier,
91  $user,
92  $fs,
93  $fu,
94  'action',
95  'save',
96  'cancel',
97  false
98  );
99 
100  $this->assertCount(
101  1,
102  $form->getCommandButtons(),
103  'Failed asserting only cancel button is given if form is not editable'
104  );
105  $this->assertArrayHasKey(
106  0,
107  $form->getCommandButtons(),
108  'Failed asserting only cancel button is given if form is not editable'
109  );
110  $this->assertEquals(
111  'cancel',
112  $form->getCommandButtons()[0]['cmd'],
113  'Failed asserting only cancel button is given if form is not editable'
114  );
115  }
116 
121  {
122  $document = $this
123  ->getMockBuilder(ilTermsOfServiceDocument::class)
124  ->disableOriginalConstructor()
125  ->setMethods(['getId', 'fetchAllCriterionAssignments'])
126  ->getMock();
127 
128  $document
129  ->expects($this->any())
130  ->method('fetchAllCriterionAssignments');
131 
132  $purifier = $this
133  ->getMockBuilder(ilHtmlPurifierInterface::class)
134  ->getMock();
135 
136  $uploadResult = new UploadResult(
137  'phpunit',
138  1024,
139  'text/xml',
140  $this->getMockBuilder(ImmutableStringMap::class)->getMock(),
141  new ProcessingStatus(ProcessingStatus::OK, 'uploaded'),
142  '/tmp'
143  );
144 
145  $user = $this
146  ->getMockBuilder(ilObjUser::class)
147  ->disableOriginalConstructor()
148  ->setMethods(['getId'])
149  ->getMock();
150 
151  $user
152  ->expects($this->exactly(2))
153  ->method('getId')
154  ->willReturn(6);
155 
156  $fs = $this
157  ->getMockBuilder(Filesystem::class)
158  ->getMock();
159 
160  $fs
161  ->expects($this->exactly(2))
162  ->method('has')
163  ->with('/agreements/' . $uploadResult->getName())
164  ->willReturn(true);
165 
166  $fs
167  ->expects($this->exactly(2))
168  ->method('read')
169  ->with('/agreements/' . $uploadResult->getName())
170  ->willReturn('phpunit');
171 
172  $purifier
173  ->expects($this->atLeast(1))
174  ->method('purify')
175  ->with('phpunit')
176  ->willReturnArgument(0);
177 
178  $fs
179  ->expects($this->exactly(2))
180  ->method('delete')
181  ->with('/agreements/' . $uploadResult->getName());
182 
183  $fu = $this
184  ->getMockBuilder(FileUpload::class)
185  ->setMethods([
186  'moveFilesTo',
187  'uploadSizeLimit',
188  'register',
189  'hasBeenProcessed',
190  'hasUploads',
191  'process',
192  'getResults',
193  'moveOneFileTo'
194  ])
195  ->getMock();
196 
197  $fu
198  ->expects($this->any())
199  ->method('hasUploads')
200  ->willReturn(true);
201 
202  $fu
203  ->expects($this->exactly(2))
204  ->method('hasBeenProcessed')
205  ->willReturn(false);
206 
207  $fu
208  ->expects($this->exactly(2))
209  ->method('process');
210 
211  $fu
212  ->expects($this->exactly(2))
213  ->method('getResults')
214  ->willReturn([
215  0 => $uploadResult
216  ]);
217 
218  $fu
219  ->expects($this->exactly(2))
220  ->method('moveOneFileTo')
221  ->with(
222  $uploadResult,
223  '/agreements',
224  Location::TEMPORARY,
225  $this->isEmpty(),
226  $this->isTrue()
227  );
228 
229  $this->setGlobalVariable('upload', $fu);
230 
231  $documentConnector = $this->getMockBuilder(arConnector::class)->getMock();
232  $criterionConnector = $this->getMockBuilder(arConnector::class)->getMock();
233 
234  $expectedSortingValueExistingDocuments = 10;
235 
236  $documentConnector
237  ->expects($this->once())
238  ->method('readSet')
239  ->willReturnCallback(function () use ($expectedSortingValueExistingDocuments) {
240  return [
241  [
242  'id' => 666,
243  'title' => 'another',
244  'sorting' => $expectedSortingValueExistingDocuments - 1,
245  ]
246  ];
247  });
248 
249  $criterionConnector
250  ->expects($this->once())
251  ->method('readSet')
252  ->willReturn([]);
253 
254  arConnectorMap::register(new ilTermsOfServiceDocument(), $documentConnector);
256  arConnectorMap::register($document, $documentConnector);
257 
258  $form = $this->getMockBuilder(ilTermsOfServiceDocumentFormGUI::class)
259  ->setConstructorArgs([
260  $document,
261  $purifier,
262  $user,
263  $fs,
264  $fu,
265  'action',
266  'save',
267  'cancel',
268  true
269  ])
270  ->setMethods(['checkInput'])
271  ->getMock();
272 
273  $form
274  ->expects($this->once())
275  ->method('checkInput')
276  ->willReturn(true);
277 
278  $_FILES['document'] = [];
279  $_POST = [
280  'title' => 'phpunit',
281  'document' => '',
282  '' => ''
283  ];
284  $form->setCheckInputCalled(true);
285 
286  $this->assertTrue($form->saveObject());
287  $this->assertFalse($form->hasTranslatedError());
288  $this->assertEmpty($form->getTranslatedError());
289  $this->assertEquals(
290  $expectedSortingValueExistingDocuments,
291  $document->getSorting(),
292  'Failed asserting that the sorting of the new document equals the maximum incremented by one when other documents exist'
293  );
294 
295  $documentConnector = $this->getMockBuilder(arConnector::class)->getMock();
296 
297  $documentConnector
298  ->expects($this->once())
299  ->method('readSet')
300  ->willReturnCallback(function () {
301  return [];
302  });
303 
304  arConnectorMap::register(new ilTermsOfServiceDocument(), $documentConnector);
305 
306  $form = $this->getMockBuilder(ilTermsOfServiceDocumentFormGUI::class)
307  ->setConstructorArgs([
308  $document,
309  $purifier,
310  $user,
311  $fs,
312  $fu,
313  'action',
314  'save',
315  'cancel',
316  true
317  ])
318  ->setMethods(['checkInput'])
319  ->getMock();
320 
321  $form
322  ->expects($this->once())
323  ->method('checkInput')
324  ->willReturn(true);
325 
326  $form->setCheckInputCalled(true);
327 
328  $this->assertTrue($form->saveObject());
329  $this->assertFalse($form->hasTranslatedError());
330  $this->assertEmpty($form->getTranslatedError());
331  $this->assertEquals(
332  1,
333  $document->getSorting(),
334  'Failed asserting that the sorting of the new document equals 1 when no other document exists'
335  );
336  }
337 
342  {
343  $document = $this
344  ->getMockBuilder(ilTermsOfServiceDocument::class)
345  ->disableOriginalConstructor()
346  ->setMethods(['getId'])
347  ->getMock();
348 
349  $document
350  ->expects($this->any())
351  ->method('getId')
352  ->willReturn(1);
353 
354  $purifier = $this
355  ->getMockBuilder(ilHtmlPurifierInterface::class)
356  ->getMock();
357 
358  $user = $this
359  ->getMockBuilder(ilObjUser::class)
360  ->disableOriginalConstructor()
361  ->setMethods(['getId'])
362  ->getMock();
363 
364  $fs = $this
365  ->getMockBuilder(Filesystem::class)
366  ->getMock();
367 
368  $fu = $this
369  ->getMockBuilder(FileUpload::class)
370  ->getMock();
371 
373  $document,
374  $purifier,
375  $user,
376  $fs,
377  $fu,
378  'action',
379  'save',
380  'cancel',
381  true
382  );
383 
384  $this->assertFalse(
385  $form->getItemByPostVar('document')->getRequired(),
386  'Failed asserting document upload is not required for existing documents'
387  );
388  }
389 
394  {
395  $expectedSorting = 10;
396 
397  $document = $this
398  ->getMockBuilder(ilTermsOfServiceDocument::class)
399  ->disableOriginalConstructor()
400  ->setMethods(['fetchAllCriterionAssignments'])
401  ->getMock();
402 
403  $document->setId(4711);
404  $document->setTitle('phpunit');
405  $document->setSorting($expectedSorting);
406 
407  $purifier = $this
408  ->getMockBuilder(ilHtmlPurifierInterface::class)
409  ->getMock();
410 
411  $user = $this
412  ->getMockBuilder(ilObjUser::class)
413  ->disableOriginalConstructor()
414  ->setMethods(['getId'])
415  ->getMock();
416 
417  $user
418  ->expects($this->once())
419  ->method('getId')
420  ->willReturn(6);
421 
422  $fs = $this
423  ->getMockBuilder(Filesystem::class)
424  ->getMock();
425 
426  $fu = $this
427  ->getMockBuilder(FileUpload::class)
428  ->setMethods([
429  'moveFilesTo',
430  'uploadSizeLimit',
431  'register',
432  'hasBeenProcessed',
433  'hasUploads',
434  'process',
435  'getResults',
436  'moveOneFileTo'
437  ])
438  ->getMock();
439 
440  $fu
441  ->expects($this->any())
442  ->method('hasUploads')
443  ->willReturn(false);
444 
445  $this->setGlobalVariable('upload', $fu);
446 
447  $documentConnector = $this->getMockBuilder(arConnector::class)->getMock();
448 
449  arConnectorMap::register(new ilTermsOfServiceDocument(), $documentConnector);
450  arConnectorMap::register($document, $documentConnector);
451 
452  $form = $this->getMockBuilder(ilTermsOfServiceDocumentFormGUI::class)
453  ->setConstructorArgs([
454  $document,
455  $purifier,
456  $user,
457  $fs,
458  $fu,
459  'action',
460  'save',
461  'cancel',
462  true
463  ])
464  ->setMethods(['checkInput'])
465  ->getMock();
466 
467  $form
468  ->expects($this->once())
469  ->method('checkInput')
470  ->willReturn(true);
471 
472  $_POST = [
473  'title' => 'phpunit',
474  'document' => '',
475  '' => ''
476  ];
477  $form->setCheckInputCalled(true);
478 
479  $this->assertTrue($form->saveObject());
480  $this->assertFalse($form->hasTranslatedError());
481  $this->assertEmpty($form->getTranslatedError());
482  $this->assertEquals(
483  $expectedSorting,
484  $document->getSorting(),
485  'Failed asserting that the sorting of the existing document has not been changed'
486  );
487  }
488 
493  {
494  $lng = $this->getLanguageMock();
495 
496  $lng
497  ->expects($this->any())
498  ->method('txt')
499  ->willReturn('translation');
500 
501  $this->setGlobalVariable('lng', $lng);
502 
503  $document = $this
504  ->getMockBuilder(ilTermsOfServiceDocument::class)
505  ->disableOriginalConstructor()
506  ->setMethods(['getId', 'fetchAllCriterionAssignments'])
507  ->getMock();
508 
509  $purifier = $this
510  ->getMockBuilder(ilHtmlPurifierInterface::class)
511  ->getMock();
512 
513  $user = $this
514  ->getMockBuilder(ilObjUser::class)
515  ->disableOriginalConstructor()
516  ->setMethods()
517  ->getMock();
518 
519  $fu = $this
520  ->getMockBuilder(FileUpload::class)
521  ->setMethods([
522  'moveFilesTo',
523  'uploadSizeLimit',
524  'register',
525  'hasBeenProcessed',
526  'hasUploads',
527  'process',
528  'getResults',
529  'moveOneFileTo'
530  ])
531  ->getMock();
532 
533  $fu
534  ->expects($this->exactly(3))
535  ->method('hasUploads')
536  ->willReturn(true);
537 
538  $fu
539  ->expects($this->exactly(3))
540  ->method('hasBeenProcessed')
541  ->willReturn(false);
542 
543  $fu
544  ->expects($this->exactly(3))
545  ->method('process');
546 
547  $uploadResult = new UploadResult(
548  'phpunit',
549  1024,
550  'text/xml',
551  $this->getMockBuilder(ImmutableStringMap::class)->getMock(),
552  new ProcessingStatus(ProcessingStatus::OK, 'uploaded'),
553  '/tmp'
554  );
555 
556  $uploadFailingResult = new UploadResult(
557  'phpunit',
558  1024,
559  'text/xml',
560  $this->getMockBuilder(ImmutableStringMap::class)->getMock(),
561  new ProcessingStatus(ProcessingStatus::REJECTED, 'not uploaded'),
562  '/tmp'
563  );
564 
565  $fu
566  ->expects($this->exactly(3))
567  ->method('getResults')
568  ->willReturnOnConsecutiveCalls(
569  [false],
570  [0 => $uploadFailingResult],
571  [0 => $uploadResult]
572  );
573 
574  $fs = $this
575  ->getMockBuilder(Filesystem::class)
576  ->getMock();
577 
578  $fs
579  ->expects($this->once())
580  ->method('has')
581  ->with('/agreements/' . $uploadResult->getName())
582  ->willReturn(false);
583 
584  $this->setGlobalVariable('upload', $fu);
585 
586  $documentConnector = $this->getMockBuilder(arConnector::class)->getMock();
587 
588  arConnectorMap::register(new ilTermsOfServiceDocument(), $documentConnector);
589  arConnectorMap::register($document, $documentConnector);
590 
591  $form = $this->getMockBuilder(ilTermsOfServiceDocumentFormGUI::class)
592  ->setConstructorArgs([
593  $document,
594  $purifier,
595  $user,
596  $fs,
597  $fu,
598  'action',
599  'save',
600  'cancel',
601  true
602  ])
603  ->setMethods(['checkInput'])
604  ->getMock();
605 
606  $form
607  ->expects($this->once())
608  ->method('checkInput')
609  ->willReturn(true);
610 
611  $_POST = [
612  'title' => '',
613  'document' => '',
614  '' => ''
615  ];
616  $form->setCheckInputCalled(true);
617 
618  $this->assertFalse($form->saveObject());
619  $this->assertTrue($form->hasTranslatedError());
620  $this->assertNotEmpty($form->getTranslatedError());
621 
622  $form = $this->getMockBuilder(ilTermsOfServiceDocumentFormGUI::class)
623  ->setConstructorArgs([
624  $document,
625  $purifier,
626  $user,
627  $fs,
628  $fu,
629  'action',
630  'save',
631  'cancel',
632  true
633  ])
634  ->setMethods(['checkInput'])
635  ->getMock();
636 
637  $form
638  ->expects($this->once())
639  ->method('checkInput')
640  ->willReturn(true);
641 
642  $form->setCheckInputCalled(true);
643 
644  $this->assertFalse($form->saveObject());
645  $this->assertTrue($form->hasTranslatedError());
646  $this->assertNotEmpty($form->getTranslatedError());
647 
648  $form = $this->getMockBuilder(ilTermsOfServiceDocumentFormGUI::class)
649  ->setConstructorArgs([
650  $document,
651  $purifier,
652  $user,
653  $fs,
654  $fu,
655  'action',
656  'save',
657  'cancel',
658  true
659  ])
660  ->setMethods(['checkInput'])
661  ->getMock();
662 
663  $form
664  ->expects($this->once())
665  ->method('checkInput')
666  ->willReturn(true);
667 
668  $form->setCheckInputCalled(true);
669 
670  $this->assertFalse($form->saveObject());
671  $this->assertTrue($form->hasTranslatedError());
672  $this->assertNotEmpty($form->getTranslatedError());
673  }
674 }
Class ilTermsOfServiceDocumentFormGUI.
setGlobalVariable(string $name, $value)
static register(ActiveRecord $ar, arConnector $connector)
Class ilTermsOfServiceAcceptanceHistoryCriteriaBagTest.
Class ilTermsOfServiceBaseTest.
Class ilTermsOfServiceDocument.
$lng
$_POST["username"]