geofield.test 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php
  2. /**
  3. * @file
  4. * Tests for geofield.module.
  5. */
  6. class GeofieldBaseTestCase extends FieldTestCase {
  7. function _testWKTFormatter($id, $geometry, $label) {
  8. $entity = field_test_entity_test_load($id);
  9. $entity->content = field_attach_view('test_entity', $entity, 'full');
  10. $this->content = drupal_render($entity->content);
  11. $value = $geometry->out('wkt');
  12. $this->assertText($value, 'WKT output for ' . $label . ' widget is correct.');
  13. }
  14. /**
  15. * Helper function for testGeofieldWidgets().
  16. */
  17. function _testGeoFieldAPISetup($widget_type, $display_type) {
  18. // Setup a field and instance
  19. $entity_type = 'test_entity';
  20. $this->field_name = drupal_strtolower($this->randomName());
  21. $this->field = array('field_name' => $this->field_name, 'type' => 'geofield');
  22. field_create_field($this->field);
  23. $this->instance = array(
  24. 'field_name' => $this->field_name,
  25. 'entity_type' => 'test_entity',
  26. 'bundle' => 'test_bundle',
  27. 'label' => $this->randomName() . '_label',
  28. 'settings' => array(
  29. ),
  30. 'widget' => array(
  31. 'type' => $widget_type,
  32. ),
  33. 'display' => array(
  34. 'full' => array(
  35. 'type' => $display_type,
  36. ),
  37. ),
  38. );
  39. field_create_instance($this->instance);
  40. }
  41. }
  42. class GeofieldWidgetTestCase extends GeofieldBaseTestCase {
  43. public static function getInfo() {
  44. return array(
  45. 'name' => 'Geofield - Widgets',
  46. 'description' => "Test the creation of geofields.",
  47. 'group' => 'Geofield'
  48. );
  49. }
  50. function setUp() {
  51. parent::setUp(array('geofield', 'field_test'));
  52. $this->admin_user = $this->drupalCreateUser(array('administer filters'));
  53. $this->web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content', 'administer modules'));
  54. $this->drupalLogin($this->web_user);
  55. }
  56. // Test fields.
  57. /**
  58. * Test lat/lon Input.
  59. */
  60. function testGeofieldFieldLatLonWidget() {
  61. // Test lat/lon widget
  62. $this->_testGeoFieldAPISetup('geofield_latlon', 'geofield_wkt');
  63. // Display creation form.
  64. $langcode = LANGUAGE_NONE;
  65. $this->drupalGet('test-entity/add/test-bundle');
  66. $this->assertFieldByName("{$this->field_name}[$langcode][0][geom][lat]", '', t('Lat/lon widget [lat] is displayed'));
  67. $this->assertFieldByName("{$this->field_name}[$langcode][0][geom][lon]", '', t('Lat/lon widget [lon] is displayed'));
  68. // Submit with some value.
  69. include_once(drupal_get_path('module', 'geofield') . '/includes/GeoGenerator.php');
  70. $generator = new GeoGenerator();
  71. $point = $generator->random_point();
  72. geophp_load();
  73. $geometry = new Point($point[0], $point[1]);
  74. $edit = array(
  75. "{$this->field_name}[$langcode][0][geom][lat]" => $geometry->getY(),
  76. "{$this->field_name}[$langcode][0][geom][lon]" => $geometry->getX(),
  77. );
  78. $this->drupalPost(NULL, $edit, t('Save'));
  79. preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
  80. $id = $match[1];
  81. $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), t('Lat/lon entity was created'));
  82. $this->assertFieldByName($this->field_name . '[und][0][geom][lat]', $geometry->getY(), 'Lat field widget set correctly');
  83. $this->assertFieldByName($this->field_name . '[und][0][geom][lon]', $geometry->getX(), 'Lon field widget set correctly');
  84. $this->_testWKTFormatter($id, $geometry, 'lat/lon');
  85. // Edge case, submit with 0,0 value.
  86. $this->drupalGet('test-entity/add/test-bundle');
  87. $edit = array(
  88. "{$this->field_name}[$langcode][0][geom][lat]" => '0',
  89. "{$this->field_name}[$langcode][0][geom][lon]" => '0',
  90. );
  91. $this->drupalPost(NULL, $edit, t('Save'));
  92. preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
  93. $id = $match[1];
  94. $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), t('Lat/lon entity was created with 0,0 value.'));
  95. $geometry = new Point(0, 0);
  96. $this->_testWKTFormatter($id, $geometry, 'lat/lon');
  97. // Edge case, no geometry submitted.
  98. $this->drupalGet('test-entity/add/test-bundle');
  99. $edit = array(
  100. "{$this->field_name}[$langcode][0][geom][lat]" => '',
  101. "{$this->field_name}[$langcode][0][geom][lon]" => '',
  102. );
  103. $this->drupalPost(NULL, $edit, t('Save'));
  104. preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
  105. $id = $match[1];
  106. $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), t('Lat/lon entity was created with null value.'));
  107. }
  108. /**
  109. * Test WKT Input.
  110. */
  111. function testGeofieldFieldWKTWidget() {
  112. // Test lat/lon widget
  113. $this->_testGeoFieldAPISetup('geofield_wkt', 'geofield_wkt');
  114. // Display creation form.
  115. $langcode = LANGUAGE_NONE;
  116. $this->drupalGet('test-entity/add/test-bundle');
  117. $this->assertFieldByName("{$this->field_name}[$langcode][0][geom]", '', t('WKT widget is displayed'));
  118. // Submit with some value.
  119. include_once(drupal_get_path('module', 'geofield') . '/includes/GeoGenerator.php');
  120. $generator = new GeoGenerator();
  121. $wkt = $generator->wkt_generate();
  122. $geometry = geoPHP::load($wkt, 'wkt');
  123. $edit = array(
  124. "{$this->field_name}[$langcode][0][geom]" => $geometry->out('wkt'),
  125. );
  126. $this->drupalPost(NULL, $edit, t('Save'));
  127. preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
  128. $id = $match[1];
  129. $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), t('WKT entity was created'));
  130. $this->_testWKTFormatter($id, $geometry, 'wkt');
  131. }
  132. /**
  133. * Test GeoJSON Input.
  134. */
  135. function testGeofieldFieldGeoJSONWidget() {
  136. // Test lat/lon widget
  137. $this->_testGeoFieldAPISetup('geofield_geojson', 'geofield_wkt');
  138. // Display creation form.
  139. $langcode = LANGUAGE_NONE;
  140. $this->drupalGet('test-entity/add/test-bundle');
  141. $this->assertFieldByName("{$this->field_name}[$langcode][0][geom]", '', t('GeoJSON widget is displayed'));
  142. // Submit with some value.
  143. include_once(drupal_get_path('module', 'geofield') . '/includes/GeoGenerator.php');
  144. $generator = new GeoGenerator();
  145. $wkt = $generator->wkt_generate();
  146. $geometry = geoPHP::load($wkt, 'wkt');
  147. $edit = array(
  148. "{$this->field_name}[$langcode][0][geom]" => $geometry->out('json'),
  149. );
  150. $this->drupalPost(NULL, $edit, t('Save'));
  151. preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
  152. $id = $match[1];
  153. $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), t('GeoJSON entity was created'));
  154. $this->_testWKTFormatter($id, $geometry, 'json');
  155. }
  156. /**
  157. * Test Bounds Input.
  158. */
  159. function testGeofieldFieldBoundsWidget() {
  160. // Test lat/lon widget
  161. $this->_testGeoFieldAPISetup('geofield_bounds', 'geofield_wkt');
  162. // Display creation form.
  163. $langcode = LANGUAGE_NONE;
  164. $this->drupalGet('test-entity/add/test-bundle');
  165. $this->assertFieldByName("{$this->field_name}[$langcode][0][geom][top]", '', t('Bounds widget field "top" is displayed.'));
  166. $this->assertFieldByName("{$this->field_name}[$langcode][0][geom][bottom]", '', t('Bounds widget field "bottom" is displayed.'));
  167. $this->assertFieldByName("{$this->field_name}[$langcode][0][geom][left]", '', t('Bounds widget field "left" is displayed.'));
  168. $this->assertFieldByName("{$this->field_name}[$langcode][0][geom][right]", '', t('Bounds widget field "right" is displayed.'));
  169. // Submit with some value.
  170. include_once(drupal_get_path('module', 'geofield') . '/includes/GeoGenerator.php');
  171. $generator = new GeoGenerator();
  172. // Instead of calculating 4 separate points, calculate a center point and
  173. // distance from it.
  174. list($lon, $lat) = $generator->random_point();
  175. $lat_diff = $generator->dd_generate(2, 10) / 100;
  176. $lon_diff = $generator->dd_generate(2, 10) / 100;
  177. $edit = array(
  178. "{$this->field_name}[$langcode][0][geom][top]" => $lat + $lat_diff,
  179. "{$this->field_name}[$langcode][0][geom][bottom]" => $lat - $lat_diff,
  180. "{$this->field_name}[$langcode][0][geom][right]" => $lon + $lon_diff,
  181. "{$this->field_name}[$langcode][0][geom][left]" => $lon - $lon_diff,
  182. );
  183. $this->drupalPost(NULL, $edit, t('Save'));
  184. preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
  185. $id = $match[1];
  186. $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), t('Bounds entity was created'));
  187. $top = $lat + $lat_diff;
  188. $bottom = $lat - $lat_diff;
  189. $left = $lon - $lon_diff;
  190. $right = $lon + $lon_diff;
  191. $wkt = 'POLYGON ((' . $right . ' ' . $bottom . ', ' . $left . ' ' . $bottom . ', ' . $left . ' ' . $top . ', ' . $right . ' ' . $top . ', ' . $right . ' ' . $bottom . '))';
  192. $geometry = geoPHP::load($wkt, 'wkt');
  193. $this->_testWKTFormatter($id, $geometry, 'wkt');
  194. // Edge case, create with 0, 0, 0, 0.
  195. $this->drupalGet('test-entity/add/test-bundle');
  196. $edit = array(
  197. "{$this->field_name}[$langcode][0][geom][top]" => 0,
  198. "{$this->field_name}[$langcode][0][geom][bottom]" => 0,
  199. "{$this->field_name}[$langcode][0][geom][right]" => 0,
  200. "{$this->field_name}[$langcode][0][geom][left]" => 0,
  201. );
  202. $this->drupalPost(NULL, $edit, t('Save'));
  203. preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
  204. $id = $match[1];
  205. $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), t('Bounds entity was created with values of 0, 0, 0, 0'));
  206. }
  207. }
  208. class GeoFieldElementTestCase extends DrupalWebTestCase {
  209. public static function getInfo() {
  210. return array(
  211. 'name' => 'Geofield FormAPI Elements',
  212. 'description' => "Test Geofield FormAPI elements.",
  213. 'group' => 'Geofield',
  214. );
  215. }
  216. function setUp() {
  217. parent::setUp(array('geofield', 'geofield_test', 'geocoder'));
  218. $this->web_user = $this->drupalCreateUser(array('access content'));
  219. $this->drupalLogin($this->web_user);
  220. }
  221. function testGeofieldLatLonElement() {
  222. // Test form element rendering
  223. $this->drupalGet('geofield-latlon-element');
  224. $this->assertFieldById('edit-geofield-latlon-simple-lat', '', 'Latitude element for simple geofield exists.');
  225. $this->assertFieldById('edit-geofield-latlon-simple-lon', '', 'Longitude element for simple geofield exists.');
  226. $this->assertFieldById('edit-geofield-latlon-verbose-lat', 41, 'Latitude element for verbose geofield exists.');
  227. $this->assertFieldById('edit-geofield-latlon-verbose-lon', -86, 'Longitude element for verbose geofield exists.');
  228. // Test form element submission.
  229. $edit = array();
  230. $edit['geofield_latlon_simple[lat]'] = 41;
  231. $edit['geofield_latlon_simple[lon]'] = -86;
  232. $edit['geofield_latlon_verbose[lat]'] = 25;
  233. $edit['geofield_latlon_verbose[lon]'] = 54;
  234. $this->drupalPost('geofield-latlon-element', $edit, t('Save'));
  235. $this->assertText('Simple - Lat: 41 Lon: -86', "Simple Geofield saved data as expected");
  236. $this->assertText('Verbose - Lat: 25 Lon: 54', "Verbose Geofield saved data as expected");
  237. }
  238. }
  239. class GeofieldFormatterTestCase extends GeofieldBaseTestCase {
  240. public static function getInfo() {
  241. return array(
  242. 'name' => 'Geofield - Formatters',
  243. 'description' => "Test the various display formatters of geofields.",
  244. 'group' => 'Geofield'
  245. );
  246. }
  247. function setUp() {
  248. parent::setUp(array('geofield', 'field_test'));
  249. $this->admin_user = $this->drupalCreateUser(array('administer filters'));
  250. $this->web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content', 'administer modules'));
  251. $this->drupalLogin($this->web_user);
  252. }
  253. function testGeofieldWKTFormatter() {
  254. // Test lat/lon widget
  255. $this->_testGeoFieldAPISetup('geofield_wkt', 'geofield_wkt');
  256. // Display creation form.
  257. $langcode = LANGUAGE_NONE;
  258. $this->drupalGet('test-entity/add/test-bundle');
  259. // Submit with some value.
  260. include_once(drupal_get_path('module', 'geofield') . '/includes/GeoGenerator.php');
  261. $generator = new GeoGenerator();
  262. $wkt = $generator->wkt_generate();
  263. geophp_load();
  264. $geometry = geoPHP::load($wkt, 'wkt');
  265. $edit = array(
  266. "{$this->field_name}[$langcode][0][geom]" => $geometry->out('wkt'),
  267. );
  268. $this->drupalPost(NULL, $edit, t('Save'));
  269. preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
  270. $id = $match[1];
  271. $this->_testWKTFormatter($id, $geometry, 'wkt');
  272. }
  273. function testGeofieldGeoJSONFormatter() {
  274. // Test lat/lon widget
  275. $this->_testGeoFieldAPISetup('geofield_wkt', 'geofield_geojson');
  276. // Display creation form.
  277. $langcode = LANGUAGE_NONE;
  278. $this->drupalGet('test-entity/add/test-bundle');
  279. // Submit with some value.
  280. include_once(drupal_get_path('module', 'geofield') . '/includes/GeoGenerator.php');
  281. $generator = new GeoGenerator();
  282. $wkt = $generator->wkt_generate();
  283. geophp_load();
  284. $geometry = geoPHP::load($wkt, 'wkt');
  285. $edit = array(
  286. "{$this->field_name}[$langcode][0][geom]" => $geometry->out('wkt'),
  287. );
  288. $this->drupalPost(NULL, $edit, t('Save'));
  289. preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
  290. $id = $match[1];
  291. // Test GeoJSON output
  292. $entity = field_test_entity_test_load($id);
  293. $entity->content = field_attach_view('test_entity', $entity, 'full');
  294. $this->content = drupal_render($entity->content);
  295. $value = $geometry->out('json');
  296. $this->assertText($value, 'GeoJSON output is correct.');
  297. }
  298. function testGeofieldDefFormatter() {
  299. // Test lat/lon widget
  300. $this->_testGeoFieldAPISetup('geofield_wkt', 'geofield_def_list');
  301. // Display creation form.
  302. $langcode = LANGUAGE_NONE;
  303. $this->drupalGet('test-entity/add/test-bundle');
  304. // Submit with some value.
  305. include_once(drupal_get_path('module', 'geofield') . '/includes/GeoGenerator.php');
  306. $generator = new GeoGenerator();
  307. $wkt = $generator->wkt_generate();
  308. geophp_load();
  309. $geometry = geoPHP::load($wkt, 'wkt');
  310. $edit = array(
  311. "{$this->field_name}[$langcode][0][geom]" => $geometry->out('wkt'),
  312. );
  313. $this->drupalPost(NULL, $edit, t('Save'));
  314. preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
  315. $id = $match[1];
  316. // Test GeoJSON output
  317. $entity = field_test_entity_test_load($id);
  318. $entity->content = field_attach_view('test_entity', $entity, 'full');
  319. $this->content = drupal_render($entity->content);
  320. // @TODO: Actually test output... :-/
  321. }
  322. }