‪TYPO3CMS  ‪main
FileLinkHandlerTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29 
30 final class ‪FileLinkHandlerTest extends UnitTestCase
31 {
32  protected bool ‪$resetSingletonInstances = true;
33 
39  public static function ‪resolveParametersForFilesDataProvider(): array
40  {
41  return [
42  'file without FAL - cool style' => [
43  [
44  'identifier' => 'fileadmin/deep/down.jpg',
45  ],
46  [
47  'file' => 'fileadmin/deep/down.jpg',
48  ],
49  't3://file?identifier=fileadmin%2Fdeep%2Fdown.jpg',
50  ],
51  'file without FAL and anchor - cool style' => [
52  [
53  'identifier' => 'fileadmin/deep/down.jpg',
54  'fragment' => 'page-13',
55  ],
56  [
57  'file' => 'fileadmin/deep/down.jpg',
58  'fragment' => 'page-13',
59  ],
60  't3://file?identifier=fileadmin%2Fdeep%2Fdown.jpg#page-13',
61  ],
62  'file with FAL uid - cool style' => [
63  [
64  'uid' => 23,
65  ],
66  [
67  'file' => '23',
68  ],
69  't3://file?uid=23',
70  ],
71  'file with FAL uid and anchor - cool style' => [
72  [
73  'uid' => 23,
74  'fragment' => 'page-13',
75  ],
76  [
77  'file' => '23',
78  'fragment' => 'page-13',
79  ],
80  't3://file?uid=23#page-13',
81  ],
82  ];
83  }
84 
88  #[DataProvider('resolveParametersForFilesDataProvider')]
89  #[Test]
90  public function ‪resolveFileReferencesToSplitParameters(array $input, array $expected): void
91  {
92  $storage = $this->getMockBuilder(ResourceStorage::class)
93  ->disableOriginalConstructor()
94  ->getMock();
95  $factory = $this->getMockBuilder(ResourceFactory::class)
96  ->disableOriginalConstructor()
97  ->getMock();
98 
99  // fake methods to return proper objects
100  $fileObject = new ‪File(['identifier' => 'fileadmin/deep/down.jpg', 'name' => 'down.jpg'], $storage);
101  $factory->method('getFileObject')->with($expected['file'])->willReturn($fileObject);
102  $factory->method('getFileObjectFromCombinedIdentifier')->with($expected['file'])->willReturn($fileObject);
103  $expected['file'] = $fileObject;
104  GeneralUtility::setSingletonInstance(ResourceFactory::class, $factory);
105 
106  $subject = new ‪FileLinkHandler();
107 
108  self::assertEquals($expected, $subject->resolveHandlerData($input));
109  }
110 
114  #[DataProvider('resolveParametersForFilesDataProvider')]
115  #[Test]
116  public function ‪splitParametersToUnifiedIdentifierForFiles(array $input, array $parameters, string $expected): void
117  {
118  $fileObject = $this->getMockBuilder(File::class)
119  ->onlyMethods(['getUid', 'getIdentifier'])
120  ->disableOriginalConstructor()
121  ->getMock();
122 
123  ‪$uid = 0;
124  if (‪MathUtility::canBeInterpretedAsInteger($parameters['file'])) {
125  ‪$uid = $parameters['file'];
126  }
127  $fileObject->expects(self::once())->method('getUid')->willReturn(‪$uid);
128  $fileObject->method('getIdentifier')->willReturn($parameters['file']);
129  $parameters['file'] = $fileObject;
130 
131  $subject = new ‪FileLinkHandler();
132  self::assertEquals($expected, $subject->asString($parameters));
133  }
134 }
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:69
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:42
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:128
‪TYPO3\CMS\Webhooks\Message\$uid
‪identifier readonly int $uid
Definition: PageModificationMessage.php:35
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52