You are here

public function UsageTest::testGetUsage in Drupal 9

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

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

File

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

Class

UsageTest
Tests file usage functions.

Namespace

Drupal\Tests\file\Kernel

Code

public function testGetUsage() {
  $file = $this
    ->createFile();
  $connection = Database::getConnection();
  $connection
    ->insert('file_usage')
    ->fields([
    'fid' => $file
      ->id(),
    'module' => 'testing',
    'type' => 'foo',
    'id' => 1,
    'count' => 1,
  ])
    ->execute();
  $connection
    ->insert('file_usage')
    ->fields([
    'fid' => $file
      ->id(),
    'module' => 'testing',
    'type' => 'bar',
    'id' => 2,
    'count' => 2,
  ])
    ->execute();
  $usage = $this->container
    ->get('file.usage')
    ->listUsage($file);
  $this
    ->assertCount(2, $usage['testing'], 'Returned the correct number of items.');
  $this
    ->assertTrue(isset($usage['testing']['foo'][1]), 'Returned the correct id.');
  $this
    ->assertTrue(isset($usage['testing']['bar'][2]), 'Returned the correct id.');
  $this
    ->assertEquals(1, $usage['testing']['foo'][1], 'Returned the correct count.');
  $this
    ->assertEquals(2, $usage['testing']['bar'][2], 'Returned the correct count.');
}