mirror of
https://github.com/farmOS/farmOS.git
synced 2024-02-23 11:37:38 +01:00
Automated tests for group asset location logic.
This commit is contained in:
parent
bbf8866a27
commit
6819f22090
3 changed files with 153 additions and 0 deletions
|
@ -0,0 +1,7 @@
|
|||
langcode: en
|
||||
status: true
|
||||
id: pasture
|
||||
label: Pasture
|
||||
description: ''
|
||||
workflow: asset_default
|
||||
new_revision: true
|
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\farm_group_test\Plugin\Asset\AssetType;
|
||||
|
||||
use Drupal\farm_entity\Plugin\Asset\AssetType\FarmAssetType;
|
||||
|
||||
/**
|
||||
* Provides the pasture asset type.
|
||||
*
|
||||
* @AssetType(
|
||||
* id = "pasture",
|
||||
* label = @Translation("Pasture"),
|
||||
* )
|
||||
*/
|
||||
class Pasture extends FarmAssetType {
|
||||
|
||||
}
|
|
@ -20,6 +20,20 @@ class GroupTest extends KernelTestBase {
|
|||
*/
|
||||
protected $groupMembership;
|
||||
|
||||
/**
|
||||
* Asset location service.
|
||||
*
|
||||
* @var \Drupal\farm_location\AssetLocationInterface
|
||||
*/
|
||||
protected $assetLocation;
|
||||
|
||||
/**
|
||||
* Log location service.
|
||||
*
|
||||
* @var \Drupal\farm_location\LogLocationInterface
|
||||
*/
|
||||
protected $logLocation;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
@ -29,6 +43,7 @@ class GroupTest extends KernelTestBase {
|
|||
'farm_field',
|
||||
'farm_group',
|
||||
'farm_group_test',
|
||||
'farm_location',
|
||||
'farm_log',
|
||||
'geofield',
|
||||
'state_machine',
|
||||
|
@ -41,6 +56,8 @@ class GroupTest extends KernelTestBase {
|
|||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
$this->groupMembership = \Drupal::service('group.membership');
|
||||
$this->assetLocation = \Drupal::service('asset.location');
|
||||
$this->logLocation = \Drupal::service('log.location');
|
||||
$this->installEntitySchema('asset');
|
||||
$this->installEntitySchema('log');
|
||||
$this->installEntitySchema('user');
|
||||
|
@ -154,4 +171,116 @@ class GroupTest extends KernelTestBase {
|
|||
$this->assertEmpty($this->groupMembership->getGroup($animal), 'Unset group membership does not reference any groups.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test asset location with group membership.
|
||||
*/
|
||||
public function testAssetLocation() {
|
||||
|
||||
// Create an animal asset.
|
||||
/** @var \Drupal\asset\Entity\AssetInterface $animal */
|
||||
$animal = Asset::create([
|
||||
'type' => 'animal',
|
||||
'name' => $this->randomMachineName(),
|
||||
'status' => 'active',
|
||||
]);
|
||||
$animal->save();
|
||||
|
||||
// Create a group asset.
|
||||
/** @var \Drupal\asset\Entity\AssetInterface $group */
|
||||
$group = Asset::create([
|
||||
'type' => 'group',
|
||||
'name' => $this->randomMachineName(),
|
||||
'status' => 'active',
|
||||
]);
|
||||
$group->save();
|
||||
|
||||
// Create a log that assigns the animal to the group.
|
||||
/** @var \Drupal\log\Entity\LogInterface $first_log */
|
||||
$first_log = Log::create([
|
||||
'type' => 'test',
|
||||
'status' => 'done',
|
||||
'is_group_assignment' => TRUE,
|
||||
'group' => ['target_id' => $group->id()],
|
||||
'asset' => ['target_id' => $animal->id()],
|
||||
]);
|
||||
$first_log->save();
|
||||
|
||||
// Create two pasture assets.
|
||||
/** @var \Drupal\asset\Entity\AssetInterface $first_pasture */
|
||||
$first_pasture = Asset::create([
|
||||
'type' => 'pasture',
|
||||
'name' => $this->randomMachineName(),
|
||||
'status' => 'active',
|
||||
]);
|
||||
$first_pasture->save();
|
||||
/** @var \Drupal\asset\Entity\AssetInterface $second_pasture */
|
||||
$second_pasture = Asset::create([
|
||||
'type' => 'pasture',
|
||||
'name' => $this->randomMachineName(),
|
||||
'status' => 'active',
|
||||
]);
|
||||
$second_pasture->save();
|
||||
|
||||
// Create a log that moves the animal to the first pasture.
|
||||
/** @var \Drupal\log\Entity\LogInterface $second_log */
|
||||
$second_log = Log::create([
|
||||
'type' => 'test',
|
||||
'status' => 'done',
|
||||
'is_movement' => TRUE,
|
||||
'location' => ['target_id' => $first_pasture->id()],
|
||||
'asset' => ['target_id' => $animal->id()],
|
||||
]);
|
||||
$second_log->save();
|
||||
|
||||
// Confirm that the animal is located in the first pasture.
|
||||
$this->assertEquals($this->logLocation->getLocation($second_log), $this->assetLocation->getLocation($animal), 'Asset location is determined by asset membership log.');
|
||||
$this->assertEquals($this->logLocation->getGeometry($second_log), $this->assetLocation->getGeometry($animal), 'Asset geometry is determined by asset membership log.');
|
||||
|
||||
// Create a log that moves the group to the second pasture.
|
||||
/** @var \Drupal\log\Entity\LogInterface $third_log */
|
||||
$third_log = Log::create([
|
||||
'type' => 'test',
|
||||
'status' => 'done',
|
||||
'is_movement' => TRUE,
|
||||
'location' => ['target_id' => $second_pasture->id()],
|
||||
'asset' => ['target_id' => $group->id()],
|
||||
]);
|
||||
$third_log->save();
|
||||
|
||||
// Confirm that the animal is located in the second pasture.
|
||||
$this->assertEquals($this->logLocation->getLocation($third_log), $this->assetLocation->getLocation($animal), 'Asset location is determined by group membership log.');
|
||||
$this->assertEquals($this->logLocation->getGeometry($third_log), $this->assetLocation->getGeometry($animal), 'Asset geometry is determined by group membership log.');
|
||||
|
||||
// Create a log that unsets the group location.
|
||||
/** @var \Drupal\log\Entity\LogInterface $fourth_log */
|
||||
$fourth_log = Log::create([
|
||||
'type' => 'test',
|
||||
'status' => 'done',
|
||||
'is_movement' => TRUE,
|
||||
'location' => [],
|
||||
'asset' => ['target_id' => $group->id()],
|
||||
]);
|
||||
$fourth_log->save();
|
||||
|
||||
// Confirm that the animal location was unset.
|
||||
$this->assertEquals($this->logLocation->getLocation($fourth_log), $this->assetLocation->getLocation($animal), 'Asset location can be unset by group membership log.');
|
||||
$this->assertEquals($this->logLocation->getGeometry($fourth_log), $this->assetLocation->getGeometry($animal), 'Asset geometry can be unset by group membership log.');
|
||||
|
||||
// Create a log that unsets the animal's group membership.
|
||||
/** @var \Drupal\log\Entity\LogInterface $fifth_log */
|
||||
$fifth_log = Log::create([
|
||||
'type' => 'test',
|
||||
'status' => 'done',
|
||||
'is_group_assignment' => TRUE,
|
||||
'group' => [],
|
||||
'asset' => ['target_id' => $animal->id()],
|
||||
]);
|
||||
$fifth_log->save();
|
||||
|
||||
// Confirm that the animal's location is determined by its own movement
|
||||
// logs now.
|
||||
$this->assertEquals($this->logLocation->getLocation($second_log), $this->assetLocation->getLocation($animal), 'Asset location is determined by asset membership log.');
|
||||
$this->assertEquals($this->logLocation->getGeometry($second_log), $this->assetLocation->getGeometry($animal), 'Asset geometry is determined by asset membership log.');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue