‪TYPO3CMS  9.5
PageArgumentValidatorTest.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 Psr\Http\Message\ResponseInterface;
20 use Psr\Http\Message\ServerRequestInterface;
21 use Psr\Http\Server\RequestHandlerInterface;
22 use Psr\Log\NullLogger;
32 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
33 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
34 
35 class ‪PageArgumentValidatorTest extends UnitTestCase
36 {
40  protected ‪$resetSingletonInstances = true;
41 
45  protected ‪$controller;
46 
50  protected ‪$responseOutputHandler;
51 
55  protected ‪$subject;
56 
57  protected function ‪setUp(): void
58  {
59  parent::setUp();
60  $timeTrackerStub = new ‪TimeTracker(false);
61  GeneralUtility::setSingletonInstance(TimeTracker::class, $timeTrackerStub);
62  $this->controller = $this->getAccessibleMock(TypoScriptFrontendController::class, ['dummy'], [], '', false);
63 
64  // A request handler which only runs through
65  $this->responseOutputHandler = new class implements RequestHandlerInterface {
66  public function handle(ServerRequestInterface $request): ResponseInterface
67  {
68  return new ‪Response();
69  }
70  };
71  }
72 
77  {
78  $incomingUrl = 'https://example.com/lotus-flower/en/mr-magpie/bloom/?cHash=XYZ';
79  $expectedResult = 'https://example.com/lotus-flower/en/mr-magpie/bloom/';
80  $this->controller->id = 13;
81  $this->controller->cHash = 'XYZ';
82 
83  $pageArguments = new ‪PageArguments(13, '1', ['cHash' => 'XYZ'], ['parameter-from' => 'path']);
84 
85  $request = new ‪ServerRequest($incomingUrl, 'GET');
86  $request = $request->withAttribute('routing', $pageArguments);
87 
88  ‪$subject = new ‪PageArgumentValidator($this->controller);
89  ‪$subject->setLogger(new NullLogger());
90 
91  $response = ‪$subject->‪process($request, $this->responseOutputHandler);
92  static::assertEquals(308, $response->getStatusCode());
93  static::assertEquals($expectedResult, $response->getHeader('Location')[0]);
94  }
95 
100  {
101  $incomingUrl = 'https://example.com/lotus-flower/en/mr-magpie/bloom/?cHash=YAZ';
102  $this->controller->id = 13;
103  $this->controller->cHash = 'XYZ';
104 
105  $pageArguments = new ‪PageArguments(13, '1', ['cHash' => 'XYZ', 'dynamic' => 'argument'], ['parameter-from' => 'path']);
106 
107  $request = new ‪ServerRequest($incomingUrl, 'GET');
108  $request = $request->withAttribute('routing', $pageArguments);
109 
110  static::expectException(PageNotFoundException::class);
111  static::expectExceptionMessage('Request parameters could not be validated (&cHash comparison failed)');
112  ‪$subject = new ‪PageArgumentValidator($this->controller);
113  ‪$subject->‪process($request, $this->responseOutputHandler);
114  }
115 }
‪TYPO3\CMS\Core\Routing\PageArguments
Definition: PageArguments.php:25
‪TYPO3\CMS\Frontend\Middleware\PageResolver
Definition: PageResolver.php:50
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\$responseOutputHandler
‪RequestHandlerInterface $responseOutputHandler
Definition: PageArgumentValidatorTest.php:47
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\$subject
‪PageResolver AccessibleObjectInterface $subject
Definition: PageArgumentValidatorTest.php:51
‪TYPO3\CMS\Core\Http\Response
Definition: Response.php:28
‪TYPO3\CMS\Core\Error\Http\PageNotFoundException
Definition: PageNotFoundException.php:21
‪TYPO3\CMS\Frontend\Middleware\PageArgumentValidator
Definition: PageArgumentValidator.php:39
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\$controller
‪TypoScriptFrontendController AccessibleObjectInterface $controller
Definition: PageArgumentValidatorTest.php:43
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: PageArgumentValidatorTest.php:39
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware
Definition: PageArgumentValidatorTest.php:4
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest
Definition: PageArgumentValidatorTest.php:36
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:35
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\givenCacheHashNotMatchingCalculatedCacheHashTriggers404
‪givenCacheHashNotMatchingCalculatedCacheHashTriggers404()
Definition: PageArgumentValidatorTest.php:95
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\givenCacheHashWithoutRequiredParametersTriggersRedirect
‪givenCacheHashWithoutRequiredParametersTriggersRedirect()
Definition: PageArgumentValidatorTest.php:72
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:97
‪TYPO3\CMS\Frontend\Middleware\PageResolver\process
‪ResponseInterface process(ServerRequestInterface $request, RequestHandlerInterface $handler)
Definition: PageResolver.php:67
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\setUp
‪setUp()
Definition: PageArgumentValidatorTest.php:53
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\TimeTracker\TimeTracker
Definition: TimeTracker.php:27