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