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