3 namespace Drupal\Tests\system\Functional\Theme;
5 use Drupal\Component\Serialization\Json;
6 use Drupal\Tests\BrowserTestBase;
7 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
8 use Symfony\Component\HttpFoundation\Request;
9 use Symfony\Component\Routing\Route;
12 * Tests low-level theme functions.
16 class ThemeTest extends BrowserTestBase {
21 public static $modules = ['theme_test', 'node'];
26 protected function setUp() {
28 \Drupal::service('theme_handler')->install(['test_theme']);
32 * Ensures preprocess functions run even for suggestion implementations.
34 * The theme hook used by this test has its base preprocess function in a
35 * separate file, so this test also ensures that that file is correctly loaded
38 public function testPreprocessForSuggestions() {
39 // Test with both an unprimed and primed theme registry.
40 drupal_theme_rebuild();
41 for ($i = 0; $i < 2; $i++) {
42 $this->drupalGet('theme-test/suggestion');
43 $this->assertText('Theme hook implementor=test_theme_theme_test__suggestion(). Foo=template_preprocess_theme_test', 'Theme hook suggestion ran with data available from a preprocess function for the base hook.');
48 * Tests the priority of some theme negotiators.
50 public function testNegotiatorPriorities() {
51 $this->drupalGet('theme-test/priority');
53 // Ensure that the custom theme negotiator was not able to set the theme.
54 $this->assertNoText('Theme hook implementor=test_theme_theme_test__suggestion(). Foo=template_preprocess_theme_test', 'Theme hook suggestion ran with data available from a preprocess function for the base hook.');
58 * Ensures that non-HTML requests never initialize themes.
60 public function testThemeOnNonHtmlRequest() {
61 $this->drupalGet('theme-test/non-html');
62 $json = Json::decode($this->getSession()->getPage()->getContent());
63 $this->assertFalse($json['theme_initialized']);
67 * Ensure page-front template suggestion is added when on front page.
69 public function testFrontPageThemeSuggestion() {
70 // Set the current route to user.login because theme_get_suggestions() will
71 // query it to see if we are on the front page.
72 $request = Request::create('/user/login');
73 $request->attributes->set(RouteObjectInterface::ROUTE_NAME, 'user.login');
74 $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/user/login'));
75 \Drupal::requestStack()->push($request);
76 $this->config('system.site')->set('page.front', '/user/login')->save();
77 $suggestions = theme_get_suggestions(['user', 'login'], 'page');
78 // Set it back to not annoy the batch runner.
79 \Drupal::requestStack()->pop();
80 $this->assertTrue(in_array('page__front', $suggestions), 'Front page template was suggested.');
84 * Ensures a theme's .info.yml file is able to override a module CSS file from being added to the page.
86 * @see test_theme.info.yml
88 public function testCSSOverride() {
89 // Reuse the same page as in testPreprocessForSuggestions(). We're testing
90 // what is output to the HTML HEAD based on what is in a theme's .info.yml
91 // file, so it doesn't matter what page we get, as long as it is themed with
92 // the test theme. First we test with CSS aggregation disabled.
93 $config = $this->config('system.performance');
94 $config->set('css.preprocess', 0);
96 $this->drupalGet('theme-test/suggestion');
97 // We add a "?" to the assertion, because drupalSettings may include
98 // information about the file; we only really care about whether it appears
99 // in a LINK or STYLE tag, for which Drupal always adds a query string for
101 $this->assertSession()->responseNotContains('js.module.css?');
103 // Also test with aggregation enabled, simply ensuring no PHP errors are
104 // triggered during drupal_build_css_cache() when a source file doesn't
105 // exist. Then allow remaining tests to continue with aggregation disabled
107 $config->set('css.preprocess', 1);
109 $this->drupalGet('theme-test/suggestion');
110 $config->set('css.preprocess', 0);
115 * Ensures a themes template is overridable based on the 'template' filename.
117 public function testTemplateOverride() {
118 $this->config('system.theme')
119 ->set('default', 'test_theme')
121 $this->drupalGet('theme-test/template-test');
122 $this->assertText('Success: Template overridden.', 'Template overridden by defined \'template\' filename.');
126 * Ensures a theme template can override a theme function.
128 public function testFunctionOverride() {
129 $this->drupalGet('theme-test/function-template-overridden');
130 $this->assertText('Success: Template overrides theme function.', 'Theme function overridden by test_theme template.');
134 * Tests that the page variable is not prematurely flattened.
136 * Some modules check the page array in template_preprocess_html(), so we
137 * ensure that it has not been rendered prematurely.
139 public function testPreprocessHtml() {
140 $this->drupalGet('');
141 $attributes = $this->xpath('/body[@theme_test_page_variable="Page variable is an array."]');
142 $this->assertTrue(count($attributes) == 1, 'In template_preprocess_html(), the page variable is still an array (not rendered yet).');
143 $this->assertText('theme test page bottom markup', 'Modules are able to set the page bottom region.');
147 * Tests that region attributes can be manipulated via preprocess functions.
149 public function testRegionClass() {
150 \Drupal::service('module_installer')->install(['block', 'theme_region_test']);
153 $this->drupalPlaceBlock('system_main_block');
154 $this->drupalGet('');
155 $elements = $this->cssSelect(".region-sidebar-first.new_class");
156 $this->assertEqual(count($elements), 1, 'New class found.');
160 * Ensures suggestion preprocess functions run for default implementations.
162 * The theme hook used by this test has its base preprocess function in a
163 * separate file, so this test also ensures that that file is correctly loaded
166 public function testSuggestionPreprocessForDefaults() {
167 $this->config('system.theme')->set('default', 'test_theme')->save();
168 // Test with both an unprimed and primed theme registry.
169 drupal_theme_rebuild();
170 for ($i = 0; $i < 2; $i++) {
171 $this->drupalGet('theme-test/preprocess-suggestions');
172 $items = $this->cssSelect('.suggestion');
180 foreach ($expected_values as $key => $value) {
181 $this->assertEqual((string) $value, $items[$key]->getText());