theme.test

  1. drupal
    1. 7 modules/simpletest/tests/theme.test
    2. 8 core/modules/system/tests/theme.test

Tests for the theme API.

Classes

NameDescription
ThemeFastTestCaseTests autocompletion not loading registry.
ThemeHookInitTestCaseFunctional test for initialization of the theme system in hook_init().
ThemeHtmlTagUnit tests for theme_html_tag().
ThemeItemListUnitTestUnit tests for theme_item_list().
ThemeLinksTestUnit tests for theme_links().
ThemeRegistryTestCaseTests for the ThemeRegistry class.
ThemeTableTestCaseUnit tests for theme_table().
ThemeTestCaseUnit tests for the Theme API.

File

modules/simpletest/tests/theme.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * Tests for the theme API.
  5. */
  6. /**
  7. * Unit tests for the Theme API.
  8. */
  9. class ThemeTestCase extends DrupalWebTestCase {
  10. protected $profile = 'testing';
  11. public static function getInfo() {
  12. return array(
  13. 'name' => 'Theme API',
  14. 'description' => 'Test low-level theme functions.',
  15. 'group' => 'Theme',
  16. );
  17. }
  18. function setUp() {
  19. parent::setUp('theme_test');
  20. theme_enable(array('test_theme'));
  21. }
  22. /**
  23. * Test function theme_get_suggestions() for SA-CORE-2009-003.
  24. */
  25. function testThemeSuggestions() {
  26. // Set the front page as something random otherwise the CLI
  27. // test runner fails.
  28. variable_set('site_frontpage', 'nobody-home');
  29. $args = array('node', '1', 'edit');
  30. $suggestions = theme_get_suggestions($args, 'page');
  31. $this->assertEqual($suggestions, array('page__node', 'page__node__%', 'page__node__1', 'page__node__edit'), t('Found expected node edit page suggestions'));
  32. // Check attack vectors.
  33. $args = array('node', '\\1');
  34. $suggestions = theme_get_suggestions($args, 'page');
  35. $this->assertEqual($suggestions, array('page__node', 'page__node__%', 'page__node__1'), t('Removed invalid \\ from suggestions'));
  36. $args = array('node', '1/');
  37. $suggestions = theme_get_suggestions($args, 'page');
  38. $this->assertEqual($suggestions, array('page__node', 'page__node__%', 'page__node__1'), t('Removed invalid / from suggestions'));
  39. $args = array('node', "1\0");
  40. $suggestions = theme_get_suggestions($args, 'page');
  41. $this->assertEqual($suggestions, array('page__node', 'page__node__%', 'page__node__1'), t('Removed invalid \\0 from suggestions'));
  42. // Define path with hyphens to be used to generate suggestions.
  43. $args = array('node', '1', 'hyphen-path');
  44. $result = array('page__node', 'page__node__%', 'page__node__1', 'page__node__hyphen_path');
  45. $suggestions = theme_get_suggestions($args, 'page');
  46. $this->assertEqual($suggestions, $result, t('Found expected page suggestions for paths containing hyphens.'));
  47. }
  48. /**
  49. * Ensures preprocess functions run even for suggestion implementations.
  50. *
  51. * The theme hook used by this test has its base preprocess function in a
  52. * separate file, so this test also ensures that that file is correctly loaded
  53. * when needed.
  54. */
  55. function testPreprocessForSuggestions() {
  56. // Test with both an unprimed and primed theme registry.
  57. drupal_theme_rebuild();
  58. for ($i = 0; $i < 2; $i++) {
  59. $this->drupalGet('theme-test/suggestion');
  60. $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.');
  61. }
  62. }
  63. /**
  64. * Ensure page-front template suggestion is added when on front page.
  65. */
  66. function testFrontPageThemeSuggestion() {
  67. $q = $_GET['q'];
  68. // Set $_GET['q'] to node because theme_get_suggestions() will query it to
  69. // see if we are on the front page.
  70. $_GET['q'] = variable_get('site_frontpage', 'node');
  71. $suggestions = theme_get_suggestions(explode('/', $_GET['q']), 'page');
  72. // Set it back to not annoy the batch runner.
  73. $_GET['q'] = $q;
  74. $this->assertTrue(in_array('page__front', $suggestions), t('Front page template was suggested.'));
  75. }
  76. /**
  77. * Ensures theme hook_*_alter() implementations can run before anything is rendered.
  78. */
  79. function testAlter() {
  80. $this->drupalGet('theme-test/alter');
  81. $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.'));
  82. }
  83. /**
  84. * Ensures a theme's .info file is able to override a module CSS file from being added to the page.
  85. *
  86. * @see test_theme.info
  87. */
  88. function testCSSOverride() {
  89. // Reuse the same page as in testPreprocessForSuggestions(). We're testing
  90. // what is output to the HTML HEAD based on what is in a theme's .info file,
  91. // so it doesn't matter what page we get, as long as it is themed with the
  92. // test theme. First we test with CSS aggregation disabled.
  93. variable_set('preprocess_css', 0);
  94. $this->drupalGet('theme-test/suggestion');
  95. $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.'));
  96. // Also test with aggregation enabled, simply ensuring no PHP errors are
  97. // triggered during drupal_build_css_cache() when a source file doesn't
  98. // exist. Then allow remaining tests to continue with aggregation disabled
  99. // by default.
  100. variable_set('preprocess_css', 1);
  101. $this->drupalGet('theme-test/suggestion');
  102. variable_set('preprocess_css', 0);
  103. }
  104. /**
  105. * Ensures the theme registry is rebuilt when modules are disabled/enabled.
  106. */
  107. function testRegistryRebuild() {
  108. $this->assertIdentical(theme('theme_test_foo', array('foo' => 'a')), 'a', 'The theme registry contains theme_test_foo.');
  109. module_disable(array('theme_test'), FALSE);
  110. $this->assertIdentical(theme('theme_test_foo', array('foo' => 'b')), '', 'The theme registry does not contain theme_test_foo, because the module is disabled.');
  111. module_enable(array('theme_test'), FALSE);
  112. $this->assertIdentical(theme('theme_test_foo', array('foo' => 'c')), 'c', 'The theme registry contains theme_test_foo again after re-enabling the module.');
  113. }
  114. }
  115. /**
  116. * Unit tests for theme_table().
  117. */
  118. class ThemeTableTestCase extends DrupalWebTestCase {
  119. public static function getInfo() {
  120. return array(
  121. 'name' => 'Theme Table',
  122. 'description' => 'Tests built-in theme functions.',
  123. 'group' => 'Theme',
  124. );
  125. }
  126. /**
  127. * Tableheader.js provides 'sticky' table headers, and is included by default.
  128. */
  129. function testThemeTableStickyHeaders() {
  130. $header = array('one', 'two', 'three');
  131. $rows = array(array(1,2,3), array(4,5,6), array(7,8,9));
  132. $this->content = theme('table', array('header' => $header, 'rows' => $rows));
  133. $js = drupal_add_js();
  134. $this->assertTrue(isset($js['misc/tableheader.js']), t('tableheader.js was included when $sticky = TRUE.'));
  135. $this->assertRaw('sticky-enabled', t('Table has a class of sticky-enabled when $sticky = TRUE.'));
  136. drupal_static_reset('drupal_add_js');
  137. }
  138. /**
  139. * If $sticky is FALSE, no tableheader.js should be included.
  140. */
  141. function testThemeTableNoStickyHeaders() {
  142. $header = array('one', 'two', 'three');
  143. $rows = array(array(1,2,3), array(4,5,6), array(7,8,9));
  144. $attributes = array();
  145. $caption = NULL;
  146. $colgroups = array();
  147. $this->content = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => $attributes, 'caption' => $caption, 'colgroups' => $colgroups, 'sticky' => FALSE));
  148. $js = drupal_add_js();
  149. $this->assertFalse(isset($js['misc/tableheader.js']), t('tableheader.js was not included because $sticky = FALSE.'));
  150. $this->assertNoRaw('sticky-enabled', t('Table does not have a class of sticky-enabled because $sticky = FALSE.'));
  151. drupal_static_reset('drupal_add_js');
  152. }
  153. /**
  154. * Tests that the table header is printed correctly even if there are no rows,
  155. * and that the empty text is displayed correctly.
  156. */
  157. function testThemeTableWithEmptyMessage() {
  158. $header = array(
  159. t('Header 1'),
  160. array(
  161. 'data' => t('Header 2'),
  162. 'colspan' => 2,
  163. ),
  164. );
  165. $this->content = theme('table', array('header' => $header, 'rows' => array(), 'empty' => t('No strings available.')));
  166. $this->assertRaw('<tr class="odd"><td colspan="3" class="empty message">No strings available.</td>', t('Correct colspan was set on empty message.'));
  167. $this->assertRaw('<thead><tr><th>Header 1</th>', t('Table header was printed.'));
  168. }
  169. }
  170. /**
  171. * Unit tests for theme_item_list().
  172. */
  173. class ThemeItemListUnitTest extends DrupalWebTestCase {
  174. public static function getInfo() {
  175. return array(
  176. 'name' => 'Theme item list',
  177. 'description' => 'Test the theme_item_list() function.',
  178. 'group' => 'Theme',
  179. );
  180. }
  181. /**
  182. * Test nested list rendering.
  183. */
  184. function testNestedList() {
  185. $items = array('a', array('data' => 'b', 'children' => array('c', 'd')), 'e');
  186. $expected = '<div class="item-list"><ul><li class="first">a</li>
  187. <li>b<div class="item-list"><ul><li class="first">c</li>
  188. <li class="last">d</li>
  189. </ul></div></li>
  190. <li class="last">e</li>
  191. </ul></div>';
  192. $output = theme('item_list', array('items' => $items));
  193. $this->assertIdentical($expected, $output, 'Nested list is rendered correctly.');
  194. }
  195. }
  196. /**
  197. * Unit tests for theme_links().
  198. */
  199. class ThemeLinksTest extends DrupalWebTestCase {
  200. public static function getInfo() {
  201. return array(
  202. 'name' => 'Links',
  203. 'description' => 'Test the theme_links() function and rendering groups of links.',
  204. 'group' => 'Theme',
  205. );
  206. }
  207. /**
  208. * Test the use of drupal_pre_render_links() on a nested array of links.
  209. */
  210. function testDrupalPreRenderLinks() {
  211. // Define the base array to be rendered, containing a variety of different
  212. // kinds of links.
  213. $base_array = array(
  214. '#theme' => 'links',
  215. '#pre_render' => array('drupal_pre_render_links'),
  216. '#links' => array(
  217. 'parent_link' => array(
  218. 'title' => 'Parent link original',
  219. 'href' => 'parent-link-original',
  220. ),
  221. ),
  222. 'first_child' => array(
  223. '#theme' => 'links',
  224. '#links' => array(
  225. // This should be rendered if 'first_child' is rendered separately,
  226. // but ignored if the parent is being rendered (since it duplicates
  227. // one of the parent's links).
  228. 'parent_link' => array(
  229. 'title' => 'Parent link copy',
  230. 'href' => 'parent-link-copy',
  231. ),
  232. // This should always be rendered.
  233. 'first_child_link' => array(
  234. 'title' => 'First child link',
  235. 'href' => 'first-child-link',
  236. ),
  237. ),
  238. ),
  239. // This should always be rendered as part of the parent.
  240. 'second_child' => array(
  241. '#theme' => 'links',
  242. '#links' => array(
  243. 'second_child_link' => array(
  244. 'title' => 'Second child link',
  245. 'href' => 'second-child-link',
  246. ),
  247. ),
  248. ),
  249. // This should never be rendered, since the user does not have access to
  250. // it.
  251. 'third_child' => array(
  252. '#theme' => 'links',
  253. '#links' => array(
  254. 'third_child_link' => array(
  255. 'title' => 'Third child link',
  256. 'href' => 'third-child-link',
  257. ),
  258. ),
  259. '#access' => FALSE,
  260. ),
  261. );
  262. // Start with a fresh copy of the base array, and try rendering the entire
  263. // thing. We expect a single <ul> with appropriate links contained within
  264. // it.
  265. $render_array = $base_array;
  266. $html = drupal_render($render_array);
  267. $dom = new DOMDocument();
  268. $dom->loadHTML($html);
  269. $this->assertEqual($dom->getElementsByTagName('ul')->length, 1, t('One "ul" tag found in the rendered HTML.'));
  270. $list_elements = $dom->getElementsByTagName('li');
  271. $this->assertEqual($list_elements->length, 3, t('Three "li" tags found in the rendered HTML.'));
  272. $this->assertEqual($list_elements->item(0)->nodeValue, 'Parent link original', t('First expected link found.'));
  273. $this->assertEqual($list_elements->item(1)->nodeValue, 'First child link', t('Second expected link found.'));
  274. $this->assertEqual($list_elements->item(2)->nodeValue, 'Second child link', t('Third expected link found.'));
  275. $this->assertIdentical(strpos($html, 'Parent link copy'), FALSE, t('"Parent link copy" link not found.'));
  276. $this->assertIdentical(strpos($html, 'Third child link'), FALSE, t('"Third child link" link not found.'));
  277. // Now render 'first_child', followed by the rest of the links, and make
  278. // sure we get two separate <ul>'s with the appropriate links contained
  279. // within each.
  280. $render_array = $base_array;
  281. $child_html = drupal_render($render_array['first_child']);
  282. $parent_html = drupal_render($render_array);
  283. // First check the child HTML.
  284. $dom = new DOMDocument();
  285. $dom->loadHTML($child_html);
  286. $this->assertEqual($dom->getElementsByTagName('ul')->length, 1, t('One "ul" tag found in the rendered child HTML.'));
  287. $list_elements = $dom->getElementsByTagName('li');
  288. $this->assertEqual($list_elements->length, 2, t('Two "li" tags found in the rendered child HTML.'));
  289. $this->assertEqual($list_elements->item(0)->nodeValue, 'Parent link copy', t('First expected link found.'));
  290. $this->assertEqual($list_elements->item(1)->nodeValue, 'First child link', t('Second expected link found.'));
  291. // Then check the parent HTML.
  292. $dom = new DOMDocument();
  293. $dom->loadHTML($parent_html);
  294. $this->assertEqual($dom->getElementsByTagName('ul')->length, 1, t('One "ul" tag found in the rendered parent HTML.'));
  295. $list_elements = $dom->getElementsByTagName('li');
  296. $this->assertEqual($list_elements->length, 2, t('Two "li" tags found in the rendered parent HTML.'));
  297. $this->assertEqual($list_elements->item(0)->nodeValue, 'Parent link original', t('First expected link found.'));
  298. $this->assertEqual($list_elements->item(1)->nodeValue, 'Second child link', t('Second expected link found.'));
  299. $this->assertIdentical(strpos($parent_html, 'First child link'), FALSE, t('"First child link" link not found.'));
  300. $this->assertIdentical(strpos($parent_html, 'Third child link'), FALSE, t('"Third child link" link not found.'));
  301. }
  302. }
  303. /**
  304. * Functional test for initialization of the theme system in hook_init().
  305. */
  306. class ThemeHookInitTestCase extends DrupalWebTestCase {
  307. public static function getInfo() {
  308. return array(
  309. 'name' => 'Theme initialization in hook_init()',
  310. 'description' => 'Tests that the theme system can be correctly initialized in hook_init().',
  311. 'group' => 'Theme',
  312. );
  313. }
  314. function setUp() {
  315. parent::setUp('theme_test');
  316. }
  317. /**
  318. * Test that the theme system can generate output when called by hook_init().
  319. */
  320. function testThemeInitializationHookInit() {
  321. $this->drupalGet('theme-test/hook-init');
  322. $this->assertRaw('Themed output generated in hook_init()', t('Themed output generated in hook_init() correctly appears on the page.'));
  323. $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()."));
  324. }
  325. }
  326. /**
  327. * Tests autocompletion not loading registry.
  328. */
  329. class ThemeFastTestCase extends DrupalWebTestCase {
  330. public static function getInfo() {
  331. return array(
  332. 'name' => 'Theme fast initialization',
  333. 'description' => 'Test that autocompletion does not load the registry.',
  334. 'group' => 'Theme'
  335. );
  336. }
  337. function setUp() {
  338. parent::setUp('theme_test');
  339. $this->account = $this->drupalCreateUser(array('access user profiles'));
  340. }
  341. /**
  342. * Tests access to user autocompletion and verify the correct results.
  343. */
  344. function testUserAutocomplete() {
  345. $this->drupalLogin($this->account);
  346. $this->drupalGet('user/autocomplete/' . $this->account->name);
  347. $this->assertText('registry not initialized', t('The registry was not initialized'));
  348. }
  349. }
  350. /**
  351. * Unit tests for theme_html_tag().
  352. */
  353. class ThemeHtmlTag extends DrupalUnitTestCase {
  354. public static function getInfo() {
  355. return array(
  356. 'name' => 'Theme HTML Tag',
  357. 'description' => 'Tests theme_html_tag() built-in theme functions.',
  358. 'group' => 'Theme',
  359. );
  360. }
  361. /**
  362. * Test function theme_html_tag()
  363. */
  364. function testThemeHtmlTag() {
  365. // Test auto-closure meta tag generation
  366. $tag['element'] = array('#tag' => 'meta', '#attributes' => array('name' => 'description', 'content' => 'Drupal test'));
  367. $this->assertEqual('<meta name="description" content="Drupal test" />'."\n", theme_html_tag($tag), t('Test auto-closure meta tag generation.'));
  368. // Test title tag generation
  369. $tag['element'] = array('#tag' => 'title', '#value' => 'title test');
  370. $this->assertEqual('<title>title test</title>'."\n", theme_html_tag($tag), t('Test title tag generation.'));
  371. }
  372. }
  373. /**
  374. * Tests for the ThemeRegistry class.
  375. */
  376. class ThemeRegistryTestCase extends DrupalWebTestCase {
  377. public static function getInfo() {
  378. return array(
  379. 'name' => 'ThemeRegistry',
  380. 'description' => 'Tests the behavior of the ThemeRegistry class',
  381. 'group' => 'Theme',
  382. );
  383. }
  384. function setUp() {
  385. parent::setUp('theme_test');
  386. }
  387. /**
  388. * Tests the behavior of the theme registry class.
  389. */
  390. function testRaceCondition() {
  391. $_SERVER['REQUEST_METHOD'] = 'GET';
  392. $cid = 'test_theme_registry';
  393. // Directly instantiate the theme registry, this will cause a base cache
  394. // entry to be written in __construct().
  395. $registry = new ThemeRegistry($cid, 'cache');
  396. $this->assertTrue(cache_get($cid), 'Cache entry was created.');
  397. // Trigger a cache miss for an offset.
  398. $this->assertTrue($registry['theme_test_template_test'], 'Offset was returned correctly from the theme registry.');
  399. // This will cause the ThemeRegistry class to write an updated version of
  400. // the cache entry when it is destroyed, usually at the end of the request.
  401. // Before that happens, manually delete the cache entry we created earlier
  402. // so that the new entry is written from scratch.
  403. cache_clear_all($cid, 'cache');
  404. // Destroy the class so that it triggers a cache write for the offset.
  405. unset($registry);
  406. $this->assertTrue(cache_get($cid), 'Cache entry was created.');
  407. // Create a new instance of the class. Confirm that both the offset
  408. // requested previously, and one that has not yet been requested are both
  409. // available.
  410. $registry = new ThemeRegistry($cid, 'cache');
  411. $this->assertTrue($registry['theme_test_template_test'], 'Offset was returned correctly from the theme registry');
  412. $this->assertTrue($registry['theme_test_template_test_2'], 'Offset was returned correctly from the theme registry');
  413. }
  414. }
Login or register to post comments