You are here

public function GroupTest::testAssetLocation in farmOS 2.x

Test asset location with group membership.

File

modules/asset/group/tests/src/Kernel/GroupTest.php, line 253

Class

GroupTest
Tests for farmOS group membership logic.

Namespace

Drupal\Tests\farm_group\Kernel

Code

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();

  // Populate a cache value dependent on the animal's cache tags.
  $this
    ->populateEntityTestCache($animal);

  // 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();

  // Assert that the animal's cache tags were invalidated.
  $this
    ->assertEntityTestCache($animal, FALSE);

  // Re-populate a cache value dependent on the animal's cache tags.
  $this
    ->populateEntityTestCache($animal);

  // 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();

  // Confirm that new locations are empty.
  $this
    ->assertEmpty($this->assetLocation
    ->getAssetsByLocation([
    $first_pasture,
  ]), 'New locations are empty.');

  // 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.');
  $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.');

  // Assert that the animal's cache tags were invalidated.
  $this
    ->assertEntityTestCache($animal, FALSE);

  // Re-populate a cache value dependent on the animal's cache tags.
  $this
    ->populateEntityTestCache($animal);

  // 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.');
  $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.');

  // Assert that the animal's cache tags were invalidated.
  $this
    ->assertEntityTestCache($animal, FALSE);

  // Re-populate a cache value dependent on the animal's cache tags.
  $this
    ->populateEntityTestCache($animal);

  // 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.');
  $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.');

  // Assert that the animal's cache tags were invalidated.
  $this
    ->assertEntityTestCache($animal, FALSE);

  // Re-populate a cache value dependent on the animal's cache tags.
  $this
    ->populateEntityTestCache($animal);

  // 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.');
  $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.');

  // Assert that the animal's cache tags were invalidated.
  $this
    ->assertEntityTestCache($animal, FALSE);

  // Delete the fifth log before re-populating asset cache.
  $fifth_log
    ->delete();

  // Re-populate a cache value dependent on the animal's cache tags.
  $this
    ->populateEntityTestCache($animal);

  // Delete the fourth log.
  // When a group's location log is deleted the group's last location is used.
  $fourth_log
    ->delete();

  // 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.');
  $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.');

  // Assert that the animal's cache tags were invalidated.
  $this
    ->assertEntityTestCache($animal, FALSE);
}