View source
<?php
namespace Drupal\Tests\entity_usage\Kernel;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\filter\Entity\FilterFormat;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
class EntityReferenceKernelTest extends EntityKernelTestBase {
use EntityReferenceTestTrait;
public static $modules = [
'entity_reference_test',
'entity_usage',
];
protected $entityType = 'entity_test';
protected $bundle = 'entity_test';
protected $fieldName = 'field_test';
protected $referencedEntity;
protected $testEntities;
protected $injectedDatabase;
protected $tableName;
protected function setUp() {
parent::setUp();
$this->injectedDatabase = $this->container
->get('database');
$this
->installSchema('entity_usage', [
'entity_usage',
]);
$this->tableName = 'entity_usage';
$this
->createEntityReferenceField($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->entityType, 'default', [], FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
FieldStorageConfig::create([
'field_name' => 'body',
'entity_type' => $this->entityType,
'type' => 'text',
'settings' => array(),
])
->save();
FieldConfig::create([
'entity_type' => $this->entityType,
'bundle' => $this->bundle,
'field_name' => 'body',
'label' => 'Body',
])
->save();
FilterFormat::create([
'format' => 'full_html',
'name' => 'Full HTML',
])
->save();
$this->referencedEntity = $this->container
->get('entity_type.manager')
->getStorage($this->entityType)
->create([
'name' => $this
->randomMachineName(),
]);
$this->referencedEntity
->save();
$this->testEntities = $this
->getTestEntities();
$this->testEntities[0]->body = [
'value' => '<p>Lorem ipsum 1</p>',
'format' => 'full_html',
];
$this->testEntities[0]
->save();
$this->testEntities[1]->body = [
'value' => '<p>Lorem ipsum 2</p>',
'format' => 'full_html',
];
$this->testEntities[1]
->save();
}
public function testEntityReferenceTracking() {
$entity_usage = $this->container
->get('entity_usage.usage');
$field_name = $this->fieldName;
$referencing_entity = $this->testEntities[0];
$usage = $entity_usage
->listUsage($this->referencedEntity);
$this
->assertSame([], $usage, 'Initial usage is correctly empty.');
$referencing_entity->{$field_name}->entity = $this->referencedEntity;
$referencing_entity
->save();
$usage = $entity_usage
->listUsage($this->referencedEntity);
$this
->assertEquals([
$referencing_entity
->getEntityTypeId() => [
$referencing_entity
->id() => 1,
],
], $usage, 'The usage count is correct.');
$referencing_entity->body = [
'value' => '<p>Modified lorem ipsum</p>',
'format' => 'full_html',
];
$referencing_entity
->save();
$usage = $entity_usage
->listUsage($this->referencedEntity);
$this
->assertEquals([
$referencing_entity
->getEntityTypeId() => [
$referencing_entity
->id() => 1,
],
], $usage, 'The usage count is correct.');
$referencing_entity->{$field_name}->entity = $this->testEntities[1];
$referencing_entity
->save();
$usage = $entity_usage
->listUsage($this->referencedEntity);
$this
->assertSame([], $usage, 'Non-referenced usage is correctly empty.');
$referencing_entity->{$field_name}->entity = $this->referencedEntity;
$referencing_entity
->save();
$usage = $entity_usage
->listUsage($this->referencedEntity);
$this
->assertEquals([
$referencing_entity
->getEntityTypeId() => [
$referencing_entity
->id() => 1,
],
], $usage, 'The usage count is correct.');
$referencing_entity
->delete();
$usage = $entity_usage
->listUsage($this->referencedEntity);
$this
->assertSame([], $usage, 'Non-referenced usage is correctly empty.');
$referencing_entity = $this->testEntities[1];
$referencing_entity->{$field_name}->entity = $this->referencedEntity;
$referencing_entity
->save();
$usage = $entity_usage
->listUsage($this->referencedEntity);
$this
->assertEquals([
$referencing_entity
->getEntityTypeId() => [
$referencing_entity
->id() => 1,
],
], $usage, 'The usage count is correct.');
}
protected function getTestEntities() {
$content_entity_1 = EntityTest::create([
'name' => $this
->randomMachineName(),
]);
$content_entity_1
->save();
$content_entity_2 = EntityTest::create([
'name' => $this
->randomMachineName(),
]);
$content_entity_2
->save();
return [
$content_entity_1,
$content_entity_2,
];
}
}