You are here

public function EntityUsageTest::testBulkDeleteTargets in Entity Usage 8.3

Same name and namespace in other branches
  1. 8.4 tests/src/Kernel/EntityUsageTest.php \Drupal\Tests\entity_usage\Kernel\EntityUsageTest::testBulkDeleteTargets()
  2. 8 tests/src/Kernel/EntityUsageTest.php \Drupal\Tests\entity_usage\Kernel\EntityUsageTest::testBulkDeleteTargets()
  3. 8.2 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 252

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([
      'target_id' => $entity
        ->id(),
      'target_type' => $entity_type,
      'source_id' => 1,
      'source_type' => 'foo',
      'source_langcode' => 'en',
      'source_vid' => 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_delete_targets', []);
  $this
    ->assertSame($event['event_name'], Events::BULK_DELETE_DESTINATIONS);
  $this
    ->assertSame($event['target_id'], NULL);
  $this
    ->assertSame($event['target_type'], $entity_type);
  $this
    ->assertSame($event['source_id'], NULL);
  $this
    ->assertSame($event['source_type'], NULL);
  $this
    ->assertSame($event['source_langcode'], NULL);
  $this
    ->assertSame($event['source_vid'], NULL);

  // Check if there are no records left.
  $usages = $this->injectedDatabase
    ->select($this->tableName, 'e')
    ->fields('e', [])
    ->condition('e.target_type', $entity_type)
    ->execute()
    ->fetchAll();
  $this
    ->assertSame(0, count($usages));

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