You are here

public function EntityUsageTest::testDeleteByField 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::testDeleteByField()

Tests the deleteByField() method.

@covers \Drupal\entity_usage\EntityUsage::deleteByField

File

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

Class

EntityUsageTest
Tests the basic API operations of our tracking service.

Namespace

Drupal\Tests\entity_usage\Kernel

Code

public function testDeleteByField() {
  $entity_type = $this->testEntities[0]
    ->getEntityTypeId();

  // Create 2 fake registers on the database table, one for each entity.
  $i = 0;
  foreach ($this->testEntities as $entity) {
    $source_vid = $entity instanceof RevisionableInterface && $entity
      ->getRevisionId() ? $entity
      ->getRevisionId() : 0;
    $this->injectedDatabase
      ->insert($this->tableName)
      ->fields([
      'target_id' => 1,
      'target_type' => 'foo',
      'source_id' => $entity
        ->id(),
      'source_type' => $entity_type,
      'source_langcode' => $entity
        ->language()
        ->getId(),
      'source_vid' => $source_vid,
      'method' => 'entity_reference',
      'field_name' => 'body' . $i++,
      'count' => 1,
    ])
      ->execute();
  }

  /** @var \Drupal\entity_usage\EntityUsage $entity_usage */
  $entity_usage = $this->container
    ->get('entity_usage.usage');

  // Delete only one of them, by field.
  $entity_usage
    ->deleteByField($entity_type, 'body1');
  $event = \Drupal::state()
    ->get('entity_usage_events_test.usage_delete_by_field', []);
  $this
    ->assertSame($event['event_name'], Events::DELETE_BY_FIELD);
  $this
    ->assertSame($event['target_id'], NULL);
  $this
    ->assertSame($event['target_type'], NULL);
  $this
    ->assertSame($event['source_id'], NULL);
  $this
    ->assertSame($event['source_type'], $entity_type);
  $this
    ->assertSame($event['source_langcode'], NULL);
  $this
    ->assertSame($event['source_vid'], NULL);
  $this
    ->assertSame($event['method'], NULL);
  $this
    ->assertSame($event['field_name'], 'body1');
  $this
    ->assertSame($event['count'], NULL);
  $result = $this->injectedDatabase
    ->select($this->tableName, 'e')
    ->fields('e')
    ->condition('e.source_type', $entity_type)
    ->execute()
    ->fetchAll();
  $source_vid = $this->testEntities[0] instanceof RevisionableInterface && $this->testEntities[0]
    ->getRevisionId() ? $this->testEntities[0]
    ->getRevisionId() : 0;
  $expected_result = [
    'target_id' => '1',
    'target_id_string' => NULL,
    'target_type' => 'foo',
    'source_id' => (string) $this->testEntities[0]
      ->id(),
    'source_id_string' => NULL,
    'source_type' => $entity_type,
    'source_langcode' => $this->testEntities[0]
      ->language()
      ->getId(),
    'source_vid' => $source_vid,
    'method' => 'entity_reference',
    'field_name' => 'body0',
    'count' => 1,
  ];
  $this
    ->assertEquals([
    (object) $expected_result,
  ], $result);

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