summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathaniel Catchpole2014-05-30 20:24:36 (GMT)
committerNathaniel Catchpole2014-05-30 20:24:36 (GMT)
commit95e63c380f3def632e24e64583ba1400e33b69ba (patch)
tree852365f0d153cdba69927589c2c113f06366749f
parentaef08a5e0a165ba96aef292745e2b2f52a43e27c (diff)
Issue #2251719 by sergeypavlenko, YesCT, chx, Pancho: Fixed Completely remove DRUPAL_MAXIMUM_TEMP_FILE_AGE.
-rw-r--r--core/modules/file/src/FileStorage.php2
-rw-r--r--core/modules/file/src/FileStorageInterface.php5
-rw-r--r--core/modules/file/src/Tests/RetrieveTemporaryFilesTest.php57
-rw-r--r--core/modules/update/src/Tests/UpdateDeleteFileIfStaleTest.php62
-rw-r--r--core/modules/update/update.module4
5 files changed, 127 insertions, 3 deletions
diff --git a/core/modules/file/src/FileStorage.php b/core/modules/file/src/FileStorage.php
index c6f2ccc..90d4bb0 100644
--- a/core/modules/file/src/FileStorage.php
+++ b/core/modules/file/src/FileStorage.php
@@ -35,7 +35,7 @@ class FileStorage extends ContentEntityDatabaseStorage implements FileStorageInt
// 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 627d577..a856b33 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 0000000..eae640c
--- /dev/null
+++ b/core/modules/file/src/Tests/RetrieveTemporaryFilesTest.php
@@ -0,0 +1,57 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\file\Tests\RetrieveTemporaryFilesTest.
+ */
+
+namespace Drupal\file\Tests;
+
+/**
+ * Provides tests for retrieving temporary files.
+ *
+ * @see \Drupal\Core\Entity\ContentEntityDatabaseStorage::retrieveTemporaryFiles()
+ */
+class RetrieveTemporaryFilesTest extends FileManagedUnitTestBase {
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function getInfo() {
+ return array(
+ 'name' => '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 0000000..4d77906
--- /dev/null
+++ b/core/modules/update/src/Tests/UpdateDeleteFileIfStaleTest.php
@@ -0,0 +1,62 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\update\Tests\UpdateDeleteFileIfStaleTest.
+ */
+
+namespace Drupal\update\Tests;
+
+/**
+ * Provides tests for update_delete_file_if_stale().
+ */
+class UpdateDeleteFileIfStaleTest extends UpdateTestBase {
+
+ /**
+ * Modules to enable.
+ *
+ * @var array
+ */
+ public static $modules = array('update');
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function getInfo() {
+ return array(
+ 'name' => '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 ee2f107..eb77313 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);
}
}