Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\system\Tests\Theme;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Utility\Xss;
|
Chris@0
|
6 use Drupal\simpletest\WebTestBase;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Tests theme suggestion alter hooks.
|
Chris@0
|
10 *
|
Chris@0
|
11 * @group Theme
|
Chris@0
|
12 */
|
Chris@0
|
13 class ThemeSuggestionsAlterTest extends WebTestBase {
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Modules to enable.
|
Chris@0
|
17 *
|
Chris@0
|
18 * @var array
|
Chris@0
|
19 */
|
Chris@0
|
20 public static $modules = ['theme_test'];
|
Chris@0
|
21
|
Chris@0
|
22 protected function setUp() {
|
Chris@0
|
23 parent::setUp();
|
Chris@0
|
24 \Drupal::service('theme_handler')->install(['test_theme']);
|
Chris@0
|
25 }
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * Tests that hooks to provide theme suggestions work.
|
Chris@0
|
29 */
|
Chris@0
|
30 public function testTemplateSuggestions() {
|
Chris@0
|
31 $this->drupalGet('theme-test/suggestion-provided');
|
Chris@0
|
32 $this->assertText('Template for testing suggestions provided by the module declaring the theme hook.');
|
Chris@0
|
33
|
Chris@0
|
34 // Install test_theme, it contains a template suggested by theme_test.module
|
Chris@0
|
35 // in theme_test_theme_suggestions_theme_test_suggestion_provided().
|
Chris@0
|
36 $this->config('system.theme')
|
Chris@0
|
37 ->set('default', 'test_theme')
|
Chris@0
|
38 ->save();
|
Chris@0
|
39
|
Chris@0
|
40 $this->drupalGet('theme-test/suggestion-provided');
|
Chris@0
|
41 $this->assertText('Template overridden based on suggestion provided by the module declaring the theme hook.');
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * Tests hook_theme_suggestions_alter().
|
Chris@0
|
46 */
|
Chris@0
|
47 public function testGeneralSuggestionsAlter() {
|
Chris@0
|
48 $this->drupalGet('theme-test/general-suggestion-alter');
|
Chris@0
|
49 $this->assertText('Original template for testing hook_theme_suggestions_alter().');
|
Chris@0
|
50
|
Chris@0
|
51 // Install test_theme and test that themes can alter template suggestions.
|
Chris@0
|
52 $this->config('system.theme')
|
Chris@0
|
53 ->set('default', 'test_theme')
|
Chris@0
|
54 ->save();
|
Chris@0
|
55 $this->drupalGet('theme-test/general-suggestion-alter');
|
Chris@0
|
56 $this->assertText('Template overridden based on new theme suggestion provided by the test_theme theme via hook_theme_suggestions_alter().');
|
Chris@0
|
57
|
Chris@0
|
58 // Enable the theme_suggestions_test module to test modules implementing
|
Chris@0
|
59 // suggestions alter hooks.
|
Chris@0
|
60 \Drupal::service('module_installer')->install(['theme_suggestions_test']);
|
Chris@0
|
61 $this->resetAll();
|
Chris@0
|
62 $this->drupalGet('theme-test/general-suggestion-alter');
|
Chris@0
|
63 $this->assertText('Template overridden based on new theme suggestion provided by a module via hook_theme_suggestions_alter().');
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 /**
|
Chris@0
|
67 * Tests that theme suggestion alter hooks work for templates.
|
Chris@0
|
68 */
|
Chris@0
|
69 public function testTemplateSuggestionsAlter() {
|
Chris@0
|
70 $this->drupalGet('theme-test/suggestion-alter');
|
Chris@0
|
71 $this->assertText('Original template for testing hook_theme_suggestions_HOOK_alter().');
|
Chris@0
|
72
|
Chris@0
|
73 // Install test_theme and test that themes can alter template suggestions.
|
Chris@0
|
74 $this->config('system.theme')
|
Chris@0
|
75 ->set('default', 'test_theme')
|
Chris@0
|
76 ->save();
|
Chris@0
|
77 $this->drupalGet('theme-test/suggestion-alter');
|
Chris@0
|
78 $this->assertText('Template overridden based on new theme suggestion provided by the test_theme theme via hook_theme_suggestions_HOOK_alter().');
|
Chris@0
|
79
|
Chris@0
|
80 // Enable the theme_suggestions_test module to test modules implementing
|
Chris@0
|
81 // suggestions alter hooks.
|
Chris@0
|
82 \Drupal::service('module_installer')->install(['theme_suggestions_test']);
|
Chris@0
|
83 $this->resetAll();
|
Chris@0
|
84 $this->drupalGet('theme-test/suggestion-alter');
|
Chris@0
|
85 $this->assertText('Template overridden based on new theme suggestion provided by a module via hook_theme_suggestions_HOOK_alter().');
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 /**
|
Chris@0
|
89 * Tests that theme suggestion alter hooks work for specific theme calls.
|
Chris@0
|
90 */
|
Chris@0
|
91 public function testSpecificSuggestionsAlter() {
|
Chris@0
|
92 // Test that the default template is rendered.
|
Chris@0
|
93 $this->drupalGet('theme-test/specific-suggestion-alter');
|
Chris@0
|
94 $this->assertText('Template for testing specific theme calls.');
|
Chris@0
|
95
|
Chris@0
|
96 $this->config('system.theme')
|
Chris@0
|
97 ->set('default', 'test_theme')
|
Chris@0
|
98 ->save();
|
Chris@0
|
99
|
Chris@0
|
100 // Test a specific theme call similar to '#theme' => 'node__article'.
|
Chris@0
|
101 $this->drupalGet('theme-test/specific-suggestion-alter');
|
Chris@0
|
102 $this->assertText('Template matching the specific theme call.');
|
Chris@0
|
103 $this->assertText('theme_test_specific_suggestions__variant', 'Specific theme call is added to the suggestions array.');
|
Chris@0
|
104
|
Chris@0
|
105 // Ensure that the base hook is used to determine the suggestion alter hook.
|
Chris@0
|
106 \Drupal::service('module_installer')->install(['theme_suggestions_test']);
|
Chris@0
|
107 $this->resetAll();
|
Chris@0
|
108 $this->drupalGet('theme-test/specific-suggestion-alter');
|
Chris@0
|
109 $this->assertText('Template overridden based on suggestion alter hook determined by the base hook.');
|
Chris@0
|
110 $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.');
|
Chris@0
|
111 }
|
Chris@0
|
112
|
Chris@0
|
113 /**
|
Chris@0
|
114 * Tests that theme suggestion alter hooks work for theme functions.
|
Chris@0
|
115 */
|
Chris@0
|
116 public function testThemeFunctionSuggestionsAlter() {
|
Chris@0
|
117 $this->drupalGet('theme-test/function-suggestion-alter');
|
Chris@0
|
118 $this->assertText('Original theme function.');
|
Chris@0
|
119
|
Chris@0
|
120 // Install test_theme and test that themes can alter theme suggestions.
|
Chris@0
|
121 $this->config('system.theme')
|
Chris@0
|
122 ->set('default', 'test_theme')
|
Chris@0
|
123 ->save();
|
Chris@0
|
124 $this->drupalGet('theme-test/function-suggestion-alter');
|
Chris@0
|
125 $this->assertText('Theme function overridden based on new theme suggestion provided by the test_theme theme.');
|
Chris@0
|
126
|
Chris@0
|
127 // Enable the theme_suggestions_test module to test modules implementing
|
Chris@0
|
128 // suggestions alter hooks.
|
Chris@0
|
129 \Drupal::service('module_installer')->install(['theme_suggestions_test']);
|
Chris@0
|
130 $this->resetAll();
|
Chris@0
|
131 $this->drupalGet('theme-test/function-suggestion-alter');
|
Chris@0
|
132 $this->assertText('Theme function overridden based on new theme suggestion provided by a module.');
|
Chris@0
|
133 }
|
Chris@0
|
134
|
Chris@0
|
135 /**
|
Chris@0
|
136 * Tests that theme suggestion alter hooks work with theme hook includes.
|
Chris@0
|
137 */
|
Chris@0
|
138 public function testSuggestionsAlterInclude() {
|
Chris@0
|
139 // Check the original theme output.
|
Chris@0
|
140 $this->drupalGet('theme-test/suggestion-alter-include');
|
Chris@0
|
141 $this->assertText('Original function before altering theme suggestions.');
|
Chris@0
|
142
|
Chris@0
|
143 // Enable theme_suggestions_test module and make two requests to make sure
|
Chris@0
|
144 // the include file is always loaded. The file will always be included for
|
Chris@0
|
145 // the first request because the theme registry is being rebuilt.
|
Chris@0
|
146 \Drupal::service('module_installer')->install(['theme_suggestions_test']);
|
Chris@0
|
147 $this->resetAll();
|
Chris@0
|
148 $this->drupalGet('theme-test/suggestion-alter-include');
|
Chris@0
|
149 $this->assertText('Function suggested via suggestion alter hook found in include file.', 'Include file loaded for initial request.');
|
Chris@0
|
150 $this->drupalGet('theme-test/suggestion-alter-include');
|
Chris@0
|
151 $this->assertText('Function suggested via suggestion alter hook found in include file.', 'Include file loaded for second request.');
|
Chris@0
|
152 }
|
Chris@0
|
153
|
Chris@0
|
154 /**
|
Chris@0
|
155 * Tests execution order of theme suggestion alter hooks.
|
Chris@0
|
156 *
|
Chris@0
|
157 * hook_theme_suggestions_alter() should fire before
|
Chris@0
|
158 * hook_theme_suggestions_HOOK_alter() within an extension (module or theme).
|
Chris@0
|
159 */
|
Chris@0
|
160 public function testExecutionOrder() {
|
Chris@0
|
161 // Install our test theme and module.
|
Chris@0
|
162 $this->config('system.theme')
|
Chris@0
|
163 ->set('default', 'test_theme')
|
Chris@0
|
164 ->save();
|
Chris@0
|
165 \Drupal::service('module_installer')->install(['theme_suggestions_test']);
|
Chris@0
|
166 $this->resetAll();
|
Chris@0
|
167
|
Chris@0
|
168 // Send two requests so that we get all the messages we've set via
|
Chris@0
|
169 // drupal_set_message().
|
Chris@0
|
170 $this->drupalGet('theme-test/suggestion-alter');
|
Chris@0
|
171 // Ensure that the order is first by extension, then for a given extension,
|
Chris@0
|
172 // the hook-specific one after the generic one.
|
Chris@0
|
173 $expected = [
|
Chris@0
|
174 'theme_suggestions_test_theme_suggestions_alter() executed.',
|
Chris@0
|
175 'theme_suggestions_test_theme_suggestions_theme_test_suggestions_alter() executed.',
|
Chris@0
|
176 'theme_test_theme_suggestions_alter() executed.',
|
Chris@0
|
177 'theme_test_theme_suggestions_theme_test_suggestions_alter() executed.',
|
Chris@0
|
178 'test_theme_theme_suggestions_alter() executed.',
|
Chris@0
|
179 'test_theme_theme_suggestions_theme_test_suggestions_alter() executed.',
|
Chris@0
|
180 ];
|
Chris@0
|
181 $content = preg_replace('/\s+/', ' ', Xss::filter($this->content, []));
|
Chris@0
|
182 $this->assert(strpos($content, implode(' ', $expected)) !== FALSE, 'Suggestion alter hooks executed in the expected order.');
|
Chris@0
|
183 }
|
Chris@0
|
184
|
Chris@0
|
185 }
|