You are here

public function EntityUsageTest::testlistSources in Entity Usage 8.3

Same name and namespace in other branches
  1. 8.4 tests/src/Kernel/EntityUsageTest.php \Drupal\Tests\entity_usage\Kernel\EntityUsageTest::testlistSources()
  2. 8.2 tests/src/Kernel/EntityUsageTest.php \Drupal\Tests\entity_usage\Kernel\EntityUsageTest::testlistSources()

Tests the listSources() and listTargets() method.

@covers \Drupal\entity_usage\EntityUsage::listSources @covers \Drupal\entity_usage\EntityUsage::listTargets

File

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

Class

EntityUsageTest
Tests the basic API operations of our tracking service.

Namespace

Drupal\Tests\entity_usage\Kernel

Code

public function testlistSources() {

  /** @var \Drupal\Core\Entity\EntityInterface $target_entity */
  $target_entity = $this->testEntities[0];

  /** @var \Drupal\Core\Entity\EntityInterface $source_entity */
  $source_entity = $this->testEntities[1];
  $source_vid = $source_entity instanceof RevisionableInterface && $source_entity
    ->getRevisionId() ? $source_entity
    ->getRevisionId() : 0;
  $this->injectedDatabase
    ->insert($this->tableName)
    ->fields([
    'target_id' => $target_entity
      ->id(),
    'target_type' => $target_entity
      ->getEntityTypeId(),
    'source_id' => $source_entity
      ->id(),
    'source_type' => $source_entity
      ->getEntityTypeId(),
    'source_langcode' => $source_entity
      ->language()
      ->getId(),
    'source_vid' => $source_vid,
  ])
    ->execute();

  /** @var \Drupal\entity_usage\EntityUsage $entity_usage */
  $entity_usage = $this->container
    ->get('entity_usage.usage');
  $real_source_list = $entity_usage
    ->listSources($target_entity);
  $expected_source_list = [
    $source_entity
      ->getEntityTypeId() => [
      (string) $source_entity
        ->id() => [
        0 => [
          'source_langcode' => $source_entity
            ->language()
            ->getId(),
          'source_vid' => $source_entity
            ->getRevisionId() ?: 0,
        ],
      ],
    ],
  ];
  $this
    ->assertEquals($expected_source_list, $real_source_list);
  $real_target_list = $entity_usage
    ->listTargets($source_entity);
  $expected_target_list = [
    $target_entity
      ->getEntityTypeId() => [
      (string) $target_entity
        ->id(),
    ],
  ];
  $this
    ->assertEquals($expected_target_list, $real_target_list);

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