View source
<?php
namespace Drupal\Tests\Core\Extension;
use Drupal\Core\Extension\Extension;
use Drupal\Core\Extension\InfoParser;
use Drupal\Core\Extension\ThemeHandler;
use Drupal\Core\KeyValueStore\KeyValueMemoryFactory;
use Drupal\Core\State\State;
use Drupal\Tests\UnitTestCase;
class ThemeHandlerTest extends UnitTestCase {
protected $infoParser;
protected $state;
protected $configFactory;
protected $moduleHandler;
protected $extensionDiscovery;
protected $themeHandler;
protected function setUp() {
parent::setUp();
$this->configFactory = $this
->getConfigFactoryStub(array(
'core.extension' => array(
'module' => array(),
'theme' => array(),
'disabled' => array(
'theme' => array(),
),
),
));
$this->moduleHandler = $this
->getMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
$this->state = new State(new KeyValueMemoryFactory());
$this->infoParser = $this
->getMock('Drupal\\Core\\Extension\\InfoParserInterface');
$this->extensionDiscovery = $this
->getMockBuilder('Drupal\\Core\\Extension\\ExtensionDiscovery')
->disableOriginalConstructor()
->getMock();
$this->themeHandler = new StubThemeHandler($this->root, $this->configFactory, $this->moduleHandler, $this->state, $this->infoParser, $this->extensionDiscovery);
$cache_tags_invalidator = $this
->getMock('Drupal\\Core\\Cache\\CacheTagsInvalidatorInterface');
$this
->getContainerWithCacheTagsInvalidator($cache_tags_invalidator);
}
public function testRebuildThemeData() {
$this->extensionDiscovery
->expects($this
->at(0))
->method('scan')
->with('theme')
->will($this
->returnValue(array(
'seven' => new Extension($this->root, 'theme', $this->root . '/core/themes/seven/seven.info.yml', 'seven.theme'),
)));
$this->extensionDiscovery
->expects($this
->at(1))
->method('scan')
->with('theme_engine')
->will($this
->returnValue(array(
'twig' => new Extension($this->root, 'theme_engine', $this->root . '/core/themes/engines/twig/twig.info.yml', 'twig.engine'),
)));
$this->infoParser
->expects($this
->once())
->method('parse')
->with($this->root . '/core/themes/seven/seven.info.yml')
->will($this
->returnCallback(function ($file) {
$info_parser = new InfoParser();
return $info_parser
->parse($file);
}));
$this->moduleHandler
->expects($this
->once())
->method('buildModuleDependencies')
->will($this
->returnArgument(0));
$this->moduleHandler
->expects($this
->once())
->method('alter');
$theme_data = $this->themeHandler
->rebuildThemeData();
$this
->assertCount(1, $theme_data);
$info = $theme_data['seven'];
$this
->assertInstanceOf('Drupal\\Core\\Extension\\Extension', $info);
$this
->assertEquals('seven', $info
->getName());
$this
->assertEquals($this->root . '/core/themes/seven/seven.info.yml', $info
->getPathname());
$this
->assertEquals($this->root . '/core/themes/seven/seven.theme', $info
->getExtensionPathname());
$this
->assertEquals($this->root . '/core/themes/engines/twig/twig.engine', $info->owner);
$this
->assertEquals('twig', $info->prefix);
$this
->assertEquals('twig', $info->info['engine']);
$this
->assertEquals(array(
'seven/global-styling',
), $info->info['libraries']);
}
public function testRebuildThemeDataWithThemeParents() {
$this->extensionDiscovery
->expects($this
->at(0))
->method('scan')
->with('theme')
->will($this
->returnValue(array(
'test_subtheme' => new Extension($this->root, 'theme', $this->root . '/core/modules/system/tests/themes/test_subtheme/test_subtheme.info.yml', 'test_subtheme.info.yml'),
'test_basetheme' => new Extension($this->root, 'theme', $this->root . '/core/modules/system/tests/themes/test_basetheme/test_basetheme.info.yml', 'test_basetheme.info.yml'),
)));
$this->extensionDiscovery
->expects($this
->at(1))
->method('scan')
->with('theme_engine')
->will($this
->returnValue(array(
'twig' => new Extension($this->root, 'theme_engine', $this->root . '/core/themes/engines/twig/twig.info.yml', 'twig.engine'),
)));
$this->infoParser
->expects($this
->at(0))
->method('parse')
->with($this->root . '/core/modules/system/tests/themes/test_subtheme/test_subtheme.info.yml')
->will($this
->returnCallback(function ($file) {
$info_parser = new InfoParser();
return $info_parser
->parse($file);
}));
$this->infoParser
->expects($this
->at(1))
->method('parse')
->with($this->root . '/core/modules/system/tests/themes/test_basetheme/test_basetheme.info.yml')
->will($this
->returnCallback(function ($file) {
$info_parser = new InfoParser();
return $info_parser
->parse($file);
}));
$this->moduleHandler
->expects($this
->once())
->method('buildModuleDependencies')
->will($this
->returnArgument(0));
$theme_data = $this->themeHandler
->rebuildThemeData();
$this
->assertCount(2, $theme_data);
$info_basetheme = $theme_data['test_basetheme'];
$info_subtheme = $theme_data['test_subtheme'];
$this
->assertInstanceOf('Drupal\\Core\\Extension\\Extension', $info_basetheme);
$this
->assertEquals('test_basetheme', $info_basetheme
->getName());
$this
->assertInstanceOf('Drupal\\Core\\Extension\\Extension', $info_subtheme);
$this
->assertEquals('test_subtheme', $info_subtheme
->getName());
$info_subtheme->info['base theme'] = 'test_basetheme';
$info_basetheme->sub_themes = array(
'test_subtheme',
);
$this
->assertEquals($this->root . '/core/themes/engines/twig/twig.engine', $info_basetheme->owner);
$this
->assertEquals('twig', $info_basetheme->prefix);
$this
->assertEquals($this->root . '/core/themes/engines/twig/twig.engine', $info_subtheme->owner);
$this
->assertEquals('twig', $info_subtheme->prefix);
}
public function testGetBaseThemes(array $themes, $theme, array $expected) {
$base_themes = $this->themeHandler
->getBaseThemes($themes, $theme);
$this
->assertEquals($expected, $base_themes);
}
public function providerTestGetBaseThemes() {
$data = array();
$themes = array();
$themes['test_1'] = (object) array(
'name' => 'test_1',
'info' => array(
'name' => 'test_1',
),
);
$data[] = array(
$themes,
'test_1',
array(),
);
$themes = array();
$themes['test_1'] = (object) array(
'name' => 'test_1',
'info' => array(
'name' => 'test_1',
'base theme' => 'test_2',
),
);
$data[] = array(
$themes,
'test_1',
array(
'test_2' => NULL,
),
);
$themes = array();
$themes['test_1'] = (object) array(
'name' => 'test_1',
'info' => array(
'name' => 'test_1',
'base theme' => 'test_2',
),
);
$themes['test_2'] = (object) array(
'name' => 'test_2',
'info' => array(
'name' => 'test_2',
),
);
$data[] = array(
$themes,
'test_1',
array(
'test_2' => 'test_2',
),
);
$themes = array();
$themes['test_1'] = (object) array(
'name' => 'test_1',
'info' => array(
'name' => 'test_1',
'base theme' => 'test_2',
),
);
$themes['test_2'] = (object) array(
'name' => 'test_2',
'info' => array(
'name' => 'test_2',
'base theme' => 'test_3',
),
);
$themes['test_3'] = (object) array(
'name' => 'test_3',
'info' => array(
'name' => 'test_3',
),
);
$data[] = array(
$themes,
'test_1',
array(
'test_2' => 'test_2',
'test_3' => 'test_3',
),
);
return $data;
}
}
class StubThemeHandler extends ThemeHandler {
protected $clearedCssCache;
protected $registryRebuild;
protected $systemList;
protected function clearCssCache() {
$this->clearedCssCache = TRUE;
}
protected function themeRegistryRebuild() {
$this->registryRebuild = TRUE;
}
protected function systemThemeList() {
return $this->systemList;
}
protected function systemListReset() {
}
}
if (!defined('DRUPAL_EXTENSION_NAME_MAX_LENGTH')) {
define('DRUPAL_EXTENSION_NAME_MAX_LENGTH', 50);
}
if (!defined('DRUPAL_PHP_FUNCTION_PATTERN')) {
define('DRUPAL_PHP_FUNCTION_PATTERN', '[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*');
}
if (!defined('DRUPAL_MINIMUM_PHP')) {
define('DRUPAL_MINIMUM_PHP', '5.3.10');
}