Merge pull request #1330 from kinglozzer/pulls/virtualpage-request-response

FIX: Ensure VirtualPage forwards request/response data to virtual controllers (fixes #1329)
This commit is contained in:
Daniel Hensby 2015-11-18 15:06:42 +00:00
commit a3cac2cb65
2 changed files with 35 additions and 7 deletions

View File

@ -413,7 +413,7 @@ class VirtualPage extends Page {
public function __get($field) {
if(parent::hasMethod($funcName = "get$field")) {
return $this->$funcName();
} else if(parent::hasField($field)) {
} else if(parent::hasField($field) || ($field === 'ID' && !$this->exists())) {
return $this->getField($field);
} else {
return $this->copyContentFrom()->$field;
@ -563,12 +563,17 @@ class VirtualPage_Controller extends Page_Controller {
} catch (Exception $e) {
// Hack... detect exception type. We really should use exception subclasses.
// 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();
$originalClass = get_class($original);
if ($originalClass == 'SiteTree') $name = 'ContentController';
else $name = $originalClass."_Controller";
$controller = new $name($this->dataRecord->copyContentFrom());
$controller = ModelAsController::controller_for($original);
// Ensure request/response data is available on virtual controller
$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);
}
}

View File

@ -1,7 +1,9 @@
<?php
class VirtualPageTest extends SapphireTest {
class VirtualPageTest extends FunctionalTest {
protected static $fixture_file = 'VirtualPageTest.yml';
protected static $use_draft_site = false;
protected $autoFollowRedirection = false;
protected $extraDataObjects = array(
'VirtualPageTest_ClassA',
@ -21,6 +23,9 @@ class VirtualPageTest extends SapphireTest {
public function setUp() {
parent::setUp();
// Ensure we always have permission to save/publish
$this->logInWithPermission("ADMIN");
$this->origInitiallyCopiedFields = VirtualPage::config()->initially_copied_fields;
Config::inst()->remove('VirtualPage', 'initially_copied_fields');
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");
}
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 {