You are here

public function GeofieldItemTest::testCrudAndUpdate in Geofield 8

Tests processed properties.

File

tests/src/Kernel/GeofieldItemTest.php, line 52

Class

GeofieldItemTest
Tests using entity fields of the geofield field type.

Namespace

Drupal\Tests\geofield\Kernel

Code

public function testCrudAndUpdate() {
  $entity_type = 'entity_test';
  $this
    ->createField($entity_type);

  // Create an entity with a random geofield field.
  $entity = EntityTest::create();
  $entity->geofield_field->value = $value = \Drupal::service('geofield.wkt_generator')
    ->WktGenerateGeometry();
  $entity->name->value = $this
    ->randomMachineName();
  $entity
    ->save();
  $entity = EntityTest::load($entity
    ->id());
  $this
    ->assertInstanceOf(FieldItemListInterface::class, $entity->geofield_field, 'Field implements interface.');
  $this
    ->assertInstanceOf(FieldItemInterface::class, $entity->geofield_field[0], 'Field item implements interface.');
  $this
    ->assertEquals($entity->geofield_field->value, $value);

  // Test computed values.
  $geom = \Drupal::service('geofield.geophp')
    ->load($value);
  if (!empty($geom)) {
    $centroid = $geom
      ->getCentroid();
    $bounding = $geom
      ->getBBox();
    $computed = [];
    $computed['geo_type'] = $geom
      ->geometryType();
    $computed['lon'] = $centroid
      ->getX();
    $computed['lat'] = $centroid
      ->getY();
    $computed['left'] = $bounding['minx'];
    $computed['top'] = $bounding['maxy'];
    $computed['right'] = $bounding['maxx'];
    $computed['bottom'] = $bounding['miny'];
    $computed['geohash'] = $geom
      ->out('geohash');
    foreach ($computed as $index => $computed_value) {
      $this
        ->assertEquals($entity->geofield_field->{$index}, $computed_value);
    }
  }

  // Test the generateSampleValue() method.
  $entity = EntityTest::create();
  $entity->geofield_field
    ->generateSampleItems();
  $this
    ->entityValidateAndSave($entity);
}