1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
<?php
// $Id$
/**
* @file
* Tests for the theme API.
*/
/**
* Unit tests for the Theme API.
*/
class ThemeUnitTest extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Theme API',
'description' => 'Test low-level theme functions.',
'group' => 'Theme',
);
}
function setUp() {
parent::setUp('theme_test');
theme_enable(array('test_theme'));
}
/**
* Test function theme_get_suggestions() for SA-CORE-2009-003.
*/
function testThemeSuggestions() {
// Set the front page as something random otherwise the CLI
// test runner fails.
variable_set('site_frontpage', 'nobody-home');
$args = array('node', '1', 'edit');
$suggestions = theme_get_suggestions($args, 'page');
$this->assertEqual($suggestions, array('page__node', 'page__node__%', 'page__node__1', 'page__node__edit'), t('Found expected node edit page suggestions'));
// Check attack vectors.
$args = array('node', '\\1');
$suggestions = theme_get_suggestions($args, 'page');
$this->assertEqual($suggestions, array('page__node', 'page__node__%', 'page__node__1'), t('Removed invalid \\ from suggestions'));
$args = array('node', '1/');
$suggestions = theme_get_suggestions($args, 'page');
$this->assertEqual($suggestions, array('page__node', 'page__node__%', 'page__node__1'), t('Removed invalid / from suggestions'));
$args = array('node', "1\0");
$suggestions = theme_get_suggestions($args, 'page');
$this->assertEqual($suggestions, array('page__node', 'page__node__%', 'page__node__1'), t('Removed invalid \\0 from suggestions'));
}
/**
* Preprocess functions for the base hook should run even for suggestion implementations.
*/
function testPreprocessForSuggestions() {
$this->drupalGet('theme-test/suggestion');
$this->assertText('test_theme_breadcrumb__suggestion: 1', t('Theme hook suggestion ran with data available from a preprocess function for the base hook.'));
}
/**
* Ensure page-front template suggestion is added when on front page.
*/
function testFrontPageThemeSuggestion() {
$q = $_GET['q'];
// Set $_GET['q'] to node because theme_get_suggestions() will query it to
// see if we are on the front page.
$_GET['q'] = variable_get('site_frontpage', 'node');
$suggestions = theme_get_suggestions(explode('/', $_GET['q']), 'page');
// Set it back to not annoy the batch runner.
$_GET['q'] = $q;
$this->assertTrue(in_array('page__front', $suggestions), t('Front page template was suggested.'));
}
/**
* Ensures theme hook_*_alter() implementations can run before anything is rendered.
*/
function testAlter() {
$this->drupalGet('theme-test/alter');
$this->assertText('The altered data is test_theme_theme_test_alter_alter was invoked.', t('The theme was able to implement an alter hook during page building before anything was rendered.'));
}
/**
* Ensures a theme's .info file is able to override a module CSS file from being added to the page.
*
* @see test_theme.info
*/
function testCSSOverride() {
// Reuse the same page as in testPreprocessForSuggestions(). We're testing
// what is output to the HTML HEAD based on what is in a theme's .info file,
// so it doesn't matter what page we get, as long as it is themed with the
// test theme. First we test with CSS aggregation disabled.
variable_set('preprocess_css', 0);
$this->drupalGet('theme-test/suggestion');
$this->assertNoText('system.base.css', t('The theme\'s .info file is able to override a module CSS file from being added to the page.'));
// Also test with aggregation enabled, simply ensuring no PHP errors are
// triggered during drupal_build_css_cache() when a source file doesn't
// exist. Then allow remaining tests to continue with aggregation disabled
// by default.
variable_set('preprocess_css', 1);
$this->drupalGet('theme-test/suggestion');
variable_set('preprocess_css', 0);
}
}
/**
* Unit tests for theme_table().
*/
class ThemeTableUnitTest extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Theme Table',
'description' => 'Tests built-in theme functions.',
'group' => 'Theme',
);
}
/**
* Tableheader.js provides 'sticky' table headers, and is included by default.
*/
function testThemeTableStickyHeaders() {
$header = array('one', 'two', 'three');
$rows = array(array(1,2,3), array(4,5,6), array(7,8,9));
$this->content = theme('table', array('header' => $header, 'rows' => $rows));
$js = drupal_add_js();
$this->assertTrue(isset($js['misc/tableheader.js']), t('tableheader.js was included when $sticky = TRUE.'));
$this->assertRaw('sticky-enabled', t('Table has a class of sticky-enabled when $sticky = TRUE.'));
drupal_static_reset('drupal_add_js');
}
/**
* If $sticky is FALSE, no tableheader.js should be included.
*/
function testThemeTableNoStickyHeaders() {
$header = array('one', 'two', 'three');
$rows = array(array(1,2,3), array(4,5,6), array(7,8,9));
$attributes = array();
$caption = NULL;
$colgroups = array();
$this->content = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => $attributes, 'caption' => $caption, 'colgroups' => $colgroups, 'sticky' => FALSE));
$js = drupal_add_js();
$this->assertFalse(isset($js['misc/tableheader.js']), t('tableheader.js was not included because $sticky = FALSE.'));
$this->assertNoRaw('sticky-enabled', t('Table does not have a class of sticky-enabled because $sticky = FALSE.'));
drupal_static_reset('drupal_add_js');
}
/**
* Tests that the table header is printed correctly even if there are no rows,
* and that the empty text is displayed correctly.
*/
function testThemeTableWithEmptyMessage() {
$header = array(
t('Header 1'),
array(
'data' => t('Header 2'),
'colspan' => 2,
),
);
$this->content = theme('table', array('header' => $header, 'rows' => array(), 'empty' => t('No strings available.')));
$this->assertRaw('<tr class="odd"><td colspan="3" class="empty message">No strings available.</td>', t('Correct colspan was set on empty message.'));
$this->assertRaw('<thead><tr><th>Header 1</th>', t('Table header was printed.'));
}
}
/**
* Unit tests for theme_item_list().
*/
class ThemeItemListUnitTest extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Theme item list',
'description' => 'Test the theme_item_list() function.',
'group' => 'Theme',
);
}
/**
* Test nested list rendering.
*/
function testNestedList() {
$items = array('a', array('data' => 'b', 'children' => array('c', 'd')), 'e');
$expected = '<div class="item-list"><ul><li class="first">a</li>
<li>b<div class="item-list"><ul><li class="first">c</li>
<li class="last">d</li>
</ul></div></li>
<li class="last">e</li>
</ul></div>';
$output = theme('item_list', array('items' => $items));
$this->assertIdentical($expected, $output, 'Nested list is rendered correctly.');
}
}
/**
* Functional test for initialization of the theme system in hook_init().
*/
class ThemeHookInitUnitTest extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Theme initialization in hook_init()',
'description' => 'Tests that the theme system can be correctly initialized in hook_init().',
'group' => 'Theme',
);
}
function setUp() {
parent::setUp('theme_test');
}
/**
* Test that the theme system can generate output when called by hook_init().
*/
function testThemeInitializationHookInit() {
$this->drupalGet('theme-test/hook-init');
$this->assertRaw('Themed output generated in hook_init()', t('Themed output generated in hook_init() correctly appears on the page.'));
$this->assertRaw('bartik/css/style.css', t("The default theme's CSS appears on the page when the theme system is initialized in hook_init()."));
}
}
/**
* Tests autocompletion not loading registry.
*/
class ThemeFastTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Theme fast initialization',
'description' => 'Test that autocompletion does not load the registry.',
'group' => 'Theme'
);
}
function setUp() {
parent::setUp('theme_test');
$this->account = $this->drupalCreateUser(array('access user profiles'));
}
/**
* Tests access to user autocompletion and verify the correct results.
*/
function testUserAutocomplete() {
$this->drupalLogin($this->account);
$this->drupalGet('user/autocomplete/' . $this->account->name);
$this->assertText('registry not initialized', t('The registry was not initialized'));
}
}
|