You are here

public function EntityUsageTest::testBulkDeleteHosts in Entity Usage 8

Tests the bulkDeleteHosts() method.

@covers \Drupal\entity_usage\EntityUsage::bulkDeleteHosts

File

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

Class

EntityUsageTest
Tests the basic API operations of our tracking service..

Namespace

Drupal\Tests\entity_usage\Kernel

Code

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

  // Create 2 fake registers on the database table, one for each entity.
  foreach ($this->testEntities as $entity) {
    $this->injectedDatabase
      ->insert($this->tableName)
      ->fields([
      't_id' => 1,
      't_type' => 'foo',
      're_id' => $entity
        ->id(),
      're_type' => $entity_type,
      'method' => 'entity_reference',
      'count' => 1,
    ])
      ->execute();
  }

  /** @var \Drupal\entity_usage\EntityUsage $entity_usage */
  $entity_usage = $this->container
    ->get('entity_usage.usage');
  $entity_usage
    ->bulkDeleteHosts($entity_type);
  $event = \Drupal::state()
    ->get('entity_usage_events_test.usage_bulk_hosts_delete', []);
  $this
    ->assertSame($event['event_name'], Events::BULK_HOSTS_DELETE);
  $this
    ->assertSame($event['target_id'], NULL);
  $this
    ->assertSame($event['target_type'], NULL);
  $this
    ->assertSame($event['referencing_id'], NULL);
  $this
    ->assertSame($event['referencing_type'], $entity_type);
  $this
    ->assertSame($event['method'], NULL);
  $this
    ->assertSame($event['count'], NULL);

  // Check if there are no records left.
  $count = $this->injectedDatabase
    ->select($this->tableName, 'e')
    ->fields('e', [
    'count',
  ])
    ->condition('e.re_type', $entity_type)
    ->execute()
    ->fetchField();
  $this
    ->assertSame(FALSE, $count, 'Successfully deleted all records of this type.');

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