[BUGFIX] Add missing namespace parts
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / LinkHandling / LegacyLinkNotationConverterTest.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\LegacyLinkNotationConverter;
18 use TYPO3\CMS\Core\LinkHandling\LinkService;
19 use TYPO3\CMS\Core\Resource\File;
20 use TYPO3\CMS\Core\Resource\Folder;
21 use TYPO3\CMS\Core\Resource\ResourceFactory;
22 use TYPO3\CMS\Core\Resource\ResourceStorage;
23 use TYPO3\CMS\Core\Utility\MathUtility;
24
25 class LegacyLinkNotationConverterTest extends \TYPO3\Components\TestingFramework\Core\Unit\UnitTestCase
26 {
27 /**
28 * Data to resolve strings to arrays and vice versa, external, mail, page
29 *
30 * @return array
31 */
32 public function resolveParametersForNonFilesDataProvider()
33 {
34 return [
35 'simple page - old style' => [
36 // original input value
37 '13',
38 // splitted values
39 [
40 'type' => LinkService::TYPE_PAGE,
41 'pageuid' => 13
42 ],
43 // final unified URN
44 't3://page?uid=13'
45 ],
46 'page with type - old style' => [
47 '13,31',
48 [
49 'type' => LinkService::TYPE_PAGE,
50 'pageuid' => 13,
51 'pagetype' => 31
52 ],
53 't3://page?uid=13&type=31'
54 ],
55 'page with type and fragment - old style' => [
56 '13,31#uncool',
57 [
58 'type' => LinkService::TYPE_PAGE,
59 'pageuid' => '13',
60 'pagetype' => '31',
61 'fragment' => 'uncool'
62 ],
63 't3://page?uid=13&type=31#uncool'
64 ],
65 'page with type and parameters and fragment - old style' => [
66 '13,31?unbel=ievable#uncool',
67 [
68 'type' => LinkService::TYPE_PAGE,
69 'pageuid' => '13',
70 'pagetype' => '31',
71 'parameters' => 'unbel=ievable',
72 'fragment' => 'uncool'
73 ],
74 't3://page?uid=13&type=31&unbel=ievable#uncool'
75 ],
76 'page with alias - old style' => [
77 'alias13',
78 [
79 'type' => LinkService::TYPE_PAGE,
80 'pagealias' => 'alias13'
81 ],
82 't3://page?alias=alias13'
83 ]
84 ];
85 }
86
87 /**
88 * @test
89 *
90 * @param string $input
91 * @param array $expected
92 * @param string $finalString
93 *
94 * @dataProvider resolveParametersForNonFilesDataProvider
95 */
96 public function resolveReturnsSplitParameters($input, $expected, $finalString)
97 {
98 $subject = new LegacyLinkNotationConverter();
99 $this->assertEquals($expected, $subject->resolve($input));
100 }
101
102 /**
103 * @test
104 *
105 * @param string $input
106 * @param array $parameters
107 * @param string $expected
108 *
109 * @dataProvider resolveParametersForNonFilesDataProvider
110 */
111 public function splitParametersToUnifiedIdentifier($input, $parameters, $expected)
112 {
113 $subject = new LinkService();
114 $this->assertEquals($expected, $subject->asString($parameters));
115 }
116
117 /**
118 * testing files and folders
119 */
120
121 /**
122 * Data provider for pointing to files
123 * t3:file:15
124 * t3:file:fileadmin/deep/down.jpg
125 * t3:file:1:myfolder/myidentifier.jpg
126 * t3:folder:1:myfolder
127 *
128 * @return array
129 */
130 public function resolveParametersForFilesDataProvider()
131 {
132 return [
133 'file without FAL - VERY old style' => [
134 'fileadmin/on/steroids.png',
135 [
136 'type' => LinkService::TYPE_FILE,
137 'file' => 'fileadmin/on/steroids.png'
138 ],
139 't3://file?identifier=fileadmin%2Fon%2Fsteroids.png'
140 ],
141 'file without FAL with file prefix - VERY old style' => [
142 'file:fileadmin/on/steroids.png',
143 [
144 'type' => LinkService::TYPE_FILE,
145 'file' => 'fileadmin/on/steroids.png'
146 ],
147 't3://file?identifier=fileadmin%2Fon%2Fsteroids.png'
148 ],
149 'file with FAL uid - old style' => [
150 'file:23',
151 [
152 'type' => LinkService::TYPE_FILE,
153 'file' => 23
154 ],
155 't3://file?uid=23'
156 ],
157 'folder without FAL - VERY old style' => [
158 'fileadmin/myimages/',
159 [
160 'type' => LinkService::TYPE_FOLDER,
161 'folder' => 'fileadmin/myimages/'
162 ],
163 't3://folder?storage=0&identifier=%2Ffileadmin%2Fmyimages%2F'
164 ],
165 'folder with combined identifier and file prefix (FAL) - old style' => [
166 'file:2:/myimages/',
167 [
168 'type' => LinkService::TYPE_FOLDER,
169 'folder' => '2:/myimages/'
170 ],
171 't3://folder?storage=2&identifier=%2Fmyimages%2F'
172 ],
173 ];
174 }
175
176 /**
177 * Helpful to know in which if() clause the stuff gets in
178 *
179 * @test
180 *
181 * @param string $input
182 * @param array $expected
183 * @param string $finalString
184 *
185 * @dataProvider resolveParametersForFilesDataProvider
186 */
187 public function resolveFileReferencesToSplitParameters($input, $expected, $finalString)
188 {
189 $storage = $this->getMockBuilder(ResourceStorage::class)
190 ->disableOriginalConstructor()
191 ->getMock();
192
193 $factory = $this->getMockBuilder(ResourceFactory::class)
194 ->disableOriginalConstructor()
195 ->getMock();
196
197 // fake methods to return proper objects
198 if ($expected['type'] === LinkService::TYPE_FILE) {
199 $fileObject = new File(['identifier' => $expected['file']], $storage);
200 $factory->expects($this->any())->method('getFileObjectFromCombinedIdentifier')->with($expected['file'])
201 ->willReturn($fileObject);
202 $factory->expects($this->any())->method('retrieveFileOrFolderObject')->with($expected['file'])
203 ->willReturn($fileObject);
204 $factory->expects($this->any())->method('getFileObject')->with($expected['file'])->willReturn($fileObject);
205 $expected['file'] = $fileObject;
206 }
207 // fake methods to return proper objects
208 if ($expected['type'] === LinkService::TYPE_FOLDER) {
209 if (substr($expected['folder'], 0, 5) === 'file:') {
210 $expected['folder'] = substr($expected['folder'], 5);
211 }
212 $folderObject = new Folder($storage, $expected['folder'], $expected['folder']);
213 $factory->expects($this->any())->method('retrieveFileOrFolderObject')->with($expected['folder'])
214 ->willReturn($folderObject);
215 $factory->expects($this->any())->method('getFolderObjectFromCombinedIdentifier')->with($expected['folder'])
216 ->willReturn($folderObject);
217 $expected['folder'] = $folderObject;
218 }
219
220 /** @var LegacyLinkNotationConverter|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\Components\TestingFramework\Core\AccessibleObjectInterface $subject */
221 $subject = $this->getAccessibleMock(LegacyLinkNotationConverter::class, ['dummy']);
222 $subject->_set('resourceFactory', $factory);
223 $this->assertEquals($expected, $subject->resolve($input));
224 }
225
226 /**
227 * Helpful to know in which if() clause the stuff gets in
228 *
229 * @test
230 *
231 * @param string $input
232 * @param array $parameters
233 * @param string $expected
234 *
235 * @dataProvider resolveParametersForFilesDataProvider
236 */
237 public function splitParametersToUnifiedIdentifierForFiles($input, $parameters, $expected)
238 {
239 // fake methods to return proper objects
240 if ($parameters['type'] === LinkService::TYPE_FILE) {
241 $fileObject = $this->getMockBuilder(File::class)
242 ->setMethods(['getUid', 'getIdentifier'])
243 ->disableOriginalConstructor()
244 ->getMock();
245 $uid = 0;
246 if (MathUtility::canBeInterpretedAsInteger($parameters['file'])) {
247 $uid = $parameters['file'];
248 }
249 $fileObject->expects($this->once())->method('getUid')->willReturn($uid);
250 $fileObject->expects($this->any())->method('getIdentifier')->willReturn($parameters['file']);
251 $parameters['file'] = $fileObject;
252 }
253 // fake methods to return proper objects
254 if ($parameters['type'] === LinkService::TYPE_FOLDER) {
255 if (substr($parameters['folder'], 0, 5) === 'file:') {
256 $parameters['folder'] = substr($parameters['folder'], 5);
257 }
258 // fake "0" storage
259 if (!MathUtility::canBeInterpretedAsInteger($parameters['folder']{0})) {
260 $parameters['folder'] = '0:' . $parameters['folder'];
261 }
262 $folderObject = $this->getMockBuilder(Folder::class)
263 ->setMethods(['getCombinedIdentifier', 'getStorage', 'getIdentifier'])
264 ->disableOriginalConstructor()
265 ->getMock();
266 $folderObject->expects($this->any())->method('getCombinedIdentifier')->willReturn($parameters['folder']);
267 $folderData = explode(':', $parameters['folder']);
268 /** @var ResourceStorage|\PHPUnit_Framework_MockObject_MockObject $storageMock */
269 $storage = $this->getMockBuilder(ResourceStorage::class)
270 ->disableOriginalConstructor()
271 ->getMock(['getUid']);
272 $storage->method('getUid')->willReturn($folderData[0]);
273 $folderObject->expects($this->any())->method('getStorage')->willReturn($storage);
274 $folderObject->expects($this->any())->method('getIdentifier')->willReturn($folderData[1]);
275 $parameters['folder'] = $folderObject;
276 }
277
278 $subject = new LinkService();
279 $this->assertEquals($expected, $subject->asString($parameters));
280 }
281 }