You are here

public function EntityUsageTest::testBulkDeleteSources in Entity Usage 8.4

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

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,
      'method' => 'entity_reference',
      'field_name' => 'body',
      'count' => 1,
    ])
      ->execute();
  }

  /** @var \Drupal\entity_usage\EntityUsageInterface $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);
  $this
    ->assertSame($event['method'], NULL);
  $this
    ->assertSame($event['field_name'], 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.source_type', $entity_type)
    ->execute()
    ->fetchField();
  $this
    ->assertSame(FALSE, $count);

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