‪TYPO3CMS  ‪main
ErrorControllerTest.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;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27 
28 final class ‪ErrorControllerTest extends UnitTestCase
29 {
30  protected bool ‪$resetSingletonInstances = true;
31 
32  #[Test]
34  {
35  $typo3InformationMock = $this->getMockBuilder(Typo3Information::class)->disableOriginalConstructor()->getMock();
36  $typo3InformationMock->method('getCopyrightYear')->willReturn('1999-20XX');
37  GeneralUtility::addInstance(Typo3Information::class, $typo3InformationMock);
38  $request = new ‪ServerRequest();
39  $request = (new ‪ServerRequest())->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
40  $subject = new ‪ErrorController();
41  $response = $subject->pageNotFoundAction($request, 'This test page was not found!');
42  self::assertSame(404, $response->getStatusCode());
43  self::assertStringContainsString('This test page was not found!', $response->getBody()->getContents());
44  }
45 
46  #[Test]
48  {
49  $typo3InformationMock = $this->getMockBuilder(Typo3Information::class)->disableOriginalConstructor()->getMock();
50  $typo3InformationMock->method('getCopyrightYear')->willReturn('1999-20XX');
51  GeneralUtility::addInstance(Typo3Information::class, $typo3InformationMock);
52  $request = new ‪ServerRequest();
53  $request = (new ‪ServerRequest())->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
54  $subject = new ‪ErrorController();
55  $response = $subject->unavailableAction($request, 'This page is temporarily unavailable.');
56  self::assertSame(503, $response->getStatusCode());
57  self::assertStringContainsString('This page is temporarily unavailable.', $response->getBody()->getContents());
58  }
59 
60  #[Test]
62  {
63  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask'] = '*';
64  ‪$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
65 
66  $this->expectExceptionMessage('All your system are belong to us!');
67  $this->expectExceptionCode(1518472181);
68  $request = new ‪ServerRequest();
69  $request = (new ‪ServerRequest())->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
70  $subject = new ‪ErrorController();
71  $subject->unavailableAction($request, 'All your system are belong to us!');
72  }
73 
74  #[Test]
76  {
77  $typo3InformationMock = $this->getMockBuilder(Typo3Information::class)->disableOriginalConstructor()->getMock();
78  $typo3InformationMock->method('getCopyrightYear')->willReturn('1999-20XX');
79  GeneralUtility::addInstance(Typo3Information::class, $typo3InformationMock);
80  $request = new ‪ServerRequest();
81  $request = (new ‪ServerRequest())->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
82  $subject = new ‪ErrorController();
83  $response = $subject->internalErrorAction($request, 'All your system are belong to us!');
84  self::assertSame(500, $response->getStatusCode());
85  self::assertStringContainsString('All your system are belong to us!', $response->getBody()->getContents());
86  }
87 
88  #[Test]
90  {
91  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask'] = '*';
92  ‪$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
93 
94  $this->expectExceptionMessage('All your system are belong to us!');
95  $this->expectExceptionCode(1607585445);
96  $request = new ‪ServerRequest();
97  $request = (new ‪ServerRequest())->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
98  $subject = new ‪ErrorController();
99  $subject->internalErrorAction($request, 'All your system are belong to us!');
100  }
101 
102  #[Test]
104  {
105  $typo3InformationMock = $this->getMockBuilder(Typo3Information::class)->disableOriginalConstructor()->getMock();
106  $typo3InformationMock->method('getCopyrightYear')->willReturn('1999-20XX');
107  GeneralUtility::addInstance(Typo3Information::class, $typo3InformationMock);
108  $request = new ‪ServerRequest();
109  $request = (new ‪ServerRequest())->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
110  $subject = new ‪ErrorController();
111  $response = $subject->pageNotFoundAction($request, 'Error handler is not configured.');
112  self::assertSame(404, $response->getStatusCode());
113  self::assertSame('text/html; charset=utf-8', $response->getHeaderLine('Content-Type'));
114  self::assertStringContainsString('Error handler is not configured.', $response->getBody()->getContents());
115  }
116 
117  #[Test]
119  {
120  $request = new ‪ServerRequest();
121  $request = (new ‪ServerRequest())->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
122  $subject = new ‪ErrorController();
123  $response = $subject->pageNotFoundAction($request->withAddedHeader('Accept', 'application/json'), 'Error handler is not configured.');
124  $responseContent = \json_decode($response->getBody()->getContents(), true);
125  self::assertSame(404, $response->getStatusCode());
126  self::assertSame('application/json; charset=utf-8', $response->getHeaderLine('Content-Type'));
127  self::assertEquals(['reason' => 'Error handler is not configured.'], $responseContent);
128  }
129 
130  #[Test]
132  {
133  $typo3InformationMock = $this->getMockBuilder(Typo3Information::class)->disableOriginalConstructor()->getMock();
134  $typo3InformationMock->method('getCopyrightYear')->willReturn('1999-20XX');
135  GeneralUtility::addInstance(Typo3Information::class, $typo3InformationMock);
136  $request = new ‪ServerRequest();
137  $request = (new ‪ServerRequest())->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
138  $subject = new ‪ErrorController();
139  $response = $subject->unavailableAction($request, 'Error handler is not configured.');
140  self::assertSame(503, $response->getStatusCode());
141  self::assertSame('text/html; charset=utf-8', $response->getHeaderLine('Content-Type'));
142  self::assertStringContainsString('Error handler is not configured.', $response->getBody()->getContents());
143  }
144 
145  #[Test]
147  {
148  $request = new ‪ServerRequest();
149  $request = (new ‪ServerRequest())->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
150  $subject = new ‪ErrorController();
151  $response = $subject->unavailableAction($request->withAddedHeader('Accept', 'application/json'), 'Error handler is not configured.');
152  $responseContent = \json_decode($response->getBody()->getContents(), true);
153  self::assertSame(503, $response->getStatusCode());
154  self::assertSame('application/json; charset=utf-8', $response->getHeaderLine('Content-Type'));
155  self::assertEquals(['reason' => 'Error handler is not configured.'], $responseContent);
156  }
157 
158  #[Test]
160  {
161  $typo3InformationMock = $this->getMockBuilder(Typo3Information::class)->disableOriginalConstructor()->getMock();
162  $typo3InformationMock->method('getCopyrightYear')->willReturn('1999-20XX');
163  GeneralUtility::addInstance(Typo3Information::class, $typo3InformationMock);
164  $request = new ‪ServerRequest();
165  $request = (new ‪ServerRequest())->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
166  $subject = new ‪ErrorController();
167  $response = $subject->internalErrorAction($request, 'Error handler is not configured.');
168  self::assertSame(500, $response->getStatusCode());
169  self::assertSame('text/html; charset=utf-8', $response->getHeaderLine('Content-Type'));
170  self::assertStringContainsString('Error handler is not configured.', $response->getBody()->getContents());
171  }
172 
173  #[Test]
175  {
176  $request = new ‪ServerRequest();
177  $request = (new ‪ServerRequest())->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
178  $subject = new ‪ErrorController();
179  $response = $subject->internalErrorAction($request->withAddedHeader('Accept', 'application/json'), 'Error handler is not configured.');
180  $responseContent = \json_decode($response->getBody()->getContents(), true);
181  self::assertSame(500, $response->getStatusCode());
182  self::assertSame('application/json; charset=utf-8', $response->getHeaderLine('Content-Type'));
183  self::assertEquals(['reason' => 'Error handler is not configured.'], $responseContent);
184  }
185 
186  #[Test]
188  {
189  $typo3InformationMock = $this->getMockBuilder(Typo3Information::class)->disableOriginalConstructor()->getMock();
190  $typo3InformationMock->method('getCopyrightYear')->willReturn('1999-20XX');
191  GeneralUtility::addInstance(Typo3Information::class, $typo3InformationMock);
192  $request = new ‪ServerRequest();
193  $request = (new ‪ServerRequest())->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
194  $subject = new ‪ErrorController();
195  $response = $subject->accessDeniedAction($request, 'Error handler is not configured.');
196  self::assertSame(403, $response->getStatusCode());
197  self::assertSame('text/html; charset=utf-8', $response->getHeaderLine('Content-Type'));
198  self::assertStringContainsString('Error handler is not configured.', $response->getBody()->getContents());
199  }
200 
201  #[Test]
203  {
204  $request = new ‪ServerRequest();
205  $request = (new ‪ServerRequest())->withAttribute('normalizedParams', ‪NormalizedParams::createFromRequest($request));
206  $subject = new ‪ErrorController();
207  $response = $subject->accessDeniedAction($request->withAddedHeader('Accept', 'application/json'), 'Error handler is not configured.');
208  $responseContent = \json_decode($response->getBody()->getContents(), true);
209  self::assertSame(403, $response->getStatusCode());
210  self::assertSame('application/json; charset=utf-8', $response->getHeaderLine('Content-Type'));
211  self::assertEquals(['reason' => 'Error handler is not configured.'], $responseContent);
212  }
213 }
‪TYPO3\CMS\Core\Information\Typo3Information
Definition: Typo3Information.php:28
‪TYPO3\CMS\Frontend\Tests\Unit\Controller\ErrorControllerTest\defaultErrorHandlerWithJsonResponseIsChosenWhenNoSiteConfiguredForInternalErrorAction
‪defaultErrorHandlerWithJsonResponseIsChosenWhenNoSiteConfiguredForInternalErrorAction()
Definition: ErrorControllerTest.php:174
‪TYPO3\CMS\Frontend\Tests\Unit\Controller\ErrorControllerTest\defaultErrorHandlerWithHtmlResponseIsChosenWhenNoSiteConfiguredForInternalErrorAction
‪defaultErrorHandlerWithHtmlResponseIsChosenWhenNoSiteConfiguredForInternalErrorAction()
Definition: ErrorControllerTest.php:159
‪TYPO3\CMS\Frontend\Tests\Unit\Controller\ErrorControllerTest\defaultErrorHandlerWithJsonResponseIsChosenWhenNoSiteConfiguredForAccessDeniedAction
‪defaultErrorHandlerWithJsonResponseIsChosenWhenNoSiteConfiguredForAccessDeniedAction()
Definition: ErrorControllerTest.php:202
‪TYPO3\CMS\Frontend\Tests\Unit\Controller\ErrorControllerTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: ErrorControllerTest.php:30
‪TYPO3\CMS\Frontend\Controller\ErrorController
Definition: ErrorController.php:38
‪TYPO3\CMS\Frontend\Tests\Unit\Controller\ErrorControllerTest\defaultErrorHandlerWithHtmlResponseIsChosenWhenNoSiteConfiguredForUnavailableAction
‪defaultErrorHandlerWithHtmlResponseIsChosenWhenNoSiteConfiguredForUnavailableAction()
Definition: ErrorControllerTest.php:131
‪TYPO3\CMS\Frontend\Tests\Unit\Controller\ErrorControllerTest\internalErrorHandlingDoesNotTriggerDueToDevIpMask
‪internalErrorHandlingDoesNotTriggerDueToDevIpMask()
Definition: ErrorControllerTest.php:89
‪TYPO3\CMS\Frontend\Tests\Unit\Controller\ErrorControllerTest\defaultErrorHandlerWithHtmlResponseIsChosenWhenNoSiteConfiguredForPageNotFoundAction
‪defaultErrorHandlerWithHtmlResponseIsChosenWhenNoSiteConfiguredForPageNotFoundAction()
Definition: ErrorControllerTest.php:103
‪TYPO3\CMS\Frontend\Tests\Unit\Controller\ErrorControllerTest\internalErrorHandlingReturns500ResponseIfNotConfigured
‪internalErrorHandlingReturns500ResponseIfNotConfigured()
Definition: ErrorControllerTest.php:75
‪TYPO3\CMS\Frontend\Tests\Unit\Controller
Definition: ErrorControllerTest.php:18
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪$_SERVER
‪$_SERVER['TYPO3_DEPRECATED_ENTRYPOINT']
Definition: legacy-backend.php:20
‪TYPO3\CMS\Frontend\Tests\Unit\Controller\ErrorControllerTest\defaultErrorHandlerWithJsonResponseIsChosenWhenNoSiteConfiguredForUnavailableAction
‪defaultErrorHandlerWithJsonResponseIsChosenWhenNoSiteConfiguredForUnavailableAction()
Definition: ErrorControllerTest.php:146
‪TYPO3\CMS\Frontend\Tests\Unit\Controller\ErrorControllerTest\pageNotFoundHandlingReturns404ResponseIfNotConfigured
‪pageNotFoundHandlingReturns404ResponseIfNotConfigured()
Definition: ErrorControllerTest.php:33
‪TYPO3\CMS\Frontend\Tests\Unit\Controller\ErrorControllerTest
Definition: ErrorControllerTest.php:29
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Frontend\Tests\Unit\Controller\ErrorControllerTest\unavailableHandlingReturns503ResponseIfNotConfigured
‪unavailableHandlingReturns503ResponseIfNotConfigured()
Definition: ErrorControllerTest.php:47
‪TYPO3\CMS\Frontend\Tests\Unit\Controller\ErrorControllerTest\defaultErrorHandlerWithHtmlResponseIsChosenWhenNoSiteConfiguredForAccessDeniedAction
‪defaultErrorHandlerWithHtmlResponseIsChosenWhenNoSiteConfiguredForAccessDeniedAction()
Definition: ErrorControllerTest.php:187
‪TYPO3\CMS\Frontend\Tests\Unit\Controller\ErrorControllerTest\unavailableHandlingDoesNotTriggerDueToDevIpMask
‪unavailableHandlingDoesNotTriggerDueToDevIpMask()
Definition: ErrorControllerTest.php:61
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Http\NormalizedParams\createFromRequest
‪static static createFromRequest(ServerRequestInterface $request, array $systemConfiguration=null)
Definition: NormalizedParams.php:840
‪TYPO3\CMS\Frontend\Tests\Unit\Controller\ErrorControllerTest\defaultErrorHandlerWithJsonResponseIsChosenWhenNoSiteConfiguredForPageNotFoundAction
‪defaultErrorHandlerWithJsonResponseIsChosenWhenNoSiteConfiguredForPageNotFoundAction()
Definition: ErrorControllerTest.php:118
‪TYPO3\CMS\Core\Http\NormalizedParams
Definition: NormalizedParams.php:38