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