‪TYPO3CMS  10.4
PageResolverTest.php
Go to the documentation of this file.
1 <?php
2 
3 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 
19 
20 use PHPUnit\Framework\MockObject\MockObject;
21 use Psr\Http\Message\ResponseInterface;
22 use Psr\Http\Message\ServerRequestInterface;
23 use Psr\Http\Server\RequestHandlerInterface;
35 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
36 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
37 
38 class ‪PageResolverTest extends UnitTestCase
39 {
43  protected ‪$resetSingletonInstances = true;
44 
48  protected ‪$controller;
49 
53  protected ‪$responseOutputHandler;
54 
58  protected ‪$subject;
59 
60  protected function ‪setUp(): void
61  {
62  self::markTestSkipped('Has to be adjusted');
63 
64  parent::setUp();
65  $this->controller = $this->getAccessibleMock(TypoScriptFrontendController::class, ['getSiteScript', 'determineId', 'isBackendUserLoggedIn'], [], '', false);
66 
67  // A request handler which expects a site with some more details are found.
68  $this->responseOutputHandler = new class() implements RequestHandlerInterface {
69  public function handle(ServerRequestInterface $request): ResponseInterface
70  {
72  $site = $request->getAttribute('site');
74  $language = $request->getAttribute('language');
76  $routeResult = $request->getAttribute('routing', false);
77  if ($routeResult) {
78  return new ‪JsonResponse(
79  [
80  'site' => $site->getIdentifier(),
81  'language-id' => $language->getLanguageId(),
82  'pageId' => $routeResult->getPageId(),
83  ]
84  );
85  }
86  return new ‪NullResponse();
87  }
88  };
89  }
90 
95  {
96  $incomingUrl = 'https://king.com/lotus-flower/en/mr-magpie/bloom';
97  $pageRecord = ['uid' => 13, 'l10n_parent' => 0, 'slug' => '/mr-magpie/bloom'];
99  $site = $this->getMockBuilder(Site::class)->setConstructorArgs([
100  'lotus-flower', 13, [
101  'base' => '/lotus-flower/',
102  'languages' => [
103  0 => [
104  'languageId' => 0,
105  'locale' => 'en_US.UTF-8',
106  'base' => '/en/'
107  ],
108  ]
109  ]
110  ])->setMethods(['getRouter'])->getMock();
111  $language = $site->getDefaultLanguage();
112 
113  $request = new ‪ServerRequest($incomingUrl, 'GET');
114  $request = $request->withAttribute('site', $site);
115  $request = $request->withAttribute('language', $language);
116  $request = $request->withAttribute('routing', new ‪SiteRouteResult($request->getUri(), $site, $language, 'mr-magpie/bloom'));
117  $expectedRouteResult = new ‪PageArguments(13, '0', []);
118 
119  $pageRouterMock = $this->getMockBuilder(PageRouter::class)->disableOriginalConstructor()->setMethods(['matchRequest'])->getMock();
120  $pageRouterMock->expects(self::once())->method('matchRequest')->willReturn($expectedRouteResult);
121  $site->expects(self::any())->method('getRouter')->willReturn($pageRouterMock);
122 
123  ‪$subject = new ‪PageResolver($this->controller);
124  $response = ‪$subject->‪process($request, $this->responseOutputHandler);
125  $result = $response->getBody()->getContents();
126  $result = json_decode($result, true);
127  self::assertEquals('lotus-flower', $result['site']);
128  self::assertEquals(13, $result['pageId']);
129  }
130 
137  {
138  $incomingUrl = 'https://king.com/lotus-flower/en/mr-magpie/bloom/';
139  $pageRecord = ['uid' => 13, 'l10n_parent' => 0, 'slug' => '/mr-magpie/bloom'];
141  $site = $this->getMockBuilder(Site::class)->setConstructorArgs([
142  'lotus-flower', 13, [
143  'base' => '/lotus-flower/',
144  'languages' => [
145  0 => [
146  'languageId' => 0,
147  'locale' => 'en_US.UTF-8',
148  'base' => '/en/'
149  ],
150  ]
151  ]
152  ])->setMethods(['getRouter'])->getMock();
153  $language = $site->getDefaultLanguage();
154 
155  $request = new ‪ServerRequest($incomingUrl, 'GET');
156  $request = $request->withAttribute('site', $site);
157  $request = $request->withAttribute('language', $language);
158  $request = $request->withAttribute('routing', new ‪SiteRouteResult($request->getUri(), $site, $language, 'mr-magpie/bloom/'));
159 
160  $expectedRouteResult = new ‪PageArguments(13, '0', []);
161  $pageRouterMock = $this->getMockBuilder(PageRouter::class)->disableOriginalConstructor()->setMethods(['matchRequest'])->getMock();
162  $pageRouterMock->expects(self::once())->method('matchRequest')->willReturn($expectedRouteResult);
163  $site->expects(self::any())->method('getRouter')->willReturn($pageRouterMock);
164 
165  ‪$subject = new ‪PageResolver($this->controller);
166  $response = ‪$subject->‪process($request, $this->responseOutputHandler);
167  self::assertEquals(307, $response->getStatusCode());
168  self::assertEquals('https://king.com/lotus-flower/en/mr-magpie/bloom', $response->getHeader('Location')[0]);
169  }
170 
177  {
178  $incomingUrl = 'https://king.com/lotus-flower/en/mr-magpie/bloom';
179  $pageRecord = ['uid' => 13, 'l10n_parent' => 0, 'slug' => '/mr-magpie/bloom/'];
181  $site = $this->getMockBuilder(Site::class)->setConstructorArgs([
182  'lotus-flower', 13, [
183  'base' => '/lotus-flower/',
184  'languages' => [
185  0 => [
186  'languageId' => 0,
187  'locale' => 'en_US.UTF-8',
188  'base' => '/en/'
189  ],
190  ]
191  ]
192  ])->setMethods(['getRouter'])->getMock();
193  $language = $site->getDefaultLanguage();
194 
195  $request = new ServerRequest($incomingUrl, 'GET');
196  $request = $request->withAttribute('site', $site);
197  $request = $request->withAttribute('language', $language);
198  $request = $request->withAttribute('routing', new SiteRouteResult($request->getUri(), $site, $language, 'mr-magpie/bloom/'));
199 
200  $expectedRouteResult = new PageArguments(13, '0', []);
201  $pageRouterMock = $this->getMockBuilder(PageRouter::class)->disableOriginalConstructor()->setMethods(['matchRequest'])->getMock();
202  $pageRouterMock->expects(self::once())->method('matchRequest')->willReturn($expectedRouteResult);
203  $site->expects(self::any())->method('getRouter')->willReturn($pageRouterMock);
204 
205  ‪$subject = new PageResolver($this->controller);
206  $response = ‪$subject->‪process($request, $this->responseOutputHandler);
207  self::assertEquals(307, $response->getStatusCode());
208  self::assertEquals('https://king.com/lotus-flower/en/mr-magpie/bloom/', $response->getHeader('Location')[0]);
209  }
210 }
‪TYPO3\CMS\Core\Site\Entity\SiteInterface
Definition: SiteInterface.php:26
‪TYPO3\CMS\Core\Routing\PageArguments
Definition: PageArguments.php:26
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageResolverTest\properSiteConfigurationLoadsPageRouterWithRedirect
‪properSiteConfigurationLoadsPageRouterWithRedirect()
Definition: PageResolverTest.php:132
‪TYPO3\CMS\Core\Http\NullResponse
Definition: NullResponse.php:26
‪TYPO3\CMS\Core\Routing\PageRouter
Definition: PageRouter.php:71
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageResolverTest\setUp
‪setUp()
Definition: PageResolverTest.php:56
‪TYPO3\CMS\Frontend\Middleware\PageResolver
Definition: PageResolver.php:42
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:40
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage
Definition: SiteLanguage.php:26
‪TYPO3\CMS\Core\Routing\SiteRouteResult
Definition: SiteRouteResult.php:30
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware
Definition: PageArgumentValidatorTest.php:18
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageResolverTest\properSiteConfigurationLoadsPageRouter
‪properSiteConfigurationLoadsPageRouter()
Definition: PageResolverTest.php:90
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageResolverTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: PageResolverTest.php:42
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:37
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageResolverTest\$responseOutputHandler
‪RequestHandlerInterface $responseOutputHandler
Definition: PageResolverTest.php:50
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:98
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:26
‪TYPO3\CMS\Frontend\Middleware\PageResolver\process
‪ResponseInterface process(ServerRequestInterface $request, RequestHandlerInterface $handler)
Definition: PageResolver.php:50
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageResolverTest\$subject
‪PageResolver AccessibleObjectInterface $subject
Definition: PageResolverTest.php:54
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageResolverTest\properSiteConfigurationLoadsPageRouterWithRedirectWithoutTrailingSlash
‪properSiteConfigurationLoadsPageRouterWithRedirectWithoutTrailingSlash()
Definition: PageResolverTest.php:172
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageResolverTest\$controller
‪TypoScriptFrontendController AccessibleObjectInterface $controller
Definition: PageResolverTest.php:46
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageResolverTest
Definition: PageResolverTest.php:39