clean install

This commit is contained in:
2024-12-10 15:08:16 +01:00
commit e14eb2d8fd
31193 changed files with 3555714 additions and 0 deletions

View File

@@ -0,0 +1,324 @@
<?php
namespace Drupal\Tests\geofield\Functional;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\field\Functional\FieldTestBase;
/**
* Tests the Geofield widgets.
*
* @group geofield
*/
class GeofieldWidgetTest extends FieldTestBase {
use StringTranslationTrait;
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = ['geofield', 'entity_test'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* A field storage with cardinality 1 to use in this test class.
*
* @var \Drupal\field\Entity\FieldStorageConfig
*/
protected $fieldStorage;
/**
* A Field to use in this test class.
*
* @var \Drupal\field\Entity\FieldConfig
*/
protected $field;
/**
* The web assert object.
*
* @var \Drupal\Tests\WebAssert
*/
protected $assertSession;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => 'geofield_field',
'entity_type' => 'entity_test',
'type' => 'geofield',
'settings' => [
'backend' => 'geofield_backend_default',
],
]);
$this->fieldStorage->save();
$this->field = FieldConfig::create([
'field_storage' => $this->fieldStorage,
'bundle' => 'entity_test',
'description' => 'Description for geofield_field',
'settings' => [
'backend' => 'geofield_backend_default',
],
'required' => TRUE,
]);
$this->field->save();
$this->assertSession = $this->assertSession();
// Create a web user.
$this->drupalLogin($this->drupalCreateUser([
'view test entity',
'administer entity_test content',
]));
}
/**
* Tests the Default widget.
*/
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)']);
}
/**
* Tests the Lat Lon widget.
*/
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 a valid point with lat 0.
$edit = [
'name[0][value]' => 'Arnedo',
'geofield_field[0][value][lat]' => 0,
'geofield_field[0][value][lon]' => -2.1021,
];
$this->submitForm($edit, 'Save');
$this->assertFieldValues($entity, 'geofield_field', ['POINT (-2.1021 0)']);
// Add a valid point with lon 0.
$edit = [
'name[0][value]' => 'Arnedo',
'geofield_field[0][value][lat]' => 42.2257,
'geofield_field[0][value][lon]' => 0,
];
$this->submitForm($edit, 'Save');
$this->assertFieldValues($entity, 'geofield_field', ['POINT (0 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.');
}
/**
* Tests the bounds widget.
*/
public function testBoundsWidget() {
EntityFormDisplay::load('entity_test.entity_test.default')
->setComponent($this->fieldStorage->getName(), [
'type' => 'geofield_bounds',
])
->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('Top');
$this->assertSession->pageTextContains('Right');
$this->assertSession->pageTextContains('Bottom');
$this->assertSession->pageTextContains('Left');
// Add valid bounds.
$edit = [
'name[0][value]' => 'Arnedo - Valladolid',
'geofield_field[0][value][top]' => 42.2257,
'geofield_field[0][value][right]' => -2.1021,
'geofield_field[0][value][bottom]' => 41.6523,
'geofield_field[0][value][left]' => -4.7245,
];
$this->submitForm($edit, 'Save');
$this->assertFieldValues($entity, 'geofield_field', ['POLYGON ((-2.1021 42.2257, -2.1021 41.6523, -4.7245 41.6523, -4.7245 42.2257, -2.1021 42.2257))']);
// Add invalid bounds.
$edit = [
'name[0][value]' => 'Invalid',
'geofield_field[0][value][top]' => 42.2257,
'geofield_field[0][value][right]' => 'non numeric',
'geofield_field[0][value][bottom]' => 45.2257,
'geofield_field[0][value][left]' => 750,
];
$this->submitForm($edit, 'Save');
$this->assertSession->pageTextContains('geofield_field: Right is not valid.');
$this->assertSession->pageTextContains('geofield_field: Left is out of bounds');
$this->assertSession->pageTextContains('geofield_field: Top must be greater than Bottom.');
}
/**
* Tests the DMS widget.
*/
public function testDmsWidget() {
EntityFormDisplay::load('entity_test.entity_test.default')
->setComponent($this->fieldStorage->getName(), [
'type' => 'geofield_dms',
])
->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');
// Add valid data.
$edit = [
'name[0][value]' => 'Arnedo',
'geofield_field[0][value][lat][orientation]' => 'N',
'geofield_field[0][value][lat][degrees]' => 42,
'geofield_field[0][value][lat][minutes]' => 13,
'geofield_field[0][value][lat][seconds]' => 32,
'geofield_field[0][value][lon][orientation]' => 'W',
'geofield_field[0][value][lon][degrees]' => 2,
'geofield_field[0][value][lon][minutes]' => 6,
'geofield_field[0][value][lon][seconds]' => 7,
];
$this->submitForm($edit, 'Save');
$this->assertFieldValues($entity, 'geofield_field', ['POINT (-2.1019444444 42.2255555556)']);
// Add invalid data.
$edit = [
'name[0][value]' => 'Arnedo',
'geofield_field[0][value][lat][orientation]' => 'N',
'geofield_field[0][value][lat][degrees]' => 72,
'geofield_field[0][value][lat][minutes]' => 555,
'geofield_field[0][value][lat][seconds]' => 32.5,
'geofield_field[0][value][lon][orientation]' => 'W',
'geofield_field[0][value][lon][degrees]' => 2,
'geofield_field[0][value][lon][minutes]' => 'non numeric',
'geofield_field[0][value][lon][seconds]' => 7,
];
$this->submitForm($edit, 'Save');
$this->assertSession->pageTextContains('geofield_field must be lower than or equal to 59.');
$this->assertSession->pageTextContains('geofield_field is not a valid number.');
$this->assertSession->pageTextContains('geofield_field must be a number.');
}
}

View File

@@ -0,0 +1,72 @@
<?php
namespace Drupal\Tests\geofield\Kernel;
use Drupal\Core\TypedData\Validation\ExecutionContext;
use Drupal\geofield\GeoPHP\GeoPHPWrapper;
use Drupal\geofield\Plugin\Validation\Constraint\GeoConstraint;
use Drupal\geofield\Plugin\Validation\Constraint\GeoConstraintValidator;
use Drupal\KernelTests\KernelTestBase;
if (!class_exists('\Drupal\Core\TypedData\Validation\ExecutionContext')) {
class_alias('\Drupal\Core\Validation\ExecutionContext', '\Drupal\Core\TypedData\Validation\ExecutionContext');
}
/**
* Tests geofield constraints.
*
* @group geofield
*/
class ConstraintsTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = ['geofield'];
/**
* Tests GeoType constraint.
*
* @covers \Drupal\geofield\Plugin\Validation\Constraint\GeoConstraintValidator
* @covers \Drupal\geofield\Plugin\Validation\Constraint\GeoConstraint
*
* @dataProvider geoProvider
*/
public function testGeoConstraint($coordinates, $expected_violation_count) {
// Check message in constraint.
$constraint = new GeoConstraint();
$this->assertEquals('"@value" is not a valid geospatial content.', $constraint->message, 'Correct constraint message found.');
$execution_context = $this->createMock(ExecutionContext::class);
if ($expected_violation_count) {
$execution_context->expects($this->exactly($expected_violation_count))
->method('addViolation')
->with($constraint->message, ['@value' => $coordinates]);
}
else {
$execution_context->expects($this->exactly($expected_violation_count))
->method('addViolation');
}
$geophp_wrapper = new GeoPHPWrapper();
$validator = new GeoConstraintValidator($geophp_wrapper);
$validator->initialize($execution_context);
$validator->validate($coordinates, $constraint);
}
/**
* Provides test data for testGeoConstraint().
*/
public static function geoProvider(): array {
return [
'valid POINT' => ['POINT (40 -3)', 0],
'invalid POAINT' => ['POAINT (40 -3)', 1],
'invalid POINT' => ['POINT (40 -A)', 1],
];
}
}

View File

@@ -0,0 +1,230 @@
<?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;
/**
* Tests the geofield formatters functionality.
*
* @group geofield
*/
class GeofieldFormatterTest extends EntityKernelTestBase {
/**
* The entity type used in this test.
*
* @var string
*/
protected $entityType = 'entity_test';
/**
* The bundle used in this test.
*
* @var string
*/
protected $bundle = 'entity_test';
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = ['geofield'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
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();
}
/**
* Tests geofield field default formatter.
*/
public function testDefaultFormatter() {
// Create the entity to be referenced.
$entity = EntityTest::create(['name' => $this->randomMachineName()]);
$value = \Drupal::service('geofield.wkt_generator')->WktGenerateGeometry();
$entity->geofield = [
'value' => $value,
];
$entity->save();
// Verify the geofield field formatter's render array.
$build = $entity->get('geofield')->view(['type' => 'geofield_default']);
\Drupal::service('renderer')->renderRoot($build[0]);
$this->assertEquals($value, $build[0]['#markup']);
}
/**
* Tests geofield field LatLon formatter.
*
* @dataProvider latLonFormatterProvider
*/
public function testLatLonFormatter($point, $format, $expected_value) {
// Create the entity to be referenced.
$entity = EntityTest::create(['name' => $this->randomMachineName()]);
$entity->geofield = [
'value' => $point,
];
$entity->save();
// Verify the geofield field formatter's render array.
$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']));
}
/**
* Provides test data for testLatLonFormatter().
*/
public static function latLonFormatterProvider(): array {
return [
'DMS Value' => [
'POINT (40 -3)',
'dms',
"<span class=\"dms dms-lat\">
3°
0'
0\"
S
</span>
,
<span class=\"dms dms-lon\">
40°
0'
0\"
E
</span>",
],
'DM Value' => [
'POINT (40 -3)',
'dm',
"<span class=\"dms dms-lat\">
3°
0.00000'
S
</span>
,
<span class=\"dms dms-lon\">
40°
0.00000'
E
</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\">
45°
37'
31\"
N
</span>
,
<span class=\"dms dms-lon\">
85°
14'
45\"
E
</span>",
],
'DM Value long' => [
'POINT (85.24587 45.625358)',
'dm',
"<span class=\"dms dms-lat\">
45°
37.51667'
N
</span>
,
<span class=\"dms dms-lon\">
85°
14.75000'
E
</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\">
42°
13'
33\"
N
</span>
,
<span class=\"dms dms-lon\">
2°
6'
8\"
W
</span>",
],
'DM Arnedo' => [
'POINT (-2.1021 42.2257)',
'dm',
"<span class=\"dms dms-lat\">
42°
13.55000'
N
</span>
,
<span class=\"dms dms-lon\">
2°
6.13333'
W
</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)',
],
];
}
}

View File

@@ -0,0 +1,123 @@
<?php
namespace Drupal\Tests\geofield\Kernel;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
/**
* Tests using entity fields of the geofield field type.
*
* @group geofield
*/
class GeofieldItemTest extends FieldKernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = ['geofield'];
/**
* Field storage entity.
*
* @var \Drupal\field\Entity\FieldStorageConfig
*/
protected $fieldStorage;
/**
* Field entity.
*
* @var \Drupal\field\Entity\FieldConfig
*/
protected $field;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('entity_test_rev');
}
/**
* Tests processed properties.
*/
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);
}
/**
* Creates a geofield field storage and field.
*
* @param string $entity_type
* Entity type for which the field should be created.
*/
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();
}
}

View File

@@ -0,0 +1,169 @@
<?php
namespace Drupal\Tests\geofield\Kernel;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests WktGenerator.
*
* @group geofield
*/
class WktGeneratorTest extends KernelTestBase {
/**
* WKT Generator service.
*
* @var \Drupal\geofield\WktGenerator
*/
public $wktGenerator;
/**
* Generic WKT point regex.
*
* @var string
*/
public $pointRegex = '/^POINT \([-]?[0-9]*\.?[0-9]+ [-]?[0-9]*\.?[0-9]+\)$/';
/**
* Generic WKT multipoint regex.
*
* @var string
*/
public $multipointRegex = '/^MULTIPOINT \((\([-]?[0-9]*\.?[0-9]+ [-]?[0-9]*\.?[0-9]+\), )*(\([-]?[0-9]*\.?[0-9]+ [-]?[0-9]*\.?[0-9]+\))\)$/';
/**
* Generic WKT linestring regex.
*
* @var string
*/
public $linestringRegex = '/^LINESTRING \(([-]?[0-9]*\.?[0-9]+ [-]?[0-9]*\.?[0-9]+\, )*[-]?[0-9]*\.?[0-9]+ [-]?[0-9]*\.?[0-9]+\)$/';
/**
* Generic WKT multilinestring regex.
*
* @var string
*/
public $multilinestringRegex = '/^MULTILINESTRING \((\(([-]?[0-9]*\.?[0-9]+ [-]?[0-9]*\.?[0-9]+\, )*[-]?[0-9]*\.?[0-9]+ [-]?[0-9]*\.?[0-9]+\), )*\(([-]?[0-9]*\.?[0-9]+ [-]?[0-9]*\.?[0-9]+\, )*[-]?[0-9]*\.?[0-9]+ [-]?[0-9]*\.?[0-9]+\)\)$/';
/**
* Generic WKT polygon regex.
*
* @var string
*/
public $polygonRegex = '/^POLYGON \(\(([-]?[0-9]*\.?[0-9]+ [-]?[0-9]*\.?[0-9]+\, )*[-]?[0-9]*\.?[0-9]+ [-]?[0-9]*\.?[0-9]+\)\)$/';
/**
* Generic WKT multipolygon regex.
*
* @var string
*/
public $multipolygonRegex = '/^MULTIPOLYGON \((\(\(([-]?[0-9]*\.?[0-9]+ [-]?[0-9]*\.?[0-9]+\, )*[-]?[0-9]*\.?[0-9]+ [-]?[0-9]*\.?[0-9]+\)\), )*\(\(([-]?[0-9]*\.?[0-9]+ [-]?[0-9]*\.?[0-9]+\, )*[-]?[0-9]*\.?[0-9]+ [-]?[0-9]*\.?[0-9]+\)\)\)$/';
/**
* {@inheritdoc}
*/
protected static $modules = [
'geofield',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->wktGenerator = \Drupal::service('geofield.wkt_generator');
}
/**
* Tests the generation of WKT points and multipoints.
*/
public function testPoint() {
$point = $this->wktGenerator->wktGeneratePoint(['3', '4']);
$this->assertEquals('POINT (3 4)', $point, 'Point generated properly');
$point = $this->wktGenerator->wktGeneratePoint();
$match = preg_match($this->pointRegex, $point);
$this->assertNotEmpty($match, 'Point generated properly');
$multipoint = $this->wktGenerator->wktGenerateMultipoint();
$match = preg_match($this->multipointRegex, $multipoint);
$this->assertNotEmpty($match, 'Multipoint generated properly');
}
/**
* Tests the generation of WKT linestrings and multilinestrings.
*/
public function testLinestring() {
$linestring = $this->wktGenerator->wktGenerateLinestring();
$match = preg_match($this->linestringRegex, $linestring);
$this->assertNotEmpty($match, 'Linestring generated properly');
$linestring = $this->wktGenerator->wktGenerateLinestring([7.34, -45.66]);
$match = preg_match('/^LINESTRING \(7.34 -45.66, ([-]?[0-9]*\.?[0-9]+ [-]?[0-9]*\.?[0-9]+\, )*[-]?[0-9]*\.?[0-9]+ [-]?[0-9]*\.?[0-9]+\)$/', $linestring);
$this->assertNotEmpty($match, 'Linestring generated properly');
$linestring = $this->wktGenerator->wktGenerateLinestring(NULL, 9);
$match = preg_match('/^LINESTRING \(([-]?[0-9]*\.?[0-9]+ [-]?[0-9]*\.?[0-9]+\, ){8}[-]?[0-9]*\.?[0-9]+ [-]?[0-9]*\.?[0-9]+\)$/', $linestring);
$this->assertNotEmpty($match, 'Linestring generated properly');
$linestring = $this->wktGenerator->wktGenerateLinestring([7, 45], 6);
$match = preg_match('/^LINESTRING \(7 45, ([-]?[0-9]*\.?[0-9]+ [-]?[0-9]*\.?[0-9]+\, ){4}[-]?[0-9]*\.?[0-9]+ [-]?[0-9]*\.?[0-9]+\)$/', $linestring);
$this->assertNotEmpty($match, 'Linestring generated properly');
$multilinestring = $this->wktGenerator->wktGenerateMultilinestring();
$match = preg_match($this->multilinestringRegex, $multilinestring);
$this->assertNotEmpty($match, 'Multilinestring generated properly');
}
/**
* Tests the generation of WKT polygons and multipolygons.
*/
public function testPolygon() {
$polygon = $this->wktGenerator->wktGeneratePolygon();
$match = preg_match($this->polygonRegex, $polygon);
$this->assertNotEmpty($match, 'Polygon generated properly');
$polygon = $this->wktGenerator->wktGeneratePolygon([7.34, -45.66]);
$match = preg_match('/^POLYGON \(\(7.34 -45.66, ([-]?[0-9]*\.?[0-9]+ [-]?[0-9]*\.?[0-9]+\, )*7.34 -45.66\)\)$/', $polygon);
$this->assertNotEmpty($match, 'Polygon generated properly');
$polygon = $this->wktGenerator->wktGeneratePolygon(NULL, 9);
$match = preg_match('/^POLYGON \(\(([-]?[0-9]*\.?[0-9]+ [-]?[0-9]*\.?[0-9]+\, ){9}[-]?[0-9]*\.?[0-9]+ [-]?[0-9]*\.?[0-9]+\)\)$/', $polygon);
$this->assertNotEmpty($match, 'Polygon generated properly');
$polygon = $this->wktGenerator->wktGeneratePolygon([7, 45], 6);
$match = preg_match('/^POLYGON \(\(7 45, ([-]?[0-9]*\.?[0-9]+ [-]?[0-9]*\.?[0-9]+\, ){5}7 45\)\)$/', $polygon);
$this->assertNotEmpty($match, 'Polygon generated properly');
$multipolygon = $this->wktGenerator->wktGenerateMultipolygon();
$match = preg_match($this->multipolygonRegex, $multipolygon);
$this->assertNotEmpty($match, 'Multipolygon generated properly');
}
/**
* Tests the generation of random WKT geometries.
*/
public function testRandomGeometry() {
$find = FALSE;
$geometry = $this->wktGenerator->wktGenerateGeometry();
$patterns = [
$this->pointRegex,
$this->multipointRegex,
$this->linestringRegex,
$this->multilinestringRegex,
$this->polygonRegex,
$this->multipolygonRegex,
];
foreach ($patterns as $pattern) {
if (preg_match($pattern, $geometry)) {
$find = TRUE;
break;
}
}
$this->assertTrue($find, 'Random geometry generated properly');
}
}

View File

@@ -0,0 +1,89 @@
<?php
namespace Drupal\Tests\geofield\Unit;
use Drupal\geofield\DmsConverter;
use Drupal\geofield\DmsPoint;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\geofield\DmsConverter
* @group geofield
*/
class DmsConverterTest extends UnitTestCase {
/**
* @covers ::dmsToDecimal
* @covers ::decimalToDms
*
* @dataProvider dataProvider
*/
public function testConverter(DmsPoint $dms, $decimal) {
$result = DmsConverter::dmsToDecimal($dms);
$this->assertEquals($decimal, $result);
$result = DmsConverter::decimalToDms($decimal[0], $decimal[1]);
$this->assertEquals($dms, $result);
}
/**
* Data provider for testConverter.
*
* @return array
* A list of equivalent DMS/Decimal coordinates.
*/
public static function dataProvider(): array {
return [
'Simple' => [
new DmsPoint([
'orientation' => 'E',
'degrees' => 40,
'minutes' => 0,
'seconds' => 0,
],
[
'orientation' => 'N',
'degrees' => 9,
'minutes' => 0,
'seconds' => 0,
]
),
[40, 9],
],
'Negative' => [
new DmsPoint([
'orientation' => 'W',
'degrees' => 40,
'minutes' => 0,
'seconds' => 0,
],
[
'orientation' => 'S',
'degrees' => 9,
'minutes' => 0,
'seconds' => 0,
]
),
[-40, -9],
],
'Decimal' => [
new DmsPoint([
'orientation' => 'W',
'degrees' => 3,
'minutes' => 3,
'seconds' => 3,
],
[
'orientation' => 'S',
'degrees' => 2,
'minutes' => 2,
'seconds' => 2,
]
),
[-3.0508333333, -2.0338888889],
],
];
}
}