You are here

GeofieldFormatterTest.php in Geofield 8

File

tests/src/Kernel/GeofieldFormatterTest.php
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;

/**
 * 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
   */
  public static $modules = [
    'geofield',
  ];

  /**
   * {@inheritdoc}
   */
  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();
  }

  /**
   * 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 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)',
      ],
    ];
  }

}

Classes

Namesort descending Description
GeofieldFormatterTest Tests the geofield formatters functionality.