TYPO3 CMS  TYPO3_8-7
AbstractTypolinkBuilderTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
26 
30 class AbstractTypolinkBuilderTest extends UnitTestCase
31 {
35  protected $singletonInstances = [];
36 
40  protected $frontendControllerMock = null;
41 
45  protected $templateServiceMock = null;
46 
50  protected function setUp()
51  {
52  $this->singletonInstances = GeneralUtility::getSingletonInstances();
54 
55  $this->templateServiceMock =
56  $this->getMockBuilder(TemplateService::class)
57  ->setMethods(['getFileName', 'linkData'])->getMock();
58  $pageRepositoryMock =
59  $this->getAccessibleMock(PageRepository::class, ['getRawRecord', 'getMountPointInfo']);
60  $this->frontendControllerMock =
61  $this->getAccessibleMock(
62  TypoScriptFrontendController::class,
63  ['dummy'],
64  [],
65  '',
66  false
67  );
68  $this->frontendControllerMock->tmpl = $this->templateServiceMock;
69  $this->frontendControllerMock->config = [];
70  $this->frontendControllerMock->page = [];
71  $this->frontendControllerMock->sys_page = $pageRepositoryMock;
73  }
74 
75  protected function tearDown()
76  {
77  GeneralUtility::resetSingletonInstances($this->singletonInstances);
78  parent::tearDown();
79  }
80 
82  // Utility functions
84 
88  protected function getFrontendController()
89  {
90  return $GLOBALS['TSFE'];
91  }
92 
96  protected function createMockedLoggerAndLogManager()
97  {
98  $logManagerMock = $this->getMockBuilder(LogManager::class)->getMock();
99  $loggerMock = $this->getMockBuilder(LoggerInterface::class)->getMock();
100  $logManagerMock->expects($this->any())
101  ->method('getLogger')
102  ->willReturn($loggerMock);
103  GeneralUtility::setSingletonInstance(LogManager::class, $logManagerMock);
104  }
105 
110  {
111  return [
112  'Missing forceAbsoluteUrl leaves URL untouched' => [
113  'foo',
114  'foo',
115  []
116  ],
117  'Absolute URL stays unchanged' => [
118  'http://example.org/',
119  'http://example.org/',
120  [
121  'forceAbsoluteUrl' => '1'
122  ]
123  ],
124  'Absolute URL stays unchanged 2' => [
125  'http://example.org/resource.html',
126  'http://example.org/resource.html',
127  [
128  'forceAbsoluteUrl' => '1'
129  ]
130  ],
131  'Scheme and host w/o ending slash stays unchanged' => [
132  'http://example.org',
133  'http://example.org',
134  [
135  'forceAbsoluteUrl' => '1'
136  ]
137  ],
138  'Scheme can be forced' => [
139  'typo3://example.org',
140  'http://example.org',
141  [
142  'forceAbsoluteUrl' => '1',
143  'forceAbsoluteUrl.' => [
144  'scheme' => 'typo3'
145  ]
146  ]
147  ],
148  'Relative path old-style' => [
149  'http://localhost/fileadmin/dummy.txt',
150  '/fileadmin/dummy.txt',
151  [
152  'forceAbsoluteUrl' => '1',
153  ]
154  ],
155  'Relative path' => [
156  'http://localhost/fileadmin/dummy.txt',
157  'fileadmin/dummy.txt',
158  [
159  'forceAbsoluteUrl' => '1',
160  ]
161  ],
162  'Scheme can be forced with pseudo-relative path' => [
163  'typo3://localhost/fileadmin/dummy.txt',
164  '/fileadmin/dummy.txt',
165  [
166  'forceAbsoluteUrl' => '1',
167  'forceAbsoluteUrl.' => [
168  'scheme' => 'typo3'
169  ]
170  ]
171  ],
172  'Hostname only is not treated as valid absolute URL' => [
173  'http://localhost/example.org',
174  'example.org',
175  [
176  'forceAbsoluteUrl' => '1'
177  ]
178  ],
179  'Scheme and host is added to local file path' => [
180  'typo3://localhost/fileadmin/my.pdf',
181  'fileadmin/my.pdf',
182  [
183  'forceAbsoluteUrl' => '1',
184  'forceAbsoluteUrl.' => [
185  'scheme' => 'typo3'
186  ]
187  ]
188  ]
189  ];
190  }
191 
199  public function forceAbsoluteUrlReturnsCorrectAbsoluteUrl($expected, $url, array $configuration)
200  {
201  $contentObjectRendererProphecy = $this->prophesize(ContentObjectRenderer::class);
202  $subject = $this->getAccessibleMock(
203  AbstractTypolinkBuilder::class,
204  ['build'],
205  [$contentObjectRendererProphecy->reveal()],
206  '',
207  false
208  );
209  // Force hostname
210  $_SERVER['HTTP_HOST'] = 'localhost';
211  $_SERVER['SCRIPT_NAME'] = '/typo3/index.php';
212  $GLOBALS['TSFE']->absRefPrefix = '';
213 
214  $this->assertEquals($expected, $subject->_call('forceAbsoluteUrl', $url, $configuration));
215  }
216 
221  {
222  $contentObjectRendererProphecy = $this->prophesize(ContentObjectRenderer::class);
223  $subject = $this->getAccessibleMock(
224  AbstractTypolinkBuilder::class,
225  ['build'],
226  [$contentObjectRendererProphecy->reveal()],
227  '',
228  false
229  );
230  // Force hostname
231  $_SERVER['HTTP_HOST'] = 'localhost';
232  $_SERVER['SCRIPT_NAME'] = '/subfolder/typo3/index.php';
233 
234  $expected = 'http://localhost/subfolder/fileadmin/my.pdf';
235  $url = 'fileadmin/my.pdf';
236  $configuration = [
237  'forceAbsoluteUrl' => '1'
238  ];
239 
240  $this->assertEquals($expected, $subject->_call('forceAbsoluteUrl', $url, $configuration));
241  }
242 
248  public function resolveTargetAttributeDataProvider(): array
249  {
250  $targetName = $this->getUniqueId('name_');
251  $target = $this->getUniqueId('target_');
252  $fallback = $this->getUniqueId('fallback_');
253  return [
254  'Take target from $conf, if $conf[$targetName] is set.' =>
255  [
256  $target,
257  [$targetName => $target], // $targetName is set
258  $targetName,
259  true,
260  $fallback,
261  'other doctype'
262  ],
263  'Else from fallback, if not $respectFrameSetOption ...' =>
264  [
265  $fallback,
266  [],
267  $targetName,
268  false, // $respectFrameSetOption false
269  $fallback,
270  'other doctype'
271  ],
272  ' ... or no doctype ... ' =>
273  [
274  $fallback,
275  [],
276  $targetName,
277  true,
278  $fallback,
279  null // no $doctype
280  ],
281  ' ... or doctype xhtml_trans... ' =>
282  [
283  $fallback,
284  [],
285  $targetName,
286  true,
287  $fallback,
288  'xhtml_trans'
289  ],
290  ' ... or doctype xhtml_basic... ' =>
291  [
292  $fallback,
293  [],
294  $targetName,
295  true,
296  $fallback,
297  'xhtml_basic'
298  ],
299  ' ... or doctype html5... ' =>
300  [
301  $fallback,
302  [],
303  $targetName,
304  true,
305  $fallback,
306  'html5'
307  ],
308  ' If all hopes fail, an empty string is returned. ' =>
309  [
310  '',
311  [],
312  $targetName,
313  true,
314  $fallback,
315  'other doctype'
316  ],
317  'It finally applies stdWrap' =>
318  [
319  'wrap_target',
320  [$targetName . '.' =>
321  [ 'ifEmpty' => 'wrap_target' ]
322  ],
323  $targetName,
324  true,
325  $fallback,
326  'other doctype'
327  ],
328  ];
329  }
330 
342  string $expected,
343  array $conf,
344  string $name,
345  bool $respectFrameSetOption,
346  string $fallbackTarget,
347  $doctype
348  ) {
349  $this->frontendControllerMock->config =
350  ['config' => [ 'doctype' => $doctype]];
351  $renderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
352  $subject = $this->getMockBuilder(AbstractTypolinkBuilder::class)
353  ->setConstructorArgs([$renderer])
354  ->setMethods(['build'])
355  ->getMock();
356  $actual = $this->callInaccessibleMethod(
357  $subject,
358  'resolveTargetAttribute',
359  $conf,
360  $name,
361  $respectFrameSetOption,
362  $fallbackTarget
363  );
364  $this->assertEquals($expected, $actual);
365  }
366 }
static setSingletonInstance($className, SingletonInterface $instance)
static makeInstance($className,... $constructorArguments)
static resetSingletonInstances(array $newSingletonInstances)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']