location_testcase.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * @file
  4. * Common functions for Location tests.
  5. */
  6. class LocationTestCase extends DrupalWebTestCase {
  7. /**
  8. * Custom assertion -- will check each element of an array against a reference value.
  9. */
  10. function assertArrayEpsilon($result, $expected, $epsilon, $message = '', $group = 'Other') {
  11. foreach ($expected as $k => $test) {
  12. $lower = $test - $epsilon;
  13. $upper = $test + $epsilon;
  14. if ($result[$k] < $lower || $result[$k] > $upper) {
  15. $this->_assert('fail', $message ? $message : t('Value deviates by @amt, which is more than @maxdev.', array('@amt' => abs($test - $result[$k]), '@maxdev' => $epsilon)), $group);
  16. }
  17. else {
  18. $this->_assert('pass', $message ? $message : t('Value within expected margin.'), $group);
  19. }
  20. }
  21. }
  22. /**
  23. * Get a set of location field defaults.
  24. * This will also enable collection on all parts of the location field.
  25. */
  26. function getLocationFieldDefaults() {
  27. // Get the (settable) defaults.
  28. $defaults = array();
  29. $d = location_invoke_locationapi($location, 'defaults');
  30. $fields = location_field_names();
  31. foreach ($fields as $k => $v) {
  32. if (!isset($d[$k]['nodiff'])) {
  33. $defaults[$k] = $d[$k];
  34. }
  35. }
  36. foreach ($defaults as $k => $v) {
  37. // Change collection to allow.
  38. $defaults[$k]['collect'] = 1;
  39. }
  40. return $defaults;
  41. }
  42. /**
  43. * Flatten a post settings array because drupalPost isn't smart enough to.
  44. */
  45. function flattenPostData(&$edit) {
  46. do {
  47. $edit_flattened = TRUE;
  48. foreach ($edit as $k => $v) {
  49. if (is_array($v)) {
  50. $edit_flattened = FALSE;
  51. foreach ($v as $kk => $vv) {
  52. $edit["{$k}[{$kk}]"] = $vv;
  53. }
  54. unset($edit[$k]);
  55. }
  56. }
  57. } while (!$edit_flattened);
  58. }
  59. function addLocationContentType(&$settings, $add = array()) {
  60. // find a non-existent random type name.
  61. do {
  62. $name = strtolower($this->randomName(3, 'type_'));
  63. } while (node_get_types('type', $name));
  64. // Get the (settable) defaults.
  65. $defaults = $this->getLocationFieldDefaults();
  66. $settings = array(
  67. 'name' => $name,
  68. 'type' => $name,
  69. 'location_settings' => array(
  70. 'multiple' => array(
  71. 'max' => 1,
  72. 'add' => 1,
  73. ),
  74. 'form' => array(
  75. 'fields' => $defaults,
  76. ),
  77. ),
  78. );
  79. //$settings['location_settings'] = array_merge_recursive($settings['location_settings'], $add);
  80. $this->flattenPostData($settings);
  81. $add = array('location_settings' => $add);
  82. $this->flattenPostData($add);
  83. $settings = array_merge($settings, $add);
  84. $this->drupalPost('admin/content/types/add', $settings, 'Save content type');
  85. $this->refreshVariables();
  86. $settings = variable_get('location_settings_node_'. $name, array());
  87. return $name;
  88. }
  89. /**
  90. * Delete a node.
  91. */
  92. function deleteNode($nid) {
  93. // Implemention taken from node_delete, with some assumptions regarding
  94. // function_exists removed.
  95. $node = node_load($nid);
  96. db_query('DELETE FROM {node} WHERE nid = %d', $node->nid);
  97. db_query('DELETE FROM {node_revisions} WHERE nid = %d', $node->nid);
  98. // Call the node-specific callback (if any):
  99. node_invoke($node, 'delete');
  100. node_invoke_nodeapi($node, 'delete');
  101. // Clear the page and block caches.
  102. cache_clear_all();
  103. }
  104. /**
  105. * Order locations in a node by LID for testing repeatability purposes.
  106. */
  107. function reorderLocations(&$node, $field = 'locations') {
  108. $locations = array();
  109. foreach ($node->{$field} as $location) {
  110. if ($location['lid']) {
  111. $locations[$location['lid']] = $location;
  112. }
  113. }
  114. ksort($locations);
  115. $node->{$field} = array();
  116. foreach ($locations as $location) {
  117. $node->{$field}[] = $location;
  118. }
  119. }
  120. /**
  121. * Creates a node based on default settings. This uses the internal simpletest
  122. * browser, meaning the node will be owned by the current simpletest _browser user.
  123. *
  124. * Code modified from #212304.
  125. * This is mainly for testing for differences between node_save() and
  126. * submitting a node/add/* form.
  127. *
  128. * @param values
  129. * An associative array of values to change from the defaults, keys are
  130. * node properties, for example 'body' => 'Hello, world!'.
  131. * @return object Created node object.
  132. */
  133. function drupalCreateNodeViaForm($values = array()) {
  134. $defaults = array(
  135. 'type' => 'page',
  136. 'title' => $this->randomName(8),
  137. );
  138. $edit = ($values + $defaults);
  139. if (empty($edit['body'])) {
  140. $content_type = db_fetch_array(db_query("select name, has_body from {node_type} where type='%s'", $edit['type']));
  141. if ($content_type['has_body']) {
  142. $edit['body'] = $this->randomName(32);
  143. }
  144. }
  145. $type = $edit['type'];
  146. unset($edit['type']); // Only used in URL.
  147. $this->flattenPostData($edit); // Added by me.
  148. $this->drupalPost('node/add/'. str_replace('_', '-', $type), $edit, t('Save'));
  149. $node = node_load(array('title' => $edit['title']));
  150. $this->assertRaw(t('@type %title has been created.', array('@type' => node_get_types('name', $node), '%title' => $edit['title'])), t('Node created successfully.'));
  151. return $node;
  152. }
  153. }