theme.test 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. * Test the list_themes() function.
  116. */
  117. function testListThemes() {
  118. $themes = list_themes();
  119. // Check if drupal_theme_access() retrieves enabled themes properly from list_themes().
  120. $this->assertTrue(drupal_theme_access('test_theme'), t('Enabled theme detected'));
  121. // Check if list_themes() returns disabled themes.
  122. $this->assertTrue(array_key_exists('test_basetheme', $themes), t('Disabled theme detected'));
  123. // Check for base theme and subtheme lists.
  124. $base_theme_list = array('test_basetheme' => 'Theme test base theme');
  125. $sub_theme_list = array('test_subtheme' => 'Theme test subtheme');
  126. $this->assertIdentical($themes['test_basetheme']->sub_themes, $sub_theme_list, t('Base theme\'s object includes list of subthemes.'));
  127. $this->assertIdentical($themes['test_subtheme']->base_themes, $base_theme_list, t('Subtheme\'s object includes list of base themes.'));
  128. // Check for theme engine in subtheme.
  129. $this->assertIdentical($themes['test_subtheme']->engine, 'phptemplate', t('Subtheme\'s object includes the theme engine.'));
  130. // Check for theme engine prefix.
  131. $this->assertIdentical($themes['test_basetheme']->prefix, 'phptemplate', t('Base theme\'s object includes the theme engine prefix.'));
  132. $this->assertIdentical($themes['test_subtheme']->prefix, 'phptemplate', t('Subtheme\'s object includes the theme engine prefix.'));
  133. }
  134. /**
  135. * Test the theme_get_setting() function.
  136. */
  137. function testThemeGetSetting() {
  138. $GLOBALS['theme_key'] = 'test_theme';
  139. $this->assertIdentical(theme_get_setting('theme_test_setting'), 'default value', t('theme_get_setting() uses the default theme automatically.'));
  140. $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.'));
  141. $this->assertIdentical(theme_get_setting('basetheme_only', 'test_subtheme'), 'base theme value', t('Base theme\'s default settings values are inherited by subtheme.'));
  142. }
  143. }
  144. /**
  145. * Unit tests for theme_table().
  146. */
  147. class ThemeTableTestCase extends DrupalWebTestCase {
  148. public static function getInfo() {
  149. return array(
  150. 'name' => 'Theme Table',
  151. 'description' => 'Tests built-in theme functions.',
  152. 'group' => 'Theme',
  153. );
  154. }
  155. /**
  156. * Tableheader.js provides 'sticky' table headers, and is included by default.
  157. */
  158. function testThemeTableStickyHeaders() {
  159. $header = array('one', 'two', 'three');
  160. $rows = array(array(1,2,3), array(4,5,6), array(7,8,9));
  161. $this->content = theme('table', array('header' => $header, 'rows' => $rows));
  162. $js = drupal_add_js();
  163. $this->assertTrue(isset($js['misc/tableheader.js']), t('tableheader.js was included when $sticky = TRUE.'));
  164. $this->assertRaw('sticky-enabled', t('Table has a class of sticky-enabled when $sticky = TRUE.'));
  165. drupal_static_reset('drupal_add_js');
  166. }
  167. /**
  168. * If $sticky is FALSE, no tableheader.js should be included.
  169. */
  170. function testThemeTableNoStickyHeaders() {
  171. $header = array('one', 'two', 'three');
  172. $rows = array(array(1,2,3), array(4,5,6), array(7,8,9));
  173. $attributes = array();
  174. $caption = NULL;
  175. $colgroups = array();
  176. $this->content = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => $attributes, 'caption' => $caption, 'colgroups' => $colgroups, 'sticky' => FALSE));
  177. $js = drupal_add_js();
  178. $this->assertFalse(isset($js['misc/tableheader.js']), t('tableheader.js was not included because $sticky = FALSE.'));
  179. $this->assertNoRaw('sticky-enabled', t('Table does not have a class of sticky-enabled because $sticky = FALSE.'));
  180. drupal_static_reset('drupal_add_js');
  181. }
  182. /**
  183. * Tests that the table header is printed correctly even if there are no rows,
  184. * and that the empty text is displayed correctly.
  185. */
  186. function testThemeTableWithEmptyMessage() {
  187. $header = array(
  188. t('Header 1'),
  189. array(
  190. 'data' => t('Header 2'),
  191. 'colspan' => 2,
  192. ),
  193. );
  194. $this->content = theme('table', array('header' => $header, 'rows' => array(), 'empty' => t('No strings available.')));
  195. $this->assertRaw('<tr class="odd"><td colspan="3" class="empty message">No strings available.</td>', t('Correct colspan was set on empty message.'));
  196. $this->assertRaw('<thead><tr><th>Header 1</th>', t('Table header was printed.'));
  197. }
  198. }
  199. /**
  200. * Unit tests for theme_item_list().
  201. */
  202. class ThemeItemListUnitTest extends DrupalWebTestCase {
  203. public static function getInfo() {
  204. return array(
  205. 'name' => 'Theme item list',
  206. 'description' => 'Test the theme_item_list() function.',
  207. 'group' => 'Theme',
  208. );
  209. }
  210. /**
  211. * Test item list rendering.
  212. */
  213. function testItemList() {
  214. $items = array('a', array('data' => 'b', 'children' => array('c' => 'c', 'd' => 'd', 'e' => 'e')), 'f');
  215. $expected = '<div class="item-list"><ul><li class="first">a</li>
  216. <li>b<div class="item-list"><ul><li class="first">c</li>
  217. <li>d</li>
  218. <li class="last">e</li>
  219. </ul></div></li>
  220. <li class="last">f</li>
  221. </ul></div>';
  222. $output = theme('item_list', array('items' => $items));
  223. $this->assertIdentical($expected, $output, 'Item list is rendered correctly.');
  224. }
  225. }
  226. /**
  227. * Unit tests for theme_links().
  228. */
  229. class ThemeLinksTest extends DrupalWebTestCase {
  230. public static function getInfo() {
  231. return array(
  232. 'name' => 'Links',
  233. 'description' => 'Test the theme_links() function and rendering groups of links.',
  234. 'group' => 'Theme',
  235. );
  236. }
  237. /**
  238. * Test the use of drupal_pre_render_links() on a nested array of links.
  239. */
  240. function testDrupalPreRenderLinks() {
  241. // Define the base array to be rendered, containing a variety of different
  242. // kinds of links.
  243. $base_array = array(
  244. '#theme' => 'links',
  245. '#pre_render' => array('drupal_pre_render_links'),
  246. '#links' => array(
  247. 'parent_link' => array(
  248. 'title' => 'Parent link original',
  249. 'href' => 'parent-link-original',
  250. ),
  251. ),
  252. 'first_child' => array(
  253. '#theme' => 'links',
  254. '#links' => array(
  255. // This should be rendered if 'first_child' is rendered separately,
  256. // but ignored if the parent is being rendered (since it duplicates
  257. // one of the parent's links).
  258. 'parent_link' => array(
  259. 'title' => 'Parent link copy',
  260. 'href' => 'parent-link-copy',
  261. ),
  262. // This should always be rendered.
  263. 'first_child_link' => array(
  264. 'title' => 'First child link',
  265. 'href' => 'first-child-link',
  266. ),
  267. ),
  268. ),
  269. // This should always be rendered as part of the parent.
  270. 'second_child' => array(
  271. '#theme' => 'links',
  272. '#links' => array(
  273. 'second_child_link' => array(
  274. 'title' => 'Second child link',
  275. 'href' => 'second-child-link',
  276. ),
  277. ),
  278. ),
  279. // This should never be rendered, since the user does not have access to
  280. // it.
  281. 'third_child' => array(
  282. '#theme' => 'links',
  283. '#links' => array(
  284. 'third_child_link' => array(
  285. 'title' => 'Third child link',
  286. 'href' => 'third-child-link',
  287. ),
  288. ),
  289. '#access' => FALSE,
  290. ),
  291. );
  292. // Start with a fresh copy of the base array, and try rendering the entire
  293. // thing. We expect a single <ul> with appropriate links contained within
  294. // it.
  295. $render_array = $base_array;
  296. $html = drupal_render($render_array);
  297. $dom = new DOMDocument();
  298. $dom->loadHTML($html);
  299. $this->assertEqual($dom->getElementsByTagName('ul')->length, 1, t('One "ul" tag found in the rendered HTML.'));
  300. $list_elements = $dom->getElementsByTagName('li');
  301. $this->assertEqual($list_elements->length, 3, t('Three "li" tags found in the rendered HTML.'));
  302. $this->assertEqual($list_elements->item(0)->nodeValue, 'Parent link original', t('First expected link found.'));
  303. $this->assertEqual($list_elements->item(1)->nodeValue, 'First child link', t('Second expected link found.'));
  304. $this->assertEqual($list_elements->item(2)->nodeValue, 'Second child link', t('Third expected link found.'));
  305. $this->assertIdentical(strpos($html, 'Parent link copy'), FALSE, t('"Parent link copy" link not found.'));
  306. $this->assertIdentical(strpos($html, 'Third child link'), FALSE, t('"Third child link" link not found.'));
  307. // Now render 'first_child', followed by the rest of the links, and make
  308. // sure we get two separate <ul>'s with the appropriate links contained
  309. // within each.
  310. $render_array = $base_array;
  311. $child_html = drupal_render($render_array['first_child']);
  312. $parent_html = drupal_render($render_array);
  313. // First check the child HTML.
  314. $dom = new DOMDocument();
  315. $dom->loadHTML($child_html);
  316. $this->assertEqual($dom->getElementsByTagName('ul')->length, 1, t('One "ul" tag found in the rendered child HTML.'));
  317. $list_elements = $dom->getElementsByTagName('li');
  318. $this->assertEqual($list_elements->length, 2, t('Two "li" tags found in the rendered child HTML.'));
  319. $this->assertEqual($list_elements->item(0)->nodeValue, 'Parent link copy', t('First expected link found.'));
  320. $this->assertEqual($list_elements->item(1)->nodeValue, 'First child link', t('Second expected link found.'));
  321. // Then check the parent HTML.
  322. $dom = new DOMDocument();
  323. $dom->loadHTML($parent_html);
  324. $this->assertEqual($dom->getElementsByTagName('ul')->length, 1, t('One "ul" tag found in the rendered parent HTML.'));
  325. $list_elements = $dom->getElementsByTagName('li');
  326. $this->assertEqual($list_elements->length, 2, t('Two "li" tags found in the rendered parent HTML.'));
  327. $this->assertEqual($list_elements->item(0)->nodeValue, 'Parent link original', t('First expected link found.'));
  328. $this->assertEqual($list_elements->item(1)->nodeValue, 'Second child link', t('Second expected link found.'));
  329. $this->assertIdentical(strpos($parent_html, 'First child link'), FALSE, t('"First child link" link not found.'));
  330. $this->assertIdentical(strpos($parent_html, 'Third child link'), FALSE, t('"Third child link" link not found.'));
  331. }
  332. }
  333. /**
  334. * Functional test for initialization of the theme system in hook_init().
  335. */
  336. class ThemeHookInitTestCase extends DrupalWebTestCase {
  337. public static function getInfo() {
  338. return array(
  339. 'name' => 'Theme initialization in hook_init()',
  340. 'description' => 'Tests that the theme system can be correctly initialized in hook_init().',
  341. 'group' => 'Theme',
  342. );
  343. }
  344. function setUp() {
  345. parent::setUp('theme_test');
  346. }
  347. /**
  348. * Test that the theme system can generate output when called by hook_init().
  349. */
  350. function testThemeInitializationHookInit() {
  351. $this->drupalGet('theme-test/hook-init');
  352. $this->assertRaw('Themed output generated in hook_init()', t('Themed output generated in hook_init() correctly appears on the page.'));
  353. $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()."));
  354. }
  355. }
  356. /**
  357. * Tests autocompletion not loading registry.
  358. */
  359. class ThemeFastTestCase extends DrupalWebTestCase {
  360. public static function getInfo() {
  361. return array(
  362. 'name' => 'Theme fast initialization',
  363. 'description' => 'Test that autocompletion does not load the registry.',
  364. 'group' => 'Theme'
  365. );
  366. }
  367. function setUp() {
  368. parent::setUp('theme_test');
  369. $this->account = $this->drupalCreateUser(array('access user profiles'));
  370. }
  371. /**
  372. * Tests access to user autocompletion and verify the correct results.
  373. */
  374. function testUserAutocomplete() {
  375. $this->drupalLogin($this->account);
  376. $this->drupalGet('user/autocomplete/' . $this->account->name);
  377. $this->assertText('registry not initialized', t('The registry was not initialized'));
  378. }
  379. }
  380. /**
  381. * Unit tests for theme_html_tag().
  382. */
  383. class ThemeHtmlTag extends DrupalUnitTestCase {
  384. public static function getInfo() {
  385. return array(
  386. 'name' => 'Theme HTML Tag',
  387. 'description' => 'Tests theme_html_tag() built-in theme functions.',
  388. 'group' => 'Theme',
  389. );
  390. }
  391. /**
  392. * Test function theme_html_tag()
  393. */
  394. function testThemeHtmlTag() {
  395. // Test auto-closure meta tag generation
  396. $tag['element'] = array('#tag' => 'meta', '#attributes' => array('name' => 'description', 'content' => 'Drupal test'));
  397. $this->assertEqual('<meta name="description" content="Drupal test" />'."\n", theme_html_tag($tag), t('Test auto-closure meta tag generation.'));
  398. // Test title tag generation
  399. $tag['element'] = array('#tag' => 'title', '#value' => 'title test');
  400. $this->assertEqual('<title>title test</title>'."\n", theme_html_tag($tag), t('Test title tag generation.'));
  401. }
  402. }
  403. /**
  404. * Tests for the ThemeRegistry class.
  405. */
  406. class ThemeRegistryTestCase extends DrupalWebTestCase {
  407. public static function getInfo() {
  408. return array(
  409. 'name' => 'ThemeRegistry',
  410. 'description' => 'Tests the behavior of the ThemeRegistry class',
  411. 'group' => 'Theme',
  412. );
  413. }
  414. function setUp() {
  415. parent::setUp('theme_test');
  416. }
  417. /**
  418. * Tests the behavior of the theme registry class.
  419. */
  420. function testRaceCondition() {
  421. $_SERVER['REQUEST_METHOD'] = 'GET';
  422. $cid = 'test_theme_registry';
  423. // Directly instantiate the theme registry, this will cause a base cache
  424. // entry to be written in __construct().
  425. $registry = new ThemeRegistry($cid, 'cache');
  426. $this->assertTrue(cache_get($cid), 'Cache entry was created.');
  427. // Trigger a cache miss for an offset.
  428. $this->assertTrue($registry['theme_test_template_test'], 'Offset was returned correctly from the theme registry.');
  429. // This will cause the ThemeRegistry class to write an updated version of
  430. // the cache entry when it is destroyed, usually at the end of the request.
  431. // Before that happens, manually delete the cache entry we created earlier
  432. // so that the new entry is written from scratch.
  433. cache_clear_all($cid, 'cache');
  434. // Destroy the class so that it triggers a cache write for the offset.
  435. unset($registry);
  436. $this->assertTrue(cache_get($cid), 'Cache entry was created.');
  437. // Create a new instance of the class. Confirm that both the offset
  438. // requested previously, and one that has not yet been requested are both
  439. // available.
  440. $registry = new ThemeRegistry($cid, 'cache');
  441. $this->assertTrue($registry['theme_test_template_test'], 'Offset was returned correctly from the theme registry');
  442. $this->assertTrue($registry['theme_test_template_test_2'], 'Offset was returned correctly from the theme registry');
  443. }
  444. }