diff --git a/core/modules/file/src/FileStorage.php b/core/modules/file/src/FileStorage.php index c6f2ccccdefa7f11376386946c840c74746277a9..90d4bb0865fb8d77675d60a3474521ea2cd3c682 100644 --- a/core/modules/file/src/FileStorage.php +++ b/core/modules/file/src/FileStorage.php @@ -35,7 +35,7 @@ public function retrieveTemporaryFiles() { // of PHP. See http://drupal.org/node/352956. 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_MAXIMUM_TEMP_FILE_AGE + ':changed' => REQUEST_TIME - \Drupal::config('system.file')->get('temporary_maximum_age'), )); } } diff --git a/core/modules/file/src/FileStorageInterface.php b/core/modules/file/src/FileStorageInterface.php index 627d577da9aae6f33dd26b368ed2946ce6f916b9..a856b336656ad7579c1e1a9eef9cb3315c0c8fc0 100644 --- a/core/modules/file/src/FileStorageInterface.php +++ b/core/modules/file/src/FileStorageInterface.php @@ -30,7 +30,10 @@ interface FileStorageInterface extends EntityStorageInterface { public function spaceUsed($uid = NULL, $status = FILE_STATUS_PERMANENT); /** - * Retrieve temporary files that are older than DRUPAL_MAXIMUM_TEMP_FILE_AGE. + * Retrieves old temporary files. + * + * 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. diff --git a/core/modules/file/src/Tests/RetrieveTemporaryFilesTest.php b/core/modules/file/src/Tests/RetrieveTemporaryFilesTest.php new file mode 100644 index 0000000000000000000000000000000000000000..eae640ca409d021c5b94cf82388f07a13ce5751b --- /dev/null +++ b/core/modules/file/src/Tests/RetrieveTemporaryFilesTest.php @@ -0,0 +1,57 @@ + 'Temporary files tests', + 'description' => 'Tests the retrieveTemporaryFiles() function.', + 'group' => 'File Managed API', + ); + } + + /** + * {@inheritdoc} + */ + public function setUp() { + parent::setUp(); + + $max_age = $this->container->get('config.factory')->get('system.file')->get('temporary_maximum_age'); + + // Create an entry for the user with the date change. + $file_array = array('uid' => 2, 'uri' => 'public://example1.txt', 'status' => 2, 'changed' => REQUEST_TIME); + db_insert('file_managed')->fields($file_array)->execute(); + + // 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)); + db_insert('file_managed')->fields($file_array)->execute(); + } + + /** + * Tests finding stale files. + */ + function testRetrieveTemporaryFiles() { + $file_storage = $this->container->get('entity.manager')->getStorage('file'); + + $count_files = count($file_storage->retrieveTemporaryFiles()->fetchAssoc()); + + $this->assertEqual($count_files, 1); + } + +} diff --git a/core/modules/update/src/Tests/UpdateDeleteFileIfStaleTest.php b/core/modules/update/src/Tests/UpdateDeleteFileIfStaleTest.php new file mode 100644 index 0000000000000000000000000000000000000000..4d779065fd2eacb89ffba2b25d175dff92fef3f9 --- /dev/null +++ b/core/modules/update/src/Tests/UpdateDeleteFileIfStaleTest.php @@ -0,0 +1,62 @@ + 'Deleting obsolete files tests.', + 'description' => 'Tests the update_delete_file_if_stale() function.', + 'group' => 'Update', + ); + } + + /** + * {@inheritdoc} + */ + public function setUp() { + parent::setUp(); + } + + /** + * Tests the deletion of stale files. + */ + function testUpdateDeleteFileIfStale() { + $file_name = file_unmanaged_save_data($this->randomName()); + $this->assertNotNull($file_name); + + // During testing the file change and the stale checking occurs in the same + // request, so the beginning of request will be before the file changes and + // REQUEST_TIME - $filectime is negative. Set the maximum age to a number + // even smaller than that. + $this->container->get('config.factory') + ->get('system.file') + ->set('temporary_maximum_age', -100000) + ->save(); + + $file_path = drupal_realpath($file_name); + update_delete_file_if_stale($file_path); + + $this->assertFalse(is_file($file_path)); + } + +} diff --git a/core/modules/update/update.module b/core/modules/update/update.module index ee2f107d2d7052ecdec180408e62deff7eb6aa7c..eb77313fd3ace8b44a12ab1a21765b9d2d4c9b86 100644 --- a/core/modules/update/update.module +++ b/core/modules/update/update.module @@ -748,7 +748,9 @@ function update_clear_update_disk_cache() { function update_delete_file_if_stale($path) { if (file_exists($path)) { $filectime = filectime($path); - if (REQUEST_TIME - $filectime > DRUPAL_MAXIMUM_TEMP_FILE_AGE || (preg_match('/.*-dev\.(tar\.gz|zip)/i', $path) && REQUEST_TIME - $filectime > 300)) { + $max_age = \Drupal::config('system.file')->get('temporary_maximum_age'); + + if (REQUEST_TIME - $filectime > $max_age || (preg_match('/.*-dev\.(tar\.gz|zip)/i', $path) && REQUEST_TIME - $filectime > 300)) { file_unmanaged_delete_recursive($path); } }