From 95e63c380f3def632e24e64583ba1400e33b69ba Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Fri, 30 May 2014 21:24:36 +0100
Subject: [PATCH] Issue #2251719 by sergeypavlenko, YesCT, chx, Pancho: Fixed
 Completely remove DRUPAL_MAXIMUM_TEMP_FILE_AGE.

---
 core/modules/file/src/FileStorage.php         |  2 +-
 .../modules/file/src/FileStorageInterface.php |  5 +-
 .../src/Tests/RetrieveTemporaryFilesTest.php  | 57 +++++++++++++++++
 .../src/Tests/UpdateDeleteFileIfStaleTest.php | 62 +++++++++++++++++++
 core/modules/update/update.module             |  4 +-
 5 files changed, 127 insertions(+), 3 deletions(-)
 create mode 100644 core/modules/file/src/Tests/RetrieveTemporaryFilesTest.php
 create mode 100644 core/modules/update/src/Tests/UpdateDeleteFileIfStaleTest.php

diff --git a/core/modules/file/src/FileStorage.php b/core/modules/file/src/FileStorage.php
index c6f2ccccdefa..90d4bb0865fb 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 627d577da9aa..a856b336656a 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 000000000000..eae640ca409d
--- /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 000000000000..4d779065fd2e
--- /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 ee2f107d2d70..eb77313fd3ac 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);
     }
   }
-- 
GitLab