function UsageTest::testGetUsage

Same name in other branches
  1. 9 core/modules/file/tests/src/Kernel/UsageTest.php \Drupal\Tests\file\Kernel\UsageTest::testGetUsage()
  2. 8.9.x core/modules/file/tests/src/Kernel/UsageTest.php \Drupal\Tests\file\Kernel\UsageTest::testGetUsage()
  3. 11.x 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 25

Class

UsageTest
Tests file usage functions.

Namespace

Drupal\Tests\file\Kernel

Code

public function testGetUsage() : void {
    $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.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.