You are here

function UsageTest::testRemoveUsage in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/file/src/Tests/UsageTest.php \Drupal\file\Tests\UsageTest::testRemoveUsage()

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

File

core/modules/file/src/Tests/UsageTest.php, line 78
Contains \Drupal\file\Tests\UsageTest.

Class

UsageTest
Tests file usage functions.

Namespace

Drupal\file\Tests

Code

function testRemoveUsage() {
  $file = $this
    ->createFile();
  $file_usage = $this->container
    ->get('file.usage');
  db_insert('file_usage')
    ->fields(array(
    'fid' => $file
      ->id(),
    'module' => 'testing',
    'type' => 'bar',
    'id' => 2,
    'count' => 3,
  ))
    ->execute();

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

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

  // Non-existent decrement.
  $file_usage
    ->delete($file, 'testing', 'bar', 2);
  $count = db_select('file_usage', 'f')
    ->fields('f', array(
    'count',
  ))
    ->condition('f.fid', $file
    ->id())
    ->execute()
    ->fetchField();
  $this
    ->assertIdentical(FALSE, $count, 'Decrementing non-exist record complete.');
}