ThemeTest.php 11.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Definition of Drupal\system\Tests\Theme\ThemeTest.
 */

namespace Drupal\system\Tests\Theme;

use Drupal\simpletest\WebTestBase;
11
use Drupal\test_theme\ThemeClass;
12 13 14 15 16

/**
 * Tests low-level theme functions.
 */
class ThemeTest extends WebTestBase {
17 18 19 20 21 22 23 24

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('theme_test');

25 26 27 28 29 30 31 32 33
  public static function getInfo() {
    return array(
      'name' => 'Theme API',
      'description' => 'Test low-level theme functions.',
      'group' => 'Theme',
    );
  }

  function setUp() {
34
    parent::setUp();
35 36 37
    theme_enable(array('test_theme'));
  }

38 39 40 41
  /**
   * Test attribute merging.
   *
   * Render arrays that use a render element and templates (and hence call
42
   * template_preprocess()) must ensure the attributes at different occasions
43 44 45 46 47 48
   * are all merged correctly:
   *   - $variables['attributes'] as passed in to theme()
   *   - the render element's #attributes
   *   - any attributes set in the template's preprocessing function
   */
  function testAttributeMerging() {
49
    $theme_test_render_element = array(
50 51 52 53 54 55
      'elements' => array(
        '#attributes' => array('data-foo' => 'bar'),
      ),
      'attributes' => array(
        'id' => 'bazinga',
      ),
56 57
    );
    $this->assertThemeOutput('theme_test_render_element', $theme_test_render_element, '<div id="bazinga" data-foo="bar" data-variables-are-preprocessed></div>' . "\n");
58 59
  }

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  /**
   * Test that theme() returns expected data types.
   */
  function testThemeDataTypes() {
    // theme_test_false is an implemented theme hook so theme() should return a
    // string, even though the theme function itself can return anything.
    $foos = array('null' => NULL, 'false' => FALSE, 'integer' => 1, 'string' => 'foo');
    foreach ($foos as $type => $example) {
      $output = theme('theme_test_foo', array('foo' => $example));
      $this->assertTrue(is_string($output), format_string('theme() returns a string for data type !type.', array('!type' => $type)));
    }

    // suggestionnotimplemented is not an implemented theme hook so theme()
    // should return FALSE instead of a string.
    $output = theme(array('suggestionnotimplemented'));
    $this->assertIdentical($output, FALSE, 'theme() returns FALSE when a hook suggestion is not implemented.');
  }

78 79 80 81 82 83
  /**
   * 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.
84
    \Drupal::config('system.site')->set('page.front', 'nobody-home')->save();
85 86
    $args = array('node', '1', 'edit');
    $suggestions = theme_get_suggestions($args, 'page');
87
    $this->assertEqual($suggestions, array('page__node', 'page__node__%', 'page__node__1', 'page__node__edit'), 'Found expected node edit page suggestions');
88 89 90
    // Check attack vectors.
    $args = array('node', '\\1');
    $suggestions = theme_get_suggestions($args, 'page');
91
    $this->assertEqual($suggestions, array('page__node', 'page__node__%', 'page__node__1'), 'Removed invalid \\ from suggestions');
92 93
    $args = array('node', '1/');
    $suggestions = theme_get_suggestions($args, 'page');
94
    $this->assertEqual($suggestions, array('page__node', 'page__node__%', 'page__node__1'), 'Removed invalid / from suggestions');
95 96
    $args = array('node', "1\0");
    $suggestions = theme_get_suggestions($args, 'page');
97
    $this->assertEqual($suggestions, array('page__node', 'page__node__%', 'page__node__1'), 'Removed invalid \\0 from suggestions');
98 99 100 101
    // Define path with hyphens to be used to generate suggestions.
    $args = array('node', '1', 'hyphen-path');
    $result = array('page__node', 'page__node__%', 'page__node__1', 'page__node__hyphen_path');
    $suggestions = theme_get_suggestions($args, 'page');
102
    $this->assertEqual($suggestions, $result, 'Found expected page suggestions for paths containing hyphens.');
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
  }

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

  /**
   * Ensure page-front template suggestion is added when on front page.
   */
  function testFrontPageThemeSuggestion() {
    $original_path = _current_path();
    // Set the current path to node because theme_get_suggestions() will query
    // it to see if we are on the front page.
128
    \Drupal::config('system.site')->set('page.front', 'node')->save();
129 130 131 132
    _current_path('node');
    $suggestions = theme_get_suggestions(array('node'), 'page');
    // Set it back to not annoy the batch runner.
    _current_path($original_path);
133
    $this->assertTrue(in_array('page__front', $suggestions), 'Front page template was suggested.');
134 135 136 137 138 139 140
  }

  /**
   * Ensures theme hook_*_alter() implementations can run before anything is rendered.
   */
  function testAlter() {
    $this->drupalGet('theme-test/alter');
141
    $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.');
142 143 144
  }

  /**
145
   * Ensures a theme's .info.yml file is able to override a module CSS file from being added to the page.
146
   *
147
   * @see test_theme.info.yml
148 149 150
   */
  function testCSSOverride() {
    // Reuse the same page as in testPreprocessForSuggestions(). We're testing
151 152 153
    // what is output to the HTML HEAD based on what is in a theme's .info.yml
    // 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.
154
    $config = \Drupal::config('system.performance');
155
    $config->set('css.preprocess', 0);
156 157
    $config->save();
    $this->drupalGet('theme-test/suggestion');
158
    $this->assertNoText('system.module.css', 'The theme\'s .info.yml file is able to override a module CSS file from being added to the page.');
159 160 161 162 163

    // 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.
164
    $config->set('css.preprocess', 1);
165 166
    $config->save();
    $this->drupalGet('theme-test/suggestion');
167
    $config->set('css.preprocess', 0);
168 169 170 171 172 173 174
    $config->save();
  }

  /**
   * Ensures a themes template is overrideable based on the 'template' filename.
   */
  function testTemplateOverride() {
175
    \Drupal::config('system.theme')
176 177
      ->set('default', 'test_theme')
      ->save();
178
    $this->drupalGet('theme-test/template-test');
179
    $this->assertText('Success: Template overridden.', 'Template overridden by defined \'template\' filename.');
180 181
  }

182 183 184 185 186 187 188 189
  /**
   * Ensures a theme template can override a theme function.
   */
  function testFunctionOverride() {
    $this->drupalGet('theme-test/function-template-overridden');
    $this->assertText('Success: Template overrides theme function.', 'Theme function overridden by test_theme template.');
  }

190 191 192 193 194 195
  /**
   * Test the list_themes() function.
   */
  function testListThemes() {
    $themes = list_themes();
    // Check if drupal_theme_access() retrieves enabled themes properly from list_themes().
196
    $this->assertTrue(drupal_theme_access('test_theme'), 'Enabled theme detected');
197
    // Check if list_themes() returns disabled themes.
198
    $this->assertTrue(array_key_exists('test_basetheme', $themes), 'Disabled theme detected');
199 200 201
    // Check for base theme and subtheme lists.
    $base_theme_list = array('test_basetheme' => 'Theme test base theme');
    $sub_theme_list = array('test_subtheme' => 'Theme test subtheme');
202 203
    $this->assertIdentical($themes['test_basetheme']->sub_themes, $sub_theme_list, 'Base theme\'s object includes list of subthemes.');
    $this->assertIdentical($themes['test_subtheme']->base_themes, $base_theme_list, 'Subtheme\'s object includes list of base themes.');
204
    // Check for theme engine in subtheme.
205
    $this->assertIdentical($themes['test_subtheme']->engine, 'twig', 'Subtheme\'s object includes the theme engine.');
206
    // Check for theme engine prefix.
207 208
    $this->assertIdentical($themes['test_basetheme']->prefix, 'twig', 'Base theme\'s object includes the theme engine prefix.');
    $this->assertIdentical($themes['test_subtheme']->prefix, 'twig', 'Subtheme\'s object includes the theme engine prefix.');
209 210 211 212 213 214 215
  }

  /**
   * Test the theme_get_setting() function.
   */
  function testThemeGetSetting() {
    $GLOBALS['theme_key'] = 'test_theme';
216 217 218
    $this->assertIdentical(theme_get_setting('theme_test_setting'), 'default value', 'theme_get_setting() uses the default theme automatically.');
    $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.');
    $this->assertIdentical(theme_get_setting('basetheme_only', 'test_subtheme'), 'base theme value', 'Base theme\'s default settings values are inherited by subtheme.');
219 220
  }

221 222 223 224 225 226 227 228 229 230
  /**
   * Tests child element rendering for 'render element' theme hooks.
   */
  function testDrupalRenderChildren() {
    $element = array(
      '#theme' => 'theme_test_render_element_children',
      'child' => array(
        '#markup' => 'Foo',
      ),
    );
231
    $this->assertThemeOutput('theme_test_render_element_children', $element, 'Foo', 'drupal_render() avoids #theme recursion loop when rendering a render element.');
232 233 234 235 236 237 238

    $element = array(
      '#theme_wrappers' => array('theme_test_render_element_children'),
      'child' => array(
        '#markup' => 'Foo',
      ),
    );
239
    $this->assertThemeOutput('theme_test_render_element_children', $element, 'Foo', 'drupal_render() avoids #theme_wrappers recursion loop when rendering a render element.');
240 241
  }

242 243 244 245 246 247
  /**
   * Tests theme can provide classes.
   */
  function testClassLoading() {
    new ThemeClass();
  }
248 249 250 251 252

  /**
   * Tests drupal_find_theme_templates().
   */
  public function testFindThemeTemplates() {
253 254 255
    $registry = $this->container->get('theme.registry')->get();
    $templates = drupal_find_theme_templates($registry, '.html.twig', drupal_get_path('theme', 'test_theme'));
    $this->assertEqual($templates['node__1']['template'], 'node--1', 'Template node--1.tpl.twig was found in test_theme.');
256
  }
257 258 259 260 261 262 263 264 265 266 267

  /**
   * Tests that the page variable is not prematurely flattened.
   *
   * Some modules check the page array in template_preprocess_html(), so we
   * ensure that it has not been rendered prematurely.
   */
  function testPreprocessHtml() {
    $this->drupalGet('');
    $attributes = $this->xpath('/html/body[@theme_test_page_variable="Page variable is an array."]');
    $this->assertTrue(count($attributes) == 1, 'In template_preprocess_html(), the page variable is still an array (not rendered yet).');
268
    $this->assertText('theme test page bottom markup', 'Modules are able to set the page bottom region.');
269 270
  }

271

272
}