getViewer will now locate and use the correct templates for pages which don't have their own controller

This commit is contained in:
micmania1 2013-12-20 20:52:11 +00:00
parent e17fb17520
commit 47582b3e3c
4 changed files with 81 additions and 0 deletions

31
code/controllers/ContentController.php Normal file → Executable file
View File

@ -369,8 +369,39 @@ HTML;
}
return i18n::convert_rfc1766($locale);
}
/**
* Return an SSViewer object to render the template for the current page.
*
* @param $action string
*
* @return SSViewer
*/
public function getViewer($action) {
// Manually set templates should be dealt with by Controller::getViewer()
if(isset($this->templates[$action]) && $this->templates[$action]
|| (isset($this->templates['index']) && $this->templates['index'])
|| $this->template
) {
return parent::getViewer($action);
}
// Prepare action for template search
if($action == "index") $action = "";
else $action = '_' . $action;
// Find templates by dataRecord
$templates = SSViewer::get_templates_by_class(get_class($this->dataRecord), $action, "SiteTree");
// Next, we need to add templates for all controllers
$templates += SSViewer::get_templates_by_class(get_class($this), $action, "Controller");
return new SSViewer($templates);
}
/**
* This action is called by the installation system
*/

48
tests/controller/ContentControllerTest.php Normal file → Executable file
View File

@ -121,6 +121,44 @@ class ContentControllerTest extends FunctionalTest {
$this->get($page->RelativeLink())->getBody(),
'"sitetree_link" shortcodes get parsed properly'
);
}
/**
* Tests that {@link ContentController::getViewer()} chooses the correct templates.
*
* @covers ContentController::getViewer()
**/
public function testGetViewer() {
// // Test a page without a controller (ContentControllerTest_PageWithoutController.ss)
$page = new ContentControllerTestPageWithoutController();
$page->URLSegment = "test";
$page->write();
$page->publish("Stage", "Live");
$response = $this->get($page->RelativeLink());
$this->assertEquals("ContentControllerTestPageWithoutController", $response->getBody());
// // This should fall over to user Page.ss
$page = new ContentControllerTestPage();
$page->URLSegment = "test";
$page->write();
$page->publish("Stage", "Live");
$response = $this->get($page->RelativeLink());
$this->assertEquals("Foo", $response->getBody());
// Test that the action template is rendered.
$page = new ContentControllerTestPage();
$page->URLSegment = "page-without-controller";
$page->write();
$page->publish("Stage", "Live");
$response = $this->get($page->RelativeLink("test"));
$this->assertEquals("ContentControllerTestPage_test", $response->getBody());
}
}
@ -142,3 +180,13 @@ class ContentControllerTest_Page_Controller extends Page_Controller {
}
}
// For testing templates
class ContentControllerTestPageWithoutController extends Page { }
class ContentControllerTestPage extends Page { }
class ContentControllerTestPage_Controller extends Page_Controller {
private static $allowed_actions = array(
"test",
);
}

View File

@ -0,0 +1 @@
ContentControllerTestPageWithoutController

View File

@ -0,0 +1 @@
ContentControllerTestPage_test