View source
<?php
namespace Drupal\Tests\geofield\Kernel;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
class GeofieldFormatterTest extends EntityKernelTestBase {
protected $entityType = 'entity_test';
protected $bundle = 'entity_test';
public static $modules = [
'geofield',
];
protected function setUp() {
parent::setUp();
FieldStorageConfig::create([
'field_name' => 'geofield',
'entity_type' => $this->entityType,
'type' => 'geofield',
'settings' => [
'backend' => 'geofield_backend_default',
],
])
->save();
FieldConfig::create([
'entity_type' => $this->entityType,
'bundle' => $this->bundle,
'field_name' => 'geofield',
'label' => 'GeoField',
'settings' => [
'backend' => 'geofield_backend_default',
],
])
->save();
}
public function testDefaultFormatter() {
$entity = EntityTest::create([
'name' => $this
->randomMachineName(),
]);
$value = \Drupal::service('geofield.wkt_generator')
->WktGenerateGeometry();
$entity->geofield = [
'value' => $value,
];
$entity
->save();
$build = $entity
->get('geofield')
->view([
'type' => 'geofield_default',
]);
\Drupal::service('renderer')
->renderRoot($build[0]);
$this
->assertEquals($value, $build[0]['#markup']);
}
public function testLatLonFormatter($point, $format, $expected_value) {
$entity = EntityTest::create([
'name' => $this
->randomMachineName(),
]);
$entity->geofield = [
'value' => $point,
];
$entity
->save();
$build = $entity
->get('geofield')
->view([
'type' => 'geofield_latlon',
'settings' => [
'output_format' => $format,
],
]);
\Drupal::service('renderer')
->renderRoot($build[0]);
$this
->assertEquals($expected_value, trim($build[0]['#markup']));
}
public function latLonFormatterProvider() {
return [
'DMS Value' => [
'POINT (40 -3)',
'dms',
"<span class=\"dms dms-lat\">\n 3°\n 0'\n 0\"\n S\n </span>\n ,\n <span class=\"dms dms-lon\">\n 40°\n 0'\n 0\"\n E\n </span>",
],
'DM Value' => [
'POINT (40 -3)',
'dm',
"<span class=\"dms dms-lat\">\n 3°\n 0.00000'\n S\n </span>\n ,\n <span class=\"dms dms-lon\">\n 40°\n 0.00000'\n E\n </span>",
],
'LatLon Value' => [
'POINT (40 -3)',
'decimal',
'<span class="latlon latlon-lat">-3</span>, <span class="latlon latlon-lon">40</span>',
],
'DMS Value long' => [
'POINT (85.24587 45.625358)',
'dms',
"<span class=\"dms dms-lat\">\n 45°\n 37'\n 31\"\n N\n </span>\n ,\n <span class=\"dms dms-lon\">\n 85°\n 14'\n 45\"\n E\n </span>",
],
'DM Value long' => [
'POINT (85.24587 45.625358)',
'dm',
"<span class=\"dms dms-lat\">\n 45°\n 37.51667'\n N\n </span>\n ,\n <span class=\"dms dms-lon\">\n 85°\n 14.75000'\n E\n </span>",
],
'LatLon Value long' => [
'POINT (85.24587 45.625358)',
'decimal',
'<span class="latlon latlon-lat">45.625358</span>, <span class="latlon latlon-lon">85.24587</span>',
],
'DMS Arnedo' => [
'POINT (-2.1021 42.2257)',
'dms',
"<span class=\"dms dms-lat\">\n 42°\n 13'\n 33\"\n N\n </span>\n ,\n <span class=\"dms dms-lon\">\n 2°\n 6'\n 8\"\n W\n </span>",
],
'DM Arnedo' => [
'POINT (-2.1021 42.2257)',
'dm',
"<span class=\"dms dms-lat\">\n 42°\n 13.55000'\n N\n </span>\n ,\n <span class=\"dms dms-lon\">\n 2°\n 6.13333'\n W\n </span>",
],
'Decimal Arnedo' => [
'POINT (-2.1021 42.2257)',
'decimal',
'<span class="latlon latlon-lat">42.2257</span>, <span class="latlon latlon-lon">-2.1021</span>',
],
'Polygon' => [
'POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))',
'wkt',
'POINT (25.454545454545 26.969696969697)',
],
];
}
}