‪TYPO3CMS  10.4
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 Psr\Log\LoggerInterface;
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29 
33 class ‪AbstractTypolinkBuilderTest extends UnitTestCase
34 {
38  protected ‪$resetSingletonInstances = true;
39 
43  protected ‪$backupEnvironment = true;
44 
49 
53  protected function ‪setUp(): void
54  {
55  parent::setUp();
57  $this->frontendControllerMock = $this
58  ->getMockBuilder(TypoScriptFrontendController::class)
59  ->disableOriginalConstructor()
60  ->getMock();
61  }
62 
64  // Utility functions
66 
70  protected function ‪createMockedLoggerAndLogManager()
71  {
72  $logManagerMock = $this->getMockBuilder(LogManager::class)->getMock();
73  $loggerMock = $this->getMockBuilder(LoggerInterface::class)->getMock();
74  $logManagerMock->expects(self::any())
75  ->method('getLogger')
76  ->willReturn($loggerMock);
77  GeneralUtility::setSingletonInstance(LogManager::class, $logManagerMock);
78  }
79 
84  {
85  return [
86  'Missing forceAbsoluteUrl leaves URL untouched' => [
87  'foo',
88  'foo',
89  []
90  ],
91  'Absolute URL stays unchanged' => [
92  'http://example.org/',
93  'http://example.org/',
94  [
95  'forceAbsoluteUrl' => '1'
96  ]
97  ],
98  'Absolute URL stays unchanged 2' => [
99  'http://example.org/resource.html',
100  'http://example.org/resource.html',
101  [
102  'forceAbsoluteUrl' => '1'
103  ]
104  ],
105  'Scheme and host w/o ending slash stays unchanged' => [
106  'http://example.org',
107  'http://example.org',
108  [
109  'forceAbsoluteUrl' => '1'
110  ]
111  ],
112  'Scheme can be forced' => [
113  'typo3://example.org',
114  'http://example.org',
115  [
116  'forceAbsoluteUrl' => '1',
117  'forceAbsoluteUrl.' => [
118  'scheme' => 'typo3'
119  ]
120  ]
121  ],
122  'Relative path old-style' => [
123  'http://localhost/fileadmin/dummy.txt',
124  '/fileadmin/dummy.txt',
125  [
126  'forceAbsoluteUrl' => '1',
127  ]
128  ],
129  'Relative path' => [
130  'http://localhost/fileadmin/dummy.txt',
131  'fileadmin/dummy.txt',
132  [
133  'forceAbsoluteUrl' => '1',
134  ]
135  ],
136  'Scheme can be forced with pseudo-relative path' => [
137  'typo3://localhost/fileadmin/dummy.txt',
138  '/fileadmin/dummy.txt',
139  [
140  'forceAbsoluteUrl' => '1',
141  'forceAbsoluteUrl.' => [
142  'scheme' => 'typo3'
143  ]
144  ]
145  ],
146  'Hostname only is not treated as valid absolute URL' => [
147  'http://localhost/example.org',
148  'example.org',
149  [
150  'forceAbsoluteUrl' => '1'
151  ]
152  ],
153  'Scheme and host is added to local file path' => [
154  'typo3://localhost/fileadmin/my.pdf',
155  'fileadmin/my.pdf',
156  [
157  'forceAbsoluteUrl' => '1',
158  'forceAbsoluteUrl.' => [
159  'scheme' => 'typo3'
160  ]
161  ]
162  ],
163  'Scheme can be forced with full URL with path' => [
164  'typo3://example.org/subfolder/file.txt',
165  'http://example.org/subfolder/file.txt',
166  [
167  'forceAbsoluteUrl' => '1',
168  'forceAbsoluteUrl.' => [
169  'scheme' => 'typo3'
170  ]
171  ]
172  ],
173  ];
174  }
175 
183  public function ‪forceAbsoluteUrlReturnsCorrectAbsoluteUrl($expected, $url, array $configuration)
184  {
187  true,
188  false,
193  ‪Environment::getBackendPath() . '/index.php',
194  ‪Environment::isWindows() ? 'WINDOWS' : 'UNIX'
195  );
196  $this->frontendControllerMock->absRefPrefix = '';
197  $contentObjectRendererProphecy = $this->prophesize(ContentObjectRenderer::class);
198  $subject = $this->getAccessibleMock(
199  AbstractTypolinkBuilder::class,
200  ['build'],
201  [$contentObjectRendererProphecy->reveal(), $this->frontendControllerMock]
202  );
203  // Force hostname
204  $_SERVER['HTTP_HOST'] = 'localhost';
205  $_SERVER['SCRIPT_NAME'] = '/typo3/index.php';
206  self::assertEquals($expected, $subject->_call('forceAbsoluteUrl', $url, $configuration));
207  }
208 
213  {
216  true,
217  false,
222  ‪Environment::getBackendPath() . '/index.php',
223  ‪Environment::isWindows() ? 'WINDOWS' : 'UNIX'
224  );
225  $contentObjectRendererProphecy = $this->prophesize(ContentObjectRenderer::class);
226  $subject = $this->getAccessibleMock(
227  AbstractTypolinkBuilder::class,
228  ['build'],
229  [$contentObjectRendererProphecy->reveal(), $this->frontendControllerMock]
230  );
231  // Force hostname
232  $_SERVER['HTTP_HOST'] = 'localhost';
233  $_SERVER['SCRIPT_NAME'] = '/subfolder/typo3/index.php';
234 
235  $expected = 'http://localhost/subfolder/fileadmin/my.pdf';
236  $url = 'fileadmin/my.pdf';
237  $configuration = [
238  'forceAbsoluteUrl' => '1'
239  ];
240 
241  self::assertEquals($expected, $subject->_call('forceAbsoluteUrl', $url, $configuration));
242  }
243 
249  public function ‪resolveTargetAttributeDataProvider(): array
250  {
251  $targetName = ‪StringUtility::getUniqueId('name_');
252  $target = ‪StringUtility::getUniqueId('target_');
253  $fallback = ‪StringUtility::getUniqueId('fallback_');
254  return [
255  'Take target from $conf, if $conf[$targetName] is set.' =>
256  [
257  $target,
258  [$targetName => $target], // $targetName is set
259  $targetName,
260  true,
261  $fallback,
262  'other doctype'
263  ],
264  'Else from fallback, if not $respectFrameSetOption ...' =>
265  [
266  $fallback,
267  ['directImageLink' => false],
268  $targetName,
269  false, // $respectFrameSetOption false
270  $fallback,
271  'other doctype'
272  ],
273  ' ... or no doctype ... ' =>
274  [
275  $fallback,
276  ['directImageLink' => false],
277  $targetName,
278  true,
279  $fallback,
280  null // no $doctype
281  ],
282  ' ... or doctype xhtml_trans... ' =>
283  [
284  $fallback,
285  ['directImageLink' => false],
286  $targetName,
287  true,
288  $fallback,
289  'xhtml_trans'
290  ],
291  ' ... or doctype xhtml_basic... ' =>
292  [
293  $fallback,
294  ['directImageLink' => false],
295  $targetName,
296  true,
297  $fallback,
298  'xhtml_basic'
299  ],
300  ' ... or doctype html5... ' =>
301  [
302  $fallback,
303  ['directImageLink' => false],
304  $targetName,
305  true,
306  $fallback,
307  'html5'
308  ],
309  ' If all hopes fail, an empty string is returned. ' =>
310  [
311  '',
312  [],
313  $targetName,
314  true,
315  $fallback,
316  'other doctype'
317  ],
318  'It finally applies stdWrap' =>
319  [
320  'wrap_target',
321  [$targetName . '.' =>
322  [ 'ifEmpty' => 'wrap_target' ]
323  ],
324  $targetName,
325  true,
326  $fallback,
327  'other doctype'
328  ],
329  ];
330  }
331 
342  public function ‪canResolveTheTargetAttribute(
343  string $expected,
344  array $conf,
345  string $name,
346  bool $respectFrameSetOption,
347  string $fallbackTarget,
348  $doctype
349  ) {
350  $this->frontendControllerMock->config =
351  ['config' => [ 'doctype' => $doctype]];
352  $renderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
353  $subject = $this->getAccessibleMockForAbstractClass(AbstractTypolinkBuilder::class, [$renderer, $this->frontendControllerMock]);
354  $actual = $subject->_call(
355  'resolveTargetAttribute',
356  $conf,
357  $name,
358  $respectFrameSetOption,
359  $fallbackTarget
360  );
361  self::assertEquals($expected, $actual);
362  }
363 }
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:180
‪TYPO3\CMS\Core\Core\Environment\isWindows
‪static bool isWindows()
Definition: Environment.php:292
‪TYPO3\CMS\Core\Core\Environment\getContext
‪static ApplicationContext getContext()
Definition: Environment.php:133
‪TYPO3\CMS\Core\Core\Environment\getProjectPath
‪static string getProjectPath()
Definition: Environment.php:169
‪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:104
‪TYPO3\CMS\Core\Core\Environment\getBackendPath
‪static string getBackendPath()
Definition: Environment.php:250
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:98
‪TYPO3\CMS\Core\Log\LogManager
Definition: LogManager.php:30
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Core\Core\Environment\getConfigPath
‪static string getConfigPath()
Definition: Environment.php:210
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:97
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:192