[!!!][TASK] Extract testing framework for TYPO3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / LinkHandling / FolderLinkHandlerTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\LinkHandling;
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\LinkHandling\FolderLinkHandler;
18 use TYPO3\CMS\Core\Resource\Folder;
19 use TYPO3\CMS\Core\Resource\ResourceFactory;
20 use TYPO3\CMS\Core\Resource\ResourceStorage;
21
22 class FolderLinkHandlerTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
23 {
24
25 /**
26 * testing folders
27 */
28
29 /**
30 * Data provider for pointing to files
31 * t3:file:1:myfolder/myidentifier.jpg
32 * t3:folder:1:myfolder
33 *
34 * @return array
35 */
36 public function resolveParametersForFilesDataProvider()
37 {
38 return [
39 'folder without FAL - cool style' => [
40 [
41 'storage' => 0,
42 'identifier' => '/fileadmin/myimages/'
43 ],
44 [
45 'folder' => '0:/fileadmin/myimages/'
46 ],
47 't3://folder?storage=0&identifier=%2Ffileadmin%2Fmyimages%2F'
48 ],
49 'folder with combined identifier and file prefix (FAL) - cool style' => [
50 [
51 'storage' => 2,
52 'identifier' => '/myimages/'
53 ],
54 [
55 'folder' => '2:/myimages/'
56 ],
57 't3://folder?storage=2&identifier=%2Fmyimages%2F'
58 ],
59 ];
60 }
61
62 /**
63 * Helpful to know in which if() clause the stuff gets in
64 *
65 * @test
66 *
67 * @param string $input
68 * @param array $expected
69 * @param string $finalString
70 *
71 * @dataProvider resolveParametersForFilesDataProvider
72 */
73 public function resolveFileReferencesToSplitParameters($input, $expected, $finalString)
74 {
75 $storage = $this->getMockBuilder(ResourceStorage::class)
76 ->disableOriginalConstructor()
77 ->getMock();
78
79 $factory = $this->getMockBuilder(ResourceFactory::class)
80 ->disableOriginalConstructor()
81 ->getMock();
82
83 // fake methods to return proper objects
84 $folderObject = new Folder($storage, $expected['folder'], $expected['folder']);
85 $factory->expects($this->once())->method('getFolderObjectFromCombinedIdentifier')->with($expected['folder'])
86 ->willReturn($folderObject);
87 $expected['folder'] = $folderObject;
88
89 /** @var FolderLinkHandler|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Components\TestingFramework\Core\AccessibleObjectInterface $subject */
90 $subject = $this->getAccessibleMock(FolderLinkHandler::class, ['dummy']);
91 $subject->_set('resourceFactory', $factory);
92 $this->assertEquals($expected, $subject->resolveHandlerData($input));
93 }
94
95 /**
96 * Helpful to know in which if() clause the stuff gets in
97 *
98 * @test
99 *
100 * @param string $input
101 * @param array $parameters
102 * @param string $expected
103 *
104 * @dataProvider resolveParametersForFilesDataProvider
105 */
106 public function splitParametersToUnifiedIdentifierForFiles($input, $parameters, $expected)
107 {
108 $folderObject = $this->getMockBuilder(Folder::class)
109 ->setMethods(['getCombinedIdentifier', 'getStorage', 'getIdentifier'])
110 ->disableOriginalConstructor()
111 ->getMock();
112
113 $folderObject->expects($this->any())->method('getCombinedIdentifier')->willReturn($parameters['folder']);
114 $folderData = explode(':', $parameters['folder']);
115 /** @var ResourceStorage|\PHPUnit_Framework_MockObject_MockObject $storageMock */
116 $storage = $this->getMockBuilder(ResourceStorage::class)
117 ->disableOriginalConstructor()
118 ->getMock(['getUid']);
119 $storage->method('getUid')->willReturn($folderData[0]);
120 $folderObject->expects($this->any())->method('getStorage')->willReturn($storage);
121 $folderObject->expects($this->any())->method('getIdentifier')->willReturn($folderData[1]);
122 $parameters['folder'] = $folderObject;
123
124 $subject = new FolderLinkHandler();
125 $this->assertEquals($expected, $subject->asString($parameters));
126 }
127 }