theme.test 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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'), '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'), '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'), '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'), '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, '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), '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.', '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', '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'), 'Enabled theme detected');
  121. // Check if list_themes() returns disabled themes.
  122. $this->assertTrue(array_key_exists('test_basetheme', $themes), '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, 'Base theme\'s object includes list of subthemes.');
  127. $this->assertIdentical($themes['test_subtheme']->base_themes, $base_theme_list, 'Subtheme\'s object includes list of base themes.');
  128. // Check for theme engine in subtheme.
  129. $this->assertIdentical($themes['test_subtheme']->engine, 'phptemplate', 'Subtheme\'s object includes the theme engine.');
  130. // Check for theme engine prefix.
  131. $this->assertIdentical($themes['test_basetheme']->prefix, 'phptemplate', 'Base theme\'s object includes the theme engine prefix.');
  132. $this->assertIdentical($themes['test_subtheme']->prefix, 'phptemplate', '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', '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'), 'Base theme\'s default settings values can be overridden by subtheme.');
  141. $this->assertIdentical(theme_get_setting('basetheme_only', 'test_subtheme'), 'base theme value', 'Base theme\'s default settings values are inherited by subtheme.');
  142. }
  143. /**
  144. * Test the drupal_add_region_content() function.
  145. */
  146. function testDrupalAddRegionContent() {
  147. $this->drupalGet('theme-test/drupal-add-region-content');
  148. $this->assertText('Hello');
  149. $this->assertText('World');
  150. }
  151. }
  152. /**
  153. * Unit tests for theme_table().
  154. */
  155. class ThemeTableTestCase extends DrupalWebTestCase {
  156. public static function getInfo() {
  157. return array(
  158. 'name' => 'Theme Table',
  159. 'description' => 'Tests built-in theme functions.',
  160. 'group' => 'Theme',
  161. );
  162. }
  163. /**
  164. * Tableheader.js provides 'sticky' table headers, and is included by default.
  165. */
  166. function testThemeTableStickyHeaders() {
  167. $header = array('one', 'two', 'three');
  168. $rows = array(array(1,2,3), array(4,5,6), array(7,8,9));
  169. $this->content = theme('table', array('header' => $header, 'rows' => $rows));
  170. $js = drupal_add_js();
  171. $this->assertTrue(isset($js['misc/tableheader.js']), 'tableheader.js was included when $sticky = TRUE.');
  172. $this->assertRaw('sticky-enabled', 'Table has a class of sticky-enabled when $sticky = TRUE.');
  173. drupal_static_reset('drupal_add_js');
  174. }
  175. /**
  176. * If $sticky is FALSE, no tableheader.js should be included.
  177. */
  178. function testThemeTableNoStickyHeaders() {
  179. $header = array('one', 'two', 'three');
  180. $rows = array(array(1,2,3), array(4,5,6), array(7,8,9));
  181. $attributes = array();
  182. $caption = NULL;
  183. $colgroups = array();
  184. $this->content = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => $attributes, 'caption' => $caption, 'colgroups' => $colgroups, 'sticky' => FALSE));
  185. $js = drupal_add_js();
  186. $this->assertFalse(isset($js['misc/tableheader.js']), 'tableheader.js was not included because $sticky = FALSE.');
  187. $this->assertNoRaw('sticky-enabled', 'Table does not have a class of sticky-enabled because $sticky = FALSE.');
  188. drupal_static_reset('drupal_add_js');
  189. }
  190. /**
  191. * Tests that the table header is printed correctly even if there are no rows,
  192. * and that the empty text is displayed correctly.
  193. */
  194. function testThemeTableWithEmptyMessage() {
  195. $header = array(
  196. t('Header 1'),
  197. array(
  198. 'data' => t('Header 2'),
  199. 'colspan' => 2,
  200. ),
  201. );
  202. $this->content = theme('table', array('header' => $header, 'rows' => array(), 'empty' => t('No strings available.')));
  203. $this->assertRaw('<tr class="odd"><td colspan="3" class="empty message">No strings available.</td>', 'Correct colspan was set on empty message.');
  204. $this->assertRaw('<thead><tr><th>Header 1</th>', 'Table header was printed.');
  205. }
  206. /**
  207. * Tests that the 'no_striping' option works correctly.
  208. */
  209. function testThemeTableWithNoStriping() {
  210. $rows = array(
  211. array(
  212. 'data' => array(1),
  213. 'no_striping' => TRUE,
  214. ),
  215. );
  216. $this->content = theme('table', array('rows' => $rows));
  217. $this->assertNoRaw('class="odd"', 'Odd/even classes were not added because $no_striping = TRUE.');
  218. $this->assertNoRaw('no_striping', 'No invalid no_striping HTML attribute was printed.');
  219. }
  220. /**
  221. * Test that the 'footer' option works correctly.
  222. */
  223. function testThemeTableFooter() {
  224. $footer = array(
  225. array(
  226. 'data' => array(1),
  227. ),
  228. array('Foo'),
  229. );
  230. $table = array(
  231. 'rows' => array(),
  232. 'footer' => $footer,
  233. );
  234. $this->content = theme('table', $table);
  235. $this->content = preg_replace('@>\s+<@', '><', $this->content);
  236. $this->assertRaw('<tfoot><tr><td>1</td></tr><tr><td>Foo</td></tr></tfoot>', 'Table footer found.');
  237. }
  238. }
  239. /**
  240. * Unit tests for theme_item_list().
  241. */
  242. class ThemeItemListUnitTest extends DrupalWebTestCase {
  243. public static function getInfo() {
  244. return array(
  245. 'name' => 'Theme item list',
  246. 'description' => 'Test the theme_item_list() function.',
  247. 'group' => 'Theme',
  248. );
  249. }
  250. /**
  251. * Test item list rendering.
  252. */
  253. function testItemList() {
  254. $items = array('a', array('data' => 'b', 'children' => array('c' => 'c', 'd' => 'd', 'e' => 'e')), 'f');
  255. $expected = '<div class="item-list"><ul><li class="first">a</li>
  256. <li>b<div class="item-list"><ul><li class="first">c</li>
  257. <li>d</li>
  258. <li class="last">e</li>
  259. </ul></div></li>
  260. <li class="last">f</li>
  261. </ul></div>';
  262. $output = theme('item_list', array('items' => $items));
  263. $this->assertIdentical($expected, $output, 'Item list is rendered correctly.');
  264. }
  265. }
  266. /**
  267. * Unit tests for theme_links().
  268. */
  269. class ThemeLinksTest extends DrupalWebTestCase {
  270. public static function getInfo() {
  271. return array(
  272. 'name' => 'Links',
  273. 'description' => 'Test the theme_links() function and rendering groups of links.',
  274. 'group' => 'Theme',
  275. );
  276. }
  277. /**
  278. * Test the use of drupal_pre_render_links() on a nested array of links.
  279. */
  280. function testDrupalPreRenderLinks() {
  281. // Define the base array to be rendered, containing a variety of different
  282. // kinds of links.
  283. $base_array = array(
  284. '#theme' => 'links',
  285. '#pre_render' => array('drupal_pre_render_links'),
  286. '#links' => array(
  287. 'parent_link' => array(
  288. 'title' => 'Parent link original',
  289. 'href' => 'parent-link-original',
  290. ),
  291. ),
  292. 'first_child' => array(
  293. '#theme' => 'links',
  294. '#links' => array(
  295. // This should be rendered if 'first_child' is rendered separately,
  296. // but ignored if the parent is being rendered (since it duplicates
  297. // one of the parent's links).
  298. 'parent_link' => array(
  299. 'title' => 'Parent link copy',
  300. 'href' => 'parent-link-copy',
  301. ),
  302. // This should always be rendered.
  303. 'first_child_link' => array(
  304. 'title' => 'First child link',
  305. 'href' => 'first-child-link',
  306. ),
  307. ),
  308. ),
  309. // This should always be rendered as part of the parent.
  310. 'second_child' => array(
  311. '#theme' => 'links',
  312. '#links' => array(
  313. 'second_child_link' => array(
  314. 'title' => 'Second child link',
  315. 'href' => 'second-child-link',
  316. ),
  317. ),
  318. ),
  319. // This should never be rendered, since the user does not have access to
  320. // it.
  321. 'third_child' => array(
  322. '#theme' => 'links',
  323. '#links' => array(
  324. 'third_child_link' => array(
  325. 'title' => 'Third child link',
  326. 'href' => 'third-child-link',
  327. ),
  328. ),
  329. '#access' => FALSE,
  330. ),
  331. );
  332. // Start with a fresh copy of the base array, and try rendering the entire
  333. // thing. We expect a single <ul> with appropriate links contained within
  334. // it.
  335. $render_array = $base_array;
  336. $html = drupal_render($render_array);
  337. $dom = new DOMDocument();
  338. $dom->loadHTML($html);
  339. $this->assertEqual($dom->getElementsByTagName('ul')->length, 1, 'One "ul" tag found in the rendered HTML.');
  340. $list_elements = $dom->getElementsByTagName('li');
  341. $this->assertEqual($list_elements->length, 3, 'Three "li" tags found in the rendered HTML.');
  342. $this->assertEqual($list_elements->item(0)->nodeValue, 'Parent link original', 'First expected link found.');
  343. $this->assertEqual($list_elements->item(1)->nodeValue, 'First child link', 'Second expected link found.');
  344. $this->assertEqual($list_elements->item(2)->nodeValue, 'Second child link', 'Third expected link found.');
  345. $this->assertIdentical(strpos($html, 'Parent link copy'), FALSE, '"Parent link copy" link not found.');
  346. $this->assertIdentical(strpos($html, 'Third child link'), FALSE, '"Third child link" link not found.');
  347. // Now render 'first_child', followed by the rest of the links, and make
  348. // sure we get two separate <ul>'s with the appropriate links contained
  349. // within each.
  350. $render_array = $base_array;
  351. $child_html = drupal_render($render_array['first_child']);
  352. $parent_html = drupal_render($render_array);
  353. // First check the child HTML.
  354. $dom = new DOMDocument();
  355. $dom->loadHTML($child_html);
  356. $this->assertEqual($dom->getElementsByTagName('ul')->length, 1, 'One "ul" tag found in the rendered child HTML.');
  357. $list_elements = $dom->getElementsByTagName('li');
  358. $this->assertEqual($list_elements->length, 2, 'Two "li" tags found in the rendered child HTML.');
  359. $this->assertEqual($list_elements->item(0)->nodeValue, 'Parent link copy', 'First expected link found.');
  360. $this->assertEqual($list_elements->item(1)->nodeValue, 'First child link', 'Second expected link found.');
  361. // Then check the parent HTML.
  362. $dom = new DOMDocument();
  363. $dom->loadHTML($parent_html);
  364. $this->assertEqual($dom->getElementsByTagName('ul')->length, 1, 'One "ul" tag found in the rendered parent HTML.');
  365. $list_elements = $dom->getElementsByTagName('li');
  366. $this->assertEqual($list_elements->length, 2, 'Two "li" tags found in the rendered parent HTML.');
  367. $this->assertEqual($list_elements->item(0)->nodeValue, 'Parent link original', 'First expected link found.');
  368. $this->assertEqual($list_elements->item(1)->nodeValue, 'Second child link', 'Second expected link found.');
  369. $this->assertIdentical(strpos($parent_html, 'First child link'), FALSE, '"First child link" link not found.');
  370. $this->assertIdentical(strpos($parent_html, 'Third child link'), FALSE, '"Third child link" link not found.');
  371. }
  372. }
  373. /**
  374. * Functional test for initialization of the theme system in hook_init().
  375. */
  376. class ThemeHookInitTestCase extends DrupalWebTestCase {
  377. public static function getInfo() {
  378. return array(
  379. 'name' => 'Theme initialization in hook_init()',
  380. 'description' => 'Tests that the theme system can be correctly initialized in hook_init().',
  381. 'group' => 'Theme',
  382. );
  383. }
  384. function setUp() {
  385. parent::setUp('theme_test');
  386. }
  387. /**
  388. * Test that the theme system can generate output when called by hook_init().
  389. */
  390. function testThemeInitializationHookInit() {
  391. $this->drupalGet('theme-test/hook-init');
  392. $this->assertRaw('Themed output generated in hook_init()', 'Themed output generated in hook_init() correctly appears on the page.');
  393. $this->assertRaw('bartik/css/style.css', "The default theme's CSS appears on the page when the theme system is initialized in hook_init().");
  394. }
  395. }
  396. /**
  397. * Tests autocompletion not loading registry.
  398. */
  399. class ThemeFastTestCase extends DrupalWebTestCase {
  400. protected $account;
  401. public static function getInfo() {
  402. return array(
  403. 'name' => 'Theme fast initialization',
  404. 'description' => 'Test that autocompletion does not load the registry.',
  405. 'group' => 'Theme'
  406. );
  407. }
  408. function setUp() {
  409. parent::setUp('theme_test');
  410. $this->account = $this->drupalCreateUser(array('access user profiles'));
  411. }
  412. /**
  413. * Tests access to user autocompletion and verify the correct results.
  414. */
  415. function testUserAutocomplete() {
  416. $this->drupalLogin($this->account);
  417. $this->drupalGet('user/autocomplete/' . $this->account->name);
  418. $this->assertText('registry not initialized', 'The registry was not initialized');
  419. }
  420. }
  421. /**
  422. * Tests the markup of core render element types passed to drupal_render().
  423. */
  424. class RenderElementTypesTestCase extends DrupalWebTestCase {
  425. public static function getInfo() {
  426. return array(
  427. 'name' => 'Render element types',
  428. 'description' => 'Tests the markup of core render element types passed to drupal_render().',
  429. 'group' => 'Theme',
  430. );
  431. }
  432. /**
  433. * Asserts that an array of elements is rendered properly.
  434. *
  435. * @param array $elements
  436. * An array of associative arrays describing render elements and their
  437. * expected markup. Each item in $elements must contain the following:
  438. * - 'name': This human readable description will be displayed on the test
  439. * results page.
  440. * - 'value': This is the render element to test.
  441. * - 'expected': This is the expected markup for the element in 'value'.
  442. */
  443. function assertElements($elements) {
  444. foreach($elements as $element) {
  445. $this->assertIdentical(drupal_render($element['value']), $element['expected'], '"' . $element['name'] . '" input rendered correctly by drupal_render().');
  446. }
  447. }
  448. /**
  449. * Tests system #type 'container'.
  450. */
  451. function testContainer() {
  452. $elements = array(
  453. // Basic container with no attributes.
  454. array(
  455. 'name' => "#type 'container' with no HTML attributes",
  456. 'value' => array(
  457. '#type' => 'container',
  458. 'child' => array(
  459. '#markup' => 'foo',
  460. ),
  461. ),
  462. 'expected' => '<div>foo</div>',
  463. ),
  464. // Container with a class.
  465. array(
  466. 'name' => "#type 'container' with a class HTML attribute",
  467. 'value' => array(
  468. '#type' => 'container',
  469. 'child' => array(
  470. '#markup' => 'foo',
  471. ),
  472. '#attributes' => array(
  473. 'class' => 'bar',
  474. ),
  475. ),
  476. 'expected' => '<div class="bar">foo</div>',
  477. ),
  478. );
  479. $this->assertElements($elements);
  480. }
  481. /**
  482. * Tests system #type 'html_tag'.
  483. */
  484. function testHtmlTag() {
  485. $elements = array(
  486. // Test auto-closure meta tag generation.
  487. array(
  488. 'name' => "#type 'html_tag' auto-closure meta tag generation",
  489. 'value' => array(
  490. '#type' => 'html_tag',
  491. '#tag' => 'meta',
  492. '#attributes' => array(
  493. 'name' => 'description',
  494. 'content' => 'Drupal test',
  495. ),
  496. ),
  497. 'expected' => '<meta name="description" content="Drupal test" />' . "\n",
  498. ),
  499. // Test title tag generation.
  500. array(
  501. 'name' => "#type 'html_tag' title tag generation",
  502. 'value' => array(
  503. '#type' => 'html_tag',
  504. '#tag' => 'title',
  505. '#value' => 'title test',
  506. ),
  507. 'expected' => '<title>title test</title>' . "\n",
  508. ),
  509. );
  510. $this->assertElements($elements);
  511. }
  512. }
  513. /**
  514. * Tests for the ThemeRegistry class.
  515. */
  516. class ThemeRegistryTestCase extends DrupalWebTestCase {
  517. public static function getInfo() {
  518. return array(
  519. 'name' => 'ThemeRegistry',
  520. 'description' => 'Tests the behavior of the ThemeRegistry class',
  521. 'group' => 'Theme',
  522. );
  523. }
  524. function setUp() {
  525. parent::setUp('theme_test');
  526. }
  527. /**
  528. * Tests the behavior of the theme registry class.
  529. */
  530. function testRaceCondition() {
  531. $_SERVER['REQUEST_METHOD'] = 'GET';
  532. $cid = 'test_theme_registry';
  533. // Directly instantiate the theme registry, this will cause a base cache
  534. // entry to be written in __construct().
  535. $registry = new ThemeRegistry($cid, 'cache');
  536. $this->assertTrue(cache_get($cid), 'Cache entry was created.');
  537. // Trigger a cache miss for an offset.
  538. $this->assertTrue($registry['theme_test_template_test'], 'Offset was returned correctly from the theme registry.');
  539. // This will cause the ThemeRegistry class to write an updated version of
  540. // the cache entry when it is destroyed, usually at the end of the request.
  541. // Before that happens, manually delete the cache entry we created earlier
  542. // so that the new entry is written from scratch.
  543. cache_clear_all($cid, 'cache');
  544. // Destroy the class so that it triggers a cache write for the offset.
  545. unset($registry);
  546. $this->assertTrue(cache_get($cid), 'Cache entry was created.');
  547. // Create a new instance of the class. Confirm that both the offset
  548. // requested previously, and one that has not yet been requested are both
  549. // available.
  550. $registry = new ThemeRegistry($cid, 'cache');
  551. $this->assertTrue($registry['theme_test_template_test'], 'Offset was returned correctly from the theme registry');
  552. $this->assertTrue($registry['theme_test_template_test_2'], 'Offset was returned correctly from the theme registry');
  553. }
  554. }
  555. /**
  556. * Tests for theme debug markup.
  557. */
  558. class ThemeDebugMarkupTestCase extends DrupalWebTestCase {
  559. public static function getInfo() {
  560. return array(
  561. 'name' => 'Theme debug markup',
  562. 'description' => 'Tests theme debug markup output.',
  563. 'group' => 'Theme',
  564. );
  565. }
  566. function setUp() {
  567. parent::setUp('theme_test', 'node');
  568. theme_enable(array('test_theme'));
  569. }
  570. /**
  571. * Tests debug markup added to template output.
  572. */
  573. function testDebugOutput() {
  574. variable_set('theme_default', 'test_theme');
  575. // Enable the debug output.
  576. variable_set('theme_debug', TRUE);
  577. $registry = theme_get_registry();
  578. $extension = '.tpl.php';
  579. // Populate array of templates.
  580. $templates = drupal_find_theme_templates($registry, $extension, drupal_get_path('theme', 'test_theme'));
  581. $templates += drupal_find_theme_templates($registry, $extension, drupal_get_path('module', 'node'));
  582. // Create a node and test different features of the debug markup.
  583. $node = $this->drupalCreateNode();
  584. $this->drupalGet('node/' . $node->nid);
  585. $this->assertRaw('<!-- THEME DEBUG -->', 'Theme debug markup found in theme output when debug is enabled.');
  586. $this->assertRaw("CALL: theme('node')", 'Theme call information found.');
  587. $this->assertRaw('x node--1' . $extension . PHP_EOL . ' * node--page' . $extension . PHP_EOL . ' * node' . $extension, 'Suggested template files found in order and node ID specific template shown as current template.');
  588. $template_filename = $templates['node__1']['path'] . '/' . $templates['node__1']['template'] . $extension;
  589. $this->assertRaw("BEGIN OUTPUT from '$template_filename'", 'Full path to current template file found.');
  590. // Create another node and make sure the template suggestions shown in the
  591. // debug markup are correct.
  592. $node2 = $this->drupalCreateNode();
  593. $this->drupalGet('node/' . $node2->nid);
  594. $this->assertRaw('* node--2' . $extension . PHP_EOL . ' * node--page' . $extension . PHP_EOL . ' x node' . $extension, 'Suggested template files found in order and base template shown as current template.');
  595. // Create another node and make sure the template suggestions shown in the
  596. // debug markup are correct.
  597. $node3 = $this->drupalCreateNode();
  598. $build = array('#theme' => 'node__foo__bar');
  599. $build += node_view($node3);
  600. $output = drupal_render($build);
  601. $this->assertTrue(strpos($output, "CALL: theme('node__foo__bar')") !== FALSE, 'Theme call information found.');
  602. $this->assertTrue(strpos($output, '* node--foo--bar' . $extension . PHP_EOL . ' * node--foo' . $extension . PHP_EOL . ' * node--3' . $extension . PHP_EOL . ' * node--page' . $extension . PHP_EOL . ' x node' . $extension) !== FALSE, 'Suggested template files found in order and base template shown as current template.');
  603. // Disable theme debug.
  604. variable_set('theme_debug', FALSE);
  605. $this->drupalGet('node/' . $node->nid);
  606. $this->assertNoRaw('<!-- THEME DEBUG -->', 'Theme debug markup not found in theme output when debug is disabled.');
  607. }
  608. }
  609. /**
  610. * Tests module-provided theme engines.
  611. */
  612. class ModuleProvidedThemeEngineTestCase extends DrupalWebTestCase {
  613. public static function getInfo() {
  614. return array(
  615. 'name' => 'Theme engine test',
  616. 'description' => 'Tests module-provided theme engines.',
  617. 'group' => 'Theme',
  618. );
  619. }
  620. function setUp() {
  621. parent::setUp('theme_test');
  622. theme_enable(array('test_theme', 'test_theme_nyan_cat'));
  623. }
  624. /**
  625. * Ensures that the module provided theme engine is found and used by core.
  626. */
  627. function testEngineIsFoundAndWorking() {
  628. variable_set('theme_default', 'test_theme_nyan_cat');
  629. variable_set('admin_theme', 'test_theme_nyan_cat');
  630. $this->drupalGet('theme-test/engine-info-test');
  631. $this->assertText('Miaou');
  632. }
  633. }