‪TYPO3CMS  10.4
PageArgumentValidatorTest.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 Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
22 use Psr\Http\Server\RequestHandlerInterface;
23 use Psr\Log\NullLogger;
33 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
34 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
35 
36 class ‪PageArgumentValidatorTest extends UnitTestCase
37 {
38  protected ‪$resetSingletonInstances = true;
39 
43  protected ‪$cacheHashCalculator;
44 
48  protected ‪$timeTrackerStub;
49 
53  protected ‪$responseOutputHandler;
54 
58  protected ‪$subject;
59 
60  protected function ‪setUp(): void
61  {
62  parent::setUp();
63  $this->timeTrackerStub = new ‪TimeTracker(false);
64  $this->cacheHashCalculator = new ‪CacheHashCalculator();
65 
66  // A request handler which only runs through
67  $this->responseOutputHandler = new class() implements RequestHandlerInterface {
68  public function handle(ServerRequestInterface $request): ResponseInterface
69  {
70  return new ‪Response();
71  }
72  };
73  }
74 
79  {
80  $incomingUrl = 'https://example.com/lotus-flower/en/mr-magpie/bloom/?cHash=XYZ';
81  $expectedResult = 'https://example.com/lotus-flower/en/mr-magpie/bloom/';
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->cacheHashCalculator, $this->timeTrackerStub);
89  ‪$subject->setLogger(new NullLogger());
90 
91  $response = ‪$subject->‪process($request, $this->responseOutputHandler);
92  self::assertEquals(308, $response->getStatusCode());
93  self::assertEquals($expectedResult, $response->getHeader('Location')[0]);
94  }
95 
100  {
101  $incomingUrl = 'https://example.com/lotus-flower/en/mr-magpie/bloom/?cHash=YAZ';
102 
103  $pageArguments = new ‪PageArguments(13, '1', ['cHash' => 'XYZ', 'dynamic' => 'argument'], ['parameter-from' => 'path']);
104 
105  $request = new ‪ServerRequest($incomingUrl, 'GET');
106  $request = $request->withAttribute('routing', $pageArguments);
107 
108  ‪$subject = new ‪PageArgumentValidator($this->cacheHashCalculator, $this->timeTrackerStub);
109  $typo3InformationProphecy = $this->prophesize(Typo3Information::class);
110  $typo3InformationProphecy->getCopyrightYear()->willReturn('1999-20XX');
111  GeneralUtility::addInstance(Typo3Information::class, $typo3InformationProphecy->reveal());
112 
113  $response = ‪$subject->‪process($request, $this->responseOutputHandler);
114  self::assertEquals(404, $response->getStatusCode());
115  }
116 
121  {
122  $incomingUrl = 'https://king.com/lotus-flower/en/mr-magpie/bloom/';
123  $request = new ‪ServerRequest($incomingUrl, 'GET');
124 
125  ‪$subject = new ‪PageArgumentValidator($this->cacheHashCalculator, $this->timeTrackerStub);
126  $typo3InformationProphecy = $this->prophesize(Typo3Information::class);
127  $typo3InformationProphecy->getCopyrightYear()->willReturn('1999-20XX');
128  GeneralUtility::addInstance(Typo3Information::class, $typo3InformationProphecy->reveal());
129  $response = ‪$subject->‪process($request, $this->responseOutputHandler);
130  self::assertEquals(404, $response->getStatusCode());
131  }
132 
137  {
138  $incomingUrl = 'https://example.com/lotus-flower/en/mr-magpie/bloom/';
139 
140  $pageArguments = new ‪PageArguments(13, '1', [], ['parameter-from' => 'path']);
141 
142  $request = new ‪ServerRequest($incomingUrl, 'GET');
143  $request = $request->withAttribute('routing', $pageArguments);
144 
145  ‪$subject = new ‪PageArgumentValidator($this->cacheHashCalculator, $this->timeTrackerStub);
146  $response = ‪$subject->‪process($request, $this->responseOutputHandler);
147  self::assertEquals(200, $response->getStatusCode());
148  }
149 
154  {
155  $incomingUrl = 'https://example.com/lotus-flower/en/mr-magpie/bloom/';
156 
157  $pageArguments = new ‪PageArguments(13, '1', ['cHash' => 'coolio', 'download' => true], ['parameter-from' => 'path']);
158 
159  $request = new ‪ServerRequest($incomingUrl, 'GET');
160  $request = $request->withAttribute('routing', $pageArguments);
161 
162  ‪$subject = new ‪PageArgumentValidator($this->cacheHashCalculator, $this->timeTrackerStub);
163  $typo3InformationProphecy = $this->prophesize(Typo3Information::class);
164  $typo3InformationProphecy->getCopyrightYear()->willReturn('1999-20XX');
165  GeneralUtility::addInstance(Typo3Information::class, $typo3InformationProphecy->reveal());
166  $response = ‪$subject->‪process($request, $this->responseOutputHandler);
167  self::assertEquals(404, $response->getStatusCode());
168  }
169 }
‪TYPO3\CMS\Core\Routing\PageArguments
Definition: PageArguments.php:26
‪TYPO3\CMS\Core\Information\Typo3Information
Definition: Typo3Information.php:26
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\invalidCacheHashWithDynamicArgumentsTriggers404
‪invalidCacheHashWithDynamicArgumentsTriggers404()
Definition: PageArgumentValidatorTest.php:149
‪TYPO3\CMS\Frontend\Middleware\PageResolver
Definition: PageResolver.php:42
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\$responseOutputHandler
‪RequestHandlerInterface $responseOutputHandler
Definition: PageArgumentValidatorTest.php:50
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\$subject
‪PageResolver AccessibleObjectInterface $subject
Definition: PageArgumentValidatorTest.php:54
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\staticPageArgumentsSkipProcessingAndReturnsSuccess
‪staticPageArgumentsSkipProcessingAndReturnsSuccess()
Definition: PageArgumentValidatorTest.php:132
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\$resetSingletonInstances
‪$resetSingletonInstances
Definition: PageArgumentValidatorTest.php:38
‪TYPO3\CMS\Core\Http\Response
Definition: Response.php:30
‪TYPO3\CMS\Frontend\Middleware\PageArgumentValidator
Definition: PageArgumentValidator.php:39
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware
Definition: PageArgumentValidatorTest.php:18
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest
Definition: PageArgumentValidatorTest.php:37
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:37
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\givenCacheHashNotMatchingCalculatedCacheHashTriggers404
‪givenCacheHashNotMatchingCalculatedCacheHashTriggers404()
Definition: PageArgumentValidatorTest.php:95
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\noPageArgumentsReturnsErrorResponse
‪noPageArgumentsReturnsErrorResponse()
Definition: PageArgumentValidatorTest.php:116
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\givenCacheHashWithoutRequiredParametersTriggersRedirect
‪givenCacheHashWithoutRequiredParametersTriggersRedirect()
Definition: PageArgumentValidatorTest.php:74
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator
Definition: CacheHashCalculator.php:25
‪TYPO3\CMS\Frontend\Middleware\PageResolver\process
‪ResponseInterface process(ServerRequestInterface $request, RequestHandlerInterface $handler)
Definition: PageResolver.php:50
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\setUp
‪setUp()
Definition: PageArgumentValidatorTest.php:56
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\TimeTracker\TimeTracker
Definition: TimeTracker.php:30
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\$timeTrackerStub
‪TimeTracker $timeTrackerStub
Definition: PageArgumentValidatorTest.php:46
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\$cacheHashCalculator
‪CacheHashCalculator $cacheHashCalculator
Definition: PageArgumentValidatorTest.php:42