You are here

public function EntityUsageTest::testGetUsage in Entity Usage 8

Tests the listUsage() method.

@covers \Drupal\entity_usage\EntityUsage::listUsage @covers \Drupal\entity_usage\EntityUsage::listReferencedEntities

File

tests/src/Kernel/EntityUsageTest.php, line 99

Class

EntityUsageTest
Tests the basic API operations of our tracking service..

Namespace

Drupal\Tests\entity_usage\Kernel

Code

public function testGetUsage() {
  $target_entity = $this->testEntities[0];
  $referencing_entity = $this->testEntities[1];
  $this->injectedDatabase
    ->insert($this->tableName)
    ->fields([
    't_id' => $target_entity
      ->id(),
    't_type' => $target_entity
      ->getEntityTypeId(),
    're_id' => $referencing_entity
      ->id(),
    're_type' => $referencing_entity
      ->getEntityTypeId(),
    'method' => 'entity_reference',
    'count' => 1,
  ])
    ->execute();

  /** @var \Drupal\entity_usage\EntityUsage $entity_usage */
  $entity_usage = $this->container
    ->get('entity_usage.usage');
  $complete_usage = $entity_usage
    ->listUsage($target_entity);
  $usage = $complete_usage[$referencing_entity
    ->getEntityTypeId()][$referencing_entity
    ->id()];
  $this
    ->assertEquals(1, $usage, 'Returned the correct count, without tracking method.');
  $complete_usage = $entity_usage
    ->listUsage($target_entity, TRUE);
  $usage = $complete_usage['entity_reference'][$referencing_entity
    ->getEntityTypeId()][$referencing_entity
    ->id()];
  $this
    ->assertEquals(1, $usage, 'Returned the correct count, with tracking method.');
  $complete_references_entities = $entity_usage
    ->listReferencedEntities($referencing_entity);
  $usage = $complete_references_entities[$target_entity
    ->getEntityTypeId()][$target_entity
    ->id()];
  $this
    ->assertEquals(1, $usage, 'Returned the correct count.');

  // Clean back the environment.
  $this->injectedDatabase
    ->truncate($this->tableName);
}