2011-03-18 04:33:47 +01:00
|
|
|
<?php
|
2016-06-16 06:57:19 +02:00
|
|
|
|
|
|
|
use SilverStripe\ORM\Versioning\Versioned;
|
|
|
|
|
2011-03-18 04:33:47 +01:00
|
|
|
/**
|
|
|
|
* @package cms
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class ErrorPageTest extends FunctionalTest {
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2013-03-18 11:47:15 +01:00
|
|
|
protected static $fixture_file = 'ErrorPageTest.yml';
|
2015-10-13 07:01:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Location of temporary cached files
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2011-03-18 04:33:47 +01:00
|
|
|
protected $tmpAssetsPath = '';
|
2015-10-13 07:01:52 +02:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function setUp() {
|
2011-03-18 04:33:47 +01:00
|
|
|
parent::setUp();
|
2015-10-13 07:01:52 +02:00
|
|
|
// Set temporary asset backend store
|
|
|
|
AssetStoreTest_SpyStore::activate('ErrorPageTest');
|
|
|
|
Config::inst()->update('ErrorPage', 'enable_static_file', true);
|
2013-03-18 11:47:15 +01:00
|
|
|
Config::inst()->update('Director', 'environment_type', 'live');
|
2015-11-03 02:23:04 +01:00
|
|
|
$this->logInWithPermission('ADMIN');
|
2011-03-18 04:33:47 +01:00
|
|
|
}
|
2015-10-13 07:01:52 +02:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function tearDown() {
|
2015-10-13 07:01:52 +02:00
|
|
|
AssetStoreTest_SpyStore::reset();
|
2011-03-18 04:33:47 +01:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function test404ErrorPage() {
|
2011-03-18 04:33:47 +01:00
|
|
|
$page = $this->objFromFixture('ErrorPage', '404');
|
|
|
|
// ensure that the errorpage exists as a physical file
|
2016-04-01 05:17:37 +02:00
|
|
|
$page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:33:47 +01:00
|
|
|
$response = $this->get('nonexistent-page');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:33:47 +01:00
|
|
|
/* We have body text from the error page */
|
|
|
|
$this->assertNotNull($response->getBody(), 'We have body text from the error page');
|
|
|
|
|
|
|
|
/* Status code of the SS_HTTPResponse for error page is "404" */
|
|
|
|
$this->assertEquals($response->getStatusCode(), '404', 'Status code of the SS_HTTPResponse for error page is "404"');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:33:47 +01:00
|
|
|
/* Status message of the SS_HTTPResponse for error page is "Not Found" */
|
|
|
|
$this->assertEquals($response->getStatusDescription(), 'Not Found', 'Status message of the HTTResponse for error page is "Not found"');
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function testBehaviourOfShowInMenuAndShowInSearchFlags() {
|
2011-03-18 04:33:47 +01:00
|
|
|
$page = $this->objFromFixture('ErrorPage', '404');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:33:47 +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');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:33:47 +01:00
|
|
|
/* Don't show the error page in the search */
|
|
|
|
$this->assertEquals($page->ShowInSearch, 0, 'Don\'t show the error page in search');
|
|
|
|
}
|
2013-11-18 04:32:15 +01:00
|
|
|
|
|
|
|
public function testBehaviourOf403() {
|
|
|
|
$page = $this->objFromFixture('ErrorPage', '403');
|
2016-04-01 05:17:37 +02:00
|
|
|
$page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2013-12-06 15:35:53 +01:00
|
|
|
$response = $this->get($page->RelativeLink());
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2013-11-18 04:32:15 +01:00
|
|
|
$this->assertEquals($response->getStatusCode(), '403');
|
|
|
|
$this->assertNotNull($response->getBody(), 'We have body text from the error page');
|
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2014-05-02 02:52:18 +02:00
|
|
|
public function testSecurityError() {
|
|
|
|
// Generate 404 page
|
|
|
|
$page = $this->objFromFixture('ErrorPage', '404');
|
2016-04-01 05:17:37 +02:00
|
|
|
$page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2014-05-02 02:52:18 +02:00
|
|
|
// 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'));
|
|
|
|
}
|
2015-10-13 07:01:52 +02:00
|
|
|
|
|
|
|
public function testStaticCaching() {
|
|
|
|
// Test new error code does not have static content
|
|
|
|
$this->assertEmpty(ErrorPage::get_content_for_errorcode('401'));
|
|
|
|
$expectedErrorPagePath = AssetStoreTest_SpyStore::base_path() . '/error-401.html';
|
|
|
|
$this->assertFileNotExists($expectedErrorPagePath, 'Error page is not automatically cached');
|
|
|
|
|
|
|
|
// Write new 401 page
|
|
|
|
$page = new ErrorPage();
|
|
|
|
$page->ErrorCode = 401;
|
|
|
|
$page->Title = 'Unauthorised';
|
|
|
|
$page->write();
|
2016-04-01 05:17:37 +02:00
|
|
|
$page->copyVersionToStage('Stage', 'Live');
|
|
|
|
$page->publishRecursive();
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2015-10-13 07:01:52 +02:00
|
|
|
// Static cache should now exist
|
|
|
|
$this->assertNotEmpty(ErrorPage::get_content_for_errorcode('401'));
|
|
|
|
$expectedErrorPagePath = AssetStoreTest_SpyStore::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() {
|
|
|
|
Config::inst()->update('ErrorPage', 'enable_static_file', false);
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
|
|
|
|
$page = new ErrorPage();
|
|
|
|
$page->ErrorCode = 405;
|
|
|
|
$page->Title = 'Method Not Allowed';
|
|
|
|
$page->write();
|
2016-04-01 05:17:37 +02:00
|
|
|
$page->publishRecursive();
|
2015-10-13 07:01:52 +02:00
|
|
|
|
2015-11-26 03:22:28 +01:00
|
|
|
// 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'));
|
2015-10-13 07:01:52 +02:00
|
|
|
$expectedErrorPagePath = AssetStoreTest_SpyStore::base_path() . '/error-405.html';
|
|
|
|
$this->assertFileNotExists($expectedErrorPagePath, 'Error page is not cached in static location');
|
|
|
|
}
|
2014-05-02 02:52:18 +02:00
|
|
|
}
|