‪TYPO3CMS  9.5
PageResolverTest.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
19 use PHPUnit\Framework\MockObject\MockObject;
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
22 use Psr\Http\Server\RequestHandlerInterface;
34 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
35 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
36 
37 class ‪PageResolverTest extends UnitTestCase
38 {
42  protected ‪$resetSingletonInstances = true;
43 
47  protected ‪$controller;
48 
52  protected ‪$responseOutputHandler;
53 
57  protected ‪$subject;
58 
59  protected function ‪setUp(): void
60  {
61  $this->markTestSkipped('Has to be adjusted');
62 
63  $this->controller = $this->getAccessibleMock(TypoScriptFrontendController::class, ['getSiteScript', 'determineId', 'isBackendUserLoggedIn'], [], '', false);
64 
65  // A request handler which expects a site with some more details are found.
66  $this->responseOutputHandler = new class implements RequestHandlerInterface {
67  public function handle(ServerRequestInterface $request): ResponseInterface
68  {
70  $site = $request->getAttribute('site');
72  $language = $request->getAttribute('language');
74  $routeResult = $request->getAttribute('routing', false);
75  if ($routeResult) {
76  return new ‪JsonResponse(
77  [
78  'site' => $site->getIdentifier(),
79  'language-id' => $language->getLanguageId(),
80  'pageId' => $routeResult->getPageId(),
81  ]
82  );
83  }
84  return new ‪NullResponse();
85  }
86  };
87  }
88 
93  {
94  $incomingUrl = 'https://king.com/lotus-flower/en/mr-magpie/bloom';
95  $pageRecord = ['uid' => 13, 'l10n_parent' => 0, 'slug' => '/mr-magpie/bloom'];
97  $site = $this->getMockBuilder(Site::class)->setConstructorArgs([
98  'lotus-flower', 13, [
99  'base' => '/lotus-flower/',
100  'languages' => [
101  0 => [
102  'languageId' => 0,
103  'locale' => 'en_US.UTF-8',
104  'base' => '/en/'
105  ],
106  ]
107  ]
108  ])->setMethods(['getRouter'])->getMock();
109  $language = $site->getDefaultLanguage();
110 
111  $request = new ‪ServerRequest($incomingUrl, 'GET');
112  $request = $request->withAttribute('site', $site);
113  $request = $request->withAttribute('language', $language);
114  $request = $request->withAttribute('routing', new ‪SiteRouteResult($request->getUri(), $site, $language, 'mr-magpie/bloom'));
115  $expectedRouteResult = new ‪PageArguments(13, '0', []);
116 
117  $pageRouterMock = $this->getMockBuilder(PageRouter::class)->disableOriginalConstructor()->setMethods(['matchRequest'])->getMock();
118  $pageRouterMock->expects($this->once())->method('matchRequest')->willReturn($expectedRouteResult);
119  $site->expects($this->any())->method('getRouter')->willReturn($pageRouterMock);
120 
121  ‪$subject = new ‪PageResolver($this->controller);
122  $response = ‪$subject->‪process($request, $this->responseOutputHandler);
123  $result = $response->getBody()->getContents();
124  $result = json_decode($result, true);
125  $this->assertEquals('lotus-flower', $result['site']);
126  $this->assertEquals(13, $result['pageId']);
127  }
128 
135  {
136  $incomingUrl = 'https://king.com/lotus-flower/en/mr-magpie/bloom/';
137  $pageRecord = ['uid' => 13, 'l10n_parent' => 0, 'slug' => '/mr-magpie/bloom'];
139  $site = $this->getMockBuilder(Site::class)->setConstructorArgs([
140  'lotus-flower', 13, [
141  'base' => '/lotus-flower/',
142  'languages' => [
143  0 => [
144  'languageId' => 0,
145  'locale' => 'en_US.UTF-8',
146  'base' => '/en/'
147  ],
148  ]
149  ]
150  ])->setMethods(['getRouter'])->getMock();
151  $language = $site->getDefaultLanguage();
152 
153  $request = new ‪ServerRequest($incomingUrl, 'GET');
154  $request = $request->withAttribute('site', $site);
155  $request = $request->withAttribute('language', $language);
156  $request = $request->withAttribute('routing', new ‪SiteRouteResult($request->getUri(), $site, $language, 'mr-magpie/bloom/'));
157 
158  $expectedRouteResult = new ‪PageArguments(13, '0', []);
159  $pageRouterMock = $this->getMockBuilder(PageRouter::class)->disableOriginalConstructor()->setMethods(['matchRequest'])->getMock();
160  $pageRouterMock->expects($this->once())->method('matchRequest')->willReturn($expectedRouteResult);
161  $site->expects($this->any())->method('getRouter')->willReturn($pageRouterMock);
162 
163  ‪$subject = new ‪PageResolver($this->controller);
164  $response = ‪$subject->‪process($request, $this->responseOutputHandler);
165  $this->assertEquals(307, $response->getStatusCode());
166  $this->assertEquals('https://king.com/lotus-flower/en/mr-magpie/bloom', $response->getHeader('Location')[0]);
167  }
168 
175  {
176  $incomingUrl = 'https://king.com/lotus-flower/en/mr-magpie/bloom';
177  $pageRecord = ['uid' => 13, 'l10n_parent' => 0, 'slug' => '/mr-magpie/bloom/'];
179  $site = $this->getMockBuilder(Site::class)->setConstructorArgs([
180  'lotus-flower', 13, [
181  'base' => '/lotus-flower/',
182  'languages' => [
183  0 => [
184  'languageId' => 0,
185  'locale' => 'en_US.UTF-8',
186  'base' => '/en/'
187  ],
188  ]
189  ]
190  ])->setMethods(['getRouter'])->getMock();
191  $language = $site->getDefaultLanguage();
192 
193  $request = new ServerRequest($incomingUrl, 'GET');
194  $request = $request->withAttribute('site', $site);
195  $request = $request->withAttribute('language', $language);
196  $request = $request->withAttribute('routing', new SiteRouteResult($request->getUri(), $site, $language, 'mr-magpie/bloom/'));
197 
198  $expectedRouteResult = new PageArguments(13, '0', []);
199  $pageRouterMock = $this->getMockBuilder(PageRouter::class)->disableOriginalConstructor()->setMethods(['matchRequest'])->getMock();
200  $pageRouterMock->expects($this->once())->method('matchRequest')->willReturn($expectedRouteResult);
201  $site->expects($this->any())->method('getRouter')->willReturn($pageRouterMock);
202 
203  ‪$subject = new PageResolver($this->controller);
204  $response = ‪$subject->‪process($request, $this->responseOutputHandler);
205  $this->assertEquals(307, $response->getStatusCode());
206  $this->assertEquals('https://king.com/lotus-flower/en/mr-magpie/bloom/', $response->getHeader('Location')[0]);
207  }
208 }
‪TYPO3\CMS\Core\Site\Entity\SiteInterface
Definition: SiteInterface.php:25
‪TYPO3\CMS\Core\Routing\PageArguments
Definition: PageArguments.php:25
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageResolverTest\properSiteConfigurationLoadsPageRouterWithRedirect
‪properSiteConfigurationLoadsPageRouterWithRedirect()
Definition: PageResolverTest.php:130
‪TYPO3\CMS\Core\Http\NullResponse
Definition: NullResponse.php:24
‪TYPO3\CMS\Core\Routing\PageRouter
Definition: PageRouter.php:75
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageResolverTest\setUp
‪setUp()
Definition: PageResolverTest.php:55
‪TYPO3\CMS\Frontend\Middleware\PageResolver
Definition: PageResolver.php:50
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:39
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage
Definition: SiteLanguage.php:25
‪TYPO3\CMS\Core\Routing\SiteRouteResult
Definition: SiteRouteResult.php:29
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware
Definition: PageArgumentValidatorTest.php:4
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageResolverTest\properSiteConfigurationLoadsPageRouter
‪properSiteConfigurationLoadsPageRouter()
Definition: PageResolverTest.php:88
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageResolverTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: PageResolverTest.php:41
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:35
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageResolverTest\$responseOutputHandler
‪RequestHandlerInterface $responseOutputHandler
Definition: PageResolverTest.php:49
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:97
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:25
‪TYPO3\CMS\Frontend\Middleware\PageResolver\process
‪ResponseInterface process(ServerRequestInterface $request, RequestHandlerInterface $handler)
Definition: PageResolver.php:67
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageResolverTest\$subject
‪PageResolver AccessibleObjectInterface $subject
Definition: PageResolverTest.php:53
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageResolverTest\properSiteConfigurationLoadsPageRouterWithRedirectWithoutTrailingSlash
‪properSiteConfigurationLoadsPageRouterWithRedirectWithoutTrailingSlash()
Definition: PageResolverTest.php:170
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageResolverTest\$controller
‪TypoScriptFrontendController AccessibleObjectInterface $controller
Definition: PageResolverTest.php:45
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageResolverTest
Definition: PageResolverTest.php:38