Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[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     $this->assertNoText('js.module.css', 'The theme\'s .info.yml file is able to override a module CSS file from being added to the page.');
98
99     // Also test with aggregation enabled, simply ensuring no PHP errors are
100     // triggered during drupal_build_css_cache() when a source file doesn't
101     // exist. Then allow remaining tests to continue with aggregation disabled
102     // by default.
103     $config->set('css.preprocess', 1);
104     $config->save();
105     $this->drupalGet('theme-test/suggestion');
106     $config->set('css.preprocess', 0);
107     $config->save();
108   }
109
110   /**
111    * Ensures a themes template is overridable based on the 'template' filename.
112    */
113   public function testTemplateOverride() {
114     $this->config('system.theme')
115       ->set('default', 'test_theme')
116       ->save();
117     $this->drupalGet('theme-test/template-test');
118     $this->assertText('Success: Template overridden.', 'Template overridden by defined \'template\' filename.');
119   }
120
121   /**
122    * Ensures a theme template can override a theme function.
123    */
124   public function testFunctionOverride() {
125     $this->drupalGet('theme-test/function-template-overridden');
126     $this->assertText('Success: Template overrides theme function.', 'Theme function overridden by test_theme template.');
127   }
128
129   /**
130    * Tests that the page variable is not prematurely flattened.
131    *
132    * Some modules check the page array in template_preprocess_html(), so we
133    * ensure that it has not been rendered prematurely.
134    */
135   public function testPreprocessHtml() {
136     $this->drupalGet('');
137     $attributes = $this->xpath('/body[@theme_test_page_variable="Page variable is an array."]');
138     $this->assertTrue(count($attributes) == 1, 'In template_preprocess_html(), the page variable is still an array (not rendered yet).');
139     $this->assertText('theme test page bottom markup', 'Modules are able to set the page bottom region.');
140   }
141
142   /**
143    * Tests that region attributes can be manipulated via preprocess functions.
144    */
145   public function testRegionClass() {
146     \Drupal::service('module_installer')->install(['block', 'theme_region_test']);
147
148     // Place a block.
149     $this->drupalPlaceBlock('system_main_block');
150     $this->drupalGet('');
151     $elements = $this->cssSelect(".region-sidebar-first.new_class");
152     $this->assertEqual(count($elements), 1, 'New class found.');
153   }
154
155   /**
156    * Ensures suggestion preprocess functions run for default implementations.
157    *
158    * The theme hook used by this test has its base preprocess function in a
159    * separate file, so this test also ensures that that file is correctly loaded
160    * when needed.
161    */
162   public function testSuggestionPreprocessForDefaults() {
163     $this->config('system.theme')->set('default', 'test_theme')->save();
164     // Test with both an unprimed and primed theme registry.
165     drupal_theme_rebuild();
166     for ($i = 0; $i < 2; $i++) {
167       $this->drupalGet('theme-test/preprocess-suggestions');
168       $items = $this->cssSelect('.suggestion');
169       $expected_values = [
170         'Suggestion',
171         'Kitten',
172         'Monkey',
173         'Kitten',
174         'Flamingo',
175       ];
176       foreach ($expected_values as $key => $value) {
177         $this->assertEqual((string) $value, $items[$key]->getText());
178       }
179     }
180   }
181
182 }