boxes.test 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. class BoxesTestCase extends DrupalWebTestCase {
  3. protected $profile = 'testing';
  4. /**
  5. * Implementation of getInfo().
  6. */
  7. public static function getInfo() {
  8. return array(
  9. 'name' => t('Boxes functionality'),
  10. 'description' => t('Add and delete custom boxes.'),
  11. 'group' => t('Boxes'),
  12. );
  13. }
  14. /**
  15. * Implementation of setUp().
  16. */
  17. function setUp() {
  18. parent::setUp(array('comment', 'ctools', 'block', 'boxes'));
  19. // Create and login user
  20. $admin_user = $this->drupalCreateUser(array('administer blocks', 'administer boxes'));
  21. $this->drupalLogin($admin_user);
  22. }
  23. /**
  24. * Test creating and deleting a box.
  25. */
  26. function testBoxes() {
  27. // Add a new box by filling out the input form on the admin/build/block/add page.
  28. $box = array();
  29. $box['description'] = $this->randomName(8);
  30. $box['title'] = $this->randomName(8);
  31. $box['body[value]'] = $this->randomName(32);
  32. $box['delta'] = strtolower($this->randomName(16));
  33. $this->drupalPost('admin/structure/block/box-add/simple', $box, t('Save'));
  34. // Confirm that the box has been created, and then query the created bid.
  35. $this->assertText(
  36. t('@description has been created.', array('@description' => $box['description'])),
  37. t('Box successfully created.'));
  38. $delta = db_query("select delta from {box} where delta = :delta", array('delta' => $box['delta']))->fetchField();
  39. $this->assertNotNull($delta, t('box found in database'));
  40. // Delete the created box & verify that it's been deleted and no longer appearing on the page.
  41. $this->drupalPost('admin/structure/block/manage/boxes/' . $delta . '/delete/', array(), t('Delete'));
  42. // TODO check confirmation message ...of course we'd need to show one first.
  43. $delta = db_query("select delta from {box} where delta = :delta", array('delta' => $box['delta']))->fetchField();
  44. $this->assertFalse($delta, t('box not found in database'));
  45. }
  46. }
  47. class BoxesAjaxTestCase extends DrupalWebTestCase {
  48. /**
  49. * Parse JSON that was generated by drupal_to_js
  50. *
  51. * Because of peculiarities of drupal_to_js we need to prepare our json
  52. * for parsing.
  53. */
  54. function parseJSON() {
  55. // Step one; undo the "HTML escaping" that drupal does.
  56. $json = str_replace(array('\x3c', '\x3e', '\x26'), array("<", ">", "&"), $this->content);
  57. // Step two; handle our escaped single quotes with extreme care,
  58. $json = str_replace(array("\'"), array("\x27"), $json);
  59. // Step three; parse!
  60. $json = json_decode($json);
  61. // JSON_ERROR_NONE == 0 in PHP 5.3
  62. $error = function_exists('json_last_error')
  63. ? json_last_error()
  64. : $json == NULL? 1 : 0;
  65. if ($error === 0) {
  66. $this->pass("Parsed JSON response");
  67. }
  68. else {
  69. $this->fail("Failed to parse JSON response");
  70. }
  71. return $json;
  72. }
  73. /**
  74. * Load a block via the context ajax callback and set the payload as the
  75. * content for simpletest.
  76. */
  77. function ajaxLoadBoxesBlock($delta, $path = 'node') {
  78. $this->drupalGet($path, array('query' => array('boxes_delta' => $delta)));
  79. $response = $this->parseJSON();
  80. $block = NULL;
  81. foreach ($response as $command) {
  82. if (($command->command == 'insert') && ($command->method == 'replaceWith')) {
  83. $block = $command->data;
  84. break;
  85. }
  86. }
  87. if ($block) {
  88. $this->pass("Loaded block");
  89. // Replace contents of the reponse with the decoded JSON
  90. $this->content = $block;
  91. }
  92. else {
  93. $this->fail('Failed to load block');
  94. }
  95. }
  96. }
  97. class BoxesBasicAjaxTestCase extends BoxesAjaxTestCase {
  98. /**
  99. * Implementation of getInfo().
  100. */
  101. public static function getInfo() {
  102. return array(
  103. 'name' => t('Boxes Ajax functionality'),
  104. 'description' => t('Add a custom boxes with AJAX.'),
  105. 'group' => t('Boxes'),
  106. );
  107. }
  108. /**
  109. * Implementation of setUp().
  110. */
  111. function setUp() {
  112. parent::setUp('ctools', 'context', 'boxes');
  113. // Create and login user
  114. $admin_user = $this->drupalCreateUser(array('administer blocks', 'administer boxes'));
  115. $this->drupalLogin($admin_user);
  116. }
  117. /**
  118. * Test creating and deleting a box.
  119. */
  120. function testAjaxBoxes() {
  121. $this->ajaxLoadBoxesBlock('boxes_add__simple');
  122. $this->assertText(t('Add custom box'), 'Found box add form');
  123. $edit = array(
  124. 'description' => $this->randomName(),
  125. 'title' => $this->randomName(),
  126. 'body[value]' => $this->randomName(32),
  127. );
  128. $this->drupalPost(NULL, $edit, t('Save'), array('query' => array('boxes_delta' => 'boxes_add__simple')));
  129. $response = $this->parseJSON();
  130. $delta = NULL;
  131. foreach ($response as $command) {
  132. if ($command->command == 'getBlock') {
  133. $delta = $command->delta;
  134. break;
  135. }
  136. }
  137. if (!$delta) {
  138. $this->fail('AJAX block submission failed');
  139. }
  140. $this->ajaxLoadBoxesBlock($delta);
  141. $this->assertText($edit['title'], 'Found box');
  142. }
  143. }