You are here

public function UsageTest::testAddUsage in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/file/tests/src/Kernel/UsageTest.php \Drupal\Tests\file\Kernel\UsageTest::testAddUsage()
  2. 10 core/modules/file/tests/src/Kernel/UsageTest.php \Drupal\Tests\file\Kernel\UsageTest::testAddUsage()

Tests \Drupal\file\FileUsage\DatabaseFileUsageBackend::add().

File

core/modules/file/tests/src/Kernel/UsageTest.php, line 57

Class

UsageTest
Tests file usage functions.

Namespace

Drupal\Tests\file\Kernel

Code

public function testAddUsage() {
  $file = $this
    ->createFile();
  $file_usage = $this->container
    ->get('file.usage');
  $file_usage
    ->add($file, 'testing', 'foo', 1);

  // Add the file twice to ensure that the count is incremented rather than
  // creating additional records.
  $file_usage
    ->add($file, 'testing', 'bar', 2);
  $file_usage
    ->add($file, 'testing', 'bar', 2);
  $usage = Database::getConnection()
    ->select('file_usage', 'f')
    ->fields('f')
    ->condition('f.fid', $file
    ->id())
    ->execute()
    ->fetchAllAssoc('id');
  $this
    ->assertCount(2, $usage, 'Created two records');
  $this
    ->assertEqual($usage[1]->module, 'testing', 'Correct module');
  $this
    ->assertEqual($usage[2]->module, 'testing', 'Correct module');
  $this
    ->assertEqual($usage[1]->type, 'foo', 'Correct type');
  $this
    ->assertEqual($usage[2]->type, 'bar', 'Correct type');
  $this
    ->assertEqual($usage[1]->count, 1, 'Correct count');
  $this
    ->assertEqual($usage[2]->count, 2, 'Correct count');
}