ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ilTermsOfServiceDocumentFormGUITest.php
Go to the documentation of this file.
1 <?php
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 
45  $form = new \ilTermsOfServiceDocumentFormGUI(
46  $document,
47  $purifier,
48  $user,
49  $fs,
50  $fu,
51  'action',
52  'save',
53  'cancel',
54  true
55  );
56 
57  $this->assertTrue($form->getItemByPostVar('document')->getRequired(), 'Failed asserting document upload is required for new documents');
58 
59  $this->assertCount(2, $form->getCommandButtons(), 'Failed asserting save and cancel buttons are given if form is editable');
60  $this->assertArrayHasKey(0, $form->getCommandButtons(), 'Failed asserting save and cancel buttons are given if form is editable');
61  $this->assertArrayHasKey(1, $form->getCommandButtons(), 'Failed asserting save and cancel buttons are given if form is editable');
62  $this->assertEquals('save', $form->getCommandButtons()[0]['cmd'], 'Failed asserting save and cancel buttons are given if form is editable');
63  $this->assertEquals('cancel', $form->getCommandButtons()[1]['cmd'], 'Failed asserting save and cancel buttons are given if form is editable');
64 
65  $form = new \ilTermsOfServiceDocumentFormGUI(
66  $document,
67  $purifier,
68  $user,
69  $fs,
70  $fu,
71  'action',
72  'save',
73  'cancel',
74  false
75  );
76 
77  $this->assertCount(1, $form->getCommandButtons(), 'Failed asserting only cancel button is given if form is not editable');
78  $this->assertArrayHasKey(0, $form->getCommandButtons(), 'Failed asserting only cancel button is given if form is not editable');
79  $this->assertEquals('cancel', $form->getCommandButtons()[0]['cmd'], 'Failed asserting only cancel button is given if form is not editable');
80  }
81 
86  {
87  $document = $this
88  ->getMockBuilder(\ilTermsOfServiceDocument::class)
89  ->disableOriginalConstructor()
90  ->setMethods(['getId', 'fetchAllCriterionAssignments'])
91  ->getMock();
92 
93  $document
94  ->expects($this->any())
95  ->method('fetchAllCriterionAssignments');
96 
97  $purifier = $this
98  ->getMockBuilder(\ilHtmlPurifierInterface::class)
99  ->getMock();
100 
101  $uploadResult = new UploadResult(
102  'phpunit',
103  1024,
104  'text/xml',
105  $this->getMockBuilder(ImmutableStringMap::class)->getMock(),
106  new ProcessingStatus(ProcessingStatus::OK, 'uploaded'),
107  '/tmp'
108  );
109 
110  $user = $this
111  ->getMockBuilder(\ilObjUser::class)
112  ->disableOriginalConstructor()
113  ->setMethods(['getId'])
114  ->getMock();
115 
116  $user
117  ->expects($this->exactly(2))
118  ->method('getId')
119  ->willReturn(6);
120 
121  $fs = $this
122  ->getMockBuilder(Filesystem::class)
123  ->getMock();
124 
125  $fs
126  ->expects($this->exactly(2))
127  ->method('has')
128  ->with('/agreements/' . $uploadResult->getName())
129  ->willReturn(true);
130 
131  $fs
132  ->expects($this->exactly(2))
133  ->method('read')
134  ->with('/agreements/' . $uploadResult->getName())
135  ->willReturn('phpunit');
136 
137  $purifier
138  ->expects($this->atLeast(1))
139  ->method('purify')
140  ->with('phpunit')
141  ->willReturnArgument(0);
142 
143  $fs
144  ->expects($this->exactly(2))
145  ->method('delete')
146  ->with('/agreements/' . $uploadResult->getName());
147 
148  $fu = $this
149  ->getMockBuilder(FileUpload::class)
150  ->setMethods(['moveFilesTo', 'uploadSizeLimit', 'register', 'hasBeenProcessed', 'hasUploads', 'process', 'getResults', 'moveOneFileTo'])
151  ->getMock();
152 
153  $fu
154  ->expects($this->any())
155  ->method('hasUploads')
156  ->willReturn(true);
157 
158  $fu
159  ->expects($this->exactly(2))
160  ->method('hasBeenProcessed')
161  ->willReturn(false);
162 
163  $fu
164  ->expects($this->exactly(2))
165  ->method('process');
166 
167  $fu
168  ->expects($this->exactly(2))
169  ->method('getResults')
170  ->willReturn([
171  0 => $uploadResult
172  ]);
173 
174  $fu
175  ->expects($this->exactly(2))
176  ->method('moveOneFileTo')
177  ->with(
178  $uploadResult,
179  '/agreements',
180  Location::TEMPORARY,
181  $this->isEmpty(),
182  $this->isTrue()
183  );
184 
185  $this->setGlobalVariable('upload', $fu);
186 
187  $documentConnector = $this->getMockBuilder(\arConnector::class)->getMock();
188  $criterionConnector = $this->getMockBuilder(\arConnector::class)->getMock();
189 
190  $expectedSortingValueExistingDocuments = 10;
191 
192  $documentConnector
193  ->expects($this->once())
194  ->method('readSet')
195  ->willReturnCallback(function () use ($expectedSortingValueExistingDocuments) {
196  return [[
197  'id' => 666,
198  'title' => 'another',
199  'sorting' => $expectedSortingValueExistingDocuments - 1,
200  ]];
201  });
202 
203  $criterionConnector
204  ->expects($this->once())
205  ->method('readSet')
206  ->willReturn([]);
207 
208  \arConnectorMap::register(new \ilTermsOfServiceDocument(), $documentConnector);
210  \arConnectorMap::register($document, $documentConnector);
211 
212  $form = $this->getMockBuilder(\ilTermsOfServiceDocumentFormGUI::class)
213  ->setConstructorArgs([
214  $document, $purifier, $user, $fs, $fu,
215  'action', 'save', 'cancel',
216  true
217  ])
218  ->setMethods(['checkInput'])
219  ->getMock();
220 
221  $form
222  ->expects($this->once())
223  ->method('checkInput')
224  ->willReturn(true);
225 
226  $_FILES['document'] = [];
227  $_POST = [
228  'title' => 'phpunit',
229  'document' => '',
230  '' => ''
231  ];
232  $form->setCheckInputCalled(true);
233 
234  $this->assertTrue($form->saveObject());
235  $this->assertFalse($form->hasTranslatedError());
236  $this->assertEmpty($form->getTranslatedError());
237  $this->assertEquals(
238  $expectedSortingValueExistingDocuments,
239  $document->getSorting(),
240  'Failed asserting that the sorting of the new document equals the maximum incremented by one when other documents exist'
241  );
242 
243  $documentConnector = $this->getMockBuilder(\arConnector::class)->getMock();
244 
245  $documentConnector
246  ->expects($this->once())
247  ->method('readSet')
248  ->willReturnCallback(function () {
249  return [];
250  });
251 
252  \arConnectorMap::register(new \ilTermsOfServiceDocument(), $documentConnector);
253 
254  $form = $this->getMockBuilder(\ilTermsOfServiceDocumentFormGUI::class)
255  ->setConstructorArgs([
256  $document, $purifier, $user, $fs, $fu,
257  'action', 'save', 'cancel',
258  true
259  ])
260  ->setMethods(['checkInput'])
261  ->getMock();
262 
263  $form
264  ->expects($this->once())
265  ->method('checkInput')
266  ->willReturn(true);
267 
268  $form->setCheckInputCalled(true);
269 
270  $this->assertTrue($form->saveObject());
271  $this->assertFalse($form->hasTranslatedError());
272  $this->assertEmpty($form->getTranslatedError());
273  $this->assertEquals(
274  1,
275  $document->getSorting(),
276  'Failed asserting that the sorting of the new document equals 1 when no other document exists'
277  );
278  }
279 
284  {
285  $document = $this
286  ->getMockBuilder(\ilTermsOfServiceDocument::class)
287  ->disableOriginalConstructor()
288  ->setMethods(['getId'])
289  ->getMock();
290 
291  $document
292  ->expects($this->any())
293  ->method('getId')
294  ->willReturn(1);
295 
296  $purifier = $this
297  ->getMockBuilder(\ilHtmlPurifierInterface::class)
298  ->getMock();
299 
300  $user = $this
301  ->getMockBuilder(\ilObjUser::class)
302  ->disableOriginalConstructor()
303  ->setMethods(['getId'])
304  ->getMock();
305 
306  $fs = $this
307  ->getMockBuilder(Filesystem::class)
308  ->getMock();
309 
310  $fu = $this
311  ->getMockBuilder(FileUpload::class)
312  ->getMock();
313 
314  $form = new \ilTermsOfServiceDocumentFormGUI(
315  $document,
316  $purifier,
317  $user,
318  $fs,
319  $fu,
320  'action',
321  'save',
322  'cancel',
323  true
324  );
325 
326  $this->assertFalse($form->getItemByPostVar('document')->getRequired(), 'Failed asserting document upload is not required for existing documents');
327  }
328 
333  {
334  $expectedSorting = 10;
335 
336  $document = $this
337  ->getMockBuilder(\ilTermsOfServiceDocument::class)
338  ->disableOriginalConstructor()
339  ->setMethods(['fetchAllCriterionAssignments'])
340  ->getMock();
341 
342  $document->setId(4711);
343  $document->setTitle('phpunit');
344  $document->setSorting($expectedSorting);
345 
346  $purifier = $this
347  ->getMockBuilder(\ilHtmlPurifierInterface::class)
348  ->getMock();
349 
350  $user = $this
351  ->getMockBuilder(\ilObjUser::class)
352  ->disableOriginalConstructor()
353  ->setMethods(['getId'])
354  ->getMock();
355 
356  $user
357  ->expects($this->once())
358  ->method('getId')
359  ->willReturn(6);
360 
361  $fs = $this
362  ->getMockBuilder(Filesystem::class)
363  ->getMock();
364 
365  $fu = $this
366  ->getMockBuilder(FileUpload::class)
367  ->setMethods(['moveFilesTo', 'uploadSizeLimit', 'register', 'hasBeenProcessed', 'hasUploads', 'process', 'getResults', 'moveOneFileTo'])
368  ->getMock();
369 
370  $fu
371  ->expects($this->any())
372  ->method('hasUploads')
373  ->willReturn(false);
374 
375  $this->setGlobalVariable('upload', $fu);
376 
377  $documentConnector = $this->getMockBuilder(\arConnector::class)->getMock();
378 
379  \arConnectorMap::register(new \ilTermsOfServiceDocument(), $documentConnector);
380  \arConnectorMap::register($document, $documentConnector);
381 
382  $form = $this->getMockBuilder(\ilTermsOfServiceDocumentFormGUI::class)
383  ->setConstructorArgs([
384  $document, $purifier, $user, $fs, $fu,
385  'action', 'save', 'cancel',
386  true
387  ])
388  ->setMethods(['checkInput'])
389  ->getMock();
390 
391  $form
392  ->expects($this->once())
393  ->method('checkInput')
394  ->willReturn(true);
395 
396  $_POST = [
397  'title' => 'phpunit',
398  'document' => '',
399  '' => ''
400  ];
401  $form->setCheckInputCalled(true);
402 
403  $this->assertTrue($form->saveObject());
404  $this->assertFalse($form->hasTranslatedError());
405  $this->assertEmpty($form->getTranslatedError());
406  $this->assertEquals(
407  $expectedSorting,
408  $document->getSorting(),
409  'Failed asserting that the sorting of the existing document has not been changed'
410  );
411  }
412 
417  {
418  $lng = $this->getLanguageMock();
419 
420  $lng
421  ->expects($this->any())
422  ->method('txt')
423  ->willReturn('translation');
424 
425  $this->setGlobalVariable('lng', $lng);
426 
427  $document = $this
428  ->getMockBuilder(\ilTermsOfServiceDocument::class)
429  ->disableOriginalConstructor()
430  ->setMethods(['getId', 'fetchAllCriterionAssignments'])
431  ->getMock();
432 
433  $purifier = $this
434  ->getMockBuilder(\ilHtmlPurifierInterface::class)
435  ->getMock();
436 
437  $user = $this
438  ->getMockBuilder(\ilObjUser::class)
439  ->disableOriginalConstructor()
440  ->setMethods()
441  ->getMock();
442 
443  $fu = $this
444  ->getMockBuilder(FileUpload::class)
445  ->setMethods(['moveFilesTo', 'uploadSizeLimit', 'register', 'hasBeenProcessed', 'hasUploads', 'process', 'getResults', 'moveOneFileTo'])
446  ->getMock();
447 
448  $fu
449  ->expects($this->exactly(3))
450  ->method('hasUploads')
451  ->willReturn(true);
452 
453  $fu
454  ->expects($this->exactly(3))
455  ->method('hasBeenProcessed')
456  ->willReturn(false);
457 
458  $fu
459  ->expects($this->exactly(3))
460  ->method('process');
461 
462  $uploadResult = new UploadResult(
463  'phpunit',
464  1024,
465  'text/xml',
466  $this->getMockBuilder(ImmutableStringMap::class)->getMock(),
467  new ProcessingStatus(ProcessingStatus::OK, 'uploaded'),
468  '/tmp'
469  );
470 
471  $uploadFailingResult = new UploadResult(
472  'phpunit',
473  1024,
474  'text/xml',
475  $this->getMockBuilder(ImmutableStringMap::class)->getMock(),
476  new ProcessingStatus(ProcessingStatus::REJECTED, 'not uploaded'),
477  '/tmp'
478  );
479 
480  $fu
481  ->expects($this->exactly(3))
482  ->method('getResults')
483  ->willReturnOnConsecutiveCalls(
484  [false],
485  [0 => $uploadFailingResult],
486  [0 => $uploadResult]
487  );
488 
489  $fs = $this
490  ->getMockBuilder(Filesystem::class)
491  ->getMock();
492 
493  $fs
494  ->expects($this->once())
495  ->method('has')
496  ->with('/agreements/' . $uploadResult->getName())
497  ->willReturn(false);
498 
499  $this->setGlobalVariable('upload', $fu);
500 
501  $documentConnector = $this->getMockBuilder(\arConnector::class)->getMock();
502 
503  \arConnectorMap::register(new \ilTermsOfServiceDocument(), $documentConnector);
504  \arConnectorMap::register($document, $documentConnector);
505 
506  $form = $this->getMockBuilder(\ilTermsOfServiceDocumentFormGUI::class)
507  ->setConstructorArgs([
508  $document, $purifier, $user, $fs, $fu,
509  'action', 'save', 'cancel',
510  true
511  ])
512  ->setMethods(['checkInput'])
513  ->getMock();
514 
515  $form
516  ->expects($this->once())
517  ->method('checkInput')
518  ->willReturn(true);
519 
520  $_POST = [
521  'title' => '',
522  'document' => '',
523  '' => ''
524  ];
525  $form->setCheckInputCalled(true);
526 
527  $this->assertFalse($form->saveObject());
528  $this->assertTrue($form->hasTranslatedError());
529  $this->assertNotEmpty($form->getTranslatedError());
530 
531  $form = $this->getMockBuilder(\ilTermsOfServiceDocumentFormGUI::class)
532  ->setConstructorArgs([
533  $document, $purifier, $user, $fs, $fu,
534  'action', 'save', 'cancel',
535  true
536  ])
537  ->setMethods(['checkInput'])
538  ->getMock();
539 
540  $form
541  ->expects($this->once())
542  ->method('checkInput')
543  ->willReturn(true);
544 
545  $form->setCheckInputCalled(true);
546 
547  $this->assertFalse($form->saveObject());
548  $this->assertTrue($form->hasTranslatedError());
549  $this->assertNotEmpty($form->getTranslatedError());
550 
551  $form = $this->getMockBuilder(\ilTermsOfServiceDocumentFormGUI::class)
552  ->setConstructorArgs([
553  $document, $purifier, $user, $fs, $fu,
554  'action', 'save', 'cancel',
555  true
556  ])
557  ->setMethods(['checkInput'])
558  ->getMock();
559 
560  $form
561  ->expects($this->once())
562  ->method('checkInput')
563  ->willReturn(true);
564 
565  $form->setCheckInputCalled(true);
566 
567  $this->assertFalse($form->saveObject());
568  $this->assertTrue($form->hasTranslatedError());
569  $this->assertNotEmpty($form->getTranslatedError());
570  }
571 }
setGlobalVariable(string $name, $value)
static register(ActiveRecord $ar, arConnector $connector)
Class ilTermsOfServiceAcceptanceHistoryCriteriaBagTest.
Class ilTermsOfServiceBaseTest.
if(isset($_POST['submit'])) $form
Class ilTermsOfServiceDocument.
$lng
once($eventName, callable $callBack, $priority=100)
Subscribe to an event exactly once.
$user
Definition: migrateto20.php:57
$_POST["username"]