function FileUsageTest::testRemoveUsage

Tests file_usage_delete().

File

modules/simpletest/tests/file.test, line 2324

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.');
}

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