You are here

public function GeofieldWidgetTest::testDefaultWidget in Geofield 8

Tests the Default widget.

File

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

Class

GeofieldWidgetTest
Tests the Geofield widgets.

Namespace

Drupal\Tests\geofield\Functional

Code

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

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

  // With no field data, no buttons are checked.
  $this
    ->drupalGet('entity_test/manage/' . $entity
    ->id() . '/edit');
  $this->assertSession
    ->pageTextContains('geofield_field');

  // Test a valid WKT value.
  $edit = [
    'name[0][value]' => 'Arnedo',
    'geofield_field[0][value]' => 'POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))',
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertFieldValues($entity, 'geofield_field', [
    'POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))',
  ]);

  // Test a valid GeoJSON value.
  $edit = [
    'name[0][value]' => 'Dinagat Islands',
    'geofield_field[0][value]' => '{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [125.6, 10.1]
  },
  "properties": {
    "name": "Dinagat Islands"
  }
}',
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertFieldValues($entity, 'geofield_field', [
    'POINT (125.6 10.1)',
  ]);

  // Test a valid WKB value.
  $edit = [
    'name[0][value]' => 'Arnedo',
    'geofield_field[0][value]' => '0101000020E6100000705F07CE19D100C0865AD3BCE31C4540',
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertFieldValues($entity, 'geofield_field', [
    'POINT (-2.1021 42.2257)',
  ]);
}