mirror of
https://github.com/farmOS/farmOS.git
synced 2024-02-23 11:37:38 +01:00
158 lines
5.2 KiB
PHP
158 lines
5.2 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace Drupal\Tests\farm_group\Kernel;
|
||
|
|
||
|
use Drupal\asset\Entity\Asset;
|
||
|
use Drupal\KernelTests\KernelTestBase;
|
||
|
use Drupal\log\Entity\Log;
|
||
|
|
||
|
/**
|
||
|
* Tests for farmOS group membership logic.
|
||
|
*
|
||
|
* @group farm
|
||
|
*/
|
||
|
class GroupTest extends KernelTestBase {
|
||
|
|
||
|
/**
|
||
|
* Group membership service.
|
||
|
*
|
||
|
* @var \Drupal\farm_group\GroupMembershipInterface
|
||
|
*/
|
||
|
protected $groupMembership;
|
||
|
|
||
|
/**
|
||
|
* {@inheritdoc}
|
||
|
*/
|
||
|
protected static $modules = [
|
||
|
'asset',
|
||
|
'log',
|
||
|
'farm_field',
|
||
|
'farm_group',
|
||
|
'farm_group_test',
|
||
|
'farm_log',
|
||
|
'geofield',
|
||
|
'state_machine',
|
||
|
'user',
|
||
|
];
|
||
|
|
||
|
/**
|
||
|
* {@inheritdoc}
|
||
|
*/
|
||
|
protected function setUp(): void {
|
||
|
parent::setUp();
|
||
|
$this->groupMembership = \Drupal::service('group.membership');
|
||
|
$this->installEntitySchema('asset');
|
||
|
$this->installEntitySchema('log');
|
||
|
$this->installEntitySchema('user');
|
||
|
$this->installConfig([
|
||
|
'farm_group',
|
||
|
'farm_group_test',
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Test asset group membership.
|
||
|
*/
|
||
|
public function testGroupMembership() {
|
||
|
|
||
|
// Create an animal asset.
|
||
|
/** @var \Drupal\asset\Entity\AssetInterface $animal */
|
||
|
$animal = Asset::create([
|
||
|
'type' => 'animal',
|
||
|
'name' => $this->randomMachineName(),
|
||
|
'status' => 'active',
|
||
|
]);
|
||
|
$animal->save();
|
||
|
|
||
|
// When an asset has no group assignment logs, it has no group membership.
|
||
|
$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.');
|
||
|
|
||
|
// Create a group asset.
|
||
|
/** @var \Drupal\asset\Entity\AssetInterface $first_group */
|
||
|
$first_group = Asset::create([
|
||
|
'type' => 'group',
|
||
|
'name' => $this->randomMachineName(),
|
||
|
'status' => 'active',
|
||
|
]);
|
||
|
$first_group->save();
|
||
|
|
||
|
// Create a "done" 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' => $first_group->id()],
|
||
|
'asset' => ['target_id' => $animal->id()],
|
||
|
]);
|
||
|
$first_log->save();
|
||
|
|
||
|
// When an asset has a done group assignment logs, it has group membership.
|
||
|
$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.');
|
||
|
|
||
|
// Create a second group asset.
|
||
|
/** @var \Drupal\asset\Entity\AssetInterface $second_group */
|
||
|
$second_group = Asset::create([
|
||
|
'type' => 'group',
|
||
|
'name' => $this->randomMachineName(),
|
||
|
'status' => 'active',
|
||
|
]);
|
||
|
$second_group->save();
|
||
|
|
||
|
// Create a "pending" log that assigns the animal to the second group.
|
||
|
/** @var \Drupal\log\Entity\LogInterface $second_log */
|
||
|
$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();
|
||
|
|
||
|
// When an asset has a pending group assignment logs, it still has the same
|
||
|
// group membership as before.
|
||
|
$this->assertEquals($first_group->id(), $this->groupMembership->getGroup($animal)[0]->id(), 'Pending group assignment logs do not affect membership.');
|
||
|
|
||
|
// When the log is marked as "done", the asset's membership is updated.
|
||
|
$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.');
|
||
|
|
||
|
// Create a third "done" log in the future.
|
||
|
/** @var \Drupal\log\Entity\LogInterface $third_log */
|
||
|
$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();
|
||
|
|
||
|
// When an asset has a "done" group assignment log in the future, the asset
|
||
|
// group membership remains the same as the previous "done" movement log.
|
||
|
$this->assertEquals($second_group->id(), $this->groupMembership->getGroup($animal)[0]->id(), 'A third group assignment log in the future does not update group membership.');
|
||
|
|
||
|
// Create a fourth log with no group reference.
|
||
|
/** @var \Drupal\log\Entity\LogInterface $fourth_log */
|
||
|
$fourth_log = Log::create([
|
||
|
'type' => 'test',
|
||
|
'status' => 'done',
|
||
|
'is_group_assignment' => TRUE,
|
||
|
'group' => [],
|
||
|
'asset' => ['target_id' => $animal->id()],
|
||
|
]);
|
||
|
$fourth_log->save();
|
||
|
|
||
|
// When a group assignment log is created with no group references, it
|
||
|
// effectively "unsets" the asset's group membership.
|
||
|
$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.');
|
||
|
}
|
||
|
|
||
|
}
|