'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')); // 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'); $this->assertEqual($suggestions, $result, t('Found expected page suggestions for paths containing hyphens.')); } /** * 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. variable_set('site_frontpage', 'node'); $_GET['q'] = '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. $config = config('system.performance'); $config->set('preprocess_css', 0); $config->save(); $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. $config->set('preprocess_css', 1); $config->save(); $this->drupalGet('theme-test/suggestion'); $config->set('preprocess_css', 0); $config->save(); } /** * Ensures a themes template is overrideable based on the 'template' filename. */ function testTemplateOverride() { variable_set('theme_default', 'test_theme'); $this->drupalGet('theme-test/template-test'); $this->assertText('Success: Template overridden.', t('Template overridden by defined \'template\' filename.')); } /** * Test the list_themes() function. */ function testListThemes() { $themes = list_themes(); // Check if drupal_theme_access() retrieves enabled themes properly from list_themes(). $this->assertTrue(drupal_theme_access('test_theme'), t('Enabled theme detected')); // Check if list_themes() returns disabled themes. $this->assertTrue(array_key_exists('test_basetheme', $themes), t('Disabled theme detected')); // 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'); $this->assertIdentical($themes['test_basetheme']->sub_themes, $sub_theme_list, t('Base theme\'s object includes list of subthemes.')); $this->assertIdentical($themes['test_subtheme']->base_themes, $base_theme_list, t('Subtheme\'s object includes list of base themes.')); // Check for theme engine in subtheme. $this->assertIdentical($themes['test_subtheme']->engine, 'phptemplate', t('Subtheme\'s object includes the theme engine.')); // Check for theme engine prefix. $this->assertIdentical($themes['test_basetheme']->prefix, 'phptemplate', t('Base theme\'s object includes the theme engine prefix.')); $this->assertIdentical($themes['test_subtheme']->prefix, 'phptemplate', t('Subtheme\'s object includes the theme engine prefix.')); } /** * Test the theme_get_setting() function. */ function testThemeGetSetting() { $GLOBALS['theme_key'] = 'test_theme'; $this->assertIdentical(theme_get_setting('theme_test_setting'), 'default value', t('theme_get_setting() uses the default theme automatically.')); $this->assertNotEqual(theme_get_setting('subtheme_override', 'test_basetheme'), theme_get_setting('subtheme_override', 'test_subtheme'), t('Base theme\'s default settings values can be overridden by subtheme.')); $this->assertIdentical(theme_get_setting('basetheme_only', 'test_subtheme'), 'base theme value', t('Base theme\'s default settings values are inherited by subtheme.')); } } /** * 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['core/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['core/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('No strings available.', t('Correct colspan was set on empty message.')); $this->assertRaw('Header 1', t('Table header was printed.')); } } /** * Tests for common theme functions. */ class ThemeFunctionsTestCase extends DrupalWebTestCase { protected $profile = 'testing'; public static function getInfo() { return array( 'name' => 'Theme functions', 'description' => 'Tests common theme functions.', 'group' => 'Theme', ); } /** * Tests theme_item_list(). */ function testItemList() { // Verify that empty variables produce no output. $variables = array(); $expected = ''; $this->assertThemeOutput('item_list', $variables, $expected, 'Empty %callback generates no output.'); $variables = array(); $variables['title'] = 'Some title'; $expected = ''; $this->assertThemeOutput('item_list', $variables, $expected, 'Empty %callback with title generates no output.'); // Verify nested item lists. $variables = array(); $variables['title'] = 'Some title'; $variables['attributes'] = array( 'id' => 'parentlist', ); $variables['items'] = array( 'a', array( 'data' => 'b', 'children' => array( 'c', // Nested children may use additional attributes. array( 'data' => 'd', 'class' => array('dee'), ), // Any string key is treated as child list attribute. 'id' => 'childlist', ), // Any other keys are treated as item attributes. 'id' => 'bee', ), array( 'data' => 'e', 'id' => 'E', ), ); $inner = '
'; $expected = '
'; $expected .= '

Some title

'; $expected .= '
'; $this->assertThemeOutput('item_list', $variables, $expected); } /** * Tests theme_links(). */ function testLinks() { // Verify that empty variables produce no output. $variables = array(); $expected = ''; $this->assertThemeOutput('links', $variables, $expected, 'Empty %callback generates no output.'); $variables = array(); $variables['heading'] = 'Some title'; $expected = ''; $this->assertThemeOutput('links', $variables, $expected, 'Empty %callback with heading generates no output.'); // Set the current path to the front page path. // Required to verify the "active" class in expected links below, and // because the current path is different when running tests manually via // simpletest.module ('batch') and via the testing framework (''). $_GET['q'] = variable_get('site_frontpage', 'user'); // Verify that a list of links is properly rendered. $variables = array(); $variables['attributes'] = array('id' => 'somelinks'); $variables['links'] = array( 'a link' => array( 'title' => 'A ', 'href' => 'a/link', ), 'plain text' => array( 'title' => 'Plain "text"', ), 'front page' => array( 'title' => 'Front page', 'href' => '', ), ); $expected_links = ''; $expected_links .= ''; // Verify that passing a string as heading works. $variables['heading'] = 'Links heading'; $expected_heading = '

Links heading

'; $expected = $expected_heading . $expected_links; $this->assertThemeOutput('links', $variables, $expected); // Verify that passing an array as heading works (core support). $variables['heading'] = array('text' => 'Links heading', 'level' => 'h3', 'class' => 'heading'); $expected_heading = '

Links heading

'; $expected = $expected_heading . $expected_links; $this->assertThemeOutput('links', $variables, $expected); // Verify that passing attributes for the heading works. $variables['heading'] = array('text' => 'Links heading', 'level' => 'h3', 'attributes' => array('id' => 'heading')); $expected_heading = '

Links heading

'; $expected = $expected_heading . $expected_links; $this->assertThemeOutput('links', $variables, $expected); } /** * Asserts themed output. * * @param $callback * The name of the theme function to invoke; e.g. 'links' for theme_links(). * @param $variables * An array of variables to pass to the theme function. * @param $expected * The expected themed output string. * @param $message * (optional) An assertion message. */ protected function assertThemeOutput($callback, array $variables = array(), $expected, $message = '') { $output = theme($callback, $variables); $this->verbose('Variables:' . '
' .  check_plain(var_export($variables, TRUE)) . '
' . '
' . 'Result:' . '
' .  check_plain(var_export($output, TRUE)) . '
' . '
' . 'Expected:' . '
' .  check_plain(var_export($expected, TRUE)) . '
' . '
' . $output ); if (!$message) { $message = '%callback rendered correctly.'; } $message = t($message, array('%callback' => 'theme_' . $callback . '()')); $this->assertIdentical($output, $expected, $message); } /** * Test the use of drupal_pre_render_links() on a nested array of links. */ function testDrupalPreRenderLinks() { // Define the base array to be rendered, containing a variety of different // kinds of links. $base_array = array( '#theme' => 'links', '#pre_render' => array('drupal_pre_render_links'), '#links' => array( 'parent_link' => array( 'title' => 'Parent link original', 'href' => 'parent-link-original', ), ), 'first_child' => array( '#theme' => 'links', '#links' => array( // This should be rendered if 'first_child' is rendered separately, // but ignored if the parent is being rendered (since it duplicates // one of the parent's links). 'parent_link' => array( 'title' => 'Parent link copy', 'href' => 'parent-link-copy', ), // This should always be rendered. 'first_child_link' => array( 'title' => 'First child link', 'href' => 'first-child-link', ), ), ), // This should always be rendered as part of the parent. 'second_child' => array( '#theme' => 'links', '#links' => array( 'second_child_link' => array( 'title' => 'Second child link', 'href' => 'second-child-link', ), ), ), // This should never be rendered, since the user does not have access to // it. 'third_child' => array( '#theme' => 'links', '#links' => array( 'third_child_link' => array( 'title' => 'Third child link', 'href' => 'third-child-link', ), ), '#access' => FALSE, ), ); // Start with a fresh copy of the base array, and try rendering the entire // thing. We expect a single