cow.test 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /**
  3. * @file
  4. * Location saving test.
  5. */
  6. require_once drupal_get_path('module', 'location') . '/tests/location_testcase.php';
  7. class CowInstanceTest extends LocationTestCase {
  8. /**
  9. * A global administrative user.
  10. */
  11. var $admin_user;
  12. /**
  13. * A global normal user.
  14. */
  15. var $normal_user;
  16. /**
  17. * Simple content type using defaults.
  18. */
  19. var $content_type;
  20. function getInfo() {
  21. return array(
  22. 'name' => t('Location Copy on Write checks'),
  23. 'description' => t('Test corner cases of the copy on write mechanism.'),
  24. 'group' => t('Location'),
  25. );
  26. }
  27. function setUp() {
  28. parent::setUp('location', 'location_node', 'devel');
  29. $web_admin = $this->drupalCreateUser(array('administer nodes', 'submit latitude/longitude', 'administer site configuration', 'access administration pages', 'administer content types'));
  30. $this->drupalLogin($web_admin);
  31. }
  32. function testCreateLocation() {
  33. $settings = array();
  34. $location_type = $this->addLocationContentType($settings);
  35. $location1_name = $this->randomName();
  36. $node = $this->drupalCreateNode(array(
  37. 'type' => $location_type,
  38. 'locations' => array(
  39. 0 => array(
  40. 'name' => $location1_name,
  41. 'location_settings' => $settings,
  42. ),
  43. ),
  44. ));
  45. // Reload the node.
  46. $node2 = node_load($node->nid, NULL, TRUE);
  47. $this->assertEqual($location1_name, $node2->locations[0]['name'], t('Testing basic save/load'));
  48. }
  49. function testLocpickOnly() {
  50. $settings = array();
  51. $location_type = $this->addLocationContentType($settings);
  52. $location1_name = $this->randomName();
  53. $node = $this->drupalCreateNode(array(
  54. 'type' => $location_type,
  55. 'locations' => array(
  56. 0 => array(
  57. 'locpick' => array(
  58. 'user_latitude' => '44.25',
  59. 'user_longitude' => '-10.25',
  60. ),
  61. 'location_settings' => $settings,
  62. ),
  63. ),
  64. ));
  65. // Reload the node.
  66. $node2 = node_load($node->nid, NULL, TRUE);
  67. $this->pass(var_export($node2->locations, TRUE));
  68. $this->assertEqual($node2->locations[0]['latitude'], 44.25, t('Testing coordinate-only save/load'));
  69. }
  70. function testMultipleLocationOnSingleNode() {
  71. $settings = array();
  72. $location_type = $this->addLocationContentType($settings, array('multiple' => array('max' => 3, 'add' => 3)));
  73. $location1_name = $this->randomName();
  74. $location2_name = $this->randomName();
  75. $location3_name = $this->randomName();
  76. $node = $this->drupalCreateNode(array(
  77. 'type' => $location_type,
  78. 'locations' => array(
  79. 0 => array(
  80. 'name' => $location1_name,
  81. 'location_settings' => $settings,
  82. ),
  83. 1 => array(
  84. 'name' => $location2_name,
  85. 'location_settings' => $settings,
  86. ),
  87. 2 => array(
  88. 'name' => $location3_name,
  89. 'location_settings' => $settings,
  90. ),
  91. ),
  92. ));
  93. // Reload the node.
  94. $node2 = node_load($node->nid, NULL, TRUE);
  95. $this->assertEqual($location1_name, $node2->locations[0]['name'], t('Testing multi location 1/3'));
  96. $this->assertEqual($location2_name, $node2->locations[1]['name'], t('Testing multi location 2/3'));
  97. $this->assertEqual($location3_name, $node2->locations[2]['name'], t('Testing multi location 3/3'));
  98. $this->assertNotEqual($node2->locations[0]['lid'], $node2->locations[1]['lid'], t('Ensuring location id uniqueness'));
  99. $this->assertNotEqual($node2->locations[1]['lid'], $node2->locations[2]['lid'], t('Ensuring location id uniqueness'));
  100. $this->assertNotEqual($node2->locations[2]['lid'], $node2->locations[0]['lid'], t('Ensuring location id uniqueness'));
  101. }
  102. function testSharedLocation() {
  103. $settings = array();
  104. $location_type = $this->addLocationContentType($settings);
  105. $location1_name = $this->randomName();
  106. $node = $this->drupalCreateNode(array(
  107. 'type' => $location_type,
  108. 'locations' => array(
  109. 0 => array(
  110. 'name' => $location1_name,
  111. 'location_settings' => $settings,
  112. ),
  113. ),
  114. ));
  115. // Reload the node.
  116. $node = node_load($node->nid, NULL, TRUE);
  117. // Get the full location
  118. $location = $node->locations[0];
  119. $node2 = $this->drupalCreateNode(array(
  120. 'type' => $location_type,
  121. 'locations' => array(
  122. '0' => $location,
  123. ),
  124. ));
  125. // Reload second node.
  126. $node2 = node_load($node2->nid, NULL, TRUE);
  127. $this->assertNotEqual($node->nid, $node2->nid, t('Ensuring nodes are seperate'));
  128. $this->assertEqual($node->locations[0]['lid'], $node2->locations[0]['lid'], t('Testing shared location'));
  129. $this->deleteNode($node->nid);
  130. // Force another reload.
  131. $node2 = node_load($node2->nid, NULL, TRUE);
  132. $this->assertEqual($node2->locations[0]['lid'], $location['lid'], t('Ensuring shared location is not prematurely garbage collected'));
  133. $this->deleteNode($node2->nid);
  134. $result = db_query('SELECT * FROM {location} WHERE lid = %d', $location['lid']);
  135. if ($row = db_fetch_object($result)) {
  136. $this->fail(t('Ensuring shared location is garbage collected'));
  137. }
  138. else {
  139. $this->pass(t('Ensuring shared location is garbage collected'));
  140. }
  141. }
  142. function testNodeRevisionCOW() {
  143. $settings = array();
  144. $location_type = $this->addLocationContentType($settings, array('multiple' => array('max' => 3, 'add' => 3)));
  145. $location1_name = $this->randomName();
  146. $location2_name = $this->randomName();
  147. $location3_name = $this->randomName();
  148. $node = $this->drupalCreateNode(array(
  149. 'type' => $location_type,
  150. 'locations' => array(
  151. 0 => array( // First
  152. 'name' => $location1_name,
  153. 'location_settings' => $settings,
  154. ),
  155. 1 => array( // Second
  156. 'name' => $location2_name,
  157. 'location_settings' => $settings,
  158. ),
  159. ),
  160. ));
  161. // Reload the node.
  162. $node = node_load($node->nid, NULL, TRUE);
  163. $changes = array(
  164. 'revision' => TRUE,
  165. 'log' => $this->randomName(20),
  166. 'locations' => array(
  167. 0 => array( // Delete First
  168. 'delete_location' => TRUE,
  169. ),
  170. 2 => array( // Third
  171. 'name' => $location3_name,
  172. ),
  173. ),
  174. );
  175. $this->flattenPostData($changes);
  176. $this->drupalPost('node/'. $node->nid .'/edit', $changes, 'Save');
  177. // Reload the node again.
  178. $node1 = node_load($node->nid, $node->vid, TRUE);
  179. $node2 = node_load($node->nid, NULL, TRUE);
  180. // Ensure locations are in a consistent order.
  181. $this->reorderLocations($node);
  182. $this->reorderLocations($node1);
  183. $this->reorderLocations($node2);
  184. $this->assertEqual(count($node1->locations), 2, t('Ensuring second revision did not affect first revision'));
  185. $this->assertEqual($node->locations[0]['lid'], $node1->locations[0]['lid'], t('Ensuring second revision did not affect first revision'));
  186. $this->assertEqual($node->locations[1]['lid'], $node1->locations[1]['lid'], t('Ensuring second revision did not affect first revision'));
  187. $this->assertEqual(count($node2->locations), 2, t('Ensuring second revision does not have stealth locations'));
  188. // Delete first revision.
  189. db_query("DELETE FROM {node_revisions} WHERE nid = %d AND vid = %d", $node1->nid, $node1->vid);
  190. node_invoke_nodeapi($node1, 'delete revision');
  191. $result = db_query('SELECT * FROM {location} WHERE lid = %d', $node1->locations[0]['lid']);
  192. if ($row = db_fetch_object($result)) {
  193. $this->fail(t('Ensuring location on deleted revision is garbage collected'));
  194. }
  195. else {
  196. $this->pass(t('Ensuring location on deleted revision is garbage collected'));
  197. }
  198. $result = db_query('SELECT * FROM {location} WHERE lid = %d', $node1->locations[1]['lid']);
  199. if ($row = db_fetch_object($result)) {
  200. $this->pass(t('Ensuring shared location on deleted revision is NOT garbage collected'));
  201. }
  202. else {
  203. $this->fail(t('Ensuring shared location on deleted revision is NOT garbage collected'));
  204. }
  205. }
  206. function testCOWConservation() {
  207. $settings = array();
  208. $location_type = $this->addLocationContentType($settings);
  209. $location1_name = $this->randomName();
  210. $node = $this->drupalCreateNode(array(
  211. 'type' => $location_type,
  212. 'locations' => array(
  213. 0 => array(
  214. 'name' => $location1_name,
  215. 'location_settings' => $settings,
  216. ),
  217. ),
  218. ));
  219. // Reload the node.
  220. $node = node_load($node->nid, NULL, TRUE);
  221. $changes = array(
  222. 'locations' => array(
  223. 0 => array(
  224. // Update name.
  225. 'name' => $location1_name .'_CHANGE',
  226. ),
  227. ),
  228. );
  229. $this->flattenPostData($changes);
  230. $this->drupalPost('node/'. $node->nid .'/edit', $changes, 'Save');
  231. // Reload the node again.
  232. $node1 = node_load($node->nid, NULL, TRUE);
  233. // Ensure locations are in a consistent order.
  234. $this->reorderLocations($node);
  235. $this->reorderLocations($node1);
  236. $this->assertEqual($node->locations[0]['lid'], $node1->locations[0]['lid'], t('Ensuring LIDs are conserved'));
  237. }
  238. }