commit 0d5ff4f60818d9b68e85191ffaa473d18b377e28 Author: fago Date: Sun Jun 1 11:21:44 2014 -0500 Fixed retrieving temporary files. diff --git a/core/modules/file/src/FileStorage.php b/core/modules/file/src/FileStorage.php index 291f680..4b30ad6 100644 --- a/core/modules/file/src/FileStorage.php +++ b/core/modules/file/src/FileStorage.php @@ -36,7 +36,7 @@ public function retrieveTemporaryFiles() { return $this->database->query('SELECT fid FROM {' . $this->entityType->getBaseTable() . '} WHERE status <> :permanent AND changed < :changed', array( ':permanent' => FILE_STATUS_PERMANENT, ':changed' => REQUEST_TIME - \Drupal::config('system.file')->get('temporary_maximum_age'), - )); + ))->fetchCol(); } /** diff --git a/core/modules/file/src/FileStorageInterface.php b/core/modules/file/src/FileStorageInterface.php index a856b33..14bdc62 100644 --- a/core/modules/file/src/FileStorageInterface.php +++ b/core/modules/file/src/FileStorageInterface.php @@ -35,8 +35,8 @@ public function spaceUsed($uid = NULL, $status = FILE_STATUS_PERMANENT); * Get files older than the temporary maximum age, * \Drupal::config('system.file')->get('temporary_maximum_age'). * - * @return array - * A list of files to be deleted. + * @return int[] + * A list of file IDs of the files to be deleted. */ public function retrieveTemporaryFiles(); diff --git a/core/modules/file/src/Tests/RetrieveTemporaryFilesTest.php b/core/modules/file/src/Tests/RetrieveTemporaryFilesTest.php index 1a1abba..ce8a4fd 100644 --- a/core/modules/file/src/Tests/RetrieveTemporaryFilesTest.php +++ b/core/modules/file/src/Tests/RetrieveTemporaryFilesTest.php @@ -16,6 +16,20 @@ class RetrieveTemporaryFilesTest extends FileManagedUnitTestBase { /** + * The file storage. + * + * @var \Drupal\file\FileStorageInterface + */ + protected $fileStorage; + + /** + * The temporary_maximum_age setting of files. + * + * @var int + */ + protected $maxAge; + + /** * {@inheritdoc} */ public static function getInfo() { @@ -32,17 +46,10 @@ public static function getInfo() { public function setUp() { parent::setUp(); - $max_age = $this->container->get('config.factory')->get('system.file')->get('temporary_maximum_age'); - + $this->maxAge = $this->container->get('config.factory')->get('system.file')->get('temporary_maximum_age'); + $this->fileStorage = $this->container->get('entity.manager')->getStorage('file'); // Create an entry for the user with the date change. - $file_array = array('uid' => 2, 'uri' => 'public://example1.txt', 'status' => 2, 'changed' => REQUEST_TIME); - $file = File::create($file_array); - $file->save(); - - // Create an entry for the user with an indication of the old date of the - // change. - $file_array = array('uid' => 2, 'uri' => 'public://example2.txt', 'status' => 2, 'changed' => REQUEST_TIME - ($max_age * 2)); - $file = File::create($file_array); + $file = $this->fileStorage->create(array('uid' => 2, 'uri' => 'public://example1.txt', 'status' => 2)); $file->save(); } @@ -50,11 +57,19 @@ public function setUp() { * Tests finding stale files. */ function testRetrieveTemporaryFiles() { - $file_storage = $this->container->get('entity.manager')->getStorage('file'); + $this->assertEqual($this->fileStorage->retrieveTemporaryFiles(), [], 'No file is to be deleted.'); - $count_files = count($file_storage->retrieveTemporaryFiles()->fetchAssoc()); + // Create an entry for the user with an indication of the old date of the + // change. As the changed field always saves the request time, we do have + // update it with a direct db query. + $file = $this->fileStorage->create(array('uid' => 2, 'uri' => 'public://example2.txt', 'status' => 2)); + $file->save(); + db_update('file_managed') + ->fields(array('changed' => REQUEST_TIME - ($this->maxAge * 2))) + ->condition('fid', $file->id()) + ->execute(); - $this->assertEqual($count_files, 1); + $this->assertEqual($this->fileStorage->retrieveTemporaryFiles(), [$file->id()], 'One file is to be deleted.'); } }