2011-03-18 04:33:47 +01:00
|
|
|
<?php
|
2016-06-16 06:57:19 +02:00
|
|
|
|
2017-03-21 05:26:46 +01:00
|
|
|
use SilverStripe\Versioned\Versioned;
|
2016-07-22 01:32:32 +02:00
|
|
|
use SilverStripe\CMS\Model\ErrorPage;
|
2016-12-28 18:10:45 +01:00
|
|
|
use SilverStripe\CMS\Controllers\ContentController;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
2016-11-15 06:06:31 +01:00
|
|
|
use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore;
|
2016-12-28 18:24:54 +01:00
|
|
|
use SilverStripe\Control\HTTPResponse_Exception;
|
2016-11-15 06:06:31 +01:00
|
|
|
|
2011-03-18 04:33:47 +01:00
|
|
|
/**
|
|
|
|
* @package cms
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2017-01-25 21:59:25 +01:00
|
|
|
class ErrorPageTest extends FunctionalTest
|
|
|
|
{
|
|
|
|
protected static $fixture_file = 'ErrorPageTest.yml';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Location of temporary cached files
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $tmpAssetsPath = '';
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
// Set temporary asset backend store
|
|
|
|
TestAssetStore::activate('ErrorPageTest');
|
2017-05-11 07:37:54 +02:00
|
|
|
Config::modify()->set(ErrorPage::class, 'enable_static_file', true);
|
2017-01-25 21:59:25 +01:00
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
TestAssetStore::reset();
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test404ErrorPage()
|
|
|
|
{
|
2017-05-11 07:37:54 +02:00
|
|
|
/** @var ErrorPage $page */
|
|
|
|
$page = $this->objFromFixture(ErrorPage::class, '404');
|
2017-01-25 21:59:25 +01:00
|
|
|
// ensure that the errorpage exists as a physical file
|
|
|
|
$page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
|
|
|
|
|
|
|
$response = $this->get('nonexistent-page');
|
|
|
|
|
|
|
|
/* We have body text from the error page */
|
|
|
|
$this->assertNotNull($response->getBody(), 'We have body text from the error page');
|
|
|
|
|
|
|
|
/* Status code of the HTTPResponse for error page is "404" */
|
|
|
|
$this->assertEquals($response->getStatusCode(), '404', 'Status code of the HTTPResponse for error page is "404"');
|
|
|
|
|
|
|
|
/* Status message of the HTTPResponse for error page is "Not Found" */
|
|
|
|
$this->assertEquals($response->getStatusDescription(), 'Not Found', 'Status message of the HTTResponse for error page is "Not found"');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBehaviourOfShowInMenuAndShowInSearchFlags()
|
|
|
|
{
|
2017-05-11 07:37:54 +02:00
|
|
|
$page = $this->objFromFixture(ErrorPage::class, '404');
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
/* Don't show the error page in the menus */
|
|
|
|
$this->assertEquals($page->ShowInMenus, 0, 'Don\'t show the error page in the menus');
|
|
|
|
|
|
|
|
/* Don't show the error page in the search */
|
|
|
|
$this->assertEquals($page->ShowInSearch, 0, 'Don\'t show the error page in search');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBehaviourOf403()
|
|
|
|
{
|
2017-05-11 07:37:54 +02:00
|
|
|
/** @var ErrorPage $page */
|
|
|
|
$page = $this->objFromFixture(ErrorPage::class, '403');
|
2017-01-25 21:59:25 +01:00
|
|
|
$page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$controller = singleton(ContentController::class);
|
|
|
|
$controller->httpError(403);
|
|
|
|
$this->fail('Expected exception to be thrown');
|
|
|
|
} catch (HTTPResponse_Exception $e) {
|
|
|
|
$response = $e->getResponse();
|
|
|
|
$this->assertEquals($response->getStatusCode(), '403');
|
|
|
|
$this->assertNotNull($response->getBody(), 'We have body text from the error page');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSecurityError()
|
|
|
|
{
|
|
|
|
// Generate 404 page
|
2017-05-11 07:37:54 +02:00
|
|
|
/** @var ErrorPage $page */
|
|
|
|
$page = $this->objFromFixture(ErrorPage::class, '404');
|
2017-01-25 21:59:25 +01:00
|
|
|
$page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
|
|
|
|
|
|
|
// Test invalid action
|
|
|
|
$response = $this->get('Security/nosuchaction');
|
|
|
|
$this->assertEquals($response->getStatusCode(), '404');
|
|
|
|
$this->assertNotNull($response->getBody());
|
|
|
|
$this->assertContains('text/html', $response->getHeader('Content-Type'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testStaticCaching()
|
|
|
|
{
|
|
|
|
// Test new error code does not have static content
|
2017-05-11 07:37:54 +02:00
|
|
|
$error = ErrorPage::get_content_for_errorcode('401');
|
|
|
|
$this->assertEmpty($error);
|
2017-01-25 21:59:25 +01:00
|
|
|
$expectedErrorPagePath = TestAssetStore::base_path() . '/error-401.html';
|
|
|
|
$this->assertFileNotExists($expectedErrorPagePath, 'Error page is not automatically cached');
|
|
|
|
|
|
|
|
// Write new 401 page
|
|
|
|
$page = new ErrorPage();
|
|
|
|
$page->Title = '401 Error';
|
|
|
|
$page->ErrorCode = 401;
|
|
|
|
$page->Title = 'Unauthorised';
|
|
|
|
$page->write();
|
|
|
|
$page->publishRecursive();
|
|
|
|
|
|
|
|
// Static cache should now exist
|
|
|
|
$this->assertNotEmpty(ErrorPage::get_content_for_errorcode('401'));
|
|
|
|
$expectedErrorPagePath = TestAssetStore::base_path() . '/error-401.html';
|
|
|
|
$this->assertFileExists($expectedErrorPagePath, 'Error page is cached');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test fallback to file generation API with enable_static_file disabled
|
|
|
|
*/
|
|
|
|
public function testGeneratedFile()
|
|
|
|
{
|
2017-05-11 07:37:54 +02:00
|
|
|
Config::modify()->set(ErrorPage::class, 'enable_static_file', false);
|
2017-01-25 21:59:25 +01:00
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
|
|
|
|
$page = new ErrorPage();
|
|
|
|
$page->ErrorCode = 405;
|
|
|
|
$page->Title = 'Method Not Allowed';
|
|
|
|
$page->write();
|
|
|
|
$page->publishRecursive();
|
|
|
|
|
|
|
|
// Dynamic content is available
|
|
|
|
$response = ErrorPage::response_for('405');
|
|
|
|
$this->assertNotEmpty($response);
|
|
|
|
$this->assertNotEmpty($response->getBody());
|
|
|
|
$this->assertEquals(405, (int)$response->getStatusCode());
|
|
|
|
|
|
|
|
// Static content is not available
|
|
|
|
$this->assertEmpty(ErrorPage::get_content_for_errorcode('405'));
|
|
|
|
$expectedErrorPagePath = TestAssetStore::base_path() . '/error-405.html';
|
|
|
|
$this->assertFileNotExists($expectedErrorPagePath, 'Error page is not cached in static location');
|
|
|
|
}
|
2014-05-02 02:52:18 +02:00
|
|
|
}
|