summaryrefslogtreecommitdiff
path: root/modules/simpletest/tests/theme.test
blob: 823a50c6772ac2634b34967282571adaaae847ae (plain)
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
<?php
// $Id$

/**
 * @file
 * Tests for the theme API.
 */

/**
 * Unit tests for the Theme API.
 */
class TemplateUnitTest extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Theme API',
      'description' => 'Test low-level theme template functions.',
      'group' => 'Theme',
    );
  }

  /**
   * Test function template_page_suggestions() for SA-CORE-2009-003.
   */
  function testTemplateSuggestions() {
    // 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 = template_page_suggestions($args, 'page');
    $this->assertEqual($suggestions, array('page-node', 'page-node-%', 'page-node-1', 'page-node-edit'), t('Found expected node edit page template suggestions'));
    // Check attack vectors.
    $args = array('node', '\\1');
    $suggestions = template_page_suggestions($args, 'page');
    $this->assertEqual($suggestions, array('page-node', 'page-node-%', 'page-node-1'), t('Removed invalid \\ from template suggestions'));
    $args = array('node', '1/');
    $suggestions = template_page_suggestions($args, 'page');
    $this->assertEqual($suggestions, array('page-node', 'page-node-%', 'page-node-1'), t('Removed invalid / from template suggestions'));
    $args = array('node', "1\0");
    $suggestions = template_page_suggestions($args, 'page');
    $this->assertEqual($suggestions, array('page-node', 'page-node-%', 'page-node-1'), t('Removed invalid \\0 from template suggestions'));
    // Tests for drupal_discover_template()
    $suggestions = array('page');
    $this->assertEqual(drupal_discover_template(array('themes/garland'), $suggestions), 'themes/garland/page.tpl.php', t('Safe template discovered'));
    $suggestions = array('page');
    $this->assertEqual(drupal_discover_template(array('themes/garland'), $suggestions, '\\.tpl.php'), 'themes/garland/page.tpl.php', t('Unsafe extension fixed'));
    $suggestions = array('page\\');
    $this->assertEqual(drupal_discover_template(array('themes/garland'), $suggestions), 'themes/garland/page.tpl.php', t('Unsafe template suggestion fixed'));
    $suggestions = array('page/');
    $this->assertEqual(drupal_discover_template(array('themes/garland'), $suggestions), 'themes/garland/page.tpl.php', t('Unsafe template suggestion fixed'));
    $suggestions = array("page\0");
    $this->assertEqual(drupal_discover_template(array('themes/garland'), $suggestions), 'themes/garland/page.tpl.php', t('Unsafe template suggestion fixed'));
  }

  /**
   * Test that not-found theme hook throw a proper exception.
   */
  function testThemeHookNotFound() {
    try {
      theme('this_does_not_exist');

      $this->fail(t('Exception not thrown for invalid theme hook.'));
    }
    catch (ThemeHookNotFoundException $e) {
      $this->assertEqual($e->getMessage(), t('Theme hook "this_does_not_exist" not found.'), t('Correct exception thrown with correct error message.'));
    }
    catch (Exception $e) {
      $this->fail(t('Exception of type "@type" thrown instead of ThemeHookNotFoundException.', array('@type' => gettype($e))));
    }
  }
}

/**
 * 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');
  }
}