public function LocationTest::testAssetLocation in farmOS 2.x
Test asset location.
File
- modules/
core/ location/ tests/ src/ Kernel/ LocationTest.php, line 153
Class
- LocationTest
- Tests for farmOS location logic.
Namespace
Drupal\Tests\farm_location\KernelCode
public function testAssetLocation() {
// Create a new asset.
/** @var \Drupal\asset\Entity\AssetInterface $asset */
$asset = Asset::create([
'type' => 'object',
'name' => $this
->randomMachineName(),
'status' => 'active',
]);
$asset
->save();
// Populate a cache value dependent on the asset's cache tags.
$this
->populateEntityTestCache($asset);
// When an asset has no movement logs, it has no location or geometry.
$this
->assertFalse($this->assetLocation
->hasLocation($asset), 'New assets do not have location.');
$this
->assertFalse($this->assetLocation
->hasGeometry($asset), 'New assets do not have geometry.');
// Assert that the asset's cache tags were not invalidated.
$this
->assertEntityTestCache($asset, TRUE);
// Create a "done" movement log that references the asset.
/** @var \Drupal\log\Entity\LogInterface $first_log */
$first_log = Log::create([
'type' => 'movement',
'status' => 'done',
'asset' => [
'target_id' => $asset
->id(),
],
'location' => [
'target_id' => $this->locations[0]
->id(),
],
'is_movement' => TRUE,
]);
$first_log
->save();
// When a movement log is created and marked as "done", the asset has
// the same location and geometry as the log.
$this
->assertTrue($this->assetLocation
->hasLocation($asset), 'Asset with movement log has location.');
$this
->assertTrue($this->assetLocation
->hasGeometry($asset), 'Asset with movement log has geometry.');
$this
->assertEquals($this->logLocation
->getLocation($first_log), $this->assetLocation
->getLocation($asset), 'Asset with movement log has same location as log.');
$this
->assertEquals($this->logLocation
->getGeometry($first_log), $this->assetLocation
->getGeometry($asset), 'Asset with movement log has same geometry as log.');
// Assert that the asset's cache tags were invalidated.
$this
->assertEntityTestCache($asset, FALSE);
// Re-populate a cache value dependent on the asset's cache tags.
$this
->populateEntityTestCache($asset);
// When a movement log's locations are changed, the asset location changes.
$first_log->location = [
'target_id' => $this->locations[1]
->id(),
];
$first_log
->save();
$this
->assertEquals($this->logLocation
->getLocation($first_log), $this->assetLocation
->getLocation($asset), 'Asset with changed movement log has same location as log.');
$this
->assertEquals($this->logLocation
->getGeometry($first_log), $this->assetLocation
->getGeometry($asset), 'Asset with changed movement log has same geometry as log.');
// Assert that the asset's cache tags were invalidated.
$this
->assertEntityTestCache($asset, FALSE);
// Re-populate a cache value dependent on the asset's cache tags.
$this
->populateEntityTestCache($asset);
// Create a "pending" movement log that references the asset.
/** @var \Drupal\log\Entity\LogInterface $second_log */
$second_log = Log::create([
'type' => 'movement',
'status' => 'pending',
'asset' => [
'target_id' => $asset
->id(),
],
'location' => [
'target_id' => $this->locations[2]
->id(),
],
'is_movement' => TRUE,
]);
$second_log
->save();
// When an asset has a "pending" movement log, the asset location and
// geometry remain the same as the previous "done" movement log.
$this
->assertEquals($this->logLocation
->getLocation($first_log), $this->assetLocation
->getLocation($asset), 'Asset with pending movement log has original location');
$this
->assertEquals($this->logLocation
->getGeometry($first_log), $this->assetLocation
->getGeometry($asset), 'Asset with pending movement log has original geometry.');
// Assert that the asset's cache tags were not invalidated.
$this
->assertEntityTestCache($asset, TRUE);
// When the log is marked as "done", the asset location is updated.
$second_log->status = 'done';
$second_log
->save();
$this
->assertEquals($this->logLocation
->getLocation($second_log), $this->assetLocation
->getLocation($asset), 'Asset with second movement log has new location');
$this
->assertEquals($this->logLocation
->getGeometry($second_log), $this->assetLocation
->getGeometry($asset), 'Asset with second movement log has new geometry.');
// Assert that the asset's cache tags were invalidated.
$this
->assertEntityTestCache($asset, FALSE);
// Re-populate a cache value dependent on the asset's cache tags.
$this
->populateEntityTestCache($asset);
// Create a third "done" movement log in the future.
/** @var \Drupal\log\Entity\LogInterface $third_log */
$third_log = Log::create([
'type' => 'movement',
'timestamp' => \Drupal::time()
->getRequestTime() + 86400,
'status' => 'done',
'asset' => [
'target_id' => $asset
->id(),
],
'location' => [
'target_id' => $this->locations[0]
->id(),
],
'is_movement' => TRUE,
]);
$third_log
->save();
// When an asset has a "done" movement log in the future, the asset
// location and geometry remain the same as the previous "done" movement
// log.
$this
->assertEquals($this->logLocation
->getLocation($second_log), $this->assetLocation
->getLocation($asset), 'Asset with future movement log has current location');
$this
->assertEquals($this->logLocation
->getGeometry($second_log), $this->assetLocation
->getGeometry($asset), 'Asset with future movement log has current geometry.');
// Assert that the asset's cache tags were not invalidated.
$this
->assertEntityTestCache($asset, TRUE);
// Create a fourth "done" movement log without location.
/** @var \Drupal\log\Entity\LogInterface $fourth_log */
$fourth_log = Log::create([
'type' => 'movement',
'status' => 'done',
'asset' => [
'target_id' => $asset
->id(),
],
'is_movement' => TRUE,
]);
$fourth_log
->save();
// When a movement log is created with no location/geometry, it effectively
// "unsets" the asset's location/geometry.
$this
->assertFalse($this->assetLocation
->hasLocation($asset), 'Asset location can be unset.');
$this
->assertFalse($this->assetLocation
->hasGeometry($asset), 'Asset geometry can be unset.');
// Assert that the asset's cache tags were invalidated.
$this
->assertEntityTestCache($asset, FALSE);
// Re-populate a cache value dependent on the asset's cache tags.
$this
->populateEntityTestCache($asset);
// Delete the fourth log.
$fourth_log
->delete();
// When a movement log is deleted, the previous location is used.
$this
->assertEquals($this->logLocation
->getLocation($second_log), $this->assetLocation
->getLocation($asset), 'When a movement log is deleted, the previous location is used.');
$this
->assertEquals($this->logLocation
->getGeometry($second_log), $this->assetLocation
->getGeometry($asset), 'When a movement log is deleted, the previous locations geometry is used. .');
// Assert that the asset's cache tags were invalidated.
$this
->assertEntityTestCache($asset, FALSE);
}