2 declare(strict_types = 1);
18 use Prophecy\Argument;
19 use Prophecy\Prophecy\ObjectProphecy;
20 use Psr\Log\LoggerInterface;
31 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
55 protected function setUp()
58 $loggerProphecy = $this->prophesize(LoggerInterface::class);
59 $this->redirectCacheServiceProphecy = $this->prophesize(RedirectCacheService::class);
60 $this->linkServiceProphecy = $this->prophesize(LinkService::class);
62 $this->redirectService->setLogger($loggerProphecy->reveal());
72 $this->redirectCacheServiceProphecy->getRedirects()->willReturn([]);
73 GeneralUtility::addInstance(RedirectCacheService::class, $this->redirectCacheServiceProphecy->reveal());
75 $result = $this->redirectService->matchRedirect(
'example.com',
'foo');
77 self::assertNull($result);
88 'target' =>
'https://example.com',
90 'keep_query_parameters' =>
'0',
91 'target_statuscode' =>
'307',
96 $this->redirectCacheServiceProphecy->getRedirects()->willReturn(
107 GeneralUtility::addInstance(RedirectCacheService::class, $this->redirectCacheServiceProphecy->reveal());
109 $result = $this->redirectService->matchRedirect(
'example.com', rawurlencode($path));
111 self::assertSame($row, $result);
126 'various special chars' => [
127 'special-chars-«-∑-€-®-†-Ω-¨-ø-π-å-‚-∂-ƒ-©-ª-º-∆-@-¥-≈-ç-√-∫-~-µ-∞-…-–'
144 'target' =>
'https://example.com',
145 'force_https' =>
'0',
146 'keep_query_parameters' =>
'0',
147 'respect_query_parameters' =>
'1',
148 'target_statuscode' =>
'307',
153 $this->redirectCacheServiceProphecy->getRedirects()->willReturn(
156 'respect_query_parameters' => [
157 'index.php?id=123' => [
164 GeneralUtility::addInstance(RedirectCacheService::class, $this->redirectCacheServiceProphecy->reveal());
166 $result = $this->redirectService->matchRedirect(
'example.com',
'index.php',
'id=123');
168 self::assertSame($row, $result);
177 'target' =>
'https://example.com',
178 'force_https' =>
'0',
179 'keep_query_parameters' =>
'0',
180 'respect_query_parameters' =>
'1',
181 'target_statuscode' =>
'307',
186 $this->redirectCacheServiceProphecy->getRedirects()->willReturn(
189 'respect_query_parameters' => [
190 'index.php/?id=123' => [
197 GeneralUtility::addInstance(RedirectCacheService::class, $this->redirectCacheServiceProphecy->reveal());
199 $result = $this->redirectService->matchRedirect(
'example.com',
'index.php',
'id=123');
201 self::assertSame($row, $result);
210 'target' =>
'https://example.com/target',
211 'force_https' =>
'0',
212 'keep_query_parameters' =>
'0',
213 'respect_query_parameters' =>
'1',
214 'target_statuscode' =>
'307',
219 $this->redirectCacheServiceProphecy->getRedirects()->willReturn(
222 'respect_query_parameters' => [
223 'index.php?id=123&a=b' => [
230 GeneralUtility::addInstance(RedirectCacheService::class, $this->redirectCacheServiceProphecy->reveal());
232 $result = $this->redirectService->matchRedirect(
'example.com',
'index.php',
'id=123&a=b');
234 self::assertSame($row, $result);
243 'target' =>
'https://example.com/target',
244 'force_https' =>
'0',
245 'keep_query_parameters' =>
'0',
246 'respect_query_parameters' =>
'1',
247 'target_statuscode' =>
'307',
252 $this->redirectCacheServiceProphecy->getRedirects()->willReturn(
255 'respect_query_parameters' => [
256 'index.php?id=123&a=b' => [
263 GeneralUtility::addInstance(RedirectCacheService::class, $this->redirectCacheServiceProphecy->reveal());
265 $result = $this->redirectService->matchRedirect(
'example.com',
'index.php',
'id=123&a=a');
267 self::assertSame(
null, $result);
276 'target' =>
'https://example.com/no-promotion',
277 'force_https' =>
'0',
278 'keep_query_parameters' =>
'0',
279 'respect_query_parameters' =>
'0',
280 'target_statuscode' =>
'307',
286 'target' =>
'https://example.com/promotion',
287 'force_https' =>
'0',
288 'keep_query_parameters' =>
'0',
289 'respect_query_parameters' =>
'1',
290 'target_statuscode' =>
'307',
295 $this->redirectCacheServiceProphecy->getRedirects()->willReturn(
304 'respect_query_parameters' => [
305 'special/page?key=998877' => [
312 GeneralUtility::addInstance(RedirectCacheService::class, $this->redirectCacheServiceProphecy->reveal());
314 $result = $this->redirectService->matchRedirect(
'example.com',
'special/page',
'key=998877');
316 self::assertSame($row2, $result);
325 'target' =>
'https://example.com',
326 'force_https' =>
'0',
327 'keep_query_parameters' =>
'0',
328 'target_statuscode' =>
'307',
334 'target' =>
'https://example.net',
335 'force_https' =>
'0',
336 'keep_query_parameters' =>
'0',
337 'target_statuscode' =>
'307',
342 $this->redirectCacheServiceProphecy->getRedirects()->willReturn(
360 GeneralUtility::addInstance(RedirectCacheService::class, $this->redirectCacheServiceProphecy->reveal());
362 $result = $this->redirectService->matchRedirect(
'example.com',
'foo');
364 self::assertSame($row1, $result);
373 'target' =>
'https://example.com',
374 'force_https' =>
'0',
375 'keep_query_parameters' =>
'0',
376 'target_statuscode' =>
'307',
381 $this->redirectCacheServiceProphecy->getRedirects()->willReturn(
392 GeneralUtility::addInstance(RedirectCacheService::class, $this->redirectCacheServiceProphecy->reveal());
394 $result = $this->redirectService->matchRedirect(
'example.com',
'foo');
396 self::assertSame($row, $result);
405 'target' =>
'https://example.com',
406 'force_https' =>
'0',
407 'keep_query_parameters' =>
'0',
408 'target_statuscode' =>
'307',
414 'target' =>
'https://example.net',
415 'force_https' =>
'0',
416 'keep_query_parameters' =>
'0',
417 'target_statuscode' =>
'307',
422 $this->redirectCacheServiceProphecy->getRedirects()->willReturn(
434 GeneralUtility::addInstance(RedirectCacheService::class, $this->redirectCacheServiceProphecy->reveal());
436 $result = $this->redirectService->matchRedirect(
'example.com',
'foo');
438 self::assertSame($row2, $result);
450 $result = $this->redirectService->getTargetUrl([
'target' =>
'invalid'], [],
new Uri(),
new Site(
'dummy', 13, []));
452 self::assertNull($result);
461 $redirectTargetMatch = [
462 'target' =>
'https://example.com',
463 'force_https' =>
'0',
464 'keep_query_parameters' =>
'0'
468 'url' =>
'https://example.com/'
473 $source =
new Uri(
'https://example.com');
474 $result = $this->redirectService->getTargetUrl($redirectTargetMatch, [], $source,
new Site(
'dummy', 13, []));
476 $uri =
new Uri(
'https://example.com/');
477 self::assertEquals($uri, $result);
486 $fileProphecy = $this->prophesize(File::class);
487 $fileProphecy->getPublicUrl()->willReturn(
'https://example.com/file.txt');
488 $redirectTargetMatch = [
489 'target' =>
'https://example.com',
490 'force_https' =>
'0',
491 'keep_query_parameters' =>
'0',
495 'file' => $fileProphecy->reveal()
500 $source =
new Uri(
'https://example.com');
501 $result = $this->redirectService->getTargetUrl($redirectTargetMatch, [], $source,
new Site(
'dummy', 13, []));
503 $uri =
new Uri(
'https://example.com/file.txt');
504 self::assertEquals($uri, $result);
513 $folderProphecy = $this->prophesize(Folder::class);
514 $folderProphecy->getPublicUrl()->willReturn(
'https://example.com/folder/');
515 $redirectTargetMatch = [
516 'target' =>
'https://example.com',
517 'force_https' =>
'0',
518 'keep_query_parameters' =>
'0',
520 $folder = $folderProphecy->reveal();
528 $source =
new Uri(
'https://example.com/');
529 $result = $this->redirectService->getTargetUrl($redirectTargetMatch, [], $source,
new Site(
'dummy', 13, []));
531 $uri =
new Uri(
'https://example.com/folder/');
532 self::assertEquals($uri, $result);
541 $redirectTargetMatch = [
542 'target' =>
'https://example.com',
543 'keep_query_parameters' =>
'0',
544 'force_https' =>
'1',
548 'url' =>
'http://example.com'
553 $source =
new Uri(
'https://example.com');
554 $result = $this->redirectService->getTargetUrl($redirectTargetMatch, [], $source,
new Site(
'dummy', 13, []));
556 $uri =
new Uri(
'https://example.com');
557 self::assertEquals($uri, $result);
566 $redirectTargetMatch = [
567 'target' =>
'https://example.com',
568 'force_https' =>
'0',
569 'keep_query_parameters' =>
'1'
573 'url' =>
'https://example.com/?foo=1&bar=2'
578 $source =
new Uri(
'https://example.com/?bar=2&baz=4&foo=1');
579 $result = $this->redirectService->getTargetUrl($redirectTargetMatch, [
'bar' => 3,
'baz' => 4], $source,
new Site(
'dummy', 13, []));
581 $uri =
new Uri(
'https://example.com/?bar=2&baz=4&foo=1');
582 self::assertEquals($uri, $result);
591 $siteFinder = $this->prophesize(SiteFinder::class);
594 RedirectService::class,
595 [
'getUriFromCustomLinkDetails']
598 $loggerProphecy = $this->prophesize(LoggerInterface::class);
601 $pageRecord =
't3://page?uid=13';
602 $redirectTargetMatch = [
603 'target' => $pageRecord .
' - - - foo=bar',
605 'keep_query_parameters' => 1
614 $queryParams[
'foo'] =
'bar';
615 $uri =
new Uri(
'/page?foo=bar');
617 $site =
new Site(
'dummy', 13, []);
619 ->with($redirectTargetMatch, $site, $linkDetails, $queryParams)
623 self::assertEquals($uri, $result);
631 $redirectTargetMatch = [
632 'source_path' =>
'#^/foo/(.*)#',
633 'target' =>
'https://anotherdomain.com/$1',
634 'force_https' =>
'0',
635 'keep_query_parameters' =>
'1',
640 'url' =>
'https://anotherdomain.com/$1'
642 $this->linkServiceProphecy->resolve($redirectTargetMatch[
'target'])->willReturn($linkDetails);
644 $source =
new Uri(
'https://example.com/foo/bar');
645 $result = $this->redirectService->getTargetUrl($redirectTargetMatch, [], $source,
new Site(
'dummy', 13, []));
647 $uri =
new Uri(
'https://anotherdomain.com/bar');
648 self::assertEquals($uri, $result);