‪TYPO3CMS  11.5
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 Prophecy\PhpUnit\ProphecyTrait;
21 use Psr\Http\Message\ResponseInterface;
22 use Psr\Http\Message\ServerRequestInterface;
23 use Psr\Http\Server\RequestHandlerInterface;
24 use Psr\Log\NullLogger;
34 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
35 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
36 
37 class ‪PageArgumentValidatorTest extends UnitTestCase
38 {
39  use ProphecyTrait;
40 
41  protected ‪$resetSingletonInstances = true;
42 
45  protected RequestHandlerInterface ‪$responseOutputHandler;
46 
50  protected AccessibleObjectInterface ‪$subject;
51 
52  protected function ‪setUp(): void
53  {
54  parent::setUp();
55  $this->timeTrackerStub = new ‪TimeTracker(false);
56  $this->cacheHashCalculator = new ‪CacheHashCalculator();
57 
58  // A request handler which only runs through
59  $this->responseOutputHandler = new class () implements RequestHandlerInterface {
60  public function handle(ServerRequestInterface $request): ResponseInterface
61  {
62  return new ‪Response();
63  }
64  };
65  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '';
66  }
67 
72  {
73  $incomingUrl = 'https://example.com/lotus-flower/en/mr-magpie/bloom/?cHash=XYZ';
74  $expectedResult = 'https://example.com/lotus-flower/en/mr-magpie/bloom/';
75 
76  $pageArguments = new ‪PageArguments(13, '1', ['cHash' => 'XYZ'], ['parameter-from' => 'path']);
77 
78  $request = new ‪ServerRequest($incomingUrl, 'GET');
79  $request = $request->withAttribute('routing', $pageArguments);
80 
81  ‪$subject = new ‪PageArgumentValidator($this->cacheHashCalculator, $this->timeTrackerStub);
82  ‪$subject->setLogger(new NullLogger());
83 
84  $response = ‪$subject->process($request, $this->responseOutputHandler);
85  self::assertEquals(308, $response->getStatusCode());
86  self::assertEquals($expectedResult, $response->getHeader('Location')[0]);
87  }
88 
93  {
94  $incomingUrl = 'https://example.com/lotus-flower/en/mr-magpie/bloom/?cHash=YAZ';
95 
96  $pageArguments = new PageArguments(13, '1', ['cHash' => 'XYZ', 'dynamic' => 'argument'], ['parameter-from' => 'path']);
97 
98  $request = new ServerRequest($incomingUrl, 'GET');
99  $request = $request->withAttribute('routing', $pageArguments);
100 
101  ‪$subject = new PageArgumentValidator($this->cacheHashCalculator, $this->timeTrackerStub);
102  $typo3InformationProphecy = $this->prophesize(Typo3Information::class);
103  $typo3InformationProphecy->getCopyrightYear()->willReturn('1999-20XX');
104  GeneralUtility::addInstance(Typo3Information::class, $typo3InformationProphecy->reveal());
105 
106  $response = ‪$subject->process($request, $this->responseOutputHandler);
107  self::assertEquals(404, $response->getStatusCode());
108  }
109 
113  public function ‪noPageArgumentsReturnsErrorResponse(): void
114  {
115  $incomingUrl = 'https://king.com/lotus-flower/en/mr-magpie/bloom/';
116  $request = new ServerRequest($incomingUrl, 'GET');
117 
118  ‪$subject = new PageArgumentValidator($this->cacheHashCalculator, $this->timeTrackerStub);
119  $typo3InformationProphecy = $this->prophesize(Typo3Information::class);
120  $typo3InformationProphecy->getCopyrightYear()->willReturn('1999-20XX');
121  GeneralUtility::addInstance(Typo3Information::class, $typo3InformationProphecy->reveal());
122  $response = ‪$subject->process($request, $this->responseOutputHandler);
123  self::assertEquals(404, $response->getStatusCode());
124  }
125 
130  {
131  $incomingUrl = 'https://example.com/lotus-flower/en/mr-magpie/bloom/';
132 
133  $pageArguments = new PageArguments(13, '1', [], ['parameter-from' => 'path']);
134 
135  $request = new ServerRequest($incomingUrl, 'GET');
136  $request = $request->withAttribute('routing', $pageArguments);
137 
138  ‪$subject = new PageArgumentValidator($this->cacheHashCalculator, $this->timeTrackerStub);
139  $response = ‪$subject->process($request, $this->responseOutputHandler);
140  self::assertEquals(200, $response->getStatusCode());
141  }
142 
147  {
148  $incomingUrl = 'https://example.com/lotus-flower/en/mr-magpie/bloom/';
149 
150  $pageArguments = new PageArguments(13, '1', ['cHash' => 'coolio', 'download' => true], ['parameter-from' => 'path']);
151 
152  $request = new ServerRequest($incomingUrl, 'GET');
153  $request = $request->withAttribute('routing', $pageArguments);
154 
155  ‪$subject = new PageArgumentValidator($this->cacheHashCalculator, $this->timeTrackerStub);
156  $typo3InformationProphecy = $this->prophesize(Typo3Information::class);
157  $typo3InformationProphecy->getCopyrightYear()->willReturn('1999-20XX');
158  GeneralUtility::addInstance(Typo3Information::class, $typo3InformationProphecy->reveal());
159  $response = ‪$subject->process($request, $this->responseOutputHandler);
160  self::assertEquals(404, $response->getStatusCode());
161  }
162 }
‪TYPO3\CMS\Core\Routing\PageArguments
Definition: PageArguments.php:26
‪TYPO3\CMS\Core\Information\Typo3Information
Definition: Typo3Information.php:28
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\invalidCacheHashWithDynamicArgumentsTriggers404
‪invalidCacheHashWithDynamicArgumentsTriggers404()
Definition: PageArgumentValidatorTest.php:145
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\$subject
‪AccessibleObjectInterface $subject
Definition: PageArgumentValidatorTest.php:49
‪TYPO3\CMS\Frontend\Middleware\PageResolver
Definition: PageResolver.php:42
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\$responseOutputHandler
‪RequestHandlerInterface $responseOutputHandler
Definition: PageArgumentValidatorTest.php:44
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\staticPageArgumentsSkipProcessingAndReturnsSuccess
‪staticPageArgumentsSkipProcessingAndReturnsSuccess()
Definition: PageArgumentValidatorTest.php:128
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\$resetSingletonInstances
‪$resetSingletonInstances
Definition: PageArgumentValidatorTest.php:40
‪TYPO3\CMS\Core\Http\Response
Definition: Response.php:30
‪TYPO3\CMS\Frontend\Middleware\PageArgumentValidator
Definition: PageArgumentValidator.php:40
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware
Definition: PageArgumentValidatorTest.php:18
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest
Definition: PageArgumentValidatorTest.php:38
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:37
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\givenCacheHashNotMatchingCalculatedCacheHashTriggers404
‪givenCacheHashNotMatchingCalculatedCacheHashTriggers404()
Definition: PageArgumentValidatorTest.php:91
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\noPageArgumentsReturnsErrorResponse
‪noPageArgumentsReturnsErrorResponse()
Definition: PageArgumentValidatorTest.php:112
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\givenCacheHashWithoutRequiredParametersTriggersRedirect
‪givenCacheHashWithoutRequiredParametersTriggersRedirect()
Definition: PageArgumentValidatorTest.php:70
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator
Definition: CacheHashCalculator.php:25
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\setUp
‪setUp()
Definition: PageArgumentValidatorTest.php:51
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\TimeTracker\TimeTracker
Definition: TimeTracker.php:31
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\$timeTrackerStub
‪TimeTracker $timeTrackerStub
Definition: PageArgumentValidatorTest.php:43
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\$cacheHashCalculator
‪CacheHashCalculator $cacheHashCalculator
Definition: PageArgumentValidatorTest.php:42