public function EntityUsageTest::testlistSources in Entity Usage 8.4
Same name and namespace in other branches
- 8.2 tests/src/Kernel/EntityUsageTest.php \Drupal\Tests\entity_usage\Kernel\EntityUsageTest::testlistSources()
- 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 103
Class
- EntityUsageTest
- Tests the basic API operations of our tracking service.
Namespace
Drupal\Tests\entity_usage\KernelCode
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->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,
'method' => 'entity_reference',
'field_name' => $field_name,
'count' => 1,
])
->execute();
/** @var \Drupal\entity_usage\EntityUsageInterface $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);
}