ErrorControllerTest.php 9.85 KB
Newer Older
1
<?php
2

3
declare(strict_types=1);
4
5
6
7
8
9
10
11
12
13
14
15
16
17

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

18
19
namespace TYPO3\CMS\Frontend\Tests\Unit\Controller;

20
use TYPO3\CMS\Core\Http\ServerRequest;
21
22
use TYPO3\CMS\Core\Information\Typo3Information;
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
use TYPO3\CMS\Frontend\Controller\ErrorController;
24
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25
26

/**
27
 * Test case
28
 */
29
class ErrorControllerTest extends UnitTestCase
30
{
31
32
    protected $resetSingletonInstances = true;

33
34
35
    /**
     * @test
     */
36
    public function pageNotFoundHandlingReturns404ResponseIfNotConfigured()
37
    {
38
39
40
        $typo3InformationProphecy = $this->prophesize(Typo3Information::class);
        $typo3InformationProphecy->getCopyrightYear()->willReturn('1999-20XX');
        GeneralUtility::addInstance(Typo3Information::class, $typo3InformationProphecy->reveal());
41
        $GLOBALS['TYPO3_REQUEST'] = [];
42
        $subject = new ErrorController();
43
        $response = $subject->pageNotFoundAction(new ServerRequest(), 'This test page was not found!');
44
45
        self::assertSame(404, $response->getStatusCode());
        self::assertStringContainsString('This test page was not found!', $response->getBody()->getContents());
46
47
48
49
50
    }

    /**
     * @test
     */
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    public function unavailableHandlingReturns503ResponseIfNotConfigured()
    {
        $typo3InformationProphecy = $this->prophesize(Typo3Information::class);
        $typo3InformationProphecy->getCopyrightYear()->willReturn('1999-20XX');
        GeneralUtility::addInstance(Typo3Information::class, $typo3InformationProphecy->reveal());
        $GLOBALS['TYPO3_REQUEST'] = [];
        $subject = new ErrorController();
        $response = $subject->unavailableAction(new ServerRequest(), 'This page is temporarily unavailable.');
        self::assertSame(503, $response->getStatusCode());
        self::assertStringContainsString('This page is temporarily unavailable.', $response->getBody()->getContents());
    }

    /**
     * @test
     */
    public function unavailableHandlingDoesNotTriggerDueToDevIpMask()
67
68
69
    {
        $GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask'] = '*';
        $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
70

71
72
73
        $this->expectExceptionMessage('All your system are belong to us!');
        $this->expectExceptionCode(1518472181);
        $subject = new ErrorController();
74
        $subject->unavailableAction(new ServerRequest(), 'All your system are belong to us!');
75
76
77
78
79
    }

    /**
     * @test
     */
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
    public function internalErrorHandlingReturns500ResponseIfNotConfigured()
    {
        $typo3InformationProphecy = $this->prophesize(Typo3Information::class);
        $typo3InformationProphecy->getCopyrightYear()->willReturn('1999-20XX');
        GeneralUtility::addInstance(Typo3Information::class, $typo3InformationProphecy->reveal());
        $subject = new ErrorController();
        $response = $subject->internalErrorAction(new ServerRequest(), 'All your system are belong to us!');
        self::assertSame(500, $response->getStatusCode());
        self::assertStringContainsString('All your system are belong to us!', $response->getBody()->getContents());
    }

    /**
     * @test
     */
    public function internalErrorHandlingDoesNotTriggerDueToDevIpMask()
95
96
97
98
99
    {
        $GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask'] = '*';
        $_SERVER['REMOTE_ADDR'] = '127.0.0.1';

        $this->expectExceptionMessage('All your system are belong to us!');
100
        $this->expectExceptionCode(1607585445);
101
        $subject = new ErrorController();
102
        $subject->internalErrorAction(new ServerRequest(), 'All your system are belong to us!');
103
    }
104

105
    /**
106
     * @test
107
     */
108
    public function defaultErrorHandlerWithHtmlResponseIsChosenWhenNoSiteConfiguredForPageNotFoundAction()
109
    {
110
111
112
        $typo3InformationProphecy = $this->prophesize(Typo3Information::class);
        $typo3InformationProphecy->getCopyrightYear()->willReturn('1999-20XX');
        GeneralUtility::addInstance(Typo3Information::class, $typo3InformationProphecy->reveal());
113
114
115
116
117
        $subject = new ErrorController();
        $response = $subject->pageNotFoundAction(new ServerRequest(), 'Error handler is not configured.');
        self::assertSame(404, $response->getStatusCode());
        self::assertSame('text/html; charset=utf-8', $response->getHeaderLine('Content-Type'));
        self::assertStringContainsString('Error handler is not configured.', $response->getBody()->getContents());
118
119
120
121
122
    }

    /**
     * @test
     */
123
124
    public function defaultErrorHandlerWithJsonResponseIsChosenWhenNoSiteConfiguredForPageNotFoundAction()
    {
125
        $subject = new ErrorController();
126
127
128
129
130
        $response = $subject->pageNotFoundAction((new ServerRequest())->withAddedHeader('Accept', 'application/json'), 'Error handler is not configured.');
        $responseContent = \json_decode($response->getBody()->getContents(), true);
        self::assertSame(404, $response->getStatusCode());
        self::assertSame('application/json; charset=utf-8', $response->getHeaderLine('Content-Type'));
        self::assertEquals(['reason' => 'Error handler is not configured.'], $responseContent);
131
132
    }

133
134
135
    /**
     * @test
     */
136
    public function defaultErrorHandlerWithHtmlResponseIsChosenWhenNoSiteConfiguredForUnavailableAction()
137
    {
138
139
140
        $typo3InformationProphecy = $this->prophesize(Typo3Information::class);
        $typo3InformationProphecy->getCopyrightYear()->willReturn('1999-20XX');
        GeneralUtility::addInstance(Typo3Information::class, $typo3InformationProphecy->reveal());
141
        $subject = new ErrorController();
142
        $response = $subject->unavailableAction(new ServerRequest(), 'Error handler is not configured.');
143
        self::assertSame(503, $response->getStatusCode());
144
145
        self::assertSame('text/html; charset=utf-8', $response->getHeaderLine('Content-Type'));
        self::assertStringContainsString('Error handler is not configured.', $response->getBody()->getContents());
146
147
    }

148
    /**
149
     * @test
150
     */
151
    public function defaultErrorHandlerWithJsonResponseIsChosenWhenNoSiteConfiguredForUnavailableAction()
152
    {
153
154
155
        $subject = new ErrorController();
        $response = $subject->unavailableAction((new ServerRequest())->withAddedHeader('Accept', 'application/json'), 'Error handler is not configured.');
        $responseContent = \json_decode($response->getBody()->getContents(), true);
156
        self::assertSame(503, $response->getStatusCode());
157
158
        self::assertSame('application/json; charset=utf-8', $response->getHeaderLine('Content-Type'));
        self::assertEquals(['reason' => 'Error handler is not configured.'], $responseContent);
159
    }
160

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    /**
     * @test
     */
    public function defaultErrorHandlerWithHtmlResponseIsChosenWhenNoSiteConfiguredForInternalErrorAction()
    {
        $typo3InformationProphecy = $this->prophesize(Typo3Information::class);
        $typo3InformationProphecy->getCopyrightYear()->willReturn('1999-20XX');
        GeneralUtility::addInstance(Typo3Information::class, $typo3InformationProphecy->reveal());
        $subject = new ErrorController();
        $response = $subject->internalErrorAction(new ServerRequest(), 'Error handler is not configured.');
        self::assertSame(500, $response->getStatusCode());
        self::assertSame('text/html; charset=utf-8', $response->getHeaderLine('Content-Type'));
        self::assertStringContainsString('Error handler is not configured.', $response->getBody()->getContents());
    }

    /**
     * @test
     */
    public function defaultErrorHandlerWithJsonResponseIsChosenWhenNoSiteConfiguredForInternalErrorAction()
    {
        $subject = new ErrorController();
        $response = $subject->internalErrorAction((new ServerRequest())->withAddedHeader('Accept', 'application/json'), 'Error handler is not configured.');
        $responseContent = \json_decode($response->getBody()->getContents(), true);
        self::assertSame(500, $response->getStatusCode());
        self::assertSame('application/json; charset=utf-8', $response->getHeaderLine('Content-Type'));
        self::assertEquals(['reason' => 'Error handler is not configured.'], $responseContent);
    }

189
190
191
192
    /**
     * @test
     */
    public function defaultErrorHandlerWithHtmlResponseIsChosenWhenNoSiteConfiguredForAccessDeniedAction()
193
    {
194
195
196
        $typo3InformationProphecy = $this->prophesize(Typo3Information::class);
        $typo3InformationProphecy->getCopyrightYear()->willReturn('1999-20XX');
        GeneralUtility::addInstance(Typo3Information::class, $typo3InformationProphecy->reveal());
197
198
199
200
201
        $subject = new ErrorController();
        $response = $subject->accessDeniedAction(new ServerRequest(), 'Error handler is not configured.');
        self::assertSame(403, $response->getStatusCode());
        self::assertSame('text/html; charset=utf-8', $response->getHeaderLine('Content-Type'));
        self::assertStringContainsString('Error handler is not configured.', $response->getBody()->getContents());
202
203
    }

204
205
206
207
    /**
     * @test
     */
    public function defaultErrorHandlerWithJsonResponseIsChosenWhenNoSiteConfiguredForAccessDeniedAction()
208
    {
209
210
211
212
213
214
        $subject = new ErrorController();
        $response = $subject->accessDeniedAction((new ServerRequest())->withAddedHeader('Accept', 'application/json'), 'Error handler is not configured.');
        $responseContent = \json_decode($response->getBody()->getContents(), true);
        self::assertSame(403, $response->getStatusCode());
        self::assertSame('application/json; charset=utf-8', $response->getHeaderLine('Content-Type'));
        self::assertEquals(['reason' => 'Error handler is not configured.'], $responseContent);
215
    }
216
}