2 declare(strict_types = 1);
18 use Prophecy\Argument;
19 use Psr\Http\Message\StreamInterface;
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
40 GeneralUtility::purgeInstances();
49 $this->expectExceptionMessage(
'This test page was not found!');
50 $this->expectExceptionCode(1518472189);
51 $GLOBALS[
'TYPO3_CONF_VARS'][
'FE'][
'pageNotFound_handling'] =
false;
54 $subject->pageNotFoundAction(
new ServerRequest(),
'This test page was not found!');
65 '404 with default errorpage' => [
67 'header' =>
'HTTP/1.0 404 Not Found',
68 'message' =>
'Custom message',
70 'type' => HtmlResponse::class,
72 'reasonPhrase' =>
'Not Found',
73 'content' =>
'Reason: Custom message',
75 'Content-Type' => [
'text/html; charset=utf-8'],
79 '404 with default errorpage setting the handler to legacy value' => [
81 'header' =>
'HTTP/1.0 404 This is a dead end',
82 'message' =>
'Come back tomorrow',
84 'type' => HtmlResponse::class,
86 'reasonPhrase' =>
'This is a dead end',
87 'content' =>
'Reason: Come back tomorrow',
89 'Content-Type' => [
'text/html; charset=utf-8'],
93 '404 with custom userfunction' => [
94 'handler' =>
'USER_FUNCTION:' . ErrorControllerTest::class .
'->mockedUserFunctionCall',
95 'header' =>
'HTTP/1.0 404 Not Found',
96 'message' =>
'Custom message',
98 'type' => HtmlResponse::class,
100 'reasonPhrase' =>
'Not Found',
101 'content' =>
'It\'s magic, Michael: Custom message',
103 'Content-Type' => [
'text/html; charset=utf-8'],
107 '404 with a readfile functionality' => [
108 'handler' =>
'READFILE:typo3/sysext/frontend/Tests/Unit/Controller/Fixtures/error.txt',
109 'header' =>
'HTTP/1.0 404 Not Found',
110 'message' =>
'Custom message',
112 'type' => HtmlResponse::class,
114 'reasonPhrase' =>
'Not Found',
115 'content' =>
'rama-lama-ding-dong',
117 'Content-Type' => [
'text/html; charset=utf-8'],
121 '404 with a readfile functionality with an invalid file' => [
122 'handler' =>
'READFILE:does_not_exist.php6',
123 'header' =>
'HTTP/1.0 404 Not Found',
124 'message' =>
'Custom message',
126 'exceptionCode' => 1518472245,
128 '404 with a redirect - never do that in production - it is bad for SEO. But with custom headers as well...' => [
129 'handler' =>
'REDIRECT:www.typo3.org',
130 'header' =>
'HTTP/1.0 404 Not Found
131 X-TYPO3-Additional-Header: Banana Stand',
132 'message' =>
'Custom message',
134 'type' => RedirectResponse::class,
136 'reasonPhrase' =>
'Not Found',
138 'location' => [
'www.typo3.org'],
139 'X-TYPO3-Additional-Header' => [
'Banana Stand'],
154 $expectedResponseDetails,
155 $expectedExceptionCode =
null
157 if ($expectedExceptionCode !==
null) {
158 $this->expectExceptionCode($expectedExceptionCode);
160 $GLOBALS[
'TYPO3_CONF_VARS'][
'FE'][
'pageNotFound_handling'] = $handler;
161 $GLOBALS[
'TYPO3_CONF_VARS'][
'FE'][
'pageNotFound_handling_statheader'] = $header;
163 $_SERVER[
'REQUEST_URI'] =
'/unit-test/';
164 $_SERVER[
'REMOTE_ADDR'] =
'127.0.0.1';
165 $_SERVER[
'HTTP_HOST'] =
'localhost';
166 $_SERVER[
'SSL_SESSION_ID'] =
true;
172 $response = $subject->pageNotFoundAction(
new ServerRequest(), $message);
173 if (is_array($expectedResponseDetails)) {
174 $this->assertInstanceOf($expectedResponseDetails[
'type'], $response);
175 $this->assertEquals($expectedResponseDetails[
'statusCode'], $response->getStatusCode());
176 $this->assertEquals($expectedResponseDetails[
'reasonPhrase'], $response->getReasonPhrase());
177 if (isset($expectedResponseDetails[
'content'])) {
178 $this->assertContains($expectedResponseDetails[
'content'], $response->getBody()->getContents());
180 $this->assertEquals($expectedResponseDetails[
'headers'], $response->getHeaders());
189 $GLOBALS[
'TYPO3_CONF_VARS'][
'FE'][
'pageNotFound_handling'] =
'/404/';
190 $GLOBALS[
'TYPO3_CONF_VARS'][
'FE'][
'pageNotFound_handling_statheader'] =
'HTTP/1.0 404 Not Found
191 X-TYPO3-Additional-Header: Banana Stand';
193 $_SERVER[
'REQUEST_URI'] =
'/unit-test/';
194 $_SERVER[
'REMOTE_ADDR'] =
'127.0.0.1';
195 $_SERVER[
'HTTP_HOST'] =
'localhost';
196 $_SERVER[
'SSL_SESSION_ID'] =
true;
202 $response = $subject->pageNotFoundAction(
new ServerRequest(),
'Custom message');
204 $expectedResponseDetails = [
205 'type' => HtmlResponse::class,
207 'reasonPhrase' =>
'Not Found',
209 'Content-Type' => [
'text/html; charset=utf-8'],
210 'X-TYPO3-Additional-Header' => [
'Banana Stand'],
213 $this->assertInstanceOf($expectedResponseDetails[
'type'], $response);
214 $this->assertEquals($expectedResponseDetails[
'statusCode'], $response->getStatusCode());
215 $this->assertEquals($expectedResponseDetails[
'reasonPhrase'], $response->getReasonPhrase());
216 if (isset($expectedResponseDetails[
'content'])) {
217 $this->assertContains($expectedResponseDetails[
'content'], $response->getBody()->getContents());
219 $this->assertEquals($expectedResponseDetails[
'headers'], $response->getHeaders());
230 '403 with default errorpage' => [
232 'header' =>
'HTTP/1.0 403 Who are you',
233 'message' =>
'Be nice, do good',
235 'type' => HtmlResponse::class,
237 'reasonPhrase' =>
'Who are you',
238 'content' =>
'Reason: Be nice, do good',
240 'Content-Type' => [
'text/html; charset=utf-8'],
253 $GLOBALS[
'TYPO3_CONF_VARS'][
'FE'][
'pageNotFound_handling'] = $handler;
254 $GLOBALS[
'TYPO3_CONF_VARS'][
'FE'][
'pageNotFound_handling_accessdeniedheader'] = $header;
256 $_SERVER[
'REQUEST_URI'] =
'/unit-test/';
257 $_SERVER[
'REMOTE_ADDR'] =
'127.0.0.1';
258 $_SERVER[
'HTTP_HOST'] =
'localhost';
259 $_SERVER[
'SSL_SESSION_ID'] =
true;
262 $response = $subject->accessDeniedAction(
new ServerRequest(), $message);
263 if (is_array($expectedResponseDetails)) {
264 $this->assertInstanceOf($expectedResponseDetails[
'type'], $response);
265 $this->assertEquals($expectedResponseDetails[
'statusCode'], $response->getStatusCode());
266 $this->assertEquals($expectedResponseDetails[
'reasonPhrase'], $response->getReasonPhrase());
267 if (isset($expectedResponseDetails[
'content'])) {
268 $this->assertContains($expectedResponseDetails[
'content'], $response->getBody()->getContents());
270 $this->assertEquals($expectedResponseDetails[
'headers'], $response->getHeaders());
279 $GLOBALS[
'TYPO3_CONF_VARS'][
'SYS'][
'devIPmask'] =
'*';
280 $GLOBALS[
'TYPO3_CONF_VARS'][
'FE'][
'pageUnavailable_handling'] =
true;
281 $_SERVER[
'REMOTE_ADDR'] =
'127.0.0.1';
282 $this->expectExceptionMessage(
'All your system are belong to us!');
283 $this->expectExceptionCode(1518472181);
284 $GLOBALS[
'TYPO3_CONF_VARS'][
'FE'][
'pageUnavailable_handling'] =
false;
286 $subject->unavailableAction(
new ServerRequest(),
'All your system are belong to us!');
294 $GLOBALS[
'TYPO3_CONF_VARS'][
'SYS'][
'devIPmask'] =
'*';
295 $GLOBALS[
'TYPO3_CONF_VARS'][
'FE'][
'pageUnavailable_handling'] =
true;
296 $_SERVER[
'REMOTE_ADDR'] =
'127.0.0.1';
298 $this->expectExceptionMessage(
'All your system are belong to us!');
299 $this->expectExceptionCode(1518472181);
301 $subject->unavailableAction(
new ServerRequest(),
'All your system are belong to us!');
312 '503 with default errorpage' => [
314 'header' =>
'HTTP/1.0 503 Service Temporarily Unavailable',
315 'message' =>
'Custom message',
317 'type' => HtmlResponse::class,
319 'reasonPhrase' =>
'Service Temporarily Unavailable',
320 'content' =>
'Reason: Custom message',
322 'Content-Type' => [
'text/html; charset=utf-8'],
326 '503 with default errorpage setting the handler to legacy value' => [
328 'header' =>
'HTTP/1.0 503 This is a dead end',
329 'message' =>
'Come back tomorrow',
331 'type' => HtmlResponse::class,
333 'reasonPhrase' =>
'This is a dead end',
334 'content' =>
'Reason: Come back tomorrow',
336 'Content-Type' => [
'text/html; charset=utf-8'],
340 '503 with custom userfunction' => [
341 'handler' =>
'USER_FUNCTION:' . ErrorControllerTest::class .
'->mockedUserFunctionCall',
342 'header' =>
'HTTP/1.0 503 Service Temporarily Unavailable',
343 'message' =>
'Custom message',
345 'type' => HtmlResponse::class,
347 'reasonPhrase' =>
'Service Temporarily Unavailable',
348 'content' =>
'It\'s magic, Michael: Custom message',
350 'Content-Type' => [
'text/html; charset=utf-8'],
354 '503 with a readfile functionality' => [
355 'handler' =>
'READFILE:typo3/sysext/frontend/Tests/Unit/Controller/Fixtures/error.txt',
356 'header' =>
'HTTP/1.0 503 Service Temporarily Unavailable',
357 'message' =>
'Custom message',
359 'type' => HtmlResponse::class,
361 'reasonPhrase' =>
'Service Temporarily Unavailable',
362 'content' =>
'Let it snow',
364 'Content-Type' => [
'text/html; charset=utf-8'],
368 '503 with a readfile functionality with an invalid file' => [
369 'handler' =>
'READFILE:does_not_exist.php6',
370 'header' =>
'HTTP/1.0 503 Service Temporarily Unavailable',
371 'message' =>
'Custom message',
373 'exceptionCode' => 1518472245,
375 '503 with a redirect - never do that in production - it is bad for SEO. But with custom headers as well...' => [
376 'handler' =>
'REDIRECT:www.typo3.org',
377 'header' =>
'HTTP/1.0 503 Service Temporarily Unavailable
378 X-TYPO3-Additional-Header: Banana Stand',
379 'message' =>
'Custom message',
381 'type' => RedirectResponse::class,
383 'reasonPhrase' =>
'Service Temporarily Unavailable',
385 'location' => [
'www.typo3.org'],
386 'X-TYPO3-Additional-Header' => [
'Banana Stand'],
401 $expectedResponseDetails,
402 $expectedExceptionCode =
null
404 if ($expectedExceptionCode !==
null) {
405 $this->expectExceptionCode($expectedExceptionCode);
407 $GLOBALS[
'TYPO3_CONF_VARS'][
'SYS'][
'devIPmask'] =
'-1';
408 $GLOBALS[
'TYPO3_CONF_VARS'][
'FE'][
'pageUnavailable_handling'] = $handler;
409 $GLOBALS[
'TYPO3_CONF_VARS'][
'FE'][
'pageUnavailable_handling_statheader'] = $header;
411 $_SERVER[
'REQUEST_URI'] =
'/unit-test/';
412 $_SERVER[
'REMOTE_ADDR'] =
'127.0.0.1';
413 $_SERVER[
'HTTP_HOST'] =
'localhost';
414 $_SERVER[
'SSL_SESSION_ID'] =
true;
419 $response = $subject->unavailableAction(
new ServerRequest(), $message);
420 if (is_array($expectedResponseDetails)) {
421 $this->assertInstanceOf($expectedResponseDetails[
'type'], $response);
422 $this->assertEquals($expectedResponseDetails[
'statusCode'], $response->getStatusCode());
423 $this->assertEquals($expectedResponseDetails[
'reasonPhrase'], $response->getReasonPhrase());
424 if (isset($expectedResponseDetails[
'content'])) {
425 $this->assertContains($expectedResponseDetails[
'content'], $response->getBody()->getContents());
427 $this->assertEquals($expectedResponseDetails[
'headers'], $response->getHeaders());
436 $GLOBALS[
'TYPO3_CONF_VARS'][
'SYS'][
'devIPmask'] =
'-1';
437 $GLOBALS[
'TYPO3_CONF_VARS'][
'FE'][
'pageUnavailable_handling'] =
'/fail/';
438 $GLOBALS[
'TYPO3_CONF_VARS'][
'FE'][
'pageUnavailable_handling_statheader'] =
'HTTP/1.0 503 Service Temporarily Unavailable
439 X-TYPO3-Additional-Header: Banana Stand';
441 $_SERVER[
'REQUEST_URI'] =
'/unit-test/';
442 $_SERVER[
'REMOTE_ADDR'] =
'127.0.0.1';
443 $_SERVER[
'HTTP_HOST'] =
'localhost';
444 $_SERVER[
'SSL_SESSION_ID'] =
true;
449 $response = $subject->unavailableAction(
new ServerRequest(),
'custom message');
451 $expectedResponseDetails = [
452 'type' => HtmlResponse::class,
454 'reasonPhrase' =>
'Service Temporarily Unavailable',
456 'Content-Type' => [
'text/html; charset=utf-8'],
457 'X-TYPO3-Additional-Header' => [
'Banana Stand'],
460 $this->assertInstanceOf($expectedResponseDetails[
'type'], $response);
461 $this->assertEquals($expectedResponseDetails[
'statusCode'], $response->getStatusCode());
462 $this->assertEquals($expectedResponseDetails[
'reasonPhrase'], $response->getReasonPhrase());
463 if (isset($expectedResponseDetails[
'content'])) {
464 $this->assertContains($expectedResponseDetails[
'content'], $response->getBody()->getContents());
466 $this->assertEquals($expectedResponseDetails[
'headers'], $response->getHeaders());
474 return '<p>It\'s magic, Michael: ' . $params[
'reasonText'] .
'</p>';
479 $errorPageControllerProphecy = $this->prophesize(ErrorPageController::class);
480 $errorPageControllerProphecy->errorAction(Argument::cetera())
483 return 'Reason: ' .
$args[1];
486 GeneralUtility::addInstance(ErrorPageController::class, $errorPageControllerProphecy->reveal());
491 $streamProphecy = $this->prophesize(StreamInterface::class);
492 $prefixPageResponseProphecy = $this->prophesize(Response::class);
493 $prefixPageResponseProphecy->getHeaders()->willReturn([]);
494 $prefixPageResponseProphecy->getBody()->willReturn($streamProphecy);
495 $prefixPageResponseProphecy->getStatusCode()->willReturn(200);
496 $prefixPageResponseProphecy->getHeaderLine(
'Content-Type')->willReturn(
'text/html; charset=utf-8');
497 $requestFactoryProphecy = $this->prophesize(RequestFactory::class);
498 $requestFactoryProphecy->request(Argument::cetera())->willReturn($prefixPageResponseProphecy->reveal());
499 GeneralUtility::addInstance(RequestFactory::class, $requestFactoryProphecy->reveal());