You are here

public function EntityUsageTest::testBulkDeleteSources 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::testBulkDeleteSources()
  2. 8.2 tests/src/Kernel/EntityUsageTest.php \Drupal\Tests\entity_usage\Kernel\EntityUsageTest::testBulkDeleteSources()

Tests the bulkDeleteSources() method.

@covers \Drupal\entity_usage\EntityUsage::bulkDeleteSources

File

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

Class

EntityUsageTest
Tests the basic API operations of our tracking service.

Namespace

Drupal\Tests\entity_usage\Kernel

Code

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

  // Create 2 fake registers on the database table, one for each entity.
  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,
    ])
      ->execute();
  }

  /** @var \Drupal\entity_usage\EntityUsage $entity_usage */
  $entity_usage = $this->container
    ->get('entity_usage.usage');
  $entity_usage
    ->bulkDeleteSources($entity_type);
  $event = \Drupal::state()
    ->get('entity_usage_events_test.usage_bulk_delete_sources', []);
  $this
    ->assertSame($event['event_name'], Events::BULK_DELETE_SOURCES);
  $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);

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

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