public function preRequest(HTTPRequest $request, Session $session, DataModel $model)
 {
     // Bootstrap session so that Session::get() accesses the right instance
     $dummyController = new Controller();
     $dummyController->setSession($session);
     $dummyController->setRequest($request);
     $dummyController->pushCurrent();
     // Block non-authenticated users from setting the stage mode
     if (!Versioned::can_choose_site_stage($request)) {
         $permissionMessage = sprintf(_t("ContentController.DRAFT_SITE_ACCESS_RESTRICTION", 'You must log in with your CMS password in order to view the draft or archived content. ' . '<a href="%s">Click here to go back to the published site.</a>'), Convert::raw2xml(Controller::join_links(Director::baseURL(), $request->getURL(), "?stage=Live")));
         // Force output since RequestFilter::preRequest doesn't support response overriding
         $response = Security::permissionFailure($dummyController, $permissionMessage);
         $session->inst_save();
         $dummyController->popCurrent();
         // Prevent output in testing
         if (class_exists('SilverStripe\\Dev\\SapphireTest', false) && SapphireTest::is_running_test()) {
             throw new HTTPResponse_Exception($response);
         }
         $response->output();
         die;
     }
     Versioned::choose_site_stage();
     $dummyController->popCurrent();
     return true;
 }
 /**
  * Tests that reading mode persists between requests
  */
 public function testReadingPersistent()
 {
     $session = Injector::inst()->create('Session', array());
     $adminID = $this->logInWithPermission('ADMIN');
     $session->inst_set('loggedInAs', $adminID);
     // Set to stage
     Director::test('/?stage=Stage', null, $session);
     $this->assertEquals('Stage.Stage', $session->inst_get('readingMode'), 'Check querystring changes reading mode to Stage');
     Director::test('/', null, $session);
     $this->assertEquals('Stage.Stage', $session->inst_get('readingMode'), 'Check that subsequent requests in the same session remain in Stage mode');
     // Test live persists
     Director::test('/?stage=Live', null, $session);
     $this->assertEquals('Stage.Live', $session->inst_get('readingMode'), 'Check querystring changes reading mode to Live');
     Director::test('/', null, $session);
     $this->assertEquals('Stage.Live', $session->inst_get('readingMode'), 'Check that subsequent requests in the same session remain in Live mode');
     // Test that session doesn't redundantly store the default stage if it doesn't need to
     $session2 = Injector::inst()->create('Session', array());
     $session2->inst_set('loggedInAs', $adminID);
     Director::test('/', null, $session2);
     $this->assertArrayNotHasKey('readingMode', $session2->inst_changedData());
     Director::test('/?stage=Live', null, $session2);
     $this->assertArrayNotHasKey('readingMode', $session2->inst_changedData());
     // Test choose_site_stage
     unset($_GET['stage']);
     unset($_GET['archiveDate']);
     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());
 }