TYPO3 CMS  TYPO3_8-7
FormPersistenceManagerTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
25 
29 class FormPersistenceManagerTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
30 {
31 
36  {
37  $this->expectException(PersistenceManagerException::class);
38  $this->expectExceptionCode(1477679819);
39 
40  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
41  'dummy'
42  ], [], '', false);
43 
44  $runtimeCache= $this->getMockBuilder(VariableFrontend::class)
45  ->setMethods(['get', 'set'])
46  ->disableOriginalConstructor()
47  ->getMock();
48 
49  $runtimeCache
50  ->expects($this->any())
51  ->method('get')
52  ->willReturn(false);
53 
54  $mockFormPersistenceManager->_set('runtimeCache', $runtimeCache);
55 
56  $input = '-1:/user_uploads/_example.php';
57  $mockFormPersistenceManager->_call('load', $input);
58  }
59 
64  {
65  $this->expectException(PersistenceManagerException::class);
66  $this->expectExceptionCode(1484071985);
67 
68  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
69  'dummy'
70  ], [], '', false);
71 
72  $runtimeCache= $this->getMockBuilder(VariableFrontend::class)
73  ->setMethods(['get', 'set'])
74  ->disableOriginalConstructor()
75  ->getMock();
76 
77  $runtimeCache
78  ->expects($this->any())
79  ->method('get')
80  ->willReturn(false);
81 
82  $mockFormPersistenceManager->_set('runtimeCache', $runtimeCache);
83 
84  $mockFormPersistenceManager->_set('formSettings', [
85  'persistenceManager' => [
86  'allowedExtensionPaths' => [],
87  ],
88  ]);
89 
90  $input = 'EXT:form/Resources/Forms/_example.form.yaml';
91  $mockFormPersistenceManager->_call('load', $input);
92  }
93 
98  {
99  $this->expectException(PersistenceManagerException::class);
100  $this->expectExceptionCode(1477679820);
101 
102  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
103  'dummy'
104  ], [], '', false);
105 
106  $input = '-1:/user_uploads/_example.php';
107  $mockFormPersistenceManager->_call('save', $input, []);
108  }
109 
114  {
115  $this->expectException(PersistenceManagerException::class);
116  $this->expectExceptionCode(1477680881);
117 
118  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
119  'dummy'
120  ], [], '', false);
121 
122  $mockFormPersistenceManager->_set('formSettings', [
123  'persistenceManager' => [
124  'allowSaveToExtensionPaths' => false,
125  ],
126  ]);
127 
128  $input = 'EXT:form/Resources/Forms/_example.form.yaml';
129  $mockFormPersistenceManager->_call('save', $input, []);
130  }
131 
136  {
137  $this->expectException(PersistenceManagerException::class);
138  $this->expectExceptionCode(1484073571);
139 
140  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
141  'dummy'
142  ], [], '', false);
143 
144  $runtimeCache= $this->getMockBuilder(VariableFrontend::class)
145  ->setMethods(['get', 'set'])
146  ->disableOriginalConstructor()
147  ->getMock();
148 
149  $runtimeCache
150  ->expects($this->any())
151  ->method('get')
152  ->willReturn(false);
153 
154  $mockFormPersistenceManager->_set('runtimeCache', $runtimeCache);
155 
156  $mockFormPersistenceManager->_set('formSettings', [
157  'persistenceManager' => [
158  'allowSaveToExtensionPaths' => true,
159  'allowedExtensionPaths' => [],
160  ],
161  ]);
162 
163  $input = 'EXT:form/Resources/Forms/_example.form.yaml';
164  $mockFormPersistenceManager->_call('save', $input, []);
165  }
166 
171  {
172  $this->expectException(PersistenceManagerException::class);
173  $this->expectExceptionCode(1472239534);
174 
175  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
176  'dummy'
177  ], [], '', false);
178 
179  $input = '-1:/user_uploads/_example.php';
180  $mockFormPersistenceManager->_call('delete', $input);
181  }
182 
187  {
188  $this->expectException(PersistenceManagerException::class);
189  $this->expectExceptionCode(1472239535);
190 
191  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
192  'exists'
193  ], [], '', false);
194 
195  $mockFormPersistenceManager
196  ->expects($this->any())
197  ->method('exists')
198  ->willReturn(false);
199 
200  $input = '-1:/user_uploads/_example.form.yaml';
201  $mockFormPersistenceManager->_call('delete', $input);
202  }
203 
208  {
209  $this->expectException(PersistenceManagerException::class);
210  $this->expectExceptionCode(1472239536);
211 
212  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
213  'exists'
214  ], [], '', false);
215 
216  $mockFormPersistenceManager
217  ->expects($this->any())
218  ->method('exists')
219  ->willReturn(true);
220 
221  $mockFormPersistenceManager->_set('formSettings', [
222  'persistenceManager' => [
223  'allowDeleteFromExtensionPaths' => false,
224  ],
225  ]);
226 
227  $input = 'EXT:form/Resources/Forms/_example.form.yaml';
228  $mockFormPersistenceManager->_call('delete', $input);
229  }
230 
235  {
236  $this->expectException(PersistenceManagerException::class);
237  $this->expectExceptionCode(1484073878);
238 
239  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
240  'exists'
241  ], [], '', false);
242 
243  $runtimeCache= $this->getMockBuilder(VariableFrontend::class)
244  ->setMethods(['get', 'set'])
245  ->disableOriginalConstructor()
246  ->getMock();
247 
248  $runtimeCache
249  ->expects($this->any())
250  ->method('get')
251  ->willReturn(false);
252 
253  $mockFormPersistenceManager->_set('runtimeCache', $runtimeCache);
254 
255  $mockFormPersistenceManager
256  ->expects($this->any())
257  ->method('exists')
258  ->willReturn(true);
259 
260  $mockFormPersistenceManager->_set('formSettings', [
261  'persistenceManager' => [
262  'allowDeleteFromExtensionPaths' => true,
263  'allowedExtensionPaths' => [],
264  ],
265  ]);
266 
267  $input = 'EXT:form/Resources/Forms/_example.form.yaml';
268  $mockFormPersistenceManager->_call('delete', $input);
269  }
270 
275  {
276  $this->expectException(PersistenceManagerException::class);
277  $this->expectExceptionCode(1472239516);
278 
279  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
280  'getStorageByUid',
281  'exists'
282  ], [], '', false);
283 
284  $mockStorage = $this->getMockBuilder(ResourceStorage::class)
285  ->disableOriginalConstructor()
286  ->getMock();
287 
288  $mockStorage
289  ->expects($this->any())
290  ->method('checkFileActionPermission')
291  ->willReturn(false);
292 
293  $file = new File(['identifier' => '', 'mime_type' => ''], $mockStorage);
294  $mockStorage
295  ->expects($this->any())
296  ->method('getFile')
297  ->willReturn($file);
298 
299  $mockFormPersistenceManager
300  ->expects($this->any())
301  ->method('getStorageByUid')
302  ->willReturn($mockStorage);
303 
304  $mockFormPersistenceManager
305  ->expects($this->any())
306  ->method('exists')
307  ->willReturn(true);
308 
309  $input = '-1:/user_uploads/_example.form.yaml';
310  $mockFormPersistenceManager->_call('delete', $input);
311  }
312 
317  {
318  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
319  'dummy'
320  ], [], '', false);
321 
322  $runtimeCache= $this->getMockBuilder(VariableFrontend::class)
323  ->setMethods(['get', 'set'])
324  ->disableOriginalConstructor()
325  ->getMock();
326 
327  $runtimeCache
328  ->expects($this->any())
329  ->method('get')
330  ->willReturn(false);
331 
332  $mockFormPersistenceManager->_set('runtimeCache', $runtimeCache);
333 
334  $mockFormPersistenceManager->_set('formSettings', [
335  'persistenceManager' => [
336  'allowedExtensionPaths' => [
337  'EXT:form/Tests/Unit/Mvc/Persistence/Fixtures/'
338  ],
339  ],
340  ]);
341 
342  $input = 'EXT:form/Tests/Unit/Mvc/Persistence/Fixtures/BlankForm.form.yaml';
343  $this->assertTrue($mockFormPersistenceManager->_call('exists', $input));
344  }
345 
350  {
351  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
352  'dummy'
353  ], [], '', false);
354 
355  $input = 'EXT:form/Tests/Unit/Mvc/Persistence/Fixtures/BlankForm.txt';
356  $this->assertFalse($mockFormPersistenceManager->_call('exists', $input));
357  }
358 
363  {
364  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
365  'dummy'
366  ], [], '', false);
367 
368  $mockFormPersistenceManager->_set('formSettings', [
369  'persistenceManager' => [
370  'allowedExtensionPaths' => [],
371  ],
372  ]);
373 
374  $input = 'EXT:form/Tests/Unit/Mvc/Persistence/Fixtures/BlankForm.yaml';
375  $this->assertFalse($mockFormPersistenceManager->_call('exists', $input));
376  }
377 
382  {
383  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
384  'dummy'
385  ], [], '', false);
386 
387  $input = 'EXT:form/Tests/Unit/Mvc/Persistence/Fixtures/_BlankForm.yaml';
388  $this->assertFalse($mockFormPersistenceManager->_call('exists', $input));
389  }
390 
395  {
396  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
397  'getStorageByUid'
398  ], [], '', false);
399 
400  $mockStorage = $this->getMockBuilder(ResourceStorage::class)
401  ->disableOriginalConstructor()
402  ->getMock();
403  $mockStorage
404  ->expects($this->any())
405  ->method('hasFile')
406  ->willReturn(true);
407 
408  $mockFormPersistenceManager
409  ->expects($this->any())
410  ->method('getStorageByUid')
411  ->willReturn($mockStorage);
412 
413  $input = '-1:/user_uploads/_example.form.yaml';
414  $this->assertTrue($mockFormPersistenceManager->_call('exists', $input));
415  }
416 
421  {
422  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
423  'getStorageByUid'
424  ], [], '', false);
425 
426  $mockStorage = $this->getMockBuilder(ResourceStorage::class)
427  ->disableOriginalConstructor()
428  ->getMock();
429  $mockStorage
430  ->expects($this->any())
431  ->method('hasFile')
432  ->willReturn(true);
433 
434  $mockFormPersistenceManager
435  ->expects($this->any())
436  ->method('getStorageByUid')
437  ->willReturn($mockStorage);
438 
439  $input = '-1:/user_uploads/_example.php';
440  $this->assertFalse($mockFormPersistenceManager->_call('exists', $input));
441  }
442 
447  {
448  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
449  'getStorageByUid'
450  ], [], '', false);
451 
452  $mockStorage = $this->getMockBuilder(ResourceStorage::class)
453  ->disableOriginalConstructor()
454  ->getMock();
455  $mockStorage
456  ->expects($this->any())
457  ->method('hasFile')
458  ->willReturn(false);
459 
460  $mockFormPersistenceManager
461  ->expects($this->any())
462  ->method('getStorageByUid')
463  ->willReturn($mockStorage);
464 
465  $input = '-1:/user_uploads/_example.yaml';
466  $this->assertFalse($mockFormPersistenceManager->_call('exists', $input));
467  }
468 
473  {
474  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
475  'exists'
476  ], [], '', false);
477 
478  $mockFormPersistenceManager
479  ->expects($this->at(0))
480  ->method('exists')
481  ->willReturn(true);
482 
483  $mockFormPersistenceManager
484  ->expects($this->at(1))
485  ->method('exists')
486  ->willReturn(true);
487 
488  $mockFormPersistenceManager
489  ->expects($this->at(2))
490  ->method('exists')
491  ->willReturn(false);
492 
493  $input = 'example';
494  $expected = '-1:/user_uploads/example_2.form.yaml';
495  $this->assertSame($expected, $mockFormPersistenceManager->_call('getUniquePersistenceIdentifier', $input, '-1:/user_uploads/'));
496  }
497 
502  {
503  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
504  'exists'
505  ], [], '', false);
506 
507  for ($attempts = 0; $attempts <= 99; $attempts++) {
508  $mockFormPersistenceManager
509  ->expects($this->at($attempts))
510  ->method('exists')
511  ->willReturn(true);
512  }
513 
514  $mockFormPersistenceManager
515  ->expects($this->at(100))
516  ->method('exists')
517  ->willReturn(false);
518 
519  $input = 'example';
520  $expected = '#^-1:/user_uploads/example_([0-9]{10}).form.yaml$#';
521 
522  $returnValue = $mockFormPersistenceManager->_call('getUniquePersistenceIdentifier', $input, '-1:/user_uploads/');
523  $this->assertEquals(1, preg_match($expected, $returnValue));
524  }
525 
530  {
531  $this->expectException(NoUniqueIdentifierException::class);
532  $this->expectExceptionCode(1477688567);
533 
534  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
535  'checkForDuplicateIdentifier'
536  ], [], '', false);
537 
538  $mockFormPersistenceManager
539  ->expects($this->any())
540  ->method('checkForDuplicateIdentifier')
541  ->willReturn(true);
542 
543  $input = 'example';
544  $mockFormPersistenceManager->_call('getUniqueIdentifier', $input);
545  }
546 
551  {
552  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
553  'checkForDuplicateIdentifier'
554  ], [], '', false);
555 
556  for ($attempts = 0; $attempts <= 99; $attempts++) {
557  $mockFormPersistenceManager
558  ->expects($this->at($attempts))
559  ->method('checkForDuplicateIdentifier')
560  ->willReturn(true);
561  }
562 
563  $mockFormPersistenceManager
564  ->expects($this->at(100))
565  ->method('checkForDuplicateIdentifier')
566  ->willReturn(false);
567 
568  $input = 'example';
569  $expected = '#^example_([0-9]{10})$#';
570 
571  $returnValue = $mockFormPersistenceManager->_call('getUniqueIdentifier', $input);
572  $this->assertEquals(1, preg_match($expected, $returnValue));
573  }
574 
579  {
580  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
581  'listForms'
582  ], [], '', false);
583 
584  $mockFormPersistenceManager
585  ->expects($this->at($attempts))
586  ->method('listForms')
587  ->willReturn([
588  0 => [
589  'identifier' => 'example',
590  ],
591  ]);
592 
593  $input = 'example';
594  $this->assertTrue($mockFormPersistenceManager->_call('checkForDuplicateIdentifier', $input));
595  }
596 
601  {
602  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
603  'listForms'
604  ], [], '', false);
605 
606  $mockFormPersistenceManager
607  ->expects($this->at($attempts))
608  ->method('listForms')
609  ->willReturn([
610  0 => [
611  'identifier' => 'example',
612  ],
613  ]);
614 
615  $input = 'other-example';
616  $this->assertFalse($mockFormPersistenceManager->_call('checkForDuplicateIdentifier', $input));
617  }
618 
623  {
624  $this->expectException(PersistenceManagerException::class);
625  $this->expectExceptionCode(1471630578);
626 
627  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
628  'dummy',
629  ], [], '', false);
630 
631  $storage = $this->getMockBuilder(ResourceStorage::class)
632  ->disableOriginalConstructor()
633  ->getMock();
634 
635  $storage
636  ->expects($this->any())
637  ->method('checkFileActionPermission')
638  ->willReturn(false);
639 
640  $file = new File(['identifier' => '', 'mime_type' => ''], $storage);
641 
642  $resourceFactory = $this->getMockBuilder(ResourceFactory::class)
643  ->disableOriginalConstructor()
644  ->getMock();
645 
646  $resourceFactory
647  ->expects($this->any())
648  ->method('retrieveFileOrFolderObject')
649  ->willReturn($file);
650 
651  $mockFormPersistenceManager->_set('resourceFactory', $resourceFactory);
652 
653  $input = '-1:/user_uploads/example.yaml';
654  $mockFormPersistenceManager->_call('retrieveFileByPersistenceIdentifier', $input);
655  }
656 
661  {
662  $this->expectException(PersistenceManagerException::class);
663  $this->expectExceptionCode(1471630579);
664 
665  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
666  'getStorageByUid',
667  ], [], '', false);
668 
669  $mockStorage = $this->getMockBuilder(ResourceStorage::class)
670  ->disableOriginalConstructor()
671  ->getMock();
672 
673  $mockStorage
674  ->expects($this->any())
675  ->method('hasFolder')
676  ->willReturn(false);
677 
678  $mockFormPersistenceManager
679  ->expects($this->any())
680  ->method('getStorageByUid')
681  ->willReturn($mockStorage);
682 
683  $input = '-1:/user_uploads/example.yaml';
684  $mockFormPersistenceManager->_call('getOrCreateFile', $input);
685  }
686 
691  {
692  $this->expectException(PersistenceManagerException::class);
693  $this->expectExceptionCode(1471630580);
694 
695  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
696  'getStorageByUid',
697  ], [], '', false);
698 
699  $mockStorage = $this->getMockBuilder(ResourceStorage::class)
700  ->disableOriginalConstructor()
701  ->getMock();
702 
703  $mockStorage
704  ->expects($this->any())
705  ->method('hasFolder')
706  ->willReturn(true);
707 
708  $mockStorage
709  ->expects($this->any())
710  ->method('checkFileActionPermission')
711  ->willReturn(false);
712 
713  $file = new File(['identifier' => '', 'mime_type' => ''], $mockStorage);
714  $mockStorage
715  ->expects($this->any())
716  ->method('getFile')
717  ->willReturn($file);
718 
719  $mockFormPersistenceManager
720  ->expects($this->any())
721  ->method('getStorageByUid')
722  ->willReturn($mockStorage);
723 
724  $input = '-1:/user_uploads/example.yaml';
725  $mockFormPersistenceManager->_call('getOrCreateFile', $input);
726  }
727 
732  {
733  $this->expectException(PersistenceManagerException::class);
734  $this->expectExceptionCode(1471630581);
735 
736  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
737  'dummy',
738  ], [], '', false);
739 
740  $mockStorageRepository = $this->getMockBuilder(StorageRepository::class)
741  ->disableOriginalConstructor()
742  ->getMock();
743 
744  $mockStorageRepository
745  ->expects($this->any())
746  ->method('findByUid')
747  ->willReturn(null);
748 
749  $mockFormPersistenceManager->_set('storageRepository', $mockStorageRepository);
750  $mockFormPersistenceManager->_call('getStorageByUid', -1);
751  }
752 
757  {
758  $this->expectException(PersistenceManagerException::class);
759  $this->expectExceptionCode(1471630581);
760 
761  $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
762  'dummy',
763  ], [], '', false);
764 
765  $mockStorageRepository = $this->getMockBuilder(StorageRepository::class)
766  ->disableOriginalConstructor()
767  ->getMock();
768 
769  $mockStorage = $this->getMockBuilder(ResourceStorage::class)
770  ->disableOriginalConstructor()
771  ->getMock();
772 
773  $mockStorage
774  ->expects($this->any())
775  ->method('isBrowsable')
776  ->willReturn(false);
777 
778  $mockStorageRepository
779  ->expects($this->any())
780  ->method('findByUid')
781  ->willReturn($mockStorage);
782 
783  $mockFormPersistenceManager->_set('storageRepository', $mockStorageRepository);
784  $mockFormPersistenceManager->_call('getStorageByUid', -1);
785  }
786 }