‪TYPO3CMS  9.5
AbstractTypolinkBuilderTest.php
Go to the documentation of this file.
1 <?php
2 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 
18 use Psr\Log\LoggerInterface;
25 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
26 
30 class ‪AbstractTypolinkBuilderTest extends UnitTestCase
31 {
35  protected ‪$resetSingletonInstances = true;
36 
40  protected ‪$backupEnvironment = true;
41 
46 
50  protected function ‪setUp()
51  {
52  parent::setUp();
54  $this->frontendControllerMock = $this->getAccessibleMock(
55  TypoScriptFrontendController::class,
56  ['dummy'],
57  [],
58  '',
59  false
60  );
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($this->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  ];
164  }
165 
173  public function ‪forceAbsoluteUrlReturnsCorrectAbsoluteUrl($expected, $url, array $configuration)
174  {
177  true,
178  false,
183  ‪Environment::getBackendPath() . '/index.php',
184  ‪Environment::isWindows() ? 'WINDOWS' : 'UNIX'
185  );
186  $this->frontendControllerMock->absRefPrefix = '';
187  $contentObjectRendererProphecy = $this->prophesize(ContentObjectRenderer::class);
188  $subject = $this->getAccessibleMock(
189  AbstractTypolinkBuilder::class,
190  ['build'],
191  [$contentObjectRendererProphecy->reveal(), $this->frontendControllerMock]
192  );
193  // Force hostname
194  $_SERVER['HTTP_HOST'] = 'localhost';
195  $_SERVER['SCRIPT_NAME'] = '/typo3/index.php';
196  $this->assertEquals($expected, $subject->_call('forceAbsoluteUrl', $url, $configuration));
197  }
198 
203  {
206  true,
207  false,
212  ‪Environment::getBackendPath() . '/index.php',
213  ‪Environment::isWindows() ? 'WINDOWS' : 'UNIX'
214  );
215  $contentObjectRendererProphecy = $this->prophesize(ContentObjectRenderer::class);
216  $subject = $this->getAccessibleMock(
217  AbstractTypolinkBuilder::class,
218  ['build'],
219  [$contentObjectRendererProphecy->reveal(), $this->frontendControllerMock]
220  );
221  // Force hostname
222  $_SERVER['HTTP_HOST'] = 'localhost';
223  $_SERVER['SCRIPT_NAME'] = '/subfolder/typo3/index.php';
224 
225  $expected = 'http://localhost/subfolder/fileadmin/my.pdf';
226  $url = 'fileadmin/my.pdf';
227  $configuration = [
228  'forceAbsoluteUrl' => '1'
229  ];
230 
231  $this->assertEquals($expected, $subject->_call('forceAbsoluteUrl', $url, $configuration));
232  }
233 
239  public function ‪resolveTargetAttributeDataProvider(): array
240  {
241  $targetName = $this->getUniqueId('name_');
242  $target = $this->getUniqueId('target_');
243  $fallback = $this->getUniqueId('fallback_');
244  return [
245  'Take target from $conf, if $conf[$targetName] is set.' =>
246  [
247  $target,
248  [$targetName => $target], // $targetName is set
249  $targetName,
250  true,
251  $fallback,
252  'other doctype'
253  ],
254  'Else from fallback, if not $respectFrameSetOption ...' =>
255  [
256  $fallback,
257  ['directImageLink' => false],
258  $targetName,
259  false, // $respectFrameSetOption false
260  $fallback,
261  'other doctype'
262  ],
263  ' ... or no doctype ... ' =>
264  [
265  $fallback,
266  ['directImageLink' => false],
267  $targetName,
268  true,
269  $fallback,
270  null // no $doctype
271  ],
272  ' ... or doctype xhtml_trans... ' =>
273  [
274  $fallback,
275  ['directImageLink' => false],
276  $targetName,
277  true,
278  $fallback,
279  'xhtml_trans'
280  ],
281  ' ... or doctype xhtml_basic... ' =>
282  [
283  $fallback,
284  ['directImageLink' => false],
285  $targetName,
286  true,
287  $fallback,
288  'xhtml_basic'
289  ],
290  ' ... or doctype html5... ' =>
291  [
292  $fallback,
293  ['directImageLink' => false],
294  $targetName,
295  true,
296  $fallback,
297  'html5'
298  ],
299  ' If all hopes fail, an empty string is returned. ' =>
300  [
301  '',
302  [],
303  $targetName,
304  true,
305  $fallback,
306  'other doctype'
307  ],
308  'It finally applies stdWrap' =>
309  [
310  'wrap_target',
311  [$targetName . '.' =>
312  [ 'ifEmpty' => 'wrap_target' ]
313  ],
314  $targetName,
315  true,
316  $fallback,
317  'other doctype'
318  ],
319  ];
320  }
321 
332  public function ‪canResolveTheTargetAttribute(
333  string $expected,
334  array $conf,
335  string $name,
336  bool $respectFrameSetOption,
337  string $fallbackTarget,
338  $doctype
339  ) {
340  $this->frontendControllerMock->config =
341  ['config' => [ 'doctype' => $doctype]];
342  $renderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
343  $subject = $this->getMockBuilder(AbstractTypolinkBuilder::class)
344  ->setConstructorArgs([$renderer, $this->frontendControllerMock])
345  ->setMethods(['build'])
346  ->getMock();
347  $actual = $this->callInaccessibleMethod(
348  $subject,
349  'resolveTargetAttribute',
350  $conf,
351  $name,
352  $respectFrameSetOption,
353  $fallbackTarget
354  );
355  $this->assertEquals($expected, $actual);
356  }
357 }
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:153
‪TYPO3\CMS\Core\Core\Environment\isWindows
‪static bool isWindows()
Definition: Environment.php:266
‪TYPO3\CMS\Core\Core\Environment\getContext
‪static ApplicationContext getContext()
Definition: Environment.php:106
‪TYPO3\CMS\Core\Core\Environment\getProjectPath
‪static string getProjectPath()
Definition: Environment.php:142
‪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:77
‪TYPO3\CMS\Core\Core\Environment\getBackendPath
‪static string getBackendPath()
Definition: Environment.php:223
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:97
‪TYPO3\CMS\Core\Log\LogManager
Definition: LogManager.php:25
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\Core\Core\Environment\getConfigPath
‪static string getConfigPath()
Definition: Environment.php:183
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:91
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:165