Projet

Général

Profil

Paste
Télécharger (20,2 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / simpletest / tests / theme.test @ f7a2490e

1
<?php
2

    
3
/**
4
 * @file
5
 * Tests for the theme API.
6
 */
7

    
8
/**
9
 * Unit tests for the Theme API.
10
 */
11
class ThemeTestCase extends DrupalWebTestCase {
12
  protected $profile = 'testing';
13

    
14
  public static function getInfo() {
15
    return array(
16
      'name' => 'Theme API',
17
      'description' => 'Test low-level theme functions.',
18
      'group' => 'Theme',
19
    );
20
  }
21

    
22
  function setUp() {
23
    parent::setUp('theme_test');
24
    theme_enable(array('test_theme'));
25
  }
26

    
27
  /**
28
   * Test function theme_get_suggestions() for SA-CORE-2009-003.
29
   */
30
  function testThemeSuggestions() {
31
    // Set the front page as something random otherwise the CLI
32
    // test runner fails.
33
    variable_set('site_frontpage', 'nobody-home');
34
    $args = array('node', '1', 'edit');
35
    $suggestions = theme_get_suggestions($args, 'page');
36
    $this->assertEqual($suggestions, array('page__node', 'page__node__%', 'page__node__1', 'page__node__edit'), 'Found expected node edit page suggestions');
37
    // Check attack vectors.
38
    $args = array('node', '\\1');
39
    $suggestions = theme_get_suggestions($args, 'page');
40
    $this->assertEqual($suggestions, array('page__node', 'page__node__%', 'page__node__1'), 'Removed invalid \\ from suggestions');
41
    $args = array('node', '1/');
42
    $suggestions = theme_get_suggestions($args, 'page');
43
    $this->assertEqual($suggestions, array('page__node', 'page__node__%', 'page__node__1'), 'Removed invalid / from suggestions');
44
    $args = array('node', "1\0");
45
    $suggestions = theme_get_suggestions($args, 'page');
46
    $this->assertEqual($suggestions, array('page__node', 'page__node__%', 'page__node__1'), 'Removed invalid \\0 from suggestions');
47
    // Define path with hyphens to be used to generate suggestions.
48
    $args = array('node', '1', 'hyphen-path');
49
    $result = array('page__node', 'page__node__%', 'page__node__1', 'page__node__hyphen_path');
50
    $suggestions = theme_get_suggestions($args, 'page');
51
    $this->assertEqual($suggestions, $result, 'Found expected page suggestions for paths containing hyphens.');
52
  }
53

    
54
  /**
55
   * Ensures preprocess functions run even for suggestion implementations.
56
   *
57
   * The theme hook used by this test has its base preprocess function in a
58
   * separate file, so this test also ensures that that file is correctly loaded
59
   * when needed.
60
   */
61
  function testPreprocessForSuggestions() {
62
    // Test with both an unprimed and primed theme registry.
63
    drupal_theme_rebuild();
64
    for ($i = 0; $i < 2; $i++) {
65
      $this->drupalGet('theme-test/suggestion');
66
      $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.');
67
    }
68
  }
69

    
70
  /**
71
   * Ensure page-front template suggestion is added when on front page.
72
   */
73
  function testFrontPageThemeSuggestion() {
74
    $q = $_GET['q'];
75
    // Set $_GET['q'] to node because theme_get_suggestions() will query it to
76
    // see if we are on the front page.
77
    $_GET['q'] = variable_get('site_frontpage', 'node');
78
    $suggestions = theme_get_suggestions(explode('/', $_GET['q']), 'page');
79
    // Set it back to not annoy the batch runner.
80
    $_GET['q'] = $q;
81
    $this->assertTrue(in_array('page__front', $suggestions), 'Front page template was suggested.');
82
  }
83

    
84
  /**
85
   * Ensures theme hook_*_alter() implementations can run before anything is rendered.
86
   */
87
  function testAlter() {
88
    $this->drupalGet('theme-test/alter');
89
    $this->assertText('The altered data is test_theme_theme_test_alter_alter was invoked.', 'The theme was able to implement an alter hook during page building before anything was rendered.');
90
  }
91

    
92
  /**
93
   * Ensures a theme's .info file is able to override a module CSS file from being added to the page.
94
   *
95
   * @see test_theme.info
96
   */
97
  function testCSSOverride() {
98
    // Reuse the same page as in testPreprocessForSuggestions(). We're testing
99
    // what is output to the HTML HEAD based on what is in a theme's .info file,
100
    // so it doesn't matter what page we get, as long as it is themed with the
101
    // test theme. First we test with CSS aggregation disabled.
102
    variable_set('preprocess_css', 0);
103
    $this->drupalGet('theme-test/suggestion');
104
    $this->assertNoText('system.base.css', 'The theme\'s .info file is able to override a module CSS file from being added to the page.');
105

    
106
    // Also test with aggregation enabled, simply ensuring no PHP errors are
107
    // triggered during drupal_build_css_cache() when a source file doesn't
108
    // exist. Then allow remaining tests to continue with aggregation disabled
109
    // by default.
110
    variable_set('preprocess_css', 1);
111
    $this->drupalGet('theme-test/suggestion');
112
    variable_set('preprocess_css', 0);
113
  }
114

    
115
  /**
116
   * Ensures the theme registry is rebuilt when modules are disabled/enabled.
117
   */
118
  function testRegistryRebuild() {
119
    $this->assertIdentical(theme('theme_test_foo', array('foo' => 'a')), 'a', 'The theme registry contains theme_test_foo.');
120

    
121
    module_disable(array('theme_test'), FALSE);
122
    $this->assertIdentical(theme('theme_test_foo', array('foo' => 'b')), '', 'The theme registry does not contain theme_test_foo, because the module is disabled.');
123

    
124
    module_enable(array('theme_test'), FALSE);
125
    $this->assertIdentical(theme('theme_test_foo', array('foo' => 'c')), 'c', 'The theme registry contains theme_test_foo again after re-enabling the module.');
126
  }
127

    
128
  /**
129
   * Test the list_themes() function.
130
   */
131
  function testListThemes() {
132
    $themes = list_themes();
133
    // Check if drupal_theme_access() retrieves enabled themes properly from list_themes().
134
    $this->assertTrue(drupal_theme_access('test_theme'), 'Enabled theme detected');
135
    // Check if list_themes() returns disabled themes.
136
    $this->assertTrue(array_key_exists('test_basetheme', $themes), 'Disabled theme detected');
137
    // Check for base theme and subtheme lists.
138
    $base_theme_list = array('test_basetheme' => 'Theme test base theme');
139
    $sub_theme_list = array('test_subtheme' => 'Theme test subtheme');
140
    $this->assertIdentical($themes['test_basetheme']->sub_themes, $sub_theme_list, 'Base theme\'s object includes list of subthemes.');
141
    $this->assertIdentical($themes['test_subtheme']->base_themes, $base_theme_list, 'Subtheme\'s object includes list of base themes.');
142
    // Check for theme engine in subtheme.
143
    $this->assertIdentical($themes['test_subtheme']->engine, 'phptemplate', 'Subtheme\'s object includes the theme engine.');
144
    // Check for theme engine prefix.
145
    $this->assertIdentical($themes['test_basetheme']->prefix, 'phptemplate', 'Base theme\'s object includes the theme engine prefix.');
146
    $this->assertIdentical($themes['test_subtheme']->prefix, 'phptemplate', 'Subtheme\'s object includes the theme engine prefix.');
147
  }
148

    
149
  /**
150
   * Test the theme_get_setting() function.
151
   */
152
  function testThemeGetSetting() {
153
    $GLOBALS['theme_key'] = 'test_theme';
154
    $this->assertIdentical(theme_get_setting('theme_test_setting'), 'default value', 'theme_get_setting() uses the default theme automatically.');
155
    $this->assertNotEqual(theme_get_setting('subtheme_override', 'test_basetheme'), theme_get_setting('subtheme_override', 'test_subtheme'), 'Base theme\'s default settings values can be overridden by subtheme.');
156
    $this->assertIdentical(theme_get_setting('basetheme_only', 'test_subtheme'), 'base theme value', 'Base theme\'s default settings values are inherited by subtheme.');
157
  }
158
}
159

    
160
/**
161
 * Unit tests for theme_table().
162
 */
163
class ThemeTableTestCase extends DrupalWebTestCase {
164
  public static function getInfo() {
165
    return array(
166
      'name' => 'Theme Table',
167
      'description' => 'Tests built-in theme functions.',
168
      'group' => 'Theme',
169
    );
170
  }
171

    
172
  /**
173
   * Tableheader.js provides 'sticky' table headers, and is included by default.
174
   */
175
  function testThemeTableStickyHeaders() {
176
    $header = array('one', 'two', 'three');
177
    $rows = array(array(1,2,3), array(4,5,6), array(7,8,9));
178
    $this->content = theme('table', array('header' => $header, 'rows' => $rows));
179
    $js = drupal_add_js();
180
    $this->assertTrue(isset($js['misc/tableheader.js']), 'tableheader.js was included when $sticky = TRUE.');
181
    $this->assertRaw('sticky-enabled', 'Table has a class of sticky-enabled when $sticky = TRUE.');
182
    drupal_static_reset('drupal_add_js');
183
  }
184

    
185
  /**
186
   * If $sticky is FALSE, no tableheader.js should be included.
187
   */
188
  function testThemeTableNoStickyHeaders() {
189
    $header = array('one', 'two', 'three');
190
    $rows = array(array(1,2,3), array(4,5,6), array(7,8,9));
191
    $attributes = array();
192
    $caption = NULL;
193
    $colgroups = array();
194
    $this->content = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => $attributes, 'caption' => $caption, 'colgroups' => $colgroups, 'sticky' => FALSE));
195
    $js = drupal_add_js();
196
    $this->assertFalse(isset($js['misc/tableheader.js']), 'tableheader.js was not included because $sticky = FALSE.');
197
    $this->assertNoRaw('sticky-enabled', 'Table does not have a class of sticky-enabled because $sticky = FALSE.');
198
    drupal_static_reset('drupal_add_js');
199
  }
200

    
201
  /**
202
   * Tests that the table header is printed correctly even if there are no rows,
203
   * and that the empty text is displayed correctly.
204
   */
205
  function testThemeTableWithEmptyMessage() {
206
    $header = array(
207
      t('Header 1'),
208
      array(
209
        'data' => t('Header 2'),
210
        'colspan' => 2,
211
      ),
212
    );
213
    $this->content = theme('table', array('header' => $header, 'rows' => array(), 'empty' => t('No strings available.')));
214
    $this->assertRaw('<tr class="odd"><td colspan="3" class="empty message">No strings available.</td>', 'Correct colspan was set on empty message.');
215
    $this->assertRaw('<thead><tr><th>Header 1</th>', 'Table header was printed.');
216
  }
217

    
218
  /**
219
   * Tests that the 'no_striping' option works correctly.
220
   */
221
  function testThemeTableWithNoStriping() {
222
    $rows = array(
223
      array(
224
        'data' => array(1),
225
        'no_striping' => TRUE,
226
      ),
227
    );
228
    $this->content = theme('table', array('rows' => $rows));
229
    $this->assertNoRaw('class="odd"', 'Odd/even classes were not added because $no_striping = TRUE.');
230
    $this->assertNoRaw('no_striping', 'No invalid no_striping HTML attribute was printed.');
231
  }
232
}
233

    
234
/**
235
 * Unit tests for theme_item_list().
236
 */
237
class ThemeItemListUnitTest extends DrupalWebTestCase {
238
  public static function getInfo() {
239
    return array(
240
      'name' => 'Theme item list',
241
      'description' => 'Test the theme_item_list() function.',
242
      'group' => 'Theme',
243
    );
244
  }
245

    
246
  /**
247
   * Test item list rendering.
248
   */
249
  function testItemList() {
250
    $items = array('a', array('data' => 'b', 'children' => array('c' => 'c', 'd' => 'd', 'e' => 'e')), 'f');
251
    $expected = '<div class="item-list"><ul><li class="first">a</li>
252
<li>b<div class="item-list"><ul><li class="first">c</li>
253
<li>d</li>
254
<li class="last">e</li>
255
</ul></div></li>
256
<li class="last">f</li>
257
</ul></div>';
258
    $output = theme('item_list', array('items' => $items));
259
    $this->assertIdentical($expected, $output, 'Item list is rendered correctly.');
260
  }
261
}
262

    
263
/**
264
 * Unit tests for theme_links().
265
 */
266
class ThemeLinksTest extends DrupalWebTestCase {
267
  public static function getInfo() {
268
    return array(
269
      'name' => 'Links',
270
      'description' => 'Test the theme_links() function and rendering groups of links.',
271
      'group' => 'Theme',
272
    );
273
  }
274

    
275
  /**
276
   * Test the use of drupal_pre_render_links() on a nested array of links.
277
   */
278
  function testDrupalPreRenderLinks() {
279
    // Define the base array to be rendered, containing a variety of different
280
    // kinds of links.
281
    $base_array = array(
282
      '#theme' => 'links',
283
      '#pre_render' => array('drupal_pre_render_links'),
284
      '#links' => array(
285
        'parent_link' => array(
286
          'title' => 'Parent link original',
287
          'href' => 'parent-link-original',
288
        ),
289
      ),
290
      'first_child' => array(
291
        '#theme' => 'links',
292
        '#links' => array(
293
          // This should be rendered if 'first_child' is rendered separately,
294
          // but ignored if the parent is being rendered (since it duplicates
295
          // one of the parent's links).
296
          'parent_link' => array(
297
            'title' => 'Parent link copy',
298
            'href' => 'parent-link-copy',
299
          ),
300
          // This should always be rendered.
301
          'first_child_link' => array(
302
            'title' => 'First child link',
303
            'href' => 'first-child-link',
304
          ),
305
        ),
306
      ),
307
      // This should always be rendered as part of the parent.
308
      'second_child' => array(
309
        '#theme' => 'links',
310
        '#links' => array(
311
          'second_child_link' => array(
312
            'title' => 'Second child link',
313
            'href' => 'second-child-link',
314
          ),
315
        ),
316
      ),
317
      // This should never be rendered, since the user does not have access to
318
      // it.
319
      'third_child' => array(
320
        '#theme' => 'links',
321
        '#links' => array(
322
          'third_child_link' => array(
323
            'title' => 'Third child link',
324
            'href' => 'third-child-link',
325
          ),
326
        ),
327
        '#access' => FALSE,
328
      ),
329
    );
330

    
331
    // Start with a fresh copy of the base array, and try rendering the entire
332
    // thing. We expect a single <ul> with appropriate links contained within
333
    // it.
334
    $render_array = $base_array;
335
    $html = drupal_render($render_array);
336
    $dom = new DOMDocument();
337
    $dom->loadHTML($html);
338
    $this->assertEqual($dom->getElementsByTagName('ul')->length, 1, 'One "ul" tag found in the rendered HTML.');
339
    $list_elements = $dom->getElementsByTagName('li');
340
    $this->assertEqual($list_elements->length, 3, 'Three "li" tags found in the rendered HTML.');
341
    $this->assertEqual($list_elements->item(0)->nodeValue, 'Parent link original', 'First expected link found.');
342
    $this->assertEqual($list_elements->item(1)->nodeValue, 'First child link', 'Second expected link found.');
343
    $this->assertEqual($list_elements->item(2)->nodeValue, 'Second child link', 'Third expected link found.');
344
    $this->assertIdentical(strpos($html, 'Parent link copy'), FALSE, '"Parent link copy" link not found.');
345
    $this->assertIdentical(strpos($html, 'Third child link'), FALSE, '"Third child link" link not found.');
346

    
347
    // Now render 'first_child', followed by the rest of the links, and make
348
    // sure we get two separate <ul>'s with the appropriate links contained
349
    // within each.
350
    $render_array = $base_array;
351
    $child_html = drupal_render($render_array['first_child']);
352
    $parent_html = drupal_render($render_array);
353
    // First check the child HTML.
354
    $dom = new DOMDocument();
355
    $dom->loadHTML($child_html);
356
    $this->assertEqual($dom->getElementsByTagName('ul')->length, 1, 'One "ul" tag found in the rendered child HTML.');
357
    $list_elements = $dom->getElementsByTagName('li');
358
    $this->assertEqual($list_elements->length, 2, 'Two "li" tags found in the rendered child HTML.');
359
    $this->assertEqual($list_elements->item(0)->nodeValue, 'Parent link copy', 'First expected link found.');
360
    $this->assertEqual($list_elements->item(1)->nodeValue, 'First child link', 'Second expected link found.');
361
    // Then check the parent HTML.
362
    $dom = new DOMDocument();
363
    $dom->loadHTML($parent_html);
364
    $this->assertEqual($dom->getElementsByTagName('ul')->length, 1, 'One "ul" tag found in the rendered parent HTML.');
365
    $list_elements = $dom->getElementsByTagName('li');
366
    $this->assertEqual($list_elements->length, 2, 'Two "li" tags found in the rendered parent HTML.');
367
    $this->assertEqual($list_elements->item(0)->nodeValue, 'Parent link original', 'First expected link found.');
368
    $this->assertEqual($list_elements->item(1)->nodeValue, 'Second child link', 'Second expected link found.');
369
    $this->assertIdentical(strpos($parent_html, 'First child link'), FALSE, '"First child link" link not found.');
370
    $this->assertIdentical(strpos($parent_html, 'Third child link'), FALSE, '"Third child link" link not found.');
371
  }
372
}
373

    
374
/**
375
 * Functional test for initialization of the theme system in hook_init().
376
 */
377
class ThemeHookInitTestCase extends DrupalWebTestCase {
378
  public static function getInfo() {
379
    return array(
380
      'name' => 'Theme initialization in hook_init()',
381
      'description' => 'Tests that the theme system can be correctly initialized in hook_init().',
382
      'group' => 'Theme',
383
    );
384
  }
385

    
386
  function setUp() {
387
    parent::setUp('theme_test');
388
  }
389

    
390
  /**
391
   * Test that the theme system can generate output when called by hook_init().
392
   */
393
  function testThemeInitializationHookInit() {
394
    $this->drupalGet('theme-test/hook-init');
395
    $this->assertRaw('Themed output generated in hook_init()', 'Themed output generated in hook_init() correctly appears on the page.');
396
    $this->assertRaw('bartik/css/style.css', "The default theme's CSS appears on the page when the theme system is initialized in hook_init().");
397
  }
398
}
399

    
400
/**
401
 * Tests autocompletion not loading registry.
402
 */
403
class ThemeFastTestCase extends DrupalWebTestCase {
404
  public static function getInfo() {
405
    return array(
406
      'name' => 'Theme fast initialization',
407
      'description' => 'Test that autocompletion does not load the registry.',
408
      'group' => 'Theme'
409
    );
410
  }
411

    
412
  function setUp() {
413
    parent::setUp('theme_test');
414
    $this->account = $this->drupalCreateUser(array('access user profiles'));
415
  }
416

    
417
  /**
418
   * Tests access to user autocompletion and verify the correct results.
419
   */
420
  function testUserAutocomplete() {
421
    $this->drupalLogin($this->account);
422
    $this->drupalGet('user/autocomplete/' . $this->account->name);
423
    $this->assertText('registry not initialized', 'The registry was not initialized');
424
  }
425
}
426

    
427
/**
428
 * Unit tests for theme_html_tag().
429
 */
430
class ThemeHtmlTag extends DrupalUnitTestCase {
431
  public static function getInfo() {
432
    return array(
433
      'name' => 'Theme HTML Tag',
434
      'description' => 'Tests theme_html_tag() built-in theme functions.',
435
      'group' => 'Theme',
436
    );
437
  }
438

    
439
  /**
440
   * Test function theme_html_tag()
441
   */
442
  function testThemeHtmlTag() {
443
    // Test auto-closure meta tag generation
444
    $tag['element'] = array('#tag' => 'meta', '#attributes' => array('name' => 'description', 'content' => 'Drupal test'));
445
    $this->assertEqual('<meta name="description" content="Drupal test" />'."\n", theme_html_tag($tag), 'Test auto-closure meta tag generation.');
446

    
447
    // Test title tag generation
448
    $tag['element'] = array('#tag' => 'title', '#value' => 'title test');
449
    $this->assertEqual('<title>title test</title>'."\n", theme_html_tag($tag), 'Test title tag generation.');
450
  }
451
}
452

    
453
/**
454
 * Tests for the ThemeRegistry class.
455
 */
456
class ThemeRegistryTestCase extends DrupalWebTestCase {
457
  public static function getInfo() {
458
    return array(
459
      'name' => 'ThemeRegistry',
460
      'description' => 'Tests the behavior of the ThemeRegistry class',
461
      'group' => 'Theme',
462
    );
463
  }
464
  function setUp() {
465
    parent::setUp('theme_test');
466
  }
467

    
468
  /**
469
   * Tests the behavior of the theme registry class.
470
   */
471
  function testRaceCondition() {
472
    $_SERVER['REQUEST_METHOD'] = 'GET';
473
    $cid = 'test_theme_registry';
474

    
475
    // Directly instantiate the theme registry, this will cause a base cache
476
    // entry to be written in __construct().
477
    $registry = new ThemeRegistry($cid, 'cache');
478

    
479
    $this->assertTrue(cache_get($cid), 'Cache entry was created.');
480

    
481
    // Trigger a cache miss for an offset.
482
    $this->assertTrue($registry['theme_test_template_test'], 'Offset was returned correctly from the theme registry.');
483
    // This will cause the ThemeRegistry class to write an updated version of
484
    // the cache entry when it is destroyed, usually at the end of the request.
485
    // Before that happens, manually delete the cache entry we created earlier
486
    // so that the new entry is written from scratch.
487
    cache_clear_all($cid, 'cache');
488

    
489
    // Destroy the class so that it triggers a cache write for the offset.
490
    unset($registry);
491

    
492
    $this->assertTrue(cache_get($cid), 'Cache entry was created.');
493

    
494
    // Create a new instance of the class. Confirm that both the offset
495
    // requested previously, and one that has not yet been requested are both
496
    // available.
497
    $registry = new ThemeRegistry($cid, 'cache');
498

    
499
    $this->assertTrue($registry['theme_test_template_test'], 'Offset was returned correctly from the theme registry');
500
    $this->assertTrue($registry['theme_test_template_test_2'], 'Offset was returned correctly from the theme registry');
501
  }
502
}