You are here

function GeofieldWidgetTestCase::testGeofieldFieldLatLonWidget in Geofield 7.2

Test lat/lon Input.

File

tests/geofield.test, line 69
Tests for geofield.module.

Class

GeofieldWidgetTestCase

Code

function testGeofieldFieldLatLonWidget() {

  // Test lat/lon widget
  $this
    ->_testGeoFieldAPISetup('geofield_latlon', 'geofield_wkt');

  // Display creation form.
  $langcode = LANGUAGE_NONE;
  $this
    ->drupalGet('test-entity/add/test-bundle');
  $this
    ->assertFieldByName("{$this->field_name}[{$langcode}][0][geom][lat]", '', t('Lat/lon widget [lat] is displayed'));
  $this
    ->assertFieldByName("{$this->field_name}[{$langcode}][0][geom][lon]", '', t('Lat/lon widget [lon] is displayed'));

  // Submit with some value.
  include_once drupal_get_path('module', 'geofield') . '/includes/GeoGenerator.php';
  $generator = new GeoGenerator();
  $point = $generator
    ->random_point();
  geophp_load();
  $geometry = new Point($point[0], $point[1]);
  $edit = array(
    "{$this->field_name}[{$langcode}][0][geom][lat]" => $geometry
      ->getY(),
    "{$this->field_name}[{$langcode}][0][geom][lon]" => $geometry
      ->getX(),
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  preg_match('|test-entity/manage/(\\d+)/edit|', $this->url, $match);
  $id = $match[1];
  $this
    ->assertRaw(t('test_entity @id has been created.', array(
    '@id' => $id,
  )), t('Lat/lon entity was created'));
  $this
    ->assertFieldByName($this->field_name . '[und][0][geom][lat]', $geometry
    ->getY(), 'Lat field widget set correctly');
  $this
    ->assertFieldByName($this->field_name . '[und][0][geom][lon]', $geometry
    ->getX(), 'Lon field widget set correctly');
  $this
    ->_testWKTFormatter($id, $geometry, 'lat/lon');

  // Edge case, submit with 0,0 value.
  $this
    ->drupalGet('test-entity/add/test-bundle');
  $edit = array(
    "{$this->field_name}[{$langcode}][0][geom][lat]" => '0',
    "{$this->field_name}[{$langcode}][0][geom][lon]" => '0',
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  preg_match('|test-entity/manage/(\\d+)/edit|', $this->url, $match);
  $id = $match[1];
  $this
    ->assertRaw(t('test_entity @id has been created.', array(
    '@id' => $id,
  )), t('Lat/lon entity was created with 0,0 value.'));
  $geometry = new Point(0, 0);
  $this
    ->_testWKTFormatter($id, $geometry, 'lat/lon');

  // Edge case, no geometry submitted.
  $this
    ->drupalGet('test-entity/add/test-bundle');
  $edit = array(
    "{$this->field_name}[{$langcode}][0][geom][lat]" => '',
    "{$this->field_name}[{$langcode}][0][geom][lon]" => '',
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  preg_match('|test-entity/manage/(\\d+)/edit|', $this->url, $match);
  $id = $match[1];
  $this
    ->assertRaw(t('test_entity @id has been created.', array(
    '@id' => $id,
  )), t('Lat/lon entity was created with null value.'));
}