BUG Ensure errorpage is built in live mode

Fixes https://github.com/silverstripe/silverstripe-framework/issues/8110
This commit is contained in:
Damian Mooyman 2018-06-01 14:53:35 +12:00 committed by Maxime Rainville
parent a3a0e9cb1a
commit c5205ecc1f
3 changed files with 70 additions and 19 deletions

View File

@ -105,15 +105,31 @@ class ErrorPage extends Page {
); );
$pageExists = ($page && $page->exists()); $pageExists = ($page && $page->exists());
$pagePath = self::get_filepath_for_errorcode($code); $pagePath = self::get_filepath_for_errorcode($code);
if(!($pageExists && file_exists($pagePath))) { if(!$pageExists || !file_exists($pagePath)) {
if(!$pageExists) { if(!$pageExists) {
$page = new ErrorPage($defaultData); $page = new ErrorPage($defaultData);
$page->write(); $page->write();
$page->publish('Stage', 'Live'); $page->publish('Stage', 'Live');
} }
// Skip unpublished records
/** @var ErrorPage $livePage */
$livePage = Versioned::get_by_stage('ErrorPage', Versioned::LIVE)
->byID($page->ID);
if (!$livePage) {
continue;
}
// Ensure a static error page is created from latest error page content // Ensure a static error page is created from latest error page content
$response = Director::test(Director::makeRelative($page->Link())); $origReadingMode = static::get_reading_mode();
$oldDefault = static::get_default_reading_mode();
Versioned::reading_stage(Versioned::LIVE);
Versioned::set_default_reading_mode(Versioned::get_reading_mode());
$response = Director::test(Director::makeRelative($livePage->Link()));
static::set_default_reading_mode($oldDefault);
static::set_reading_mode($origReadingMode);
// Write contents
$written = null; $written = null;
if($fh = fopen($pagePath, 'w')) { if($fh = fopen($pagePath, 'w')) {
$written = fwrite($fh, $response->getBody()); $written = fwrite($fh, $response->getBody());

View File

@ -4,17 +4,17 @@
* @subpackage tests * @subpackage tests
*/ */
class ErrorPageTest extends FunctionalTest { class ErrorPageTest extends FunctionalTest {
protected static $fixture_file = 'ErrorPageTest.yml'; protected static $fixture_file = 'ErrorPageTest.yml';
protected $orig = array(); protected $orig = array();
protected $tmpAssetsPath = ''; protected $tmpAssetsPath = '';
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
$this->orig['ErrorPage_staticfilepath'] = ErrorPage::config()->static_filepath; $this->orig['ErrorPage_staticfilepath'] = ErrorPage::config()->static_filepath;
$this->tmpAssetsPath = sprintf('%s/_tmp_assets_%s', TEMP_FOLDER, rand()); $this->tmpAssetsPath = sprintf('%s/_tmp_assets_%s', TEMP_FOLDER, rand());
Filesystem::makeFolder($this->tmpAssetsPath . '/ErrorPageTest'); Filesystem::makeFolder($this->tmpAssetsPath . '/ErrorPageTest');
ErrorPage::config()->static_filepath = $this->tmpAssetsPath . '/ErrorPageTest'; ErrorPage::config()->static_filepath = $this->tmpAssetsPath . '/ErrorPageTest';
@ -22,41 +22,41 @@ class ErrorPageTest extends FunctionalTest {
$this->origEnvType = Config::inst()->get('Director', 'environment_type'); $this->origEnvType = Config::inst()->get('Director', 'environment_type');
Config::inst()->update('Director', 'environment_type', 'live'); Config::inst()->update('Director', 'environment_type', 'live');
} }
public function tearDown() { public function tearDown() {
parent::tearDown(); parent::tearDown();
ErrorPage::config()->static_filepath = $this->orig['ErrorPage_staticfilepath']; ErrorPage::config()->static_filepath = $this->orig['ErrorPage_staticfilepath'];
Filesystem::removeFolder($this->tmpAssetsPath . '/ErrorPageTest'); Filesystem::removeFolder($this->tmpAssetsPath . '/ErrorPageTest');
Filesystem::removeFolder($this->tmpAssetsPath); Filesystem::removeFolder($this->tmpAssetsPath);
Config::inst()->update('Director', 'environment_type', $this->origEnvType); Config::inst()->update('Director', 'environment_type', $this->origEnvType);
} }
public function test404ErrorPage() { public function test404ErrorPage() {
$page = $this->objFromFixture('ErrorPage', '404'); $page = $this->objFromFixture('ErrorPage', '404');
// ensure that the errorpage exists as a physical file // ensure that the errorpage exists as a physical file
$page->publish('Stage', 'Live'); $page->publish('Stage', 'Live');
$response = $this->get('nonexistent-page'); $response = $this->get('nonexistent-page');
/* We have body text from the error page */ /* We have body text from the error page */
$this->assertNotNull($response->getBody(), '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" */ /* 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"'); $this->assertEquals($response->getStatusCode(), '404', 'Status code of the SS_HTTPResponse for error page is "404"');
/* Status message of the SS_HTTPResponse for error page is "Not Found" */ /* 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"'); $this->assertEquals($response->getStatusDescription(), 'Not Found', 'Status message of the HTTResponse for error page is "Not found"');
} }
public function testBehaviourOfShowInMenuAndShowInSearchFlags() { public function testBehaviourOfShowInMenuAndShowInSearchFlags() {
$page = $this->objFromFixture('ErrorPage', '404'); $page = $this->objFromFixture('ErrorPage', '404');
/* Don't show the error page in the menus */ /* Don't show the error page in the menus */
$this->assertEquals($page->ShowInMenus, 0, '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 */ /* Don't show the error page in the search */
$this->assertEquals($page->ShowInSearch, 0, 'Don\'t show the error page in search'); $this->assertEquals($page->ShowInSearch, 0, 'Don\'t show the error page in search');
} }
@ -81,11 +81,41 @@ class ErrorPageTest extends FunctionalTest {
// Generate 404 page // Generate 404 page
$page = $this->objFromFixture('ErrorPage', '404'); $page = $this->objFromFixture('ErrorPage', '404');
$page->publish('Stage', 'Live'); $page->publish('Stage', 'Live');
// Test invalid action // Test invalid action
$response = $this->get('Security/nosuchaction'); $response = $this->get('Security/nosuchaction');
$this->assertEquals($response->getStatusCode(), '404'); $this->assertEquals($response->getStatusCode(), '404');
$this->assertNotNull($response->getBody()); $this->assertNotNull($response->getBody());
$this->assertContains('text/html', $response->getHeader('Content-Type')); $this->assertContains('text/html', $response->getHeader('Content-Type'));
} }
public function testRequireDefaultRecords()
{
$this->logInWithPermission('ADMIN');
Config::inst()->update('ErrorPage', 'static_filepath', ASSETS_PATH . '/erropagetest');
Filesystem::makeFolder(ASSETS_PATH . '/erropagetest');
// Ensure 404 page is published
/** @var ErrorPage $page404 */
$page404 = $this->objFromFixture('ErrorPage', '404');
$page404->publish('Stage', 'Live');
// Ensure 500 page isn't pubilshed
/** @var ErrorPage $page500 */
$page500 = $this->objFromFixture('ErrorPage', '500');
$page500->doUnpublish();
// Run regeneration
/** @var ErrorPage $singleton */
$singleton = singleton('ErrorPage');
$singleton->requireDefaultRecords();
// 404 page exists
$path404 = ErrorPage::get_filepath_for_errorcode(404);
$this->assertFileExists($path404);
// 500 doesn't exist due to it being unpubilshed
$path500 = ErrorPage::get_filepath_for_errorcode(500);
$this->assertFileNotExists($path500);
}
} }

View File

@ -4,6 +4,11 @@ ErrorPage:
URLSegment: page-not-found URLSegment: page-not-found
Content: My error page body Content: My error page body
ErrorCode: 404 ErrorCode: 404
500:
Title: Server error
URLSegment: server-error
Content: My error page body
ErrorCode: 500
403: 403:
Title: Permission Failure Title: Permission Failure
URLSegment: permission-denied URLSegment: permission-denied