Merge pull request #1899 from dhensby/pulls/3.5/vp-render-fix

FIX Make sure VirtualPage renders correct templates
This commit is contained in:
Loz Calver 2017-07-18 11:10:37 +01:00 committed by GitHub
commit be0c0ef5b5
4 changed files with 28 additions and 10 deletions

View File

@ -496,10 +496,8 @@ class VirtualPage_Controller extends Page_Controller {
}
public function getViewer($action) {
$originalClass = get_class($this->CopyContentFrom());
if ($originalClass == 'SiteTree') $name = 'Page_Controller';
else $name = $originalClass."_Controller";
$controller = new $name();
$object = $this->CopyContentFrom();
$controller = ModelAsController::controller_for($object, $action);
return $controller->getViewer($action);
}
@ -539,10 +537,7 @@ class VirtualPage_Controller extends Page_Controller {
public function hasMethod($method) {
$haveIt = parent::hasMethod($method);
if (!$haveIt) {
$originalClass = get_class($this->CopyContentFrom());
if ($originalClass == 'SiteTree') $name = 'ContentController';
else $name = $originalClass."_Controller";
$controller = new $name($this->dataRecord->copyContentFrom());
$controller = ModelAsController::controller_for($this->CopyContentFrom());
$haveIt = $controller->hasMethod($method);
}
return $haveIt;

View File

@ -9,7 +9,7 @@ class VirtualPageTest extends FunctionalTest {
'VirtualPageTest_ClassA',
'VirtualPageTest_ClassB',
'VirtualPageTest_VirtualPageSub',
'VirtualPageTest_PageWithAllowedChildren'
'VirtualPageTest_PageWithAllowedChildren',
);
protected $illegalExtensions = array(
@ -667,6 +667,27 @@ class VirtualPageTest extends FunctionalTest {
$this->assertEquals(301, $response->getStatusCode());
$this->assertEquals('http://google.com', $response->getHeader('Location'));
}
public function testVirtualPageRendersCorrectTemplate() {
$self = $this;
$this->useDraftSite(true);
$this->useTestTheme(dirname(__FILE__), 'virtualpagetest', function() use ($self) {
$page = new VirtualPageTest_ClassA();
$page->Title = 'Test Page';
$page->Content = 'NotThisContent';
$page->MyInitiallyCopiedField = 'TestContent';
$page->write();
$vp = new VirtualPage();
$vp->CopyContentFromID = $page->ID;
$vp->write();
$response = $self->get($vp->Link());
$self->assertEquals(200, $response->getStatusCode());
$self->assertContains('TestContent', $response->getBody());
$self->assertNotContains('NotThisContent', $response->getBody());
});
}
}
class VirtualPageTest_ClassA extends Page implements TestOnly {

View File

@ -0,0 +1 @@
<div>$Content</div>

View File

@ -0,0 +1 @@
<div>$MyInitiallyCopiedField</div>