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