1 file.test FileUsageTest::testRemoveUsage()

Tests file_usage_delete().

File

core/modules/simpletest/tests/file.test, line 2257
This provides SimpleTests for the core file handling functionality. These include FileValidateTest and FileSaveTest.

Class

FileUsageTest
Tests file usage functions.

Code

function testRemoveUsage() {
  $file = $this->createFile();
  db_insert('file_usage')
    ->fields(array(
      'fid' => $file->fid,
      '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->fid)
    ->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->fid)
    ->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->fid)
    ->execute()
    ->fetchField();
  $this->assertIdentical(FALSE, $count, 'Decrementing non-exist record complete.');
}