You are here

public function LocationTest::testPopulateLogGeometry in farmOS 2.x

Test auto population of log geometry field.

File

modules/core/location/tests/src/Kernel/LocationTest.php, line 109

Class

LocationTest
Tests for farmOS location logic.

Namespace

Drupal\Tests\farm_location\Kernel

Code

public function testPopulateLogGeometry() {

  // When a log is saved with a location and without a geometry, the geometry
  // is copied from the location.

  /** @var \Drupal\log\Entity\LogInterface $log */
  $log = Log::create([
    'type' => 'movement',
    'status' => 'pending',
    'location' => [
      'target_id' => $this->locations[0]
        ->id(),
    ],
  ]);
  $log
    ->save();
  $this
    ->assertEquals($this->locations[0]
    ->get('intrinsic_geometry')->value, $log
    ->get('geometry')->value, 'Empty geometry is populated from location.');

  // When multiple locations are added, all of their geometries are combined.
  $log->location = [
    [
      'target_id' => $this->locations[0]
        ->id(),
    ],
    [
      'target_id' => $this->locations[1]
        ->id(),
    ],
  ];
  $log->geometry->value = '';
  $log
    ->save();
  $combined = $this
    ->combineWkt([
    $this->polygons[0],
    $this->polygons[1],
  ]);
  $this
    ->assertEquals($combined, $log
    ->get('geometry')->value, 'Geometries from multiple locations are combined.');

  // When a log's locations change, and the geometry is not customized, the
  // geometry is updated.
  $log->location = [
    'target_id' => $this->locations[1]
      ->id(),
  ];
  $log
    ->save();
  $this
    ->assertEquals($this->locations[1]
    ->get('intrinsic_geometry')->value, $log
    ->get('geometry')->value, 'Geometry is updated when locations are changed.');

  // When a log's geometry is set, it is saved.
  $log->geometry->value = $this->polygons[2];
  $log
    ->save();
  $this
    ->assertEquals($this->polygons[2], $log
    ->get('geometry')->value, 'Custom geometry can be saved.');

  // When a log's locations change, and the geometry is customized, the
  // geometry is not updated.
  $log->location = [
    'target_id' => $this->locations[0]
      ->id(),
  ];
  $log
    ->save();
  $this
    ->assertEquals($this->polygons[2], $log
    ->get('geometry')->value, 'Custom geometry is not overwritten when locations change.');
}