diff --git a/code/Controllers/ContentController.php b/code/Controllers/ContentController.php index f5ed8ba7..b72f844b 100644 --- a/code/Controllers/ContentController.php +++ b/code/Controllers/ContentController.php @@ -289,14 +289,6 @@ class ContentController extends Controller return new ArrayList($visible); } - /** - * @return ArrayList - */ - public function Menu($level) - { - return $this->getMenu($level); - } - /** * Returns the default log-in form. * @@ -419,7 +411,7 @@ HTML; } // Find templates for the controller + action together - e.g. PageController_action.ss - $templatesFound[] = SSViewer::get_templates_by_class(static::class, $action, Controller::class); + $templatesFound[] = SSViewer::get_templates_by_class(static::class, $action ?? '', Controller::class); // Find templates for the record without an action - e.g. Page.ss if ($this->dataRecord instanceof SiteTree) { @@ -427,9 +419,9 @@ HTML; } // Find the templates for the controller without an action - e.g. PageController.ss - $templatesFound[] = SSViewer::get_templates_by_class(static::class, "", Controller::class); + $templatesFound[] = SSViewer::get_templates_by_class(static::class, '', Controller::class); $templates = array_merge(...$templatesFound); - return SSViewer::create($templates); + return SSViewer::create($templates, $this->getTemplateEngine()); } } diff --git a/code/Model/SiteTree.php b/code/Model/SiteTree.php index 2ee8e6b1..4d053e5f 100755 --- a/code/Model/SiteTree.php +++ b/code/Model/SiteTree.php @@ -1454,7 +1454,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi $tags['title'] = [ 'tag' => 'title', - 'content' => $this->obj('Title')->forTemplate() + 'content' => $this->obj('Title')?->forTemplate() ]; $generator = $this->getGenerator(); @@ -1601,7 +1601,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi $tagString = implode("\n", $tags); if ($this->ExtraMeta) { - $tagString .= $this->obj('ExtraMeta')->forTemplate(); + $tagString .= $this->obj('ExtraMeta')?->forTemplate(); } $this->extend('updateMetaTags', $tagString); diff --git a/tests/php/Controllers/ContentControllerTest.php b/tests/php/Controllers/ContentControllerTest.php index b661fd6d..674dc073 100644 --- a/tests/php/Controllers/ContentControllerTest.php +++ b/tests/php/Controllers/ContentControllerTest.php @@ -5,6 +5,8 @@ namespace SilverStripe\CMS\Tests\Controllers; use SilverStripe\CMS\Controllers\ContentController; use SilverStripe\CMS\Controllers\RootURLController; use SilverStripe\CMS\Model\SiteTree; +use SilverStripe\CMS\Tests\Controllers\ContentControllerTest\DummyTemplateContentController; +use SilverStripe\CMS\Tests\Controllers\ContentControllerTest\DummyTemplateEngine; use SilverStripe\Control\Director; use SilverStripe\Control\HTTPResponse_Exception; use SilverStripe\Core\Config\Config; @@ -195,16 +197,23 @@ class ContentControllerTest extends FunctionalTest $response = $this->get($page->RelativeLink("testwithouttemplate")); $this->assertEquals("Page", trim($response->getBody() ?? '')); - // Test that an action with a template will render the both action template *and* the + // Test that an action with a template will render both the action template *and* the // correct parent template - $controller = new ContentController($page); + $controller = new DummyTemplateContentController($page); $viewer = $controller->getViewer('test'); - $this->assertEquals( - __DIR__ - . '/themes/controllertest/templates/SilverStripe/CMS/Tests/Controllers/' - . 'ContentControllerTestPage_test.ss', - $viewer->templates()['main'] - ); + /** @var DummyTemplateEngine $engine */ + $engine = $viewer->getTemplateEngine(); + $templateCandidates = $engine->getTemplates(); + // Check for the action templates + $this->assertContains('SilverStripe\CMS\Tests\Controllers\ContentControllerTestPage_test', $templateCandidates); + $this->assertContains('SilverStripe\CMS\Model\SiteTree_test', $templateCandidates); + $this->assertContains('SilverStripe\CMS\Tests\Controllers\ContentControllerTest\DummyTemplateContentController_test', $templateCandidates); + $this->assertContains('SilverStripe\CMS\Controllers\ContentController_test', $templateCandidates); + // Check for the parent templates + $this->assertContains('SilverStripe\CMS\Tests\Controllers\ContentControllerTestPage', $templateCandidates); + $this->assertContains('SilverStripe\CMS\Model\SiteTree', $templateCandidates); + $this->assertContains('SilverStripe\CMS\Tests\Controllers\ContentControllerTest\DummyTemplateContentController', $templateCandidates); + $this->assertContains('SilverStripe\CMS\Controllers\ContentController', $templateCandidates); }); } } diff --git a/tests/php/Controllers/ContentControllerTest/DummyTemplateContentController.php b/tests/php/Controllers/ContentControllerTest/DummyTemplateContentController.php new file mode 100644 index 00000000..15078b66 --- /dev/null +++ b/tests/php/Controllers/ContentControllerTest/DummyTemplateContentController.php @@ -0,0 +1,15 @@ +'; + + private string|array $templates; + + public function __construct(string|array $templateCandidates = []) + { + $this->templates = $templateCandidates; + } + + public function setTemplate(string|array $templateCandidates): static + { + $this->templates = $templateCandidates; + return $this; + } + + public function hasTemplate(string|array $templateCandidates): bool + { + return true; + } + + public function renderString(string $template, ViewLayerData $model, array $overlay = [], bool $cache = true): string + { + return $this->output; + } + + public function render(ViewLayerData $model, array $overlay = []): string + { + return $this->output; + } + + public function setOutput(string $output): void + { + $this->output = $output; + } + + /** + * Returns the template candidates that were passed to the constructor or to setTemplate() + */ + public function getTemplates(): array + { + return $this->templates; + } +}