You are here

public function EntityUsageTest::testlistSources in Entity Usage 8.2

Same name and namespace in other branches
  1. 8.4 tests/src/Kernel/EntityUsageTest.php \Drupal\Tests\entity_usage\Kernel\EntityUsageTest::testlistSources()
  2. 8.3 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 106

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;
  $field_name = 'body';
  $this
    ->insertEntityUsage($source_entity, $target_entity, $field_name);

  /** @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,
          'method' => 'entity_reference',
          'field_name' => $field_name,
          'count' => 1,
        ],
      ],
    ],
  ];
  $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() => [
        0 => [
          'method' => 'entity_reference',
          'field_name' => $field_name,
          'count' => 1,
        ],
      ],
    ],
  ];
  $this
    ->assertEquals($expected_target_list, $real_target_list);

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