diff --git a/includes/utilities.inc b/includes/utilities.inc index 639481ed..fd6c1c3e 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -46,6 +46,37 @@ function islandora_convert_bytes_to_human_readable($bytes, $precision = 2) { } } +/** + * Add a file as managed if is not already. + * + * @param string $file_uri + * The given file URI location. + * + * @return object + * The file, as returned from file_save(). + */ +function islandora_temp_file_entry($file_uri, $mime = NULL) { + $query = new EntityFieldQuery(); + $result = $query + ->entityCondition('entity_type', 'file') + ->propertyCondition('uri', $file_uri) + ->execute(); + if (isset($result['file'])) { + $fid = current($result['file'])->fid; + $file = file_load($fid); + } + else { + $file = new stdClass(); + $file->uri = $file_uri; + $file->filename = drupal_basename($file_uri); + if (isset($mime)) { + $file->filemime = $mime; + } + } + $file->status = 0; + return file_save($file); +} + /** * Creates a label for control group symbols. */ diff --git a/islandora.info b/islandora.info index 0142f1ce..e06d0de9 100644 --- a/islandora.info +++ b/islandora.info @@ -22,5 +22,6 @@ files[] = tests/islandora_manage_permissions.test files[] = tests/datastream_versions.test files[] = tests/datastream_cache.test files[] = tests/derivatives.test +files[] = tests/islandora_manage_temp_file.test files[] = tests/datastream_validator_tests.test php = 5.3 diff --git a/tests/islandora_manage_temp_file.test b/tests/islandora_manage_temp_file.test new file mode 100644 index 00000000..65003ed8 --- /dev/null +++ b/tests/islandora_manage_temp_file.test @@ -0,0 +1,83 @@ + 'Islandora Managed Tempfile Interface', + 'description' => 'Ensure that our managed tempfile interface returns appropriate results.', + 'group' => 'Islandora', + ); + } + + /** + * Creates an admin user and a connection to a fedora repository. + * + * @see IslandoraWebTestCase::setUp() + */ + public function setUp() { + parent::setUp('islandora'); + $this->tempUri = file_create_filename('temp.txt', 'temporary://'); + $this->publicUri = file_create_filename('temp.txt', 'public://'); + } + + /** + * Free any objects/resources created for this test. + * + * @see IslandoraWebTestCase::tearDown() + */ + public function tearDown() { + parent::tearDown(); + file_unmanaged_delete($this->tempUri); + file_unmanaged_delete($this->publicUri); + } + + /** + * Existing files are made temporary. + */ + public function testExistingFile() { + $temp_file = file_save_data('blah', $this->tempUri, FILE_EXISTS_REPLACE); + $public_file = file_save_data('blah', $this->publicUri, FILE_EXISTS_REPLACE); + + $this->existingFileHelper($temp_file); + $this->existingFileHelper($public_file); + } + + /** + * Helper function; ensure file is permanent (as file_save_data() creates). + */ + protected function existingFileHelper($file_object) { + $this->assertEqual($file_object->status & FILE_STATUS_PERMANENT, FILE_STATUS_PERMANENT, 'Existing file is permanent.'); + $this->baseFileHelper($file_object->uri); + $this->assertTrue(file_delete($file_object)); + } + + /** + * Helper function; ensure our function produces an temporary file object. + */ + protected function baseFileHelper($file_uri) { + $temp_file = islandora_temp_file_entry($file_uri); + $this->assertNotEqual($temp_file->status & FILE_STATUS_PERMANENT, FILE_STATUS_PERMANENT, 'File has been made temporary.'); + } + + /** + * Unmanaged files start being managed. + */ + public function testNewFileUri() { + file_put_contents($this->tempUri, 'test'); + file_put_contents($this->publicUri, 'test'); + + $this->baseFileHelper($this->tempUri); + $this->baseFileHelper($this->publicUri); + } +}