diff options
author | Dries Buytaert <dries@buytaert.net> | 2009-01-06 12:00:40 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2009-01-06 12:00:40 +0000 |
commit | 2e1827d0402de6d1dc355e30756b8cf8070e4044 (patch) | |
tree | f9ea1d921d82ab620098a9c6e8d9ecee07099073 | |
parent | cce322378c7b16258cf088082664ee796c3c8eda (diff) | |
download | brdo-2e1827d0402de6d1dc355e30756b8cf8070e4044.tar.gz brdo-2e1827d0402de6d1dc355e30756b8cf8070e4044.tar.bz2 |
- Patch #341910 by Josh Waihi, drewish et al: file_space_used() was not properly checking bitmapped status values. Added unit tests too.
-rw-r--r-- | includes/file.inc | 9 | ||||
-rw-r--r-- | modules/simpletest/tests/file.test | 61 |
2 files changed, 68 insertions, 2 deletions
diff --git a/includes/file.inc b/includes/file.inc index 3b7e03019..b7a096f98 100644 --- a/includes/file.inc +++ b/includes/file.inc @@ -767,10 +767,15 @@ function file_unmanaged_delete($path) { * An integer containing the number of bytes used. */ function file_space_used($uid = NULL, $status = FILE_STATUS_PERMANENT) { + $query = db_select('files', 'f'); + // Use separate placeholders for the status to avoid a bug in some versions + // of PHP. @see http://drupal.org/node/352956 + $query->where('f.status & :status1 = :status2', array(':status1' => $status, ':status2' => $status)); + $query->addExpression('SUM(f.filesize)', 'filesize'); if (!is_null($uid)) { - return db_query('SELECT SUM(filesize) FROM {files} WHERE uid = :uid AND status = :status', array(':uid' => $uid, ':status' => $status))->fetchField(); + $query->condition('f.uid', $uid); } - return db_query('SELECT SUM(filesize) FROM {files} WHERE status = :status', array(':status' => $status))->fetchField(); + return $query->execute()->fetchField(); } /** diff --git a/modules/simpletest/tests/file.test b/modules/simpletest/tests/file.test index 19b54c9dc..ee7c9ef62 100644 --- a/modules/simpletest/tests/file.test +++ b/modules/simpletest/tests/file.test @@ -130,6 +130,67 @@ class FileHookTestCase extends FileTestCase { } } + +/** + * This will run tests against the file_space_used() function. + */ +class FileSpaceUsedTest extends FileTestCase { + function getInfo() { + return array( + 'name' => t('File space used tests'), + 'description' => t('Tests the file_space_used() function.'), + 'group' => t('File'), + ); + } + + function setUp() { + parent::setUp(); + + // Create records for a couple of users with different sizes. + drupal_write_record('files', $file = array('uid' => 2, 'filesize' => 50, 'status' => FILE_STATUS_PERMANENT)); + drupal_write_record('files', $file = array('uid' => 2, 'filesize' => 20, 'status' => FILE_STATUS_PERMANENT)); + drupal_write_record('files', $file = array('uid' => 3, 'filesize' => 100, 'status' => FILE_STATUS_PERMANENT)); + drupal_write_record('files', $file = array('uid' => 3, 'filesize' => 200, 'status' => FILE_STATUS_PERMANENT)); + + // Now create some with other statuses. These values were chosen arbitrarily + // for the sole purpose of testing that bitwise operators were used + // correctly on the field. + drupal_write_record('files', $file = array('uid' => 2, 'filesize' => 1, 'status' => 2 | 8)); + drupal_write_record('files', $file = array('uid' => 3, 'filesize' => 3, 'status' => 2 | 4)); + } + + /** + * Test different users with the default status. + */ + function testUser() { + $this->assertEqual(file_space_used(2), 70, t("Found the size of the first user's files.")); + $this->assertEqual(file_space_used(3), 300, t("Found the size of the second user's files.")); + $this->assertEqual(file_space_used(), 370, t("Found the size of all user's files.")); + } + + /** + * Test the status fields + */ + function testStatus() { + // Check selection with a single bit set. + $this->assertEqual(file_space_used(NULL, 2), 4, t("Found the size of all user's files with status 2.")); + $this->assertEqual(file_space_used(NULL, 4), 3, t("Found the size of all user's files with status 4.")); + // Check that the bitwise AND operator is used when selecting so that we + // only get files with the 2 AND 4 bits set. + $this->assertEqual(file_space_used(NULL, 2 | 4), 3, t("Found the size of all user's files with status 6.")); + } + + /** + * Test both the user and status. + */ + function testUserAndStatus() { + $this->assertEqual(file_space_used(1, 8), 0, t("Found the size of the admin user's files with status 8.")); + $this->assertEqual(file_space_used(2, 8), 1, t("Found the size of the first user's files with status 8.")); + $this->assertEqual(file_space_used(2, 2), 1, t("Found the size of the first user's files with status 2.")); + $this->assertEqual(file_space_used(3, 2), 3, t("Found the size of the second user's files with status 2.")); + } +} + /** * This will run tests against the file validation functions (file_validate_*). */ |