[TASK] Make sysext/form/Tests/Unit/Mvc/ notice free
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Tests / Unit / Mvc / Persistence / FormPersistenceManagerTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Form\Tests\Unit\Mvc\Persistence;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\Resource\File;
19 use TYPO3\CMS\Core\Resource\ResourceStorage;
20 use TYPO3\CMS\Core\Resource\StorageRepository;
21 use TYPO3\CMS\Form\Mvc\Persistence\Exception\NoUniqueIdentifierException;
22 use TYPO3\CMS\Form\Mvc\Persistence\Exception\PersistenceManagerException;
23 use TYPO3\CMS\Form\Mvc\Persistence\FormPersistenceManager;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25
26 /**
27 * Test case
28 */
29 class FormPersistenceManagerTest extends UnitTestCase
30 {
31 /**
32 * @test
33 */
34 public function loadThrowsExceptionIfPersistenceIdentifierHasNoYamlExtension(): void
35 {
36 $this->expectException(PersistenceManagerException::class);
37 $this->expectExceptionCode(1477679819);
38
39 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
40 'dummy'
41 ], [], '', false);
42
43 $input = '-1:/user_uploads/_example.php';
44 $mockFormPersistenceManager->_call('load', $input);
45 }
46
47 /**
48 * @test
49 */
50 public function loadThrowsExceptionIfPersistenceIdentifierIsAExtensionLocationWhichIsNotAllowed(): void
51 {
52 $this->expectException(PersistenceManagerException::class);
53 $this->expectExceptionCode(1484071985);
54
55 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
56 'dummy'
57 ], [], '', false);
58
59 $mockFormPersistenceManager->_set('formSettings', [
60 'persistenceManager' => [
61 'allowedExtensionPaths' => [],
62 ],
63 ]);
64
65 $input = 'EXT:form/Resources/Forms/_example.yaml';
66 $mockFormPersistenceManager->_call('load', $input);
67 }
68
69 /**
70 * @test
71 */
72 public function saveThrowsExceptionIfPersistenceIdentifierHasNoYamlExtension(): void
73 {
74 $this->expectException(PersistenceManagerException::class);
75 $this->expectExceptionCode(1477679820);
76
77 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
78 'dummy'
79 ], [], '', false);
80
81 $input = '-1:/user_uploads/_example.php';
82 $mockFormPersistenceManager->_call('save', $input, []);
83 }
84
85 /**
86 * @test
87 */
88 public function saveThrowsExceptionIfPersistenceIdentifierIsAExtensionLocationAndSaveToExtensionLocationIsNotAllowed(): void
89 {
90 $this->expectException(PersistenceManagerException::class);
91 $this->expectExceptionCode(1477680881);
92
93 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
94 'dummy'
95 ], [], '', false);
96
97 $mockFormPersistenceManager->_set('formSettings', [
98 'persistenceManager' => [
99 'allowSaveToExtensionPaths' => false,
100 ],
101 ]);
102
103 $input = 'EXT:form/Resources/Forms/_example.yaml';
104 $mockFormPersistenceManager->_call('save', $input, []);
105 }
106
107 /**
108 * @test
109 */
110 public function saveThrowsExceptionIfPersistenceIdentifierIsAExtensionLocationWhichIsNotAllowed(): void
111 {
112 $this->expectException(PersistenceManagerException::class);
113 $this->expectExceptionCode(1484073571);
114
115 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
116 'dummy'
117 ], [], '', false);
118
119 $mockFormPersistenceManager->_set('formSettings', [
120 'persistenceManager' => [
121 'allowSaveToExtensionPaths' => true,
122 'allowedExtensionPaths' => [],
123 ],
124 ]);
125
126 $input = 'EXT:form/Resources/Forms/_example.yaml';
127 $mockFormPersistenceManager->_call('save', $input, []);
128 }
129
130 /**
131 * @test
132 */
133 public function deleteThrowsExceptionIfPersistenceIdentifierHasNoYamlExtension(): void
134 {
135 $this->expectException(PersistenceManagerException::class);
136 $this->expectExceptionCode(1472239534);
137
138 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
139 'dummy'
140 ], [], '', false);
141
142 $input = '-1:/user_uploads/_example.php';
143 $mockFormPersistenceManager->_call('delete', $input);
144 }
145
146 /**
147 * @test
148 */
149 public function deleteThrowsExceptionIfPersistenceIdentifierFileDoesNotExists(): void
150 {
151 $this->expectException(PersistenceManagerException::class);
152 $this->expectExceptionCode(1472239535);
153
154 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
155 'exists'
156 ], [], '', false);
157
158 $mockFormPersistenceManager
159 ->expects($this->any())
160 ->method('exists')
161 ->willReturn(false);
162
163 $input = '-1:/user_uploads/_example.yaml';
164 $mockFormPersistenceManager->_call('delete', $input);
165 }
166
167 /**
168 * @test
169 */
170 public function deleteThrowsExceptionIfPersistenceIdentifierIsExtensionLocationAndDeleteFromExtensionLocationsIsNotAllowed(): void
171 {
172 $this->expectException(PersistenceManagerException::class);
173 $this->expectExceptionCode(1472239536);
174
175 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
176 'exists'
177 ], [], '', false);
178
179 $mockFormPersistenceManager
180 ->expects($this->any())
181 ->method('exists')
182 ->willReturn(true);
183
184 $mockFormPersistenceManager->_set('formSettings', [
185 'persistenceManager' => [
186 'allowDeleteFromExtensionPaths' => false,
187 ],
188 ]);
189
190 $input = 'EXT:form/Resources/Forms/_example.yaml';
191 $mockFormPersistenceManager->_call('delete', $input);
192 }
193
194 /**
195 * @test
196 */
197 public function deleteThrowsExceptionIfPersistenceIdentifierIsExtensionLocationWhichIsNotAllowed(): void
198 {
199 $this->expectException(PersistenceManagerException::class);
200 $this->expectExceptionCode(1484073878);
201
202 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
203 'exists'
204 ], [], '', false);
205
206 $mockFormPersistenceManager
207 ->expects($this->any())
208 ->method('exists')
209 ->willReturn(true);
210
211 $mockFormPersistenceManager->_set('formSettings', [
212 'persistenceManager' => [
213 'allowDeleteFromExtensionPaths' => true,
214 'allowedExtensionPaths' => [],
215 ],
216 ]);
217
218 $input = 'EXT:form/Resources/Forms/_example.yaml';
219 $mockFormPersistenceManager->_call('delete', $input);
220 }
221
222 /**
223 * @test
224 */
225 public function deleteThrowsExceptionIfPersistenceIdentifierIsStorageLocationAndDeleteFromStorageIsNotAllowed(): void
226 {
227 $this->expectException(PersistenceManagerException::class);
228 $this->expectExceptionCode(1472239516);
229
230 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
231 'getStorageByUid',
232 'exists'
233 ], [], '', false);
234
235 $mockStorage = $this->getMockBuilder(ResourceStorage::class)
236 ->disableOriginalConstructor()
237 ->getMock();
238
239 $mockStorage
240 ->expects($this->any())
241 ->method('checkFileActionPermission')
242 ->willReturn(false);
243
244 $file = new File(['name' => 'foo', 'identifier' => '', 'mime_type' => ''], $mockStorage);
245 $mockStorage
246 ->expects($this->any())
247 ->method('getFile')
248 ->willReturn($file);
249
250 $mockFormPersistenceManager
251 ->expects($this->any())
252 ->method('getStorageByUid')
253 ->willReturn($mockStorage);
254
255 $mockFormPersistenceManager
256 ->expects($this->any())
257 ->method('exists')
258 ->willReturn(true);
259
260 $input = '-1:/user_uploads/_example.yaml';
261 $mockFormPersistenceManager->_call('delete', $input);
262 }
263
264 /**
265 * @test
266 */
267 public function existsReturnsTrueIfPersistenceIdentifierIsExtensionLocationAndFileExistsAndFileHasYamlExtension(): void
268 {
269 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
270 'dummy'
271 ], [], '', false);
272
273 $mockFormPersistenceManager->_set('formSettings', [
274 'persistenceManager' => [
275 'allowedExtensionPaths' => [
276 'EXT:form/Tests/Unit/Mvc/Persistence/Fixtures/'
277 ],
278 ],
279 ]);
280
281 $input = 'EXT:form/Tests/Unit/Mvc/Persistence/Fixtures/BlankForm.yaml';
282 $this->assertTrue($mockFormPersistenceManager->_call('exists', $input));
283 }
284
285 /**
286 * @test
287 */
288 public function existsReturnsFalseIfPersistenceIdentifierIsExtensionLocationAndFileExistsAndFileHasNoYamlExtension(): void
289 {
290 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
291 'dummy'
292 ], [], '', false);
293
294 $input = 'EXT:form/Tests/Unit/Mvc/Persistence/Fixtures/BlankForm.txt';
295 $this->assertFalse($mockFormPersistenceManager->_call('exists', $input));
296 }
297
298 /**
299 * @test
300 */
301 public function existsReturnsFalseIfPersistenceIdentifierIsExtensionLocationAndFileExistsAndExtensionLocationIsNotAllowed(): void
302 {
303 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
304 'dummy'
305 ], [], '', false);
306
307 $mockFormPersistenceManager->_set('formSettings', [
308 'persistenceManager' => [
309 'allowedExtensionPaths' => [],
310 ],
311 ]);
312
313 $input = 'EXT:form/Tests/Unit/Mvc/Persistence/Fixtures/BlankForm.yaml';
314 $this->assertFalse($mockFormPersistenceManager->_call('exists', $input));
315 }
316
317 /**
318 * @test
319 */
320 public function existsReturnsFalseIfPersistenceIdentifierIsExtensionLocationAndFileNotExistsAndFileHasYamlExtension(): void
321 {
322 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
323 'dummy'
324 ], [], '', false);
325
326 $input = 'EXT:form/Tests/Unit/Mvc/Persistence/Fixtures/_BlankForm.yaml';
327 $this->assertFalse($mockFormPersistenceManager->_call('exists', $input));
328 }
329
330 /**
331 * @test
332 */
333 public function existsReturnsTrueIfPersistenceIdentifierIsStorageLocationAndFileExistsAndFileHasYamlExtension(): void
334 {
335 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
336 'getStorageByUid'
337 ], [], '', false);
338
339 $mockStorage = $this->getMockBuilder(ResourceStorage::class)
340 ->disableOriginalConstructor()
341 ->getMock();
342 $mockStorage
343 ->expects($this->any())
344 ->method('hasFile')
345 ->willReturn(true);
346
347 $mockFormPersistenceManager
348 ->expects($this->any())
349 ->method('getStorageByUid')
350 ->willReturn($mockStorage);
351
352 $input = '-1:/user_uploads/_example.yaml';
353 $this->assertTrue($mockFormPersistenceManager->_call('exists', $input));
354 }
355
356 /**
357 * @test
358 */
359 public function existsReturnsFalseIfPersistenceIdentifierIsStorageLocationAndFileExistsAndFileNoYamlExtension(): void
360 {
361 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
362 'getStorageByUid'
363 ], [], '', false);
364
365 $mockStorage = $this->getMockBuilder(ResourceStorage::class)
366 ->disableOriginalConstructor()
367 ->getMock();
368 $mockStorage
369 ->expects($this->any())
370 ->method('hasFile')
371 ->willReturn(true);
372
373 $mockFormPersistenceManager
374 ->expects($this->any())
375 ->method('getStorageByUid')
376 ->willReturn($mockStorage);
377
378 $input = '-1:/user_uploads/_example.php';
379 $this->assertFalse($mockFormPersistenceManager->_call('exists', $input));
380 }
381
382 /**
383 * @test
384 */
385 public function existsReturnsFalseIfPersistenceIdentifierIsStorageLocationAndFileNotExistsAndFileHasYamlExtension(): void
386 {
387 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
388 'getStorageByUid'
389 ], [], '', false);
390
391 $mockStorage = $this->getMockBuilder(ResourceStorage::class)
392 ->disableOriginalConstructor()
393 ->getMock();
394 $mockStorage
395 ->expects($this->any())
396 ->method('hasFile')
397 ->willReturn(false);
398
399 $mockFormPersistenceManager
400 ->expects($this->any())
401 ->method('getStorageByUid')
402 ->willReturn($mockStorage);
403
404 $input = '-1:/user_uploads/_example.yaml';
405 $this->assertFalse($mockFormPersistenceManager->_call('exists', $input));
406 }
407
408 /**
409 * @test
410 */
411 public function getUniquePersistenceIdentifierAppendNumberIfPersistenceIdentifierExists(): void
412 {
413 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
414 'exists'
415 ], [], '', false);
416
417 $mockFormPersistenceManager
418 ->expects($this->at(0))
419 ->method('exists')
420 ->willReturn(true);
421
422 $mockFormPersistenceManager
423 ->expects($this->at(1))
424 ->method('exists')
425 ->willReturn(true);
426
427 $mockFormPersistenceManager
428 ->expects($this->at(2))
429 ->method('exists')
430 ->willReturn(false);
431
432 $input = 'example';
433 $expected = '-1:/user_uploads/example_2.yaml';
434 $this->assertSame($expected, $mockFormPersistenceManager->_call('getUniquePersistenceIdentifier', $input, '-1:/user_uploads/'));
435 }
436
437 /**
438 * @test
439 */
440 public function getUniquePersistenceIdentifierAppendTimestampIfPersistenceIdentifierExists(): void
441 {
442 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
443 'exists'
444 ], [], '', false);
445
446 for ($attempts = 0; $attempts <= 99; $attempts++) {
447 $mockFormPersistenceManager
448 ->expects($this->at($attempts))
449 ->method('exists')
450 ->willReturn(true);
451 }
452
453 $mockFormPersistenceManager
454 ->expects($this->at(100))
455 ->method('exists')
456 ->willReturn(false);
457
458 $input = 'example';
459 $expected = '#^-1:/user_uploads/example_([0-9]{10}).yaml$#';
460
461 $returnValue = $mockFormPersistenceManager->_call('getUniquePersistenceIdentifier', $input, '-1:/user_uploads/');
462 $this->assertEquals(1, preg_match($expected, $returnValue));
463 }
464
465 /**
466 * @test
467 */
468 public function getUniqueIdentifierThrowsExceptionIfIdentifierExists(): void
469 {
470 $this->expectException(NoUniqueIdentifierException::class);
471 $this->expectExceptionCode(1477688567);
472
473 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
474 'checkForDuplicateIdentifier'
475 ], [], '', false);
476
477 $mockFormPersistenceManager
478 ->expects($this->any())
479 ->method('checkForDuplicateIdentifier')
480 ->willReturn(true);
481
482 $input = 'example';
483 $mockFormPersistenceManager->_call('getUniqueIdentifier', $input);
484 }
485
486 /**
487 * @test
488 */
489 public function getUniqueIdentifierAppendTimestampIfIdentifierExists(): void
490 {
491 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
492 'checkForDuplicateIdentifier'
493 ], [], '', false);
494
495 for ($attempts = 0; $attempts <= 99; $attempts++) {
496 $mockFormPersistenceManager
497 ->expects($this->at($attempts))
498 ->method('checkForDuplicateIdentifier')
499 ->willReturn(true);
500 }
501
502 $mockFormPersistenceManager
503 ->expects($this->at(100))
504 ->method('checkForDuplicateIdentifier')
505 ->willReturn(false);
506
507 $input = 'example';
508 $expected = '#^example_([0-9]{10})$#';
509
510 $returnValue = $mockFormPersistenceManager->_call('getUniqueIdentifier', $input);
511 $this->assertEquals(1, preg_match($expected, $returnValue));
512 }
513
514 /**
515 * @test
516 */
517 public function checkForDuplicateIdentifierReturnsTrueIfIdentifierIsUsed(): void
518 {
519 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
520 'listForms'
521 ], [], '', false);
522
523 $mockFormPersistenceManager
524 ->expects($this->at(null))
525 ->method('listForms')
526 ->willReturn([
527 0 => [
528 'identifier' => 'example',
529 ],
530 ]);
531
532 $input = 'example';
533 $this->assertTrue($mockFormPersistenceManager->_call('checkForDuplicateIdentifier', $input));
534 }
535
536 /**
537 * @test
538 */
539 public function checkForDuplicateIdentifierReturnsFalseIfIdentifierIsUsed(): void
540 {
541 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
542 'listForms'
543 ], [], '', false);
544
545 $mockFormPersistenceManager
546 ->expects($this->at(null))
547 ->method('listForms')
548 ->willReturn([
549 0 => [
550 'identifier' => 'example',
551 ],
552 ]);
553
554 $input = 'other-example';
555 $this->assertFalse($mockFormPersistenceManager->_call('checkForDuplicateIdentifier', $input));
556 }
557
558 /**
559 * @test
560 */
561 public function getFileByIdentifierThrowsExceptionIfReadFromStorageIsNotAllowed(): void
562 {
563 $this->expectException(PersistenceManagerException::class);
564 $this->expectExceptionCode(1471630578);
565
566 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
567 'getStorageByUid',
568 ], [], '', false);
569
570 $mockStorage = $this->getMockBuilder(ResourceStorage::class)
571 ->disableOriginalConstructor()
572 ->getMock();
573
574 $mockStorage
575 ->expects($this->any())
576 ->method('checkFileActionPermission')
577 ->willReturn(false);
578
579 $file = new File(['name' => 'foo', 'identifier' => '', 'mime_type' => ''], $mockStorage);
580 $mockStorage
581 ->expects($this->any())
582 ->method('getFile')
583 ->willReturn($file);
584
585 $mockFormPersistenceManager
586 ->expects($this->any())
587 ->method('getStorageByUid')
588 ->willReturn($mockStorage);
589
590 $input = '-1:/user_uploads/example.yaml';
591 $mockFormPersistenceManager->_call('getFileByIdentifier', $input);
592 }
593
594 /**
595 * @test
596 */
597 public function getOrCreateFileThrowsExceptionIfFolderNotExistsInStorage(): void
598 {
599 $this->expectException(PersistenceManagerException::class);
600 $this->expectExceptionCode(1471630579);
601
602 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
603 'getStorageByUid',
604 ], [], '', false);
605
606 $mockStorage = $this->getMockBuilder(ResourceStorage::class)
607 ->disableOriginalConstructor()
608 ->getMock();
609
610 $mockStorage
611 ->expects($this->any())
612 ->method('hasFolder')
613 ->willReturn(false);
614
615 $mockFormPersistenceManager
616 ->expects($this->any())
617 ->method('getStorageByUid')
618 ->willReturn($mockStorage);
619
620 $input = '-1:/user_uploads/example.yaml';
621 $mockFormPersistenceManager->_call('getOrCreateFile', $input);
622 }
623
624 /**
625 * @test
626 */
627 public function getOrCreateFileThrowsExceptionIfWriteToStorageIsNotAllowed(): void
628 {
629 $this->expectException(PersistenceManagerException::class);
630 $this->expectExceptionCode(1471630580);
631
632 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
633 'getStorageByUid',
634 ], [], '', false);
635
636 $mockStorage = $this->getMockBuilder(ResourceStorage::class)
637 ->disableOriginalConstructor()
638 ->getMock();
639
640 $mockStorage
641 ->expects($this->any())
642 ->method('hasFolder')
643 ->willReturn(true);
644
645 $mockStorage
646 ->expects($this->any())
647 ->method('checkFileActionPermission')
648 ->willReturn(false);
649
650 $file = new File(['name' => 'foo', 'identifier' => '', 'mime_type' => ''], $mockStorage);
651 $mockStorage
652 ->expects($this->any())
653 ->method('getFile')
654 ->willReturn($file);
655
656 $mockFormPersistenceManager
657 ->expects($this->any())
658 ->method('getStorageByUid')
659 ->willReturn($mockStorage);
660
661 $input = '-1:/user_uploads/example.yaml';
662 $mockFormPersistenceManager->_call('getOrCreateFile', $input);
663 }
664
665 /**
666 * @test
667 */
668 public function getStorageByUidThrowsExceptionIfStorageNotExists(): void
669 {
670 $this->expectException(PersistenceManagerException::class);
671 $this->expectExceptionCode(1471630581);
672
673 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
674 'dummy',
675 ], [], '', false);
676
677 $mockStorageRepository = $this->getMockBuilder(StorageRepository::class)
678 ->disableOriginalConstructor()
679 ->getMock();
680
681 $mockStorageRepository
682 ->expects($this->any())
683 ->method('findByUid')
684 ->willReturn(null);
685
686 $mockFormPersistenceManager->_set('storageRepository', $mockStorageRepository);
687 $mockFormPersistenceManager->_call('getStorageByUid', -1);
688 }
689
690 /**
691 * @test
692 */
693 public function getStorageByUidThrowsExceptionIfStorageIsNotBrowsable(): void
694 {
695 $this->expectException(PersistenceManagerException::class);
696 $this->expectExceptionCode(1471630581);
697
698 $mockFormPersistenceManager = $this->getAccessibleMock(FormPersistenceManager::class, [
699 'dummy',
700 ], [], '', false);
701
702 $mockStorageRepository = $this->getMockBuilder(StorageRepository::class)
703 ->disableOriginalConstructor()
704 ->getMock();
705
706 $mockStorage = $this->getMockBuilder(ResourceStorage::class)
707 ->disableOriginalConstructor()
708 ->getMock();
709
710 $mockStorage
711 ->expects($this->any())
712 ->method('isBrowsable')
713 ->willReturn(false);
714
715 $mockStorageRepository
716 ->expects($this->any())
717 ->method('findByUid')
718 ->willReturn($mockStorage);
719
720 $mockFormPersistenceManager->_set('storageRepository', $mockStorageRepository);
721 $mockFormPersistenceManager->_call('getStorageByUid', -1);
722 }
723 }