You are here

public function EntityReferenceKernelTest::testEntityReferenceTracking in Entity Usage 8

Tests basic entity tracking on test entities using entityreference fields.

File

tests/src/Kernel/EntityReferenceKernelTest.php, line 136

Class

EntityReferenceKernelTest
Tests basic usage tracking on generic entities.

Namespace

Drupal\Tests\entity_usage\Kernel

Code

public function testEntityReferenceTracking() {

  /** @var \Drupal\entity_usage\EntityUsage $entity_usage */
  $entity_usage = $this->container
    ->get('entity_usage.usage');
  $field_name = $this->fieldName;
  $referencing_entity = $this->testEntities[0];

  // First check usage is 0 for the referenced entity.
  $usage = $entity_usage
    ->listUsage($this->referencedEntity);
  $this
    ->assertSame([], $usage, 'Initial usage is correctly empty.');

  // Reference from other entity and check that the usage increases to 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.');

  // Update other values on the referencing entity, check usage remains 1.
  $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.');

  // Delete the field value from the entityreference field and check that the
  // usage goes back to 0.
  $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.');

  // Create a reference again, check the value is back to 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.');

  // Delete the whole referencing entity, check usage goes back to 0.
  $referencing_entity
    ->delete();
  $usage = $entity_usage
    ->listUsage($this->referencedEntity);
  $this
    ->assertSame([], $usage, 'Non-referenced usage is correctly empty.');

  // Create a reference again, check the value is back to 1.
  $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.');

  // Unpublish the host entity, check usage goes back to 0.
  // We don't deal with entities statuses yet.

  /*
      $referencing_entity->status = FALSE;
      $referencing_entity->save();
      $usage = $entity_usage->listUsage($this->referencedEntity);
      $this->assertSame([], $usage, 'Non-referenced usage is correctly empty.');
  */
}