RetrieveTemporaryFilesTest.php 1.99 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?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 {

17 18 19 20 21 22 23 24 25 26 27 28 29 30
  /**
   * The file storage.
   *
   * @var \Drupal\file\FileStorageInterface
   */
  protected $fileStorage;

  /**
   * The temporary_maximum_age setting of files.
   *
   * @var int
   */
  protected $maxAge;

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  /**
   * {@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();

48 49
    $this->maxAge = $this->container->get('config.factory')->get('system.file')->get('temporary_maximum_age');
    $this->fileStorage = $this->container->get('entity.manager')->getStorage('file');
50
    // Create an entry for the user with the date change.
51 52
    $file = $this->fileStorage->create(array('uid' => 2, 'uri' => $this->createUri(), 'status' => 2));
    $file->save();
53 54 55 56 57 58
  }

  /**
   * Tests finding stale files.
   */
  function testRetrieveTemporaryFiles() {
59
    $this->assertEqual($this->fileStorage->retrieveTemporaryFiles(), [], 'No file is to be deleted.');
60

61 62 63 64 65 66 67 68 69
    // 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' => $this->createUri(), 'status' => 2));
    $file->save();
    db_update('file_managed')
      ->fields(array('changed' => REQUEST_TIME - ($this->maxAge * 2)))
      ->condition('fid', $file->id())
      ->execute();
70

71
    $this->assertEqual($this->fileStorage->retrieveTemporaryFiles(), [$file->id()], 'One file is to be deleted.');
72 73 74
  }

}