render_example.test 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * @file
  4. * Test for the render example module.
  5. */
  6. /**
  7. * Functional tests for the Render Example module.
  8. *
  9. * @ingroup render_example
  10. */
  11. class RenderExampleTestCase extends DrupalWebTestCase {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public static function getInfo() {
  16. return array(
  17. 'name' => 'Render example functionality',
  18. 'description' => 'Test Render Example',
  19. 'group' => 'Examples',
  20. 'dependencies' => array('devel'),
  21. );
  22. }
  23. /**
  24. * Enable modules and create user with specific permissions.
  25. */
  26. public function setUp() {
  27. parent::setUp('devel', 'render_example');
  28. }
  29. /**
  30. * Assert that all of the xpaths in the array have results.
  31. *
  32. * @param array $xpath_array
  33. * An array of xpaths, each of which must return something.
  34. */
  35. public function assertRenderResults($xpath_array) {
  36. foreach ($xpath_array as $xpath) {
  37. $result = $this->xpath($xpath);
  38. $this->assertTrue(!empty($result), format_string('Found xpath %xpath', array('%xpath' => $xpath)));
  39. }
  40. }
  41. /**
  42. * Asserts that the string value of the result is the same as the passed text.
  43. *
  44. * @param array $xpath_array
  45. * Array of keyed arrays of tests to be made. Each child array consists of
  46. * $xpath => $expected_text
  47. */
  48. public function assertRenderedText($xpath_array) {
  49. foreach ($xpath_array as $xpath => $text) {
  50. $result = $this->xpath($xpath);
  51. $this->assertTrue((string) $result[0][0] == $text, format_string('%ary selects text %text', array('%ary' => $xpath, '%text' => $text)));
  52. }
  53. }
  54. /**
  55. * Basic test of rendering through user interaction.
  56. *
  57. * Login user, create an example node, and test blog functionality through
  58. * the admin and user interfaces.
  59. */
  60. public function testRenderExampleBasic() {
  61. // Create a user that can access devel information and log in.
  62. $web_user = $this->drupalCreateUser(array('access devel information', 'access content'));
  63. $this->drupalLogin($web_user);
  64. // Turn on the block render array display and make sure it shows up.
  65. $edit = array(
  66. 'render_example_show_block' => TRUE,
  67. );
  68. $this->drupalPost('examples/render_example/altering', $edit, t('Save configuration'));
  69. $xpath_array = array(
  70. "//div[@id='sidebar-first']//fieldset[starts-with(@id, 'edit-render-example-block-fieldset')]",
  71. '//*[@id="content"]//fieldset[contains(@id,"edit-render-example-block-fieldset")]',
  72. );
  73. $this->assertRenderResults($xpath_array);
  74. // Turn off block render array display and turn on the page render array
  75. // display.
  76. $edit = array(
  77. 'render_example_show_page' => TRUE,
  78. 'render_example_show_block' => FALSE,
  79. );
  80. $this->drupalPost('examples/render_example/altering', $edit, t('Save configuration'));
  81. $xpath_array = array(
  82. '//*[@id="content"]//fieldset[starts-with(@id,"edit-render-example-page-fieldset")]',
  83. );
  84. $this->assertRenderResults($xpath_array);
  85. // Add note about render arrays to the top of sidebar_first.
  86. $edit = array(
  87. 'render_example_note_about_render_arrays' => TRUE,
  88. );
  89. $this->drupalPost('examples/render_example/altering', $edit, t('Save configuration'));
  90. $xpath_array = array(
  91. '//*[@id="sidebar-first"]//ol//li[starts-with(.,"Render arrays are everywhere")]',
  92. );
  93. $this->assertRenderResults($xpath_array);
  94. // Move the navigation menu to the top of the content area.
  95. $edit = array(
  96. 'render_example_move_navigation_menu' => TRUE,
  97. );
  98. $this->drupalPost('examples/render_example/altering', $edit, t('Save configuration'));
  99. $xpath_array = array(
  100. '//*[@id="content"]//h2[starts-with(.,"Navigation")]',
  101. );
  102. $this->assertRenderResults($xpath_array);
  103. // Skip a test for reversing order of sidebar_first as I think it would
  104. // be too fragile.
  105. //
  106. // Test the addition of #prefix and #suffix
  107. $edit = array(
  108. 'render_example_prefix' => TRUE,
  109. );
  110. $this->drupalPost('examples/render_example/altering', $edit, t('Save configuration'));
  111. $xpath_array = array(
  112. '//*[@id="sidebar-first"]//*[contains(@class, "block-prefix")]/span[contains(@class, "block-suffix")]',
  113. );
  114. $this->assertRenderResults($xpath_array);
  115. // Test some rendering facets of the various render examples.
  116. $this->drupalGet('examples/render_example/arrays');
  117. $content = $this->xpath('//*[@class="render-array"][1]');
  118. $xpath_array = array(
  119. '//div[@class="rendered"][starts-with(.,"Some basic text in a #markup")]' => 'Some basic text in a #markup (shows basic markup and how it is rendered)',
  120. '//div[@class="rendered"][starts-with(.,"This is some text that should be put to")]' => 'This is some text that should be put together | This is some more text that we need | ',
  121. '//div[@class="rendered"][starts-with(.,"The current time was")]' => 'The current time was when this was cached. Updated every seconds',
  122. '//div[@class="rendered"]/div[text()][starts-with(.,"(prefix)This one")]' => '(prefix)This one adds a prefix and suffix, which put a div around the item(suffix)',
  123. '//div[@class="rendered"]/div[text()][starts-with(.,"markup for pre_")]' => 'markup for pre_render and post_render example',
  124. '//div[@class="rendered"]/div[text()][starts-with(.,"This markup was added")]' => 'This markup was added after rendering by a #post_render',
  125. '//div[@class="rendered"]/div[text()][starts-with(.,"This #suffix")]' => 'This #suffix was added by a #pre_render',
  126. );
  127. $this->assertRenderedText($xpath_array);
  128. }
  129. }