‪TYPO3CMS  ‪main
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 PHPUnit\Framework\Attributes\Test;
21 use Psr\Http\Message\ResponseInterface;
22 use Psr\Http\Message\ServerRequestInterface;
23 use Psr\Http\Server\RequestHandlerInterface;
24 use Psr\Log\NullLogger;
32 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
33 
34 final class ‪PageArgumentValidatorTest extends UnitTestCase
35 {
36  protected bool ‪$resetSingletonInstances = true;
37 
38  private RequestHandlerInterface ‪$responseOutputHandler;
39 
40  protected function ‪setUp(): void
41  {
42  parent::setUp();
43  // A request handler which only runs through
44  $this->responseOutputHandler = new class () implements RequestHandlerInterface {
45  public function handle(ServerRequestInterface $request): ResponseInterface
46  {
47  return new ‪Response();
48  }
49  };
50  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '';
51  }
52 
53  #[Test]
55  {
56  $incomingUrl = 'https://example.com/lotus-flower/en/mr-magpie/bloom/?cHash=XYZ';
57  $expectedResult = 'https://example.com/lotus-flower/en/mr-magpie/bloom/';
58 
59  $pageArguments = new ‪PageArguments(13, '1', ['cHash' => 'XYZ'], ['parameter-from' => 'path']);
60 
61  $request = new ‪ServerRequest($incomingUrl, 'GET');
62  $request = $request->withAttribute('routing', $pageArguments);
63 
65  $subject->setLogger(new NullLogger());
66 
67  $response = $subject->process($request, $this->responseOutputHandler);
68  self::assertEquals(308, $response->getStatusCode());
69  self::assertEquals($expectedResult, $response->getHeader('Location')[0]);
70  }
71 
72  #[Test]
74  {
75  $incomingUrl = 'https://example.com/lotus-flower/en/mr-magpie/bloom/?cHash=YAZ';
76 
77  $pageArguments = new ‪PageArguments(13, '1', ['cHash' => 'XYZ', 'dynamic' => 'argument'], ['parameter-from' => 'path']);
78 
79  $request = new ‪ServerRequest($incomingUrl, 'GET');
80  $request = $request->withAttribute('routing', $pageArguments);
81 
83  $typo3InformationMock = $this->getMockBuilder(Typo3Information::class)->disableOriginalConstructor()->getMock();
84  $typo3InformationMock->expects(self::once())->method('getCopyrightYear')->willReturn('1999-20XX');
85  GeneralUtility::addInstance(Typo3Information::class, $typo3InformationMock);
86 
87  $response = $subject->process($request, $this->responseOutputHandler);
88  self::assertEquals(404, $response->getStatusCode());
89  }
90 
91  #[Test]
93  {
94  $incomingUrl = 'https://king.com/lotus-flower/en/mr-magpie/bloom/';
95  $request = new ‪ServerRequest($incomingUrl, 'GET');
96 
98  $typo3InformationMock = $this->getMockBuilder(Typo3Information::class)->disableOriginalConstructor()->getMock();
99  $typo3InformationMock->expects(self::once())->method('getCopyrightYear')->willReturn('1999-20XX');
100  GeneralUtility::addInstance(Typo3Information::class, $typo3InformationMock);
101  $response = $subject->process($request, $this->responseOutputHandler);
102  self::assertEquals(404, $response->getStatusCode());
103  }
104 
105  #[Test]
107  {
108  $incomingUrl = 'https://example.com/lotus-flower/en/mr-magpie/bloom/';
109 
110  $pageArguments = new ‪PageArguments(13, '1', [], ['parameter-from' => 'path']);
111 
112  $request = new ‪ServerRequest($incomingUrl, 'GET');
113  $request = $request->withAttribute('routing', $pageArguments);
114 
115  $subject = new ‪PageArgumentValidator(new ‪CacheHashCalculator());
116  $response = $subject->process($request, $this->responseOutputHandler);
117  self::assertEquals(200, $response->getStatusCode());
118  }
119 
120  #[Test]
122  {
123  $incomingUrl = 'https://example.com/lotus-flower/en/mr-magpie/bloom/';
124 
125  $pageArguments = new ‪PageArguments(13, '1', ['cHash' => 'coolio', 'download' => true], ['parameter-from' => 'path']);
126 
127  $request = new ‪ServerRequest($incomingUrl, 'GET');
128  $request = $request->withAttribute('routing', $pageArguments);
129 
130  $subject = new ‪PageArgumentValidator(new ‪CacheHashCalculator());
131  $typo3InformationMock = $this->getMockBuilder(Typo3Information::class)->disableOriginalConstructor()->getMock();
132  $typo3InformationMock->expects(self::once())->method('getCopyrightYear')->willReturn('1999-20XX');
133  GeneralUtility::addInstance(Typo3Information::class, $typo3InformationMock);
134  $response = $subject->process($request, $this->responseOutputHandler);
135  self::assertEquals(404, $response->getStatusCode());
136  }
137 }
‪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:121
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\$responseOutputHandler
‪RequestHandlerInterface $responseOutputHandler
Definition: PageArgumentValidatorTest.php:38
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\staticPageArgumentsSkipProcessingAndReturnsSuccess
‪staticPageArgumentsSkipProcessingAndReturnsSuccess()
Definition: PageArgumentValidatorTest.php:106
‪TYPO3\CMS\Core\Http\Response
Definition: Response.php:32
‪TYPO3\CMS\Frontend\Middleware\PageArgumentValidator
Definition: PageArgumentValidator.php:39
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: PageArgumentValidatorTest.php:36
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware
Definition: PageArgumentValidatorTest.php:18
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest
Definition: PageArgumentValidatorTest.php:35
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\givenCacheHashNotMatchingCalculatedCacheHashTriggers404
‪givenCacheHashNotMatchingCalculatedCacheHashTriggers404()
Definition: PageArgumentValidatorTest.php:73
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\noPageArgumentsReturnsErrorResponse
‪noPageArgumentsReturnsErrorResponse()
Definition: PageArgumentValidatorTest.php:92
‪TYPO3\CMS\Frontend\Tests\Unit\Middleware\PageArgumentValidatorTest\givenCacheHashWithoutRequiredParametersTriggersRedirect
‪givenCacheHashWithoutRequiredParametersTriggersRedirect()
Definition: PageArgumentValidatorTest.php:54
‪$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:40
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52