diff --git a/model/Versioned.php b/model/Versioned.php index b30c2635f..03567f95c 100644 --- a/model/Versioned.php +++ b/model/Versioned.php @@ -939,23 +939,28 @@ class Versioned extends DataExtension implements TemplateGlobalProvider { * @param Session $session Optional session within which to store the resulting stage */ public static function choose_site_stage($session = null) { - if(!$session) $session = Session::current_session(); + // Default to Live + $mode = 'Stage.Live'; + // Get reading mode if(isset($_GET['stage'])) { $stage = ucfirst(strtolower($_GET['stage'])); - if(!in_array($stage, array('Stage', 'Live'))) $stage = 'Live'; - - $session->inst_set('readingMode', 'Stage.' . $stage); - } - if(isset($_GET['archiveDate']) && strtotime($_GET['archiveDate'])) { - $session->inst_set('readingMode', 'Archive.' . $_GET['archiveDate']); + $mode = 'Stage.' . $stage; + } elseif (isset($_GET['archiveDate']) && strtotime($_GET['archiveDate'])) { + $mode = 'Archive.' . $_GET['archiveDate']; + } elseif($session) { + $mode = $session->inst_get('readingMode') ?: $mode; + } else { + $mode = Session::get('readingMode') ?: $mode; } - if($mode = $session->inst_get('readingMode')) { - Versioned::set_reading_mode($mode); + // Save reading mode + Versioned::set_reading_mode($mode); + if($session) { + $session->inst_set('readingMode', $mode); } else { - Versioned::reading_stage("Live"); + Session::set('readingMode', $mode); } if(!headers_sent() && !Director::is_cli()) { diff --git a/tests/model/VersionedTest.php b/tests/model/VersionedTest.php index 81d3faaa9..b8330d874 100644 --- a/tests/model/VersionedTest.php +++ b/tests/model/VersionedTest.php @@ -439,7 +439,6 @@ class VersionedTest extends SapphireTest { $testData->Content = 'After Content'; $testData->write(); - $_GET['archiveDate'] = '2009-01-01 19:00:00'; Versioned::reading_archived_date('2009-01-01 19:00:00'); $fetchedData = VersionedTest_DataObject::get()->byId($id); @@ -568,6 +567,16 @@ class VersionedTest extends SapphireTest { 'Check that subsequent requests in the same session remain in Live mode' ); + // Test choose_site_stage + Session::set('readingMode', 'Stage.Stage'); + Versioned::choose_site_stage(); + $this->assertEquals('Stage.Stage', Versioned::get_reading_mode()); + Session::set('readingMode', 'Archive.2014-01-01'); + Versioned::choose_site_stage(); + $this->assertEquals('Archive.2014-01-01', Versioned::get_reading_mode()); + Session::clear('readingMode'); + Versioned::choose_site_stage(); + $this->assertEquals('Stage.Live', Versioned::get_reading_mode()); } }