2011-03-24 11:30:57 +01:00
|
|
|
<?php
|
2016-07-13 14:36:52 +02:00
|
|
|
|
|
|
|
use SilverStripe\View\TemplateLoader;
|
|
|
|
use SilverStripe\View\ThemeManifest;
|
|
|
|
|
2011-03-24 11:30:57 +01:00
|
|
|
/**
|
2016-07-13 14:36:52 +02:00
|
|
|
* Tests for the {@link TemplateLoader} class.
|
2011-03-24 11:30:57 +01:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2011-03-24 11:30:57 +01:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class TemplateLoaderTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-01-13 21:33:22 +01:00
|
|
|
private $base;
|
2016-07-19 03:17:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ThemeManifest
|
|
|
|
*/
|
2014-01-13 21:33:22 +01:00
|
|
|
private $manifest;
|
2016-07-19 03:17:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var TemplateLoader
|
|
|
|
*/
|
2014-01-13 21:33:22 +01:00
|
|
|
private $loader;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-01-13 21:33:22 +01:00
|
|
|
/**
|
|
|
|
* Set up manifest before each test
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
2016-07-13 14:36:52 +02:00
|
|
|
// Fake project root
|
2014-01-13 21:33:22 +01:00
|
|
|
$this->base = dirname(__FILE__) . '/fixtures/templatemanifest';
|
2016-07-13 14:36:52 +02:00
|
|
|
// New ThemeManifest for that root
|
2016-07-19 03:17:07 +02:00
|
|
|
$this->manifest = new ThemeManifest($this->base, 'myproject', false, true);
|
2016-07-13 14:36:52 +02:00
|
|
|
// New Loader for that root
|
2016-07-19 03:17:07 +02:00
|
|
|
$this->loader = new TemplateLoader($this->base);
|
2016-07-13 14:36:52 +02:00
|
|
|
$this->loader->addSet('$default', $this->manifest);
|
2014-01-13 21:33:22 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-01-13 21:33:22 +01:00
|
|
|
/**
|
|
|
|
* Test that 'main' and 'Layout' templates are loaded from module
|
|
|
|
*/
|
|
|
|
public function testFindTemplatesInModule() {
|
2016-07-13 14:36:52 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
"$this->base/module/templates/Page.ss",
|
|
|
|
$this->loader->findTemplate('Page', ['$default'])
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
"$this->base/module/templates/Layout/Page.ss",
|
|
|
|
$this->loader->findTemplate(['type' => 'Layout', 'Page'], ['$default'])
|
2011-03-24 11:30:57 +01:00
|
|
|
);
|
2014-01-13 21:33:22 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-19 03:17:07 +02:00
|
|
|
public function testFindNestedThemeTemplates() {
|
|
|
|
// Without including the theme this template cannot be found
|
|
|
|
$this->assertEquals(null, $this->loader->findTemplate('NestedThemePage', ['$default']));
|
|
|
|
|
|
|
|
// With a nested theme available then it is available
|
|
|
|
$this->assertEquals(
|
|
|
|
"{$this->base}/module/themes/subtheme/templates/NestedThemePage.ss",
|
|
|
|
$this->loader->findTemplate('NestedThemePage', [
|
|
|
|
'silverstripe/module:subtheme',
|
|
|
|
'$default'
|
|
|
|
])
|
|
|
|
);
|
|
|
|
|
|
|
|
// Can also be found if excluding $default theme
|
|
|
|
$this->assertEquals(
|
|
|
|
"{$this->base}/module/themes/subtheme/templates/NestedThemePage.ss",
|
|
|
|
$this->loader->findTemplate('NestedThemePage', [
|
|
|
|
'silverstripe/module:subtheme',
|
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-01-13 21:33:22 +01:00
|
|
|
/**
|
|
|
|
* Test that 'main' and 'Layout' templates are loaded from set theme
|
|
|
|
*/
|
|
|
|
public function testFindTemplatesInTheme() {
|
2016-07-13 14:36:52 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
"$this->base/themes/theme/templates/Page.ss",
|
|
|
|
$this->loader->findTemplate('Page', ['theme'])
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
"$this->base/themes/theme/templates/Layout/Page.ss",
|
|
|
|
$this->loader->findTemplate(['type' => 'Layout', 'Page'], ['theme'])
|
2011-03-24 11:30:57 +01:00
|
|
|
);
|
2014-01-13 21:33:22 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-01-13 21:33:22 +01:00
|
|
|
/**
|
|
|
|
* Test that 'main' and 'Layout' templates are loaded from project without a set theme
|
|
|
|
*/
|
|
|
|
public function testFindTemplatesInApplication() {
|
2016-07-13 14:36:52 +02:00
|
|
|
// TODO: replace with one that doesn't create temporary files (so bad)
|
2014-01-13 21:33:22 +01:00
|
|
|
$templates = array(
|
|
|
|
$this->base . '/myproject/templates/Page.ss',
|
|
|
|
$this->base . '/myproject/templates/Layout/Page.ss'
|
2011-03-24 11:30:57 +01:00
|
|
|
);
|
2014-01-13 21:33:22 +01:00
|
|
|
$this->createTestTemplates($templates);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-13 14:36:52 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
"$this->base/myproject/templates/Page.ss",
|
|
|
|
$this->loader->findTemplate('Page', ['$default'])
|
2013-04-29 17:19:51 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-13 14:36:52 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
"$this->base/myproject/templates/Layout/Page.ss",
|
|
|
|
$this->loader->findTemplate(['type' => 'Layout', 'Page'], ['$default'])
|
2014-01-13 21:33:22 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-13 14:36:52 +02:00
|
|
|
$this->removeTestTemplates($templates);
|
2014-01-13 21:33:22 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-01-13 21:33:22 +01:00
|
|
|
/**
|
|
|
|
* Test that 'main' template is found in theme and 'Layout' is found in module
|
|
|
|
*/
|
|
|
|
public function testFindTemplatesMainThemeLayoutModule() {
|
2016-07-13 14:36:52 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
"$this->base/themes/theme/templates/CustomThemePage.ss",
|
|
|
|
$this->loader->findTemplate('CustomThemePage', ['theme', '$default'])
|
2014-01-13 21:33:22 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-07-13 14:36:52 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
"$this->base/module/templates/Layout/CustomThemePage.ss",
|
|
|
|
$this->loader->findTemplate(['type' => 'Layout', 'CustomThemePage'], ['theme', '$default'])
|
2014-01-13 21:33:22 +01:00
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-01-13 21:33:22 +01:00
|
|
|
protected function createTestTemplates($templates) {
|
|
|
|
foreach ($templates as $template) {
|
|
|
|
file_put_contents($template, '');
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-01-13 21:33:22 +01:00
|
|
|
protected function removeTestTemplates($templates) {
|
|
|
|
foreach ($templates as $template) {
|
|
|
|
unlink($template);
|
|
|
|
}
|
2012-11-02 08:28:39 +01:00
|
|
|
}
|
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|