You are here

public function EntityUsageTest::testListTargetRevisions in Entity Usage 8.2

Tests the listTargets() method using the revision option.

@covers \Drupal\entity_usage\EntityUsage::listTargets

File

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

Class

EntityUsageTest
Tests the basic API operations of our tracking service.

Namespace

Drupal\Tests\entity_usage\Kernel

Code

public function testListTargetRevisions() {

  /** @var \Drupal\Core\Entity\EntityInterface $target_entity */
  $target_entity = $this->testEntities[0];
  $storage = $this->entityTypeManager
    ->getStorage('entity_test_mulrevpub');

  /** @var \Drupal\entity_usage\EntityUsage $entity_usage */
  $entity_usage = $this->container
    ->get('entity_usage.usage');
  $field_name = 'body';

  // Original entity with no usage.
  $source_entity = EntityTestMulRevPub::create([
    'name' => $this
      ->randomMachineName(),
  ]);
  $source_entity
    ->save();
  $original_revision_id = $source_entity
    ->getRevisionId();

  // Revisioned entity with 1 usage.
  $source_entity
    ->set('name', $this
    ->randomMachineName());
  $source_entity
    ->setNewRevision(TRUE);
  $source_entity
    ->save();
  $revision1_revision_id = $source_entity
    ->getRevisionId();
  $this
    ->insertEntityUsage($source_entity, $target_entity, $field_name);

  // Revisioned again with 1 usage.
  $source_entity
    ->set('name', $this
    ->randomMachineName());
  $source_entity
    ->setNewRevision(TRUE);
  $source_entity
    ->save();
  $this
    ->insertEntityUsage($source_entity, $target_entity, $field_name);

  // Get targets across all revisions.
  $real_target_list = $entity_usage
    ->listTargets($source_entity);
  $expected_target_list = [
    $target_entity
      ->getEntityTypeId() => [
      (string) $target_entity
        ->id() => [
        0 => [
          'method' => 'entity_reference',
          'field_name' => $field_name,
          'count' => 1,
        ],
        1 => [
          'method' => 'entity_reference',
          'field_name' => $field_name,
          'count' => 1,
        ],
      ],
    ],
  ];
  $this
    ->assertEquals($expected_target_list, $real_target_list);

  // Get targets from original entity.
  $real_target_list = $entity_usage
    ->listTargets($source_entity, $original_revision_id);
  $this
    ->assertEquals([], $real_target_list);

  // Get targets from revisioned entity.
  $real_target_list = $entity_usage
    ->listTargets($source_entity, $revision1_revision_id);
  $expected_target_list = [
    $target_entity
      ->getEntityTypeId() => [
      (string) $target_entity
        ->id() => [
        0 => [
          'method' => 'entity_reference',
          'field_name' => $field_name,
          'count' => 1,
        ],
      ],
    ],
  ];
  $this
    ->assertEquals($expected_target_list, $real_target_list);

  // Invalid revision ID.
  $real_target_list = $entity_usage
    ->listTargets($source_entity, 9999);
  $this
    ->assertEquals([], $real_target_list);

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