GeofieldItemTest.php in Geofield 8
File
tests/src/Kernel/GeofieldItemTest.php
View source
<?php
namespace Drupal\Tests\geofield\Kernel;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
class GeofieldItemTest extends FieldKernelTestBase {
public static $modules = [
'geofield',
];
protected $fieldStorage;
protected $field;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('entity_test_rev');
}
public function testCrudAndUpdate() {
$entity_type = 'entity_test';
$this
->createField($entity_type);
$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);
$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);
}
}
$entity = EntityTest::create();
$entity->geofield_field
->generateSampleItems();
$this
->entityValidateAndSave($entity);
}
protected function createField($entity_type) {
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => 'geofield_field',
'entity_type' => $entity_type,
'type' => 'geofield',
'settings' => [
'backend' => 'geofield_backend_default',
],
]);
$this->fieldStorage
->save();
$this->field = FieldConfig::create([
'field_storage' => $this->fieldStorage,
'bundle' => $entity_type,
'description' => 'Description for geofield_field',
'settings' => [
'backend' => 'geofield_backend_default',
],
'required' => TRUE,
]);
$this->field
->save();
}
}