FIX: Ensure VirtualPage forwards request/response data to virtual controllers (fixes #1329)

This commit is contained in:
Loz Calver 2015-11-16 15:36:52 +00:00
parent a40c8e034a
commit 2983d823d1
2 changed files with 34 additions and 6 deletions

View File

@ -563,12 +563,17 @@ class VirtualPage_Controller extends Page_Controller {
} catch (Exception $e) { } catch (Exception $e) {
// Hack... detect exception type. We really should use exception subclasses. // Hack... detect exception type. We really should use exception subclasses.
// if the exception isn't a 'no method' error, rethrow it // if the exception isn't a 'no method' error, rethrow it
if ($e->getCode() !== 2175) throw $e; if ($e->getCode() !== 2175) {
throw $e;
}
$original = $this->copyContentFrom(); $original = $this->copyContentFrom();
$originalClass = get_class($original); $controller = ModelAsController::controller_for($original);
if ($originalClass == 'SiteTree') $name = 'ContentController';
else $name = $originalClass."_Controller"; // Ensure request/response data is available on virtual controller
$controller = new $name($this->dataRecord->copyContentFrom()); $controller->setRequest($this->getRequest());
$controller->response = $this->response; // @todo - replace with getter/setter in 3.3
return call_user_func_array(array($controller, $method), $args); return call_user_func_array(array($controller, $method), $args);
} }
} }

View File

@ -1,7 +1,9 @@
<?php <?php
class VirtualPageTest extends SapphireTest { class VirtualPageTest extends FunctionalTest {
protected static $fixture_file = 'VirtualPageTest.yml'; protected static $fixture_file = 'VirtualPageTest.yml';
protected static $use_draft_site = false;
protected $autoFollowRedirection = false;
protected $extraDataObjects = array( protected $extraDataObjects = array(
'VirtualPageTest_ClassA', 'VirtualPageTest_ClassA',
@ -21,6 +23,9 @@ class VirtualPageTest extends SapphireTest {
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
// Ensure we always have permission to save/publish
$this->logInWithPermission("ADMIN");
$this->origInitiallyCopiedFields = VirtualPage::config()->initially_copied_fields; $this->origInitiallyCopiedFields = VirtualPage::config()->initially_copied_fields;
Config::inst()->remove('VirtualPage', 'initially_copied_fields'); Config::inst()->remove('VirtualPage', 'initially_copied_fields');
VirtualPage::config()->initially_copied_fields = array_merge( VirtualPage::config()->initially_copied_fields = array_merge(
@ -644,6 +649,24 @@ class VirtualPageTest extends SapphireTest {
if(!$isDetected) $this->fail("Shouldn't be allowed to write a VirtualPage that links to a disallowed child"); if(!$isDetected) $this->fail("Shouldn't be allowed to write a VirtualPage that links to a disallowed child");
} }
public function testVirtualPagePointingToRedirectorPage() {
if (!class_exists('RedirectorPage')) {
$this->markTestSkipped('RedirectorPage required');
}
$rp = new RedirectorPage(array('ExternalURL' => 'http://google.com', 'RedirectionType' => 'External'));
$rp->write();
$rp->doPublish();
$vp = new VirtualPage(array('URLSegment' => 'vptest', 'CopyContentFromID' => $rp->ID));
$vp->write();
$vp->doPublish();
$response = $this->get($vp->Link());
$this->assertEquals(301, $response->getStatusCode());
$this->assertEquals('http://google.com', $response->getHeader('Location'));
}
} }
class VirtualPageTest_ClassA extends Page implements TestOnly { class VirtualPageTest_ClassA extends Page implements TestOnly {