You are here

public function GeofieldWidgetTest::testLatLonWidget in Geofield 8

Tests the Lat Lon widget.

File

tests/src/Functional/GeofieldWidgetTest.php, line 145

Class

GeofieldWidgetTest
Tests the Geofield widgets.

Namespace

Drupal\Tests\geofield\Functional

Code

public function testLatLonWidget() {
  EntityFormDisplay::load('entity_test.entity_test.default')
    ->setComponent($this->fieldStorage
    ->getName(), [
    'type' => 'geofield_latlon',
  ])
    ->save();

  // Create an entity.
  $entity = EntityTest::create([
    'user_id' => 1,
    'name' => $this
      ->randomMachineName(),
  ]);
  $entity
    ->save();

  // Check basic data.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this->assertSession
    ->pageTextContains('geofield_field');
  $this->assertSession
    ->pageTextContains('Latitude');
  $this->assertSession
    ->pageTextContains('Longitude');

  // Add a valid point.
  $edit = [
    'name[0][value]' => 'Arnedo',
    'geofield_field[0][value][lat]' => 42.2257,
    'geofield_field[0][value][lon]' => -2.1021,
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertFieldValues($entity, 'geofield_field', [
    'POINT (-2.1021 42.2257)',
  ]);

  // Add values out of range.
  $edit = [
    'name[0][value]' => 'Out of bounds',
    'geofield_field[0][value][lat]' => 92.2257,
    'geofield_field[0][value][lon]' => -200.1021,
  ];
  $this
    ->submitForm($edit, 'Save');
  $this->assertSession
    ->pageTextContains('geofield_field: Latitude is out of bounds');
  $this->assertSession
    ->pageTextContains('geofield_field: Longitude is out of bounds');

  // Add non numeric values.
  $edit = [
    'name[0][value]' => 'Not numeric',
    'geofield_field[0][value][lat]' => 'Not',
    'geofield_field[0][value][lon]' => 'Numeric',
  ];
  $this
    ->submitForm($edit, 'Save');
  $this->assertSession
    ->pageTextContains('geofield_field: Latitude is not valid.');
  $this->assertSession
    ->pageTextContains('geofield_field: Longitude is not valid.');
}