‪TYPO3CMS  ‪main
AbstractTypolinkBuilderTest.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;
22 use Psr\EventDispatcher\EventDispatcherInterface;
23 use Psr\Log\LoggerInterface;
24 use Symfony\Component\DependencyInjection\Container;
33 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
34 
35 final class ‪AbstractTypolinkBuilderTest extends UnitTestCase
36 {
37  protected bool ‪$resetSingletonInstances = true;
38  protected bool ‪$backupEnvironment = true;
39 
40  protected function ‪setUp(): void
41  {
42  parent::setUp();
43  $logManagerMock = $this->getMockBuilder(LogManager::class)->getMock();
44  $loggerMock = $this->getMockBuilder(LoggerInterface::class)->getMock();
45  $logManagerMock->method('getLogger')->willReturn($loggerMock);
46  GeneralUtility::setSingletonInstance(LogManager::class, $logManagerMock);
47  }
48 
50  {
51  return [
52  'Missing forceAbsoluteUrl leaves URL untouched' => [
53  'foo',
54  'foo',
55  [],
56  ],
57  'Absolute URL stays unchanged' => [
58  'http://example.org/',
59  'http://example.org/',
60  [
61  'forceAbsoluteUrl' => '1',
62  ],
63  ],
64  'Absolute URL stays unchanged 2' => [
65  'http://example.org/resource.html',
66  'http://example.org/resource.html',
67  [
68  'forceAbsoluteUrl' => '1',
69  ],
70  ],
71  'Scheme and host w/o ending slash stays unchanged' => [
72  'http://example.org',
73  'http://example.org',
74  [
75  'forceAbsoluteUrl' => '1',
76  ],
77  ],
78  'Scheme can be forced' => [
79  'typo3://example.org',
80  'http://example.org',
81  [
82  'forceAbsoluteUrl' => '1',
83  'forceAbsoluteUrl.' => [
84  'scheme' => 'typo3',
85  ],
86  ],
87  ],
88  'Relative path old-style' => [
89  'http://localhost/fileadmin/dummy.txt',
90  '/fileadmin/dummy.txt',
91  [
92  'forceAbsoluteUrl' => '1',
93  ],
94  ],
95  'Relative path' => [
96  'http://localhost/fileadmin/dummy.txt',
97  'fileadmin/dummy.txt',
98  [
99  'forceAbsoluteUrl' => '1',
100  ],
101  ],
102  'Scheme can be forced with pseudo-relative path' => [
103  'typo3://localhost/fileadmin/dummy.txt',
104  '/fileadmin/dummy.txt',
105  [
106  'forceAbsoluteUrl' => '1',
107  'forceAbsoluteUrl.' => [
108  'scheme' => 'typo3',
109  ],
110  ],
111  ],
112  'Hostname only is not treated as valid absolute URL' => [
113  'http://localhost/example.org',
114  'example.org',
115  [
116  'forceAbsoluteUrl' => '1',
117  ],
118  ],
119  'Scheme and host is added to local file path' => [
120  'typo3://localhost/fileadmin/my.pdf',
121  'fileadmin/my.pdf',
122  [
123  'forceAbsoluteUrl' => '1',
124  'forceAbsoluteUrl.' => [
125  'scheme' => 'typo3',
126  ],
127  ],
128  ],
129  'Scheme can be forced with full URL with path' => [
130  'typo3://example.org/subfolder/file.txt',
131  'http://example.org/subfolder/file.txt',
132  [
133  'forceAbsoluteUrl' => '1',
134  'forceAbsoluteUrl.' => [
135  'scheme' => 'typo3',
136  ],
137  ],
138  ],
139  ];
140  }
141 
142  #[DataProvider('forceAbsoluteUrlReturnsCorrectAbsoluteUrlDataProvider')]
143  #[Test]
144  public function ‪forceAbsoluteUrlReturnsCorrectAbsoluteUrl(string $expected, string ‪$url, array $configuration): void
145  {
146  $frontendControllerMock = $this->createMock(TypoScriptFrontendController::class);
147  $frontendControllerMock->absRefPrefix = '';
148  $cObj = new ‪ContentObjectRenderer($frontendControllerMock, new Container());
149  // Force hostname
150  $serverRequest = new ‪ServerRequest(
151  'http://localhost/index.php',
152  'GET',
153  null,
154  [],
155  ['HTTP_HOST' => 'localhost', 'SCRIPT_NAME' => '/index.php']
156  );
157  $cObj->setRequest($serverRequest);
158  $subject = new ‪AbstractTypolinkBuilderFixture($cObj, $frontendControllerMock);
159  self::assertEquals($expected, $subject->forceAbsoluteUrl(‪$url, $configuration));
160  }
161 
162  #[Test]
164  {
165  $frontendControllerMock = $this->createMock(TypoScriptFrontendController::class);
166  $cObj = new ‪ContentObjectRenderer($frontendControllerMock, new Container());
167  // Force hostname
168  $serverRequest = new ‪ServerRequest(
169  'http://localhost/subfolder/index.php',
170  'GET',
171  null,
172  [],
173  ['HTTP_HOST' => 'localhost', 'SCRIPT_NAME' => '/subfolder/index.php']
174  );
175  $cObj->setRequest($serverRequest);
176  $subject = new ‪AbstractTypolinkBuilderFixture($cObj, $frontendControllerMock);
177  $expected = 'http://localhost/subfolder/fileadmin/my.pdf';
178  ‪$url = 'fileadmin/my.pdf';
179  $configuration = [
180  'forceAbsoluteUrl' => '1',
181  ];
182  self::assertEquals($expected, $subject->forceAbsoluteUrl(‪$url, $configuration));
183  }
184 
185  public static function ‪resolveTargetAttributeDataProvider(): array
186  {
187  $targetName = ‪StringUtility::getUniqueId('name_');
188  $target = ‪StringUtility::getUniqueId('target_');
189  return [
190  'Take target from $conf, if $conf[$targetName] is set.' =>
191  [
192  'expected' => $target,
193  'conf' => [$targetName => $target], // $targetName is set
194  'name' => $targetName,
195  ],
196  ' If all hopes fail, an empty string is returned. ' =>
197  [
198  'expected' => '',
199  'conf' => [],
200  'name' => $targetName,
201  ],
202  'It finally applies stdWrap' =>
203  [
204  'expected' => 'wrap_target',
205  'conf' => [$targetName . '.' =>
206  [ 'ifEmpty' => 'wrap_target' ],
207  ],
208  'name' => $targetName,
209  ],
210  ];
211  }
212 
213  #[DataProvider('resolveTargetAttributeDataProvider')]
214  #[Test]
216  string $expected,
217  array $conf,
218  string $name,
219  ): void {
220  $frontendControllerMock = $this->createMock(TypoScriptFrontendController::class);
221  $cObj = new ‪ContentObjectRenderer($frontendControllerMock, new Container());
222  $serverRequest = new ‪ServerRequest(
223  'http://localhost/subfolder/index.php',
224  'GET',
225  null,
226  [],
227  ['HTTP_HOST' => 'localhost', 'SCRIPT_NAME' => '/subfolder/index.php']
228  );
229  $cObj->setRequest($serverRequest);
230  $container = new Container();
231  $container->set(EventDispatcherInterface::class, new ‪NoopEventDispatcher());
232  GeneralUtility::setContainer($container);
233  $subject = new ‪AbstractTypolinkBuilderFixture($cObj, $frontendControllerMock);
234  self::assertEquals($expected, $subject->resolveTargetAttribute($conf, $name));
235  }
236 }
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Webhooks\Message\$url
‪identifier readonly UriInterface $url
Definition: LoginErrorOccurredMessage.php:36
‪TYPO3\CMS\Core\EventDispatcher\NoopEventDispatcher
Definition: NoopEventDispatcher.php:29
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:58
‪TYPO3\CMS\Core\Log\LogManager
Definition: LogManager.php:33
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:102
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static getUniqueId(string $prefix='')
Definition: StringUtility.php:57