diff --git a/file_entity.install b/file_entity.install index a1f86c2..d84aa02 100644 --- a/file_entity.install +++ b/file_entity.install @@ -225,6 +225,20 @@ function file_entity_install() { if (module_exists('pathauto')) { variable_set('pathauto_file_pattern', 'file/[file:name]'); } + + // Classify existing files according to the currently defined file types. + // Queue all files to be classified during cron runs using the Queue API. + $queue = DrupalQueue::get('file_entity_type_determine'); + + $result = db_query('SELECT fid FROM {file_managed}'); + + foreach ($result as $record) { + $queue->createItem($record->fid); + } + + // Warn users that existing files will not have a file type until the queue + // has been processed. + drupal_set_message(t('Existing files must be classified according to the currently defined file types. These files have been queued for processing and will have their file type determined during cron runs.')); } /** diff --git a/file_entity.module b/file_entity.module index afeb63b..2d11e3b 100644 --- a/file_entity.module +++ b/file_entity.module @@ -426,6 +426,29 @@ function file_entity_permission() { return $permissions; } +/* + * Implements hook_cron_queue_info(). + */ +function file_entity_cron_queue_info() { + $queues['file_entity_type_determine'] = array( + 'worker callback' => 'file_entity_type_determine', + ); + return $queues; +} + +/* + * Determines file type for a given file ID and saves the file. + * + * @param $fid + * A file ID. + */ +function file_entity_type_determine($fid) { + $file = file_load($fid); + + // The file type will be automatically determined when saving the file. + file_save($file); +} + /** * Gather the rankings from the the hook_ranking implementations. * diff --git a/tests/file_entity.test b/tests/file_entity.test index 2435075..63854a1 100644 --- a/tests/file_entity.test +++ b/tests/file_entity.test @@ -183,6 +183,73 @@ class FileEntityTestHelper extends DrupalWebTestCase { } } +class FileEntityFileTypeClassificationTestCase extends DrupalWebTestCase { + public static function getInfo() { + return array( + 'name' => 'File entity classification', + 'description' => 'Test existing file entity classification functionality.', + 'group' => 'File entity', + ); + } + + function setUp() { + parent::setUp(); + } + + /** + * Get the file type of a given file. + * + * @param $file + * A file object. + * + * @return + * The file's file type as a string. + */ + function getFileType($file) { + $type = db_select('file_managed', 'fm') + ->fields('fm', array('type')) + ->condition('fid', $file->fid, '=') + ->execute() + ->fetchAssoc(); + + return $type; + } + + /** + * Test that existing files are properly classified by file type. + */ + function testFileTypeClassification() { + // Get test text and image files. + $file = current($this->drupalGetTestFiles('text')); + $text_file = file_save($file); + $file = current($this->drupalGetTestFiles('image')); + $image_file = file_save($file); + + // Enable file entity which adds adds a file type property to files and + // queues up existing files for classification. + module_enable(array('file_entity')); + + // Existing files have yet to be classified and should have an undefined + // file type. + $file_type = $this->getFileType($text_file); + $this->assertEqual($file_type['type'], 'undefined', t('The text file has an undefined file type.')); + $file_type = $this->getFileType($image_file); + $this->assertEqual($file_type['type'], 'undefined', t('The image file has an undefined file type.')); + + // The classification queue is processed during cron runs. Run cron to + // trigger the classification process. + $this->cronRun(); + + // The classification process should assign a file type to any file whose + // MIME type is assigned to a file type. Check to see if each file was + // assigned a proper file type. + $file_type = $this->getFileType($text_file); + $this->assertEqual($file_type['type'], 'document', t('The text file was properly assigned the Document file type.')); + $file_type = $this->getFileType($image_file); + $this->assertEqual($file_type['type'], 'image', t('The image file was properly assigned the Image file type.')); + } +} + class FileEntityUnitTestCase extends FileEntityTestHelper { public static function getInfo() { return array(