View source
<?php
namespace Drupal\system\Tests\Theme;
use Drupal\Component\Utility\Xss;
use Drupal\simpletest\WebTestBase;
class ThemeSuggestionsAlterTest extends WebTestBase {
public static $modules = array(
'theme_test',
);
protected function setUp() {
parent::setUp();
\Drupal::service('theme_handler')
->install(array(
'test_theme',
));
}
function testTemplateSuggestions() {
$this
->drupalGet('theme-test/suggestion-provided');
$this
->assertText('Template for testing suggestions provided by the module declaring the theme hook.');
$this
->config('system.theme')
->set('default', 'test_theme')
->save();
$this
->drupalGet('theme-test/suggestion-provided');
$this
->assertText('Template overridden based on suggestion provided by the module declaring the theme hook.');
}
function testGeneralSuggestionsAlter() {
$this
->drupalGet('theme-test/general-suggestion-alter');
$this
->assertText('Original template for testing hook_theme_suggestions_alter().');
$this
->config('system.theme')
->set('default', 'test_theme')
->save();
$this
->drupalGet('theme-test/general-suggestion-alter');
$this
->assertText('Template overridden based on new theme suggestion provided by the test_theme theme via hook_theme_suggestions_alter().');
\Drupal::service('module_installer')
->install(array(
'theme_suggestions_test',
));
$this
->resetAll();
$this
->drupalGet('theme-test/general-suggestion-alter');
$this
->assertText('Template overridden based on new theme suggestion provided by a module via hook_theme_suggestions_alter().');
}
function testTemplateSuggestionsAlter() {
$this
->drupalGet('theme-test/suggestion-alter');
$this
->assertText('Original template for testing hook_theme_suggestions_HOOK_alter().');
$this
->config('system.theme')
->set('default', 'test_theme')
->save();
$this
->drupalGet('theme-test/suggestion-alter');
$this
->assertText('Template overridden based on new theme suggestion provided by the test_theme theme via hook_theme_suggestions_HOOK_alter().');
\Drupal::service('module_installer')
->install(array(
'theme_suggestions_test',
));
$this
->resetAll();
$this
->drupalGet('theme-test/suggestion-alter');
$this
->assertText('Template overridden based on new theme suggestion provided by a module via hook_theme_suggestions_HOOK_alter().');
}
function testSpecificSuggestionsAlter() {
$this
->drupalGet('theme-test/specific-suggestion-alter');
$this
->assertText('Template for testing specific theme calls.');
$this
->config('system.theme')
->set('default', 'test_theme')
->save();
$this
->drupalGet('theme-test/specific-suggestion-alter');
$this
->assertText('Template matching the specific theme call.');
$this
->assertText('theme_test_specific_suggestions__variant', 'Specific theme call is added to the suggestions array.');
\Drupal::service('module_installer')
->install(array(
'theme_suggestions_test',
));
$this
->resetAll();
$this
->drupalGet('theme-test/specific-suggestion-alter');
$this
->assertText('Template overridden based on suggestion alter hook determined by the base hook.');
$this
->assertTrue(strpos($this
->getRawContent(), 'theme_test_specific_suggestions__variant') < strpos($this
->getRawContent(), 'theme_test_specific_suggestions__variant__foo'), 'Specific theme call is added to the suggestions array before the suggestions alter hook.');
}
function testThemeFunctionSuggestionsAlter() {
$this
->drupalGet('theme-test/function-suggestion-alter');
$this
->assertText('Original theme function.');
$this
->config('system.theme')
->set('default', 'test_theme')
->save();
$this
->drupalGet('theme-test/function-suggestion-alter');
$this
->assertText('Theme function overridden based on new theme suggestion provided by the test_theme theme.');
\Drupal::service('module_installer')
->install(array(
'theme_suggestions_test',
));
$this
->resetAll();
$this
->drupalGet('theme-test/function-suggestion-alter');
$this
->assertText('Theme function overridden based on new theme suggestion provided by a module.');
}
public function testSuggestionsAlterInclude() {
$this
->drupalGet('theme-test/suggestion-alter-include');
$this
->assertText('Original function before altering theme suggestions.');
\Drupal::service('module_installer')
->install(array(
'theme_suggestions_test',
));
$this
->resetAll();
$this
->drupalGet('theme-test/suggestion-alter-include');
$this
->assertText('Function suggested via suggestion alter hook found in include file.', 'Include file loaded for initial request.');
$this
->drupalGet('theme-test/suggestion-alter-include');
$this
->assertText('Function suggested via suggestion alter hook found in include file.', 'Include file loaded for second request.');
}
function testExecutionOrder() {
$this
->config('system.theme')
->set('default', 'test_theme')
->save();
\Drupal::service('module_installer')
->install(array(
'theme_suggestions_test',
));
$this
->resetAll();
$this
->drupalGet('theme-test/suggestion-alter');
$expected = array(
'theme_suggestions_test_theme_suggestions_alter() executed.',
'theme_suggestions_test_theme_suggestions_theme_test_suggestions_alter() executed.',
'theme_test_theme_suggestions_alter() executed.',
'theme_test_theme_suggestions_theme_test_suggestions_alter() executed.',
'test_theme_theme_suggestions_alter() executed.',
'test_theme_theme_suggestions_theme_test_suggestions_alter() executed.',
);
$content = preg_replace('/\\s+/', ' ', Xss::filter($this->content, array()));
$this
->assert(strpos($content, implode(' ', $expected)) !== FALSE, 'Suggestion alter hooks executed in the expected order.');
}
}