You are here

public function EntityUsageTest::testBulkDeleteTargets in Entity Usage 8

Same name and namespace in other branches
  1. 8.4 tests/src/Kernel/EntityUsageTest.php \Drupal\Tests\entity_usage\Kernel\EntityUsageTest::testBulkDeleteTargets()
  2. 8.2 tests/src/Kernel/EntityUsageTest.php \Drupal\Tests\entity_usage\Kernel\EntityUsageTest::testBulkDeleteTargets()
  3. 8.3 tests/src/Kernel/EntityUsageTest.php \Drupal\Tests\entity_usage\Kernel\EntityUsageTest::testBulkDeleteTargets()

Tests the bulkDeleteTargets() method.

@covers \Drupal\entity_usage\EntityUsage::bulkDeleteTargets

File

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

Class

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

Namespace

Drupal\Tests\entity_usage\Kernel

Code

public function testBulkDeleteTargets() {
  $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' => $entity
        ->id(),
      't_type' => $entity_type,
      're_id' => 1,
      're_type' => 'foo',
      'method' => 'entity_reference',
      'count' => 1,
    ])
      ->execute();
  }

  /** @var \Drupal\entity_usage\EntityUsage $entity_usage */
  $entity_usage = $this->container
    ->get('entity_usage.usage');
  $entity_usage
    ->bulkDeleteTargets($entity_type);
  $event = \Drupal::state()
    ->get('entity_usage_events_test.usage_bulk_target_delete', []);
  $this
    ->assertSame($event['event_name'], Events::BULK_TARGETS_DELETE);
  $this
    ->assertSame($event['target_id'], NULL);
  $this
    ->assertSame($event['target_type'], $entity_type);
  $this
    ->assertSame($event['referencing_id'], NULL);
  $this
    ->assertSame($event['referencing_type'], NULL);
  $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.t_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);
}