View source
<?php
namespace Drupal\Tests\farm_group\Kernel;
use Drupal\asset\Entity\Asset;
use Drupal\KernelTests\KernelTestBase;
use Drupal\log\Entity\Log;
use Drupal\Tests\farm_test\Kernel\FarmEntityCacheTestTrait;
class GroupTest extends KernelTestBase {
use FarmEntityCacheTestTrait;
protected $groupMembership;
protected $assetLocation;
protected $logLocation;
protected static $modules = [
'asset',
'log',
'farm_field',
'farm_group',
'farm_group_test',
'farm_location',
'farm_log',
'geofield',
'state_machine',
'user',
];
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');
$this
->installConfig([
'farm_group',
'farm_group_test',
]);
}
public function testGroupMembership() {
$animal = Asset::create([
'type' => 'animal',
'name' => $this
->randomMachineName(),
'status' => 'active',
]);
$animal
->save();
$this
->populateEntityTestCache($animal);
$first_group = Asset::create([
'type' => 'group',
'name' => $this
->randomMachineName(),
'status' => 'active',
]);
$first_group
->save();
$second_group = Asset::create([
'type' => 'group',
'name' => $this
->randomMachineName(),
'status' => 'active',
]);
$second_group
->save();
$this
->assertFalse($this->groupMembership
->hasGroup($animal), 'New assets do not have group membership.');
$this
->assertEmpty($this->groupMembership
->getGroup($animal), 'New assets do not reference any groups.');
$this
->assertEmpty($this->groupMembership
->getGroupMembers([
$first_group,
]), 'New groups have no members.');
$this
->assertEmpty($this->groupMembership
->getGroupMembers([
$second_group,
]), 'New groups have no members.');
$this
->assertEntityTestCache($animal, TRUE);
$first_log = Log::create([
'type' => 'test',
'status' => 'done',
'is_group_assignment' => TRUE,
'group' => [
'target_id' => $first_group
->id(),
],
'asset' => [
'target_id' => $animal
->id(),
],
]);
$first_log
->save();
$this
->assertTrue($this->groupMembership
->hasGroup($animal), 'Asset with group assignment has group membership.');
$this
->assertEquals($first_group
->id(), $this->groupMembership
->getGroup($animal)[0]
->id(), 'Asset with group assignment is in the assigned group.');
$this
->assertEquals(1, count($this->groupMembership
->getGroupMembers([
$first_group,
])), 'When an asset becomes a group member, the group has one member.');
$this
->assertEmpty($this->groupMembership
->getGroupMembers([
$second_group,
]), 'When an asset becomes a group member, other groups are unaffected.');
$this
->assertEntityTestCache($animal, FALSE);
$this
->populateEntityTestCache($animal);
$second_log = Log::create([
'type' => 'test',
'status' => 'pending',
'is_group_assignment' => TRUE,
'group' => [
'target_id' => $second_group
->id(),
],
'asset' => [
'target_id' => $animal
->id(),
],
]);
$second_log
->save();
$this
->assertEquals($first_group
->id(), $this->groupMembership
->getGroup($animal)[0]
->id(), 'Pending group assignment logs do not affect membership.');
$this
->assertEmpty($this->groupMembership
->getGroupMembers([
$second_group,
]), 'Groups with only pending membership have zero members.');
$this
->assertEntityTestCache($animal, TRUE);
$second_log->status = 'done';
$second_log
->save();
$this
->assertEquals($second_group
->id(), $this->groupMembership
->getGroup($animal)[0]
->id(), 'A second group assignment log updates group membership.');
$this
->assertEquals(1, count($this->groupMembership
->getGroupMembers([
$second_group,
])), 'Completed group assignment logs add group members.');
$this
->assertEntityTestCache($animal, FALSE);
$this
->populateEntityTestCache($animal);
$third_log = Log::create([
'type' => 'test',
'timestamp' => \Drupal::time()
->getRequestTime() + 86400,
'status' => 'done',
'is_group_assignment' => TRUE,
'group' => [
'target_id' => $first_group
->id(),
],
'asset' => [
'target_id' => $animal
->id(),
],
]);
$third_log
->save();
$this
->assertEquals($second_group
->id(), $this->groupMembership
->getGroup($animal)[0]
->id(), 'A third group assignment log in the future does not update group membership.');
$this
->assertEquals(1, count($this->groupMembership
->getGroupMembers([
$second_group,
])), 'Future group assignment logs do not affect members.');
$this
->assertEntityTestCache($animal, TRUE);
$fourth_log = Log::create([
'type' => 'test',
'status' => 'done',
'is_group_assignment' => TRUE,
'group' => [],
'asset' => [
'target_id' => $animal
->id(),
],
]);
$fourth_log
->save();
$this
->assertFalse($this->groupMembership
->hasGroup($animal), 'Asset group membership can be unset.');
$this
->assertEmpty($this->groupMembership
->getGroup($animal), 'Unset group membership does not reference any groups.');
$this
->assertEquals(0, count($this->groupMembership
->getGroupMembers([
$first_group,
])), 'Unset group membership unsets group members.');
$this
->assertEquals(0, count($this->groupMembership
->getGroupMembers([
$second_group,
])), 'Unset group membership unsets group members.');
$this
->assertEntityTestCache($animal, FALSE);
$this
->populateEntityTestCache($animal);
$fourth_log
->delete();
$this
->assertEquals($second_group
->id(), $this->groupMembership
->getGroup($animal)[0]
->id(), 'Deleting a group membership log updates group membership.');
$this
->assertEquals(1, count($this->groupMembership
->getGroupMembers([
$second_group,
])), 'Deleting a group membership log updates group members.');
$this
->assertEntityTestCache($animal, FALSE);
$second_animal = Asset::create([
'type' => 'animal',
'name' => $this
->randomMachineName(),
'status' => 'active',
]);
$second_animal
->save();
$fifth_log = Log::create([
'type' => 'test',
'status' => 'done',
'is_group_assignment' => TRUE,
'group' => [
'target_id' => $first_group
->id(),
],
'asset' => [
'target_id' => $second_animal
->id(),
],
]);
$fifth_log
->save();
$this
->assertEquals($second_group
->id(), $this->groupMembership
->getGroup($animal)[0]
->id(), 'The first animal is in the second group.');
$this
->assertEquals($first_group
->id(), $this->groupMembership
->getGroup($second_animal)[0]
->id(), 'The second animal is in the first group.');
$group_members = $this->groupMembership
->getGroupMembers([
$first_group,
$second_group,
]);
$this
->assertEquals(2, count($group_members), 'Group members from multiple groups can be queried together.');
}
public function testAssetLocation() {
$animal = Asset::create([
'type' => 'animal',
'name' => $this
->randomMachineName(),
'status' => 'active',
]);
$animal
->save();
$this
->populateEntityTestCache($animal);
$group = Asset::create([
'type' => 'group',
'name' => $this
->randomMachineName(),
'status' => 'active',
]);
$group
->save();
$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();
$this
->assertEntityTestCache($animal, FALSE);
$this
->populateEntityTestCache($animal);
$first_pasture = Asset::create([
'type' => 'pasture',
'name' => $this
->randomMachineName(),
'status' => 'active',
]);
$first_pasture
->save();
$second_pasture = Asset::create([
'type' => 'pasture',
'name' => $this
->randomMachineName(),
'status' => 'active',
]);
$second_pasture
->save();
$this
->assertEmpty($this->assetLocation
->getAssetsByLocation([
$first_pasture,
]), 'New locations are empty.');
$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();
$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.');
$this
->assertEquals(1, count($this->assetLocation
->getAssetsByLocation([
$first_pasture,
])), 'Locations have assets that are moved to them.');
$this
->assertEmpty($this->assetLocation
->getAssetsByLocation([
$second_pasture,
]), 'Locations that do not have assets moved to them are unaffected.');
$this
->assertEntityTestCache($animal, FALSE);
$this
->populateEntityTestCache($animal);
$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();
$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.');
$this
->assertEmpty($this->assetLocation
->getAssetsByLocation([
$first_pasture,
]), 'A group movement removes assets from their previous location.');
$this
->assertEquals(2, count($this->assetLocation
->getAssetsByLocation([
$second_pasture,
])), 'A group movement adds assets to their new location.');
$this
->assertEntityTestCache($animal, FALSE);
$this
->populateEntityTestCache($animal);
$fourth_log = Log::create([
'type' => 'test',
'status' => 'done',
'is_movement' => TRUE,
'location' => [],
'asset' => [
'target_id' => $group
->id(),
],
]);
$fourth_log
->save();
$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.');
$this
->assertEmpty($this->assetLocation
->getAssetsByLocation([
$first_pasture,
]), 'Unsetting group location removes member assets from all locations.');
$this
->assertEmpty($this->assetLocation
->getAssetsByLocation([
$second_pasture,
]), 'Unsetting group location removes member assets from all locations.');
$this
->assertEntityTestCache($animal, FALSE);
$this
->populateEntityTestCache($animal);
$fifth_log = Log::create([
'type' => 'test',
'status' => 'done',
'is_group_assignment' => TRUE,
'group' => [],
'asset' => [
'target_id' => $animal
->id(),
],
]);
$fifth_log
->save();
$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.');
$this
->assertEquals(1, count($this->assetLocation
->getAssetsByLocation([
$first_pasture,
])), 'Unsetting group membership adds assets to their previous location.');
$this
->assertEmpty($this->assetLocation
->getAssetsByLocation([
$second_pasture,
]), 'Unsetting group membership removes member assets from the group location.');
$this
->assertEntityTestCache($animal, FALSE);
$fifth_log
->delete();
$this
->populateEntityTestCache($animal);
$fourth_log
->delete();
$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.');
$this
->assertEmpty($this->assetLocation
->getAssetsByLocation([
$first_pasture,
]), 'A group movement removes assets from their previous location.');
$this
->assertEquals(2, count($this->assetLocation
->getAssetsByLocation([
$second_pasture,
])), 'A group movement adds assets to their new location.');
$this
->assertEntityTestCache($animal, FALSE);
}
}