location_cck.test 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <?php
  2. /**
  3. * @file
  4. * Tests for location_cck.module.
  5. */
  6. require_once drupal_get_path('module', 'location') . '/tests/location_testcase.php';
  7. class LocationCCKTest 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. * A default content type.
  18. */
  19. var $content_type;
  20. function getInfo() {
  21. return array(
  22. 'name' => t('Location CCK checks'),
  23. 'description' => t('Test corner cases of the CCK Location module.'),
  24. 'group' => t('Location'),
  25. );
  26. }
  27. function setUp() {
  28. parent::setUp('location', 'content', 'location_cck', 'devel');
  29. $this->admin_user = $this->drupalCreateUser(array('administer nodes', 'submit latitude/longitude', 'administer site configuration', 'access administration pages', 'administer content types'));
  30. $this->normal_user = $this->drupalCreateUser(array('access content'));
  31. $this->drupalLogin($this->admin_user);
  32. }
  33. function addLocationContentType(&$settings, $add = array()) {
  34. $field_name = 'loctest';
  35. // Let the caller mess with some relevant cck stuff.
  36. $required = isset($add['cck_required']) ? $add['cck_required'] : FALSE;
  37. $multiple = isset($add['cck_multiple']) ? $add['cck_multiple'] : 0;
  38. unset($add['cck_required']);
  39. unset($add['cck_multiple']);
  40. // find a non-existent random type name.
  41. do {
  42. $name = strtolower($this->randomName(3, 'type_'));
  43. } while (node_get_types('type', $name));
  44. $form = array(
  45. 'name' => $name,
  46. 'type' => $name,
  47. );
  48. $this->flattenPostData($form);
  49. //$add = array('location_settings' => $add);
  50. //$this->flattenPostData($add);
  51. //$settings = array_merge($settings, $add);
  52. $this->drupalPost('admin/content/types/add', $form, 'Save content type');
  53. $this->refreshVariables();
  54. $form = array(
  55. '_add_new_field' => array(
  56. 'label' => 'Location',
  57. 'weight' => 10,
  58. // 'hidden_name' => '_add_new_field',
  59. 'field_name' => $field_name,
  60. 'type' => 'location',
  61. 'widget_type' => 'location',
  62. ),
  63. );
  64. $this->flattenPostData($form);
  65. $this->drupalPost('admin/content/node-type/'. str_replace('_', '-', $name) .'/fields', $form, 'Save');
  66. $this->refreshVariables();
  67. drupal_get_schema(NULL, TRUE);
  68. // Get the (settable) defaults.
  69. $defaults = $this->getLocationFieldDefaults();
  70. $form = array(
  71. 'required' => $required,
  72. 'multiple' => $multiple,
  73. 'location_settings' => array(
  74. 'form' => array(
  75. 'fields' => $defaults,
  76. ),
  77. // 'display'
  78. ),
  79. );
  80. $this->flattenPostData($form);
  81. $add = array('location_settings' => $add);
  82. $this->flattenPostData($add);
  83. $this->drupalPost(NULL, $form, 'Save field settings');
  84. // @@@ This is stupid, but I don't know the api for pulling this stuff up.
  85. // @@@ _content_type_info() perhaps?
  86. $result = db_query("SELECT global_settings FROM {content_node_field} WHERE field_name = 'field_%s' AND type = 'location'", $field_name);
  87. $row = db_fetch_object($result);
  88. $settings = array();
  89. $settings = unserialize($row->global_settings);
  90. $settings = $settings['location_settings'];
  91. $this->refreshVariables();
  92. // Reset the schema again, if it was a multiple value field added,
  93. // schema has a new table to worry about.
  94. drupal_get_schema(NULL, TRUE);
  95. // Reset the content type info in the context of the test framework.
  96. // This took me way too long to figure out.
  97. _content_type_info(TRUE);
  98. return $name;
  99. }
  100. /**
  101. * Create a location via cck.
  102. */
  103. function testCreateLocation() {
  104. $settings = array();
  105. $location_type = $this->addLocationContentType($settings);
  106. $location1_name = $this->randomName();
  107. $node = $this->drupalCreateNode(array(
  108. 'type' => $location_type,
  109. 'field_loctest' => array(
  110. array(
  111. 'name' => $location1_name,
  112. 'street' => '48072 289th St.',
  113. 'province' => 'SD',
  114. 'country' => 'us',
  115. // 'location_settings' => $settings,
  116. ),
  117. ),
  118. ));
  119. cache_clear_all();
  120. // Reload the node.
  121. $node2 = node_load($node->nid, NULL, TRUE);
  122. $location = $node2->field_loctest[0];
  123. $this->assertEqual($location1_name, $location['name'], t('Testing basic save/load'));
  124. }
  125. function testLocpickOnly() {
  126. $settings = array();
  127. $location_type = $this->addLocationContentType($settings);
  128. $location1_name = $this->randomName();
  129. $node = $this->drupalCreateNode(array(
  130. 'type' => $location_type,
  131. 'field_loctest' => array(
  132. array(
  133. 'locpick' => array(
  134. 'user_latitude' => '44.25',
  135. 'user_longitude' => '-10.25',
  136. ),
  137. // 'location_settings' => $settings,
  138. ),
  139. ),
  140. ));
  141. // Reload the node.
  142. $node2 = node_load($node->nid, NULL, TRUE);
  143. $this->pass(var_export($node2->locations, TRUE));
  144. $this->assertEqual($node2->field_loctest[0]['latitude'], 44.25, t('Testing coordinate-only save/load'));
  145. }
  146. function testMultipleLocationOnSingleNode() {
  147. $settings = array();
  148. $location_type = $this->addLocationContentType($settings, array('cck_multiple' => 10));
  149. $location1_name = $this->randomName();
  150. $location2_name = $this->randomName();
  151. $location3_name = $this->randomName();
  152. $node = $this->drupalCreateNode(array(
  153. 'type' => $location_type,
  154. 'field_loctest' => array(
  155. array(
  156. 'name' => $location1_name,
  157. // 'location_settings' => $settings,
  158. ),
  159. array(
  160. 'name' => $location2_name,
  161. // 'location_settings' => $settings,
  162. ),
  163. array(
  164. 'name' => $location3_name,
  165. // 'location_settings' => $settings,
  166. ),
  167. ),
  168. ));
  169. // Reload the node.
  170. $node2 = node_load($node->nid, NULL, TRUE);
  171. $this->assertEqual($location1_name, $node2->field_loctest[0]['name'], t('Testing multi location 1/3'));
  172. $this->assertEqual($location2_name, $node2->field_loctest[1]['name'], t('Testing multi location 2/3'));
  173. $this->assertEqual($location3_name, $node2->field_loctest[2]['name'], t('Testing multi location 3/3'));
  174. $this->assertNotEqual($node2->field_loctest[0]['lid'], $node2->field_loctest[1]['lid'], t('Ensuring location id uniqueness'));
  175. $this->assertNotEqual($node2->field_loctest[1]['lid'], $node2->field_loctest[2]['lid'], t('Ensuring location id uniqueness'));
  176. $this->assertNotEqual($node2->field_loctest[2]['lid'], $node2->field_loctest[0]['lid'], t('Ensuring location id uniqueness'));
  177. }
  178. function testSharedLocation() {
  179. $settings = array();
  180. $location_type = $this->addLocationContentType($settings);
  181. $location1_name = $this->randomName();
  182. $node = $this->drupalCreateNode(array(
  183. 'type' => $location_type,
  184. 'field_loctest' => array(
  185. array(
  186. 'name' => $location1_name,
  187. // 'location_settings' => $settings,
  188. ),
  189. ),
  190. ));
  191. // Reload the node.
  192. $node = node_load($node->nid, NULL, TRUE);
  193. // Get the full location
  194. $location = $node->field_loctest[0];
  195. $node2 = $this->drupalCreateNode(array(
  196. 'type' => $location_type,
  197. 'field_loctest' => array(
  198. 0 => $location,
  199. ),
  200. ));
  201. // Reload second node.
  202. $node2 = node_load($node2->nid, NULL, TRUE);
  203. $this->assertNotEqual($node->nid, $node2->nid, t('Ensuring nodes are seperate'));
  204. $this->pass(var_export($node, TRUE));
  205. $this->pass(var_export($node2, TRUE));
  206. $this->assertEqual($node->field_loctest[0]['lid'], $node2->field_loctest[0]['lid'], t('Testing shared location'));
  207. $this->deleteNode($node->nid);
  208. // Force another reload.
  209. $node2 = node_load($node2->nid, NULL, TRUE);
  210. $this->assertEqual($node2->field_loctest[0]['lid'], $location['lid'], t('Ensuring shared location is not prematurely garbage collected'));
  211. $this->deleteNode($node2->nid);
  212. $result = db_query('SELECT * FROM {location} WHERE lid = %d', $location['lid']);
  213. if ($row = db_fetch_object($result)) {
  214. $this->fail(t('Ensuring shared location is garbage collected'));
  215. }
  216. else {
  217. $this->pass(t('Ensuring shared location is garbage collected'));
  218. }
  219. }
  220. function testNodeRevisionCOW() {
  221. $settings = array();
  222. $location_type = $this->addLocationContentType($settings, array('cck_multiple' => 10));
  223. $location1_name = $this->randomName();
  224. $location2_name = $this->randomName();
  225. $location3_name = $this->randomName();
  226. $node = $this->drupalCreateNode(array(
  227. 'type' => $location_type,
  228. 'field_loctest' => array(
  229. 0 => array( // First
  230. 'name' => $location1_name,
  231. // 'location_settings' => $settings,
  232. ),
  233. 1 => array( // Second
  234. 'name' => $location2_name,
  235. // 'location_settings' => $settings,
  236. ),
  237. ),
  238. ));
  239. // Reload the node.
  240. $node = node_load($node->nid, NULL, TRUE);
  241. $changes = array(
  242. 'revision' => TRUE,
  243. 'log' => $this->randomName(20),
  244. 'field_loctest' => array(
  245. 0 => array( // Delete First
  246. 'delete_location' => TRUE,
  247. ),
  248. 2 => array( // Third
  249. 'name' => $location3_name,
  250. ),
  251. ),
  252. );
  253. $this->flattenPostData($changes);
  254. $this->drupalPost('node/'. $node->nid .'/edit', $changes, 'Save');
  255. // Reload the node again.
  256. $node1 = node_load($node->nid, $node->vid, TRUE);
  257. $node2 = node_load($node->nid, NULL, TRUE);
  258. // Ensure locations are in a consistent order.
  259. $this->reorderLocations($node, 'field_loctest');
  260. $this->reorderLocations($node1, 'field_loctest');
  261. $this->reorderLocations($node2, 'field_loctest');
  262. $this->assertEqual(count($node1->field_loctest), 2, t('Ensuring second revision did not affect first revision'));
  263. $this->pass(count($node1->field_loctest));
  264. $this->assertEqual($node->field_loctest[0]['lid'], $node1->field_loctest[0]['lid'], t('Ensuring second revision did not affect first revision'));
  265. $this->assertEqual($node->field_loctest[1]['lid'], $node1->field_loctest[1]['lid'], t('Ensuring second revision did not affect first revision'));
  266. $this->assertEqual(count($node2->field_loctest), 2, t('Ensuring second revision does not have stealth locations'));
  267. // Delete first revision.
  268. db_query("DELETE FROM {node_revisions} WHERE nid = %d AND vid = %d", $node1->nid, $node1->vid);
  269. node_invoke_nodeapi($node1, 'delete revision');
  270. $result = db_query('SELECT * FROM {location} WHERE lid = %d', $node1->field_loctest[0]['lid']);
  271. if ($row = db_fetch_object($result)) {
  272. $this->fail(t('Ensuring location on deleted revision is garbage collected'));
  273. }
  274. else {
  275. $this->pass(t('Ensuring location on deleted revision is garbage collected'));
  276. }
  277. $result = db_query('SELECT * FROM {location} WHERE lid = %d', $node1->field_loctest[1]['lid']);
  278. if ($row = db_fetch_object($result)) {
  279. $this->pass(t('Ensuring shared location on deleted revision is NOT garbage collected'));
  280. }
  281. else {
  282. $this->fail(t('Ensuring shared location on deleted revision is NOT garbage collected'));
  283. }
  284. }
  285. function testNodeRevisionCleanup() {
  286. $settings = array();
  287. $location_type = $this->addLocationContentType($settings);
  288. $location1_name = $this->randomName();
  289. $node = $this->drupalCreateNode(array(
  290. 'type' => $location_type,
  291. 'field_loctest' => array(
  292. array( // First
  293. 'name' => $location1_name,
  294. // 'location_settings' => $settings,
  295. ),
  296. ),
  297. ));
  298. // Reload the node.
  299. $node = node_load($node->nid, NULL, TRUE);
  300. $changes = array(
  301. 'revision' => TRUE,
  302. 'log' => $this->randomName(20),
  303. // 'field_loctest' => array(
  304. // $node->field_loctest[0],
  305. // ),
  306. );
  307. $this->flattenPostData($changes);
  308. $this->drupalPost('node/'. $node->nid .'/edit', $changes, 'Save');
  309. // Reload the node again.
  310. $node1 = node_load($node->nid, $node->vid, TRUE);
  311. $node2 = node_load($node->nid, NULL, TRUE);
  312. $this->deleteNode($node->nid);
  313. $result = db_query('SELECT * FROM {location} WHERE lid = %d', $node1->field_loctest[0]['lid']);
  314. if ($row = db_fetch_object($result)) {
  315. $this->fail(t('Ensuring all revisions are cleaned up when a multiple revision node is deleted'));
  316. }
  317. else {
  318. $this->pass(t('Ensuring all revisions are cleaned up when a multiple revision node is deleted'));
  319. }
  320. }
  321. function testCOWConservation() {
  322. $settings = array();
  323. $location_type = $this->addLocationContentType($settings);
  324. $location1_name = $this->randomName();
  325. $node = $this->drupalCreateNode(array(
  326. 'type' => $location_type,
  327. 'field_loctest' => array(
  328. 0 => array(
  329. 'name' => $location1_name,
  330. 'location_settings' => $settings,
  331. ),
  332. ),
  333. ));
  334. // Reload the node.
  335. $node = node_load($node->nid, NULL, TRUE);
  336. $changes = array(
  337. 'field_loctest' => array(
  338. 0 => array(
  339. // Update name.
  340. 'name' => $location1_name .'_CHANGE',
  341. ),
  342. ),
  343. );
  344. $this->flattenPostData($changes);
  345. $this->drupalPost('node/'. $node->nid .'/edit', $changes, 'Save');
  346. // Reload the node again.
  347. $node1 = node_load($node->nid, NULL, TRUE);
  348. // Ensure locations are in a consistent order.
  349. $this->reorderLocations($node, 'field_loctest');
  350. $this->reorderLocations($node1, 'field_loctest');
  351. $this->assertEqual($node->field_loctest[0]['lid'], $node1->field_loctest[0]['lid'], t('Ensuring LIDs are conserved'));
  352. }
  353. }