‪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\MockObject\MockObject;
21 use Psr\Log\LoggerInterface;
22 use Symfony\Component\DependencyInjection\Container;
31 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
32 
33 final class ‪AbstractTypolinkBuilderTest extends UnitTestCase
34 {
35  protected bool ‪$resetSingletonInstances = true;
36 
37  protected bool ‪$backupEnvironment = true;
38 
40 
41  protected function ‪setUp(): void
42  {
43  parent::setUp();
45  $this->frontendControllerMock = $this
46  ->getMockBuilder(TypoScriptFrontendController::class)
47  ->disableOriginalConstructor()
48  ->getMock();
49  }
50 
52  // Utility functions
54 
58  protected function ‪createMockedLoggerAndLogManager(): void
59  {
60  $logManagerMock = $this->getMockBuilder(LogManager::class)->getMock();
61  $loggerMock = $this->getMockBuilder(LoggerInterface::class)->getMock();
62  $logManagerMock
63  ->method('getLogger')
64  ->willReturn($loggerMock);
65  GeneralUtility::setSingletonInstance(LogManager::class, $logManagerMock);
66  }
67 
72  {
73  return [
74  'Missing forceAbsoluteUrl leaves URL untouched' => [
75  'foo',
76  'foo',
77  [],
78  ],
79  'Absolute URL stays unchanged' => [
80  'http://example.org/',
81  'http://example.org/',
82  [
83  'forceAbsoluteUrl' => '1',
84  ],
85  ],
86  'Absolute URL stays unchanged 2' => [
87  'http://example.org/resource.html',
88  'http://example.org/resource.html',
89  [
90  'forceAbsoluteUrl' => '1',
91  ],
92  ],
93  'Scheme and host w/o ending slash stays unchanged' => [
94  'http://example.org',
95  'http://example.org',
96  [
97  'forceAbsoluteUrl' => '1',
98  ],
99  ],
100  'Scheme can be forced' => [
101  'typo3://example.org',
102  'http://example.org',
103  [
104  'forceAbsoluteUrl' => '1',
105  'forceAbsoluteUrl.' => [
106  'scheme' => 'typo3',
107  ],
108  ],
109  ],
110  'Relative path old-style' => [
111  'http://localhost/fileadmin/dummy.txt',
112  '/fileadmin/dummy.txt',
113  [
114  'forceAbsoluteUrl' => '1',
115  ],
116  ],
117  'Relative path' => [
118  'http://localhost/fileadmin/dummy.txt',
119  'fileadmin/dummy.txt',
120  [
121  'forceAbsoluteUrl' => '1',
122  ],
123  ],
124  'Scheme can be forced with pseudo-relative path' => [
125  'typo3://localhost/fileadmin/dummy.txt',
126  '/fileadmin/dummy.txt',
127  [
128  'forceAbsoluteUrl' => '1',
129  'forceAbsoluteUrl.' => [
130  'scheme' => 'typo3',
131  ],
132  ],
133  ],
134  'Hostname only is not treated as valid absolute URL' => [
135  'http://localhost/example.org',
136  'example.org',
137  [
138  'forceAbsoluteUrl' => '1',
139  ],
140  ],
141  'Scheme and host is added to local file path' => [
142  'typo3://localhost/fileadmin/my.pdf',
143  'fileadmin/my.pdf',
144  [
145  'forceAbsoluteUrl' => '1',
146  'forceAbsoluteUrl.' => [
147  'scheme' => 'typo3',
148  ],
149  ],
150  ],
151  'Scheme can be forced with full URL with path' => [
152  'typo3://example.org/subfolder/file.txt',
153  'http://example.org/subfolder/file.txt',
154  [
155  'forceAbsoluteUrl' => '1',
156  'forceAbsoluteUrl.' => [
157  'scheme' => 'typo3',
158  ],
159  ],
160  ],
161  ];
162  }
163 
171  public function ‪forceAbsoluteUrlReturnsCorrectAbsoluteUrl(string $expected, string ‪$url, array $configuration): void
172  {
175  true,
176  false,
181  ‪Environment::getPublicPath() . '/index.php',
182  ‪Environment::isWindows() ? 'WINDOWS' : 'UNIX'
183  );
184  $this->frontendControllerMock->absRefPrefix = '';
185  $cObj = new ‪ContentObjectRenderer($this->frontendControllerMock, new Container());
186  // Force hostname
187  $serverRequest = new ‪ServerRequest(
188  'http://localhost/index.php',
189  'GET',
190  null,
191  [],
192  ['HTTP_HOST' => 'localhost', 'SCRIPT_NAME' => '/index.php']
193  );
194  $cObj->setRequest($serverRequest);
195  $subject = $this->getAccessibleMock(
196  AbstractTypolinkBuilder::class,
197  ['build'],
198  [$cObj, $this->frontendControllerMock]
199  );
200 
201  self::assertEquals($expected, $subject->_call('forceAbsoluteUrl', ‪$url, $configuration));
202  }
203 
208  {
211  true,
212  false,
217  ‪Environment::getPublicPath() . '/index.php',
218  ‪Environment::isWindows() ? 'WINDOWS' : 'UNIX'
219  );
220  $cObj = new ‪ContentObjectRenderer($this->frontendControllerMock, new Container());
221 
222  // Force hostname
223  $serverRequest = new ‪ServerRequest(
224  'http://localhost/subfolder/index.php',
225  'GET',
226  null,
227  [],
228  ['HTTP_HOST' => 'localhost', 'SCRIPT_NAME' => '/subfolder/index.php']
229  );
230  $cObj->setRequest($serverRequest);
231  $subject = $this->getAccessibleMock(
232  AbstractTypolinkBuilder::class,
233  ['build'],
234  [$cObj, $this->frontendControllerMock]
235  );
236 
237  $expected = 'http://localhost/subfolder/fileadmin/my.pdf';
238  ‪$url = 'fileadmin/my.pdf';
239  $configuration = [
240  'forceAbsoluteUrl' => '1',
241  ];
242 
243  self::assertEquals($expected, $subject->_call('forceAbsoluteUrl', ‪$url, $configuration));
244  }
245 
251  public static function ‪resolveTargetAttributeDataProvider(): array
252  {
253  $targetName = ‪StringUtility::getUniqueId('name_');
254  $target = ‪StringUtility::getUniqueId('target_');
255  return [
256  'Take target from $conf, if $conf[$targetName] is set.' =>
257  [
258  $target,
259  [$targetName => $target], // $targetName is set
260  $targetName,
261  ],
262  ' If all hopes fail, an empty string is returned. ' =>
263  [
264  '',
265  [],
266  $targetName,
267  ],
268  'It finally applies stdWrap' =>
269  [
270  'wrap_target',
271  [$targetName . '.' =>
272  [ 'ifEmpty' => 'wrap_target' ],
273  ],
274  $targetName,
275  ],
276  ];
277  }
278 
284  string $expected,
285  array $conf,
286  string $name,
287  ): void {
288  $cObj = new ‪ContentObjectRenderer($this->frontendControllerMock, new Container());
289  $serverRequest = new ‪ServerRequest(
290  'http://localhost/subfolder/index.php',
291  'GET',
292  null,
293  [],
294  ['HTTP_HOST' => 'localhost', 'SCRIPT_NAME' => '/subfolder/index.php']
295  );
296  $cObj->setRequest($serverRequest);
297  $subject = $this->getAccessibleMockForAbstractClass(AbstractTypolinkBuilder::class, [$cObj, $this->frontendControllerMock]);
298  $actual = $subject->_call(
299  'resolveTargetAttribute',
300  $conf,
301  $name,
302  );
303  self::assertEquals($expected, $actual);
304  }
305 }
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static getPublicPath()
Definition: Environment.php:187
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static getVarPath()
Definition: Environment.php:197
‪TYPO3\CMS\Core\Core\Environment\getConfigPath
‪static getConfigPath()
Definition: Environment.php:212
‪TYPO3\CMS\Core\Core\Environment\getProjectPath
‪static string getProjectPath()
Definition: Environment.php:160
‪TYPO3\CMS\Core\Core\Environment\initialize
‪static initialize(ApplicationContext $context, bool $cli, bool $composerMode, string $projectPath, string $publicPath, string $varPath, string $configPath, string $currentScript, string $os)
Definition: Environment.php:100
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Webhooks\Message\$url
‪identifier readonly UriInterface $url
Definition: LoginErrorOccurredMessage.php:36
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:102
‪TYPO3\CMS\Core\Log\LogManager
Definition: LogManager.php:33
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:92
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Core\Core\Environment\getContext
‪static getContext()
Definition: Environment.php:128
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static getUniqueId(string $prefix='')
Definition: StringUtility.php:57
‪TYPO3\CMS\Core\Core\Environment\isWindows
‪static isWindows()
Definition: Environment.php:276