render_example.test

  1. examples
    1. 7 render_example/render_example.test
    2. 8 render_example/render_example.test

Test for the render example module.

Classes

NameDescription
RenderExampleTestCase@file Test for the render example module.

File

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