2011-03-24 11:30:57 +01:00
|
|
|
<?php
|
2016-07-13 14:36:52 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
namespace SilverStripe\Core\Tests\Manifest;
|
|
|
|
|
2018-10-05 14:58:48 +02:00
|
|
|
use Psr\SimpleCache\CacheInterface;
|
2017-09-21 07:54:06 +02:00
|
|
|
use SilverStripe\Control\Director;
|
2017-07-13 00:27:27 +02:00
|
|
|
use SilverStripe\Core\Manifest\ModuleLoader;
|
2016-07-19 04:09:15 +02:00
|
|
|
use SilverStripe\View\ThemeResourceLoader;
|
2016-07-13 14:36:52 +02:00
|
|
|
use SilverStripe\View\ThemeManifest;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2017-07-13 00:27:27 +02:00
|
|
|
use SilverStripe\Core\Manifest\ModuleManifest;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
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
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
class ThemeResourceLoaderTest extends SapphireTest
|
|
|
|
{
|
2017-03-14 03:20:51 +01:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
private $base;
|
2016-07-19 03:17:07 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
|
|
|
* @var ThemeManifest
|
|
|
|
*/
|
|
|
|
private $manifest;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
|
|
|
* @var ThemeResourceLoader
|
|
|
|
*/
|
|
|
|
private $loader;
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
|
|
|
* Set up manifest before each test
|
|
|
|
*/
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function setUp(): void
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// Fake project root
|
|
|
|
$this->base = dirname(__FILE__) . '/fixtures/templatemanifest';
|
2017-09-21 07:54:06 +02:00
|
|
|
Director::config()->set('alternate_base_folder', $this->base);
|
2017-07-13 00:27:27 +02:00
|
|
|
ModuleManifest::config()->set('module_priority', ['$project', '$other_modules']);
|
|
|
|
ModuleManifest::config()->set('project', 'myproject');
|
|
|
|
|
|
|
|
$moduleManifest = new ModuleManifest($this->base);
|
|
|
|
$moduleManifest->init();
|
|
|
|
$moduleManifest->sort();
|
|
|
|
ModuleLoader::inst()->pushManifest($moduleManifest);
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// New ThemeManifest for that root
|
2017-07-13 00:27:27 +02:00
|
|
|
$this->manifest = new ThemeManifest($this->base);
|
|
|
|
$this->manifest->setProject('myproject');
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->manifest->init();
|
2016-12-16 05:34:21 +01:00
|
|
|
// New Loader for that root
|
|
|
|
$this->loader = new ThemeResourceLoader($this->base);
|
|
|
|
$this->loader->addSet('$default', $this->manifest);
|
2018-10-04 17:49:35 +02:00
|
|
|
|
2018-10-05 14:58:48 +02:00
|
|
|
// Ensure the cache is flushed between tests
|
|
|
|
ThemeResourceLoader::flush();
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2016-07-13 14:36:52 +02:00
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function tearDown(): void
|
2017-07-13 00:27:27 +02:00
|
|
|
{
|
|
|
|
ModuleLoader::inst()->popManifest();
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
|
|
|
* Test that 'main' and 'Layout' templates are loaded from module
|
|
|
|
*/
|
|
|
|
public function testFindTemplatesInModule()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
"$this->base/module/templates/Page.ss",
|
|
|
|
$this->loader->findTemplate('Page', ['$default'])
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
"$this->base/module/templates/Layout/Page.ss",
|
|
|
|
$this->loader->findTemplate(['type' => 'Layout', 'Page'], ['$default'])
|
|
|
|
);
|
|
|
|
}
|
2016-07-19 03:17:07 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testFindNestedThemeTemplates()
|
|
|
|
{
|
|
|
|
// Without including the theme this template cannot be found
|
|
|
|
$this->assertEquals(null, $this->loader->findTemplate('NestedThemePage', ['$default']));
|
2016-07-19 03:17:07 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// With a nested theme available then it is available
|
|
|
|
$this->assertEquals(
|
|
|
|
"{$this->base}/module/themes/subtheme/templates/NestedThemePage.ss",
|
|
|
|
$this->loader->findTemplate(
|
|
|
|
'NestedThemePage',
|
|
|
|
[
|
2017-09-21 07:54:06 +02:00
|
|
|
'silverstripe/module:subtheme',
|
|
|
|
'$default'
|
2016-12-16 05:34:21 +01:00
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
2016-07-19 03:17:07 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// Can also be found if excluding $default theme
|
|
|
|
$this->assertEquals(
|
|
|
|
"{$this->base}/module/themes/subtheme/templates/NestedThemePage.ss",
|
|
|
|
$this->loader->findTemplate(
|
|
|
|
'NestedThemePage',
|
|
|
|
[
|
2017-09-21 07:54:06 +02:00
|
|
|
'silverstripe/module:subtheme',
|
2016-12-16 05:34:21 +01:00
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2016-08-05 06:00:38 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testFindTemplateByType()
|
|
|
|
{
|
|
|
|
// Test that "type" is respected properly
|
|
|
|
$this->assertEquals(
|
|
|
|
"{$this->base}/module/templates/MyNamespace/Layout/MyClass.ss",
|
|
|
|
$this->loader->findTemplate(
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'type' => 'Layout',
|
|
|
|
'MyNamespace/NonExistantTemplate'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'type' => 'Layout',
|
|
|
|
'MyNamespace/MyClass'
|
|
|
|
],
|
|
|
|
'MyNamespace/MyClass'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'silverstripe/module:subtheme',
|
|
|
|
'theme',
|
|
|
|
'$default',
|
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
2016-08-05 06:00:38 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// Non-typed template can be found even if looking for typed theme at a lower priority
|
|
|
|
$this->assertEquals(
|
|
|
|
"{$this->base}/module/templates/MyNamespace/MyClass.ss",
|
|
|
|
$this->loader->findTemplate(
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'type' => 'Layout',
|
|
|
|
'MyNamespace/NonExistantTemplate'
|
|
|
|
],
|
|
|
|
'MyNamespace/MyClass',
|
|
|
|
[
|
|
|
|
'type' => 'Layout',
|
|
|
|
'MyNamespace/MyClass'
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'silverstripe/module',
|
|
|
|
'theme',
|
|
|
|
'$default',
|
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2016-08-23 04:32:26 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testFindTemplatesByPath()
|
|
|
|
{
|
|
|
|
// Items given as full paths are returned directly
|
|
|
|
$this->assertEquals(
|
|
|
|
"$this->base/themes/theme/templates/Page.ss",
|
|
|
|
$this->loader->findTemplate("$this->base/themes/theme/templates/Page.ss", ['theme'])
|
|
|
|
);
|
2016-08-23 04:32:26 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
"$this->base/themes/theme/templates/Page.ss",
|
|
|
|
$this->loader->findTemplate(
|
|
|
|
[
|
2017-09-21 07:54:06 +02:00
|
|
|
"$this->base/themes/theme/templates/Page.ss",
|
|
|
|
"Page"
|
2016-12-16 05:34:21 +01:00
|
|
|
],
|
|
|
|
['theme']
|
|
|
|
)
|
|
|
|
);
|
2016-08-23 04:32:26 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// Ensure checks for file_exists
|
|
|
|
$this->assertEquals(
|
|
|
|
"$this->base/themes/theme/templates/Page.ss",
|
|
|
|
$this->loader->findTemplate(
|
|
|
|
[
|
2017-09-21 07:54:06 +02:00
|
|
|
"$this->base/themes/theme/templates/NotAPage.ss",
|
|
|
|
"$this->base/themes/theme/templates/Page.ss",
|
2016-12-16 05:34:21 +01:00
|
|
|
],
|
|
|
|
['theme']
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2016-07-13 14:36:52 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
|
|
|
* Test that 'main' and 'Layout' templates are loaded from set theme
|
|
|
|
*/
|
|
|
|
public function testFindTemplatesInTheme()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
"$this->base/themes/theme/templates/Page.ss",
|
|
|
|
$this->loader->findTemplate('Page', ['theme'])
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
"$this->base/themes/theme/templates/Layout/Page.ss",
|
|
|
|
$this->loader->findTemplate(['type' => 'Layout', 'Page'], ['theme'])
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
|
|
|
* Test that 'main' and 'Layout' templates are loaded from project without a set theme
|
|
|
|
*/
|
|
|
|
public function testFindTemplatesInApplication()
|
|
|
|
{
|
|
|
|
// TODO: replace with one that doesn't create temporary files (so bad)
|
2020-04-20 19:58:09 +02:00
|
|
|
$templates = [
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->base . '/myproject/templates/Page.ss',
|
|
|
|
$this->base . '/myproject/templates/Layout/Page.ss'
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->createTestTemplates($templates);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
"$this->base/myproject/templates/Page.ss",
|
|
|
|
$this->loader->findTemplate('Page', ['$default'])
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
"$this->base/myproject/templates/Layout/Page.ss",
|
|
|
|
$this->loader->findTemplate(['type' => 'Layout', 'Page'], ['$default'])
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->removeTestTemplates($templates);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
|
|
|
* Test that 'main' template is found in theme and 'Layout' is found in module
|
|
|
|
*/
|
|
|
|
public function testFindTemplatesMainThemeLayoutModule()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
"$this->base/themes/theme/templates/CustomThemePage.ss",
|
|
|
|
$this->loader->findTemplate('CustomThemePage', ['theme', '$default'])
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
"$this->base/module/templates/Layout/CustomThemePage.ss",
|
|
|
|
$this->loader->findTemplate(['type' => 'Layout', 'CustomThemePage'], ['theme', '$default'])
|
|
|
|
);
|
|
|
|
}
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testFindThemedCSS()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
"myproject/css/project.css",
|
|
|
|
$this->loader->findThemedCSS('project', ['$default', 'theme'])
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
"themes/theme/css/project.css",
|
|
|
|
$this->loader->findThemedCSS('project', ['theme', '$default'])
|
|
|
|
);
|
|
|
|
$this->assertEmpty(
|
|
|
|
$this->loader->findThemedCSS('nofile', ['theme', '$default'])
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'module/css/content.css',
|
|
|
|
$this->loader->findThemedCSS('content', ['/module', 'theme'])
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'module/css/content.css',
|
|
|
|
$this->loader->findThemedCSS('content', ['/module', 'theme', '$default'])
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'module/css/content.css',
|
|
|
|
$this->loader->findThemedCSS('content', ['$default', '/module', 'theme'])
|
|
|
|
);
|
|
|
|
}
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testFindThemedJavascript()
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
|
|
|
"myproject/javascript/project.js",
|
|
|
|
$this->loader->findThemedJavascript('project', ['$default', 'theme'])
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
"themes/theme/javascript/project.js",
|
|
|
|
$this->loader->findThemedJavascript('project', ['theme', '$default'])
|
|
|
|
);
|
|
|
|
$this->assertEmpty(
|
|
|
|
$this->loader->findThemedJavascript('nofile', ['theme', '$default'])
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'module/javascript/content.js',
|
|
|
|
$this->loader->findThemedJavascript('content', ['/module', 'theme'])
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'module/javascript/content.js',
|
|
|
|
$this->loader->findThemedJavascript('content', ['/module', 'theme', '$default'])
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'module/javascript/content.js',
|
|
|
|
$this->loader->findThemedJavascript('content', ['$default', '/module', 'theme'])
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
protected function createTestTemplates($templates)
|
|
|
|
{
|
|
|
|
foreach ($templates as $template) {
|
|
|
|
file_put_contents($template, '');
|
|
|
|
}
|
|
|
|
}
|
2012-11-02 08:28:39 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
protected function removeTestTemplates($templates)
|
|
|
|
{
|
|
|
|
foreach ($templates as $template) {
|
|
|
|
unlink($template);
|
|
|
|
}
|
|
|
|
}
|
2017-09-21 07:54:06 +02:00
|
|
|
|
|
|
|
public function providerTestGetPath()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
// Legacy theme
|
|
|
|
[
|
|
|
|
'theme',
|
|
|
|
'themes/theme',
|
|
|
|
],
|
|
|
|
// Module themes
|
|
|
|
[
|
|
|
|
'silverstripe/vendormodule:vendortheme',
|
|
|
|
'vendor/silverstripe/vendormodule/themes/vendortheme',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'module:subtheme',
|
|
|
|
'module/themes/subtheme',
|
|
|
|
],
|
|
|
|
// Module absolute paths
|
|
|
|
[
|
|
|
|
'silverstripe/vendormodule:/themes/vendortheme',
|
|
|
|
'vendor/silverstripe/vendormodule/themes/vendortheme',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'module:/themes/subtheme',
|
|
|
|
'module/themes/subtheme',
|
|
|
|
],
|
|
|
|
// Module root directory
|
|
|
|
[
|
|
|
|
'silverstripe/vendormodule:/',
|
|
|
|
'vendor/silverstripe/vendormodule',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'silverstripe/vendormodule:',
|
|
|
|
'vendor/silverstripe/vendormodule',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'silverstripe/vendormodule',
|
|
|
|
'vendor/silverstripe/vendormodule',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'module:',
|
|
|
|
'module',
|
|
|
|
],
|
|
|
|
// Absolute paths
|
|
|
|
[
|
|
|
|
'/vendor/silverstripe/vendormodule/themes/vendortheme',
|
|
|
|
'vendor/silverstripe/vendormodule/themes/vendortheme',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'/module/themes/subtheme',
|
|
|
|
'module/themes/subtheme'
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerTestGetPath
|
|
|
|
* @param string $name Theme identifier
|
|
|
|
* @param string $path Path to theme
|
|
|
|
*/
|
|
|
|
public function testGetPath($name, $path)
|
|
|
|
{
|
|
|
|
$this->assertEquals($path, $this->loader->getPath($name));
|
|
|
|
}
|
2018-10-05 14:58:48 +02:00
|
|
|
|
|
|
|
public function testFindTemplateWithCacheMiss()
|
|
|
|
{
|
|
|
|
$mockCache = $this->createMock(CacheInterface::class);
|
|
|
|
$mockCache->expects($this->once())->method('has')->willReturn(false);
|
|
|
|
$mockCache->expects($this->never())->method('get');
|
|
|
|
$mockCache->expects($this->once())->method('set');
|
|
|
|
|
|
|
|
$loader = new ThemeResourceLoader();
|
|
|
|
$loader->setCache($mockCache);
|
|
|
|
$loader->findTemplate('Page', ['$default']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFindTemplateWithCacheHit()
|
|
|
|
{
|
|
|
|
$mockCache = $this->createMock(CacheInterface::class);
|
2018-10-05 16:07:33 +02:00
|
|
|
$mockCache->expects($this->once())->method('has')->willReturn(true);
|
2018-10-05 14:58:48 +02:00
|
|
|
$mockCache->expects($this->never())->method('set');
|
|
|
|
$mockCache->expects($this->once())->method('get')->willReturn('mock_template.ss');
|
|
|
|
|
|
|
|
$loader = new ThemeResourceLoader();
|
|
|
|
$loader->setCache($mockCache);
|
|
|
|
$this->assertSame('mock_template.ss', $loader->findTemplate('Page', ['$default']));
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|