You are here

public function UsageTest::doTestRemoveUsage in Drupal 10

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

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

File

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

Class

UsageTest
Tests file usage functions.

Namespace

Drupal\Tests\file\Kernel

Code

public function doTestRemoveUsage() {
  $file = $this
    ->createFile();
  $file
    ->setPermanent();
  $file_usage = $this->container
    ->get('file.usage');
  $connection = Database::getConnection();
  $connection
    ->insert('file_usage')
    ->fields([
    'fid' => $file
      ->id(),
    'module' => 'testing',
    'type' => 'bar',
    'id' => 2,
    'count' => 3,
  ])
    ->execute();

  // Normal decrement.
  $file_usage
    ->delete($file, 'testing', 'bar', 2);
  $count = $connection
    ->select('file_usage', 'f')
    ->fields('f', [
    'count',
  ])
    ->condition('f.fid', $file
    ->id())
    ->execute()
    ->fetchField();
  $this
    ->assertEquals(2, $count, 'The count was decremented correctly.');

  // Multiple decrement and removal.
  $file_usage
    ->delete($file, 'testing', 'bar', 2, 2);
  $count = $connection
    ->select('file_usage', 'f')
    ->fields('f', [
    'count',
  ])
    ->condition('f.fid', $file
    ->id())
    ->execute()
    ->fetchField();
  $this
    ->assertFalse($count, 'The count was removed entirely when empty.');

  // Non-existent decrement.
  $file_usage
    ->delete($file, 'testing', 'bar', 2);
  $count = $connection
    ->select('file_usage', 'f')
    ->fields('f', [
    'count',
  ])
    ->condition('f.fid', $file
    ->id())
    ->execute()
    ->fetchField();
  $this
    ->assertFalse($count, 'Decrementing non-exist record complete.');
  return $file;
}