From 4a7bb638fb3243a35dd2ce82a4cafc78d2f6b9d2 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Sun, 22 Aug 2010 13:52:59 +0000 Subject: - Patch #353458 by quicksketch, drewish, jpetso, sun, noahb, aaron, chx, mikey_p, dhthwy: hook_file_references() was not designed for a highly flexible field storage. --- modules/simpletest/tests/file.test | 148 +++++++++++++++++++++++++++--- modules/simpletest/tests/file_test.module | 24 ++--- 2 files changed, 144 insertions(+), 28 deletions(-) (limited to 'modules/simpletest/tests') diff --git a/modules/simpletest/tests/file.test b/modules/simpletest/tests/file.test index cf661c63b..543c8c2af 100644 --- a/modules/simpletest/tests/file.test +++ b/modules/simpletest/tests/file.test @@ -256,13 +256,13 @@ class FileHookTestCase extends FileTestCase { $this->assertTrue(FALSE, t('Expected hooks %expected to be called but %uncalled was not called.', array('%expected' => implode(', ', $expected), '%uncalled' => implode(', ', $uncalled)))); } else { - $this->assertTrue(TRUE, t('All the expected hooks were called: %expected', array('%expected' => implode(', ', $expected)))); + $this->assertTrue(TRUE, t('All the expected hooks were called: %expected', array('%expected' => empty($expected) ? t('(none)') : implode(', ', $expected)))); } // Determine if there were any unexpected calls. $unexpected = array_diff($actual, $expected); if (count($unexpected)) { - $this->assertTrue(FALSE, t('Unexpected hooks were called: %unexpected.', array('%unexpected' => implode(', ', $unexpected)))); + $this->assertTrue(FALSE, t('Unexpected hooks were called: %unexpected.', array('%unexpected' => empty($unexpected) ? t('(none)') : implode(', ', $unexpected)))); } else { $this->assertTrue(TRUE, t('No unexpected hooks were called.')); @@ -1422,20 +1422,44 @@ class FileDeleteTest extends FileHookTestCase { } /** - * Try deleting a normal file (as opposed to a directory, symlink, etc). + * Tries deleting a normal file (as opposed to a directory, symlink, etc). */ - function testNormal() { + function testUnused() { $file = $this->createFile(); // Check that deletion removes the file and database record. - $this->assertTrue(is_file($file->uri), t("File exists.")); - $this->assertIdentical(file_delete($file), TRUE, t("Delete worked.")); - $this->assertFileHooksCalled(array('references', 'delete')); - $this->assertFalse(file_exists($file->uri), t("Test file has actually been deleted.")); + $this->assertTrue(is_file($file->uri), t('File exists.')); + $this->assertIdentical(file_delete($file), TRUE, t('Delete worked.')); + $this->assertFileHooksCalled(array('delete')); + $this->assertFalse(file_exists($file->uri), t('Test file has actually been deleted.')); $this->assertFalse(file_load($file->fid), t('File was removed from the database.')); + } + + /** + * Tries deleting a file that is in use. + */ + function testInUse() { + $file = $this->createFile(); + file_usage_add($file, 'testing', 'test', 1); + file_usage_add($file, 'testing', 'test', 1); - // TODO: implement hook_file_references() in file_test.module and report a - // file in use and test the $force parameter. + file_usage_delete($file, 'testing', 'test', 1); + file_delete($file); + $usage = file_usage_list($file); + $this->assertEqual($usage['testing']['test'], array('id' => 1, 'count' => 1), t('Test file is still in use.')); + $this->assertTrue(file_exists($file->uri), t('File still exists on the disk.')); + $this->assertTrue(file_load($file->fid), t('File still exists in the database.')); + + // Clear out the call to hook_file_load(). + file_test_reset(); + + file_usage_delete($file, 'testing', 'test', 1); + file_delete($file); + $usage = file_usage_list($file); + $this->assertFileHooksCalled(array('delete')); + $this->assertTrue(empty($usage), t('File usage data was removed.')); + $this->assertFalse(file_exists($file->uri), t('File has been deleted after its last usage was removed.')); + $this->assertFalse(file_load($file->fid), t('File was removed from the database.')); } } @@ -1537,7 +1561,7 @@ class FileMoveTest extends FileHookTestCase { $this->assertTrue($result, t('File moved sucessfully.')); // Check that the correct hooks were called. - $this->assertFileHooksCalled(array('move', 'update', 'delete', 'references', 'load')); + $this->assertFileHooksCalled(array('move', 'update', 'delete', 'load')); // Reload the file from the database and check that the changes were // actually saved. @@ -1886,6 +1910,108 @@ class FileSaveTest extends FileHookTestCase { } } +/** + * Tests file usage functions. + */ +class FileUsageTest extends FileTestCase { + function getInfo() { + return array( + 'name' => 'File usage', + 'description' => 'Tests the file usage functions.', + 'group' => 'File', + ); + } + + /** + * Tests file_usage_list(). + */ + function testGetUsage() { + $file = $this->createFile(); + db_insert('file_usage') + ->fields(array( + 'fid' => $file->fid, + 'module' => 'testing', + 'type' => 'foo', + 'id' => 1, + 'count' => 1 + )) + ->execute(); + db_insert('file_usage') + ->fields(array( + 'fid' => $file->fid, + 'module' => 'testing', + 'type' => 'bar', + 'id' => 2, + 'count' => 2 + )) + ->execute(); + + $usage = file_usage_list($file); + + $this->assertEqual(count($usage['testing']), 2, t('Returned the correct number of items.')); + $this->assertEqual($usage['testing']['foo']['id'], 1, t('Returned the correct id.')); + $this->assertEqual($usage['testing']['bar']['id'], 2, t('Returned the correct id.')); + $this->assertEqual($usage['testing']['foo']['count'], 1, t('Returned the correct count.')); + $this->assertEqual($usage['testing']['bar']['count'], 2, t('Returned the correct count.')); + } + + /** + * Tests file_usage_add(). + */ + function testAddUsage() { + $file = $this->createFile(); + file_usage_add($file, 'testing', 'foo', 1); + // Add the file twice to ensure that the count is incremented rather than + // creating additional records. + file_usage_add($file, 'testing', 'bar', 2); + file_usage_add($file, 'testing', 'bar', 2); + + $usage = db_select('file_usage', 'f') + ->fields('f') + ->condition('f.fid', $file->fid) + ->execute() + ->fetchAllAssoc('id'); + $this->assertEqual(count($usage), 2, t('Created two records')); + $this->assertEqual($usage[1]->module, 'testing', t('Correct module')); + $this->assertEqual($usage[2]->module, 'testing', t('Correct module')); + $this->assertEqual($usage[1]->type, 'foo', t('Correct type')); + $this->assertEqual($usage[2]->type, 'bar', t('Correct type')); + $this->assertEqual($usage[1]->count, 1, t('Correct count')); + $this->assertEqual($usage[2]->count, 2, t('Correct count')); + } + + /** + * Tests file_usage_delete(). + */ + function testRemoveUsage() { + $file = $this->createFile(); + db_insert('file_usage') + ->fields(array( + 'fid' => $file->fid, + 'module' => 'testing', + 'type' => 'bar', + 'id' => 2, + 'count' => 2 + )) + ->execute(); + + 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(1, $count, t('The count was decremented correctly.')); + + 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(0, $count, t('The count was decremented correctly.')); + } +} /** * Tests the file_validate() function.. diff --git a/modules/simpletest/tests/file_test.module b/modules/simpletest/tests/file_test.module index 3009c465b..b509805d8 100644 --- a/modules/simpletest/tests/file_test.module +++ b/modules/simpletest/tests/file_test.module @@ -143,7 +143,6 @@ function file_test_reset() { 'load' => array(), 'validate' => array(), 'download' => array(), - 'references' => array(), 'insert' => array(), 'update' => array(), 'copy' => array(), @@ -156,7 +155,6 @@ function file_test_reset() { $return = array( 'validate' => array(), 'download' => NULL, - 'references' => NULL, ); variable_set('file_test_return', $return); } @@ -167,7 +165,7 @@ function file_test_reset() { * * @param $op * One of the hook_file_* operations: 'load', 'validate', 'download', - * 'references', 'insert', 'update', 'copy', 'move', 'delete'. + * 'insert', 'update', 'copy', 'move', 'delete'. * * @return * Array of the parameters passed to each call. @@ -184,9 +182,9 @@ function file_test_get_calls($op) { * Get an array with the calls for all hooks. * * @return - * An array keyed by hook name ('load', 'validate', 'download', - * 'references', 'insert', 'update', 'copy', 'move', 'delete') with values - * being arrays of parameters passed to each call. + * An array keyed by hook name ('load', 'validate', 'download', 'insert', + * 'update', 'copy', 'move', 'delete') with values being arrays of parameters + * passed to each call. */ function file_test_get_all_calls() { return variable_get('file_test_results', array()); @@ -197,7 +195,7 @@ function file_test_get_all_calls() { * * @param $op * One of the hook_file_* operations: 'load', 'validate', 'download', - * 'references', 'insert', 'update', 'copy', 'move', 'delete'. + * 'insert', 'update', 'copy', 'move', 'delete'. * @param $args * Values passed to hook. * @@ -214,7 +212,7 @@ function _file_test_log_call($op, $args) { * Load the appropriate return value. * * @param $op - * One of the hook_file_[validate,download,references] operations. + * One of the hook_file_[validate,download] operations. * * @return * Value set by file_test_set_return(). @@ -231,7 +229,7 @@ function _file_test_get_return($op) { * Assign a return value for a given operation. * * @param $op - * One of the hook_file_[validate,download,references] operations. + * One of the hook_file_[validate,download] operations. * @param $value * Value for the hook to return. * @@ -272,14 +270,6 @@ function file_test_file_download($uri) { return _file_test_get_return('download'); } -/** - * Implements hook_file_references(). - */ -function file_test_file_references($file) { - _file_test_log_call('references', array($file)); - return _file_test_get_return('references'); -} - /** * Implements hook_file_insert(). */ -- cgit v1.2.3