Pull merge.
[yaffs-website] / web / core / modules / system / tests / src / Functional / Theme / ThemeTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Theme;
4
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;
10
11 /**
12  * Tests low-level theme functions.
13  *
14  * @group Theme
15  */
16 class ThemeTest extends BrowserTestBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   public static $modules = ['theme_test', 'node'];
22
23   /**
24    * {@inheritdoc}
25    */
26   protected function setUp() {
27     parent::setUp();
28     \Drupal::service('theme_handler')->install(['test_theme']);
29   }
30
31   /**
32    * Ensures preprocess functions run even for suggestion implementations.
33    *
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
36    * when needed.
37    */
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.');
44     }
45   }
46
47   /**
48    * Tests the priority of some theme negotiators.
49    */
50   public function testNegotiatorPriorities() {
51     $this->drupalGet('theme-test/priority');
52
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.');
55   }
56
57   /**
58    * Ensures that non-HTML requests never initialize themes.
59    */
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']);
64   }
65
66   /**
67    * Ensure page-front template suggestion is added when on front page.
68    */
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.');
81   }
82
83   /**
84    * Ensures a theme's .info.yml file is able to override a module CSS file from being added to the page.
85    *
86    * @see test_theme.info.yml
87    */
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);
95     $config->save();
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
100     // cache control.
101     $this->assertSession()->responseNotContains('js.module.css?');
102
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
106     // by default.
107     $config->set('css.preprocess', 1);
108     $config->save();
109     $this->drupalGet('theme-test/suggestion');
110     $config->set('css.preprocess', 0);
111     $config->save();
112   }
113
114   /**
115    * Ensures a themes template is overridable based on the 'template' filename.
116    */
117   public function testTemplateOverride() {
118     $this->config('system.theme')
119       ->set('default', 'test_theme')
120       ->save();
121     $this->drupalGet('theme-test/template-test');
122     $this->assertText('Success: Template overridden.', 'Template overridden by defined \'template\' filename.');
123   }
124
125   /**
126    * Ensures a theme template can override a theme function.
127    */
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.');
131   }
132
133   /**
134    * Tests that the page variable is not prematurely flattened.
135    *
136    * Some modules check the page array in template_preprocess_html(), so we
137    * ensure that it has not been rendered prematurely.
138    */
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.');
144   }
145
146   /**
147    * Tests that region attributes can be manipulated via preprocess functions.
148    */
149   public function testRegionClass() {
150     \Drupal::service('module_installer')->install(['block', 'theme_region_test']);
151
152     // Place a block.
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.');
157   }
158
159   /**
160    * Ensures suggestion preprocess functions run for default implementations.
161    *
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
164    * when needed.
165    */
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');
173       $expected_values = [
174         'Suggestion',
175         'Kitten',
176         'Monkey',
177         'Kitten',
178         'Flamingo',
179       ];
180       foreach ($expected_values as $key => $value) {
181         $this->assertEqual((string) $value, $items[$key]->getText());
182       }
183     }
184   }
185
186 }