boxes.test

  1. nittany7 modules/contrib/boxes/tests/boxes.test
  2. cis7 modules/contrib/boxes/tests/boxes.test
  3. mooc7 modules/contrib/boxes/tests/boxes.test

Classes

File

modules/contrib/boxes/tests/boxes.test
View source
  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. }