/
RedirectServiceTest.php
112 lines (94 loc) · 4.04 KB
/
RedirectServiceTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Redirects\Tests\Functional\Service;
use Psr\Log\LoggerInterface;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Http\Uri;
use TYPO3\CMS\Core\LinkHandling\LinkService;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
use TYPO3\CMS\Redirects\Service\RedirectCacheService;
use TYPO3\CMS\Redirects\Service\RedirectService;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
class RedirectServiceTest extends FunctionalTestCase
{
use \Prophecy\PhpUnit\ProphecyTrait;
use SiteBasedTestTrait;
/**
* @var string[]
*/
protected $coreExtensionsToLoad = ['redirects'];
protected array $testFilesToDelete = [];
protected function tearDown(): void
{
foreach ($this->testFilesToDelete as $filename) {
if (@is_file($filename)) {
unlink($filename);
}
}
parent::tearDown();
}
/**
* @test
*/
public function linkForRedirectToAccessRestrictedPageIsBuild(): void
{
$this->importDataSet(__DIR__ . '/Fixtures/RedirectToAccessRestrictedPages.xml');
$this->writeSiteConfiguration(
'acme-com',
$this->buildSiteConfiguration(1, 'https://acme.com/')
);
$typoscriptFile = Environment::getVarPath() . '/transient/setup.typoscript';
file_put_contents($typoscriptFile, 'page = PAGE' . PHP_EOL . 'page.typeNum = 0');
$this->testFilesToDelete[] = $typoscriptFile;
$this->setUpFrontendRootPage(1, [$typoscriptFile]);
$logger = $this->prophesize(LoggerInterface::class);
$frontendUserAuthentication = new FrontendUserAuthentication();
$frontendUserAuthentication->setLogger($logger->reveal());
$siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
$uri = new Uri('https://acme.com/redirect-to-access-restricted-site');
$request = $GLOBALS['TYPO3_REQUEST'] = (new ServerRequest($uri))
->withAttribute('site', $siteFinder->getSiteByRootPageId(1))
->withAttribute('frontend.user', $frontendUserAuthentication)
->withAttribute('applicationType', SystemEnvironmentBuilder::REQUESTTYPE_FE);
$linkServiceProphecy = $this->prophesize(LinkService::class);
$linkServiceProphecy->resolve('t3://page?uid=2')->willReturn(
[
'pageuid' => 2,
'type' => LinkService::TYPE_PAGE
]
);
$redirectService = new RedirectService(
new RedirectCacheService(),
$linkServiceProphecy->reveal(),
$siteFinder
);
$redirectService->setLogger($logger->reveal());
// Assert correct redirect is matched
$redirectMatch = $redirectService->matchRedirect($uri->getHost(), $uri->getPath(), $uri->getQuery());
self::assertEquals(1, $redirectMatch['uid']);
self::assertEquals('t3://page?uid=2', $redirectMatch['target']);
// Ensure we deal with an unauthorized request!
self::assertFalse(GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('frontend.user', 'isLoggedIn'));
// Assert link to access restricted page is build
$targetUrl = $redirectService->getTargetUrl($redirectMatch, $request);
self::assertEquals(new Uri('https://acme.com/access-restricted'), $targetUrl);
}
}