FileListingTest.php 5.25 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\file\Tests;

5
use Drupal\node\Entity\Node;
6
use Drupal\file\Entity\File;
7

8 9
/**
 * Tests file listing page functionality.
10 11
 *
 * @group file
12 13 14 15 16 17 18 19 20 21
 */
class FileListingTest extends FileFieldTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('views', 'file', 'image');

22 23 24 25 26 27 28
  /**
   * An authenticated user.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $baseUser;

29
  protected function setUp() {
30 31
    parent::setUp();

32 33
    $this->adminUser = $this->drupalCreateUser(array('access files overview', 'bypass node access'));
    $this->baseUser = $this->drupalCreateUser();
34
    $this->createFileField('file', 'node', 'article', array(), array('file_extensions' => 'txt png'));
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  }

  /**
   * Calculates total count of usages for a file.
   *
   * @param $usage array
   *   Array of file usage information as returned from file_usage subsystem.
   * @return int
   *   Total usage count.
   */
  protected function sumUsages($usage) {
    $count = 0;
    foreach ($usage as $module) {
      foreach ($module as $entity_type) {
        foreach ($entity_type as $entity) {
          $count += $entity;
        }
      }
    }

    return $count;
  }

  /**
   * Tests file overview with different user permissions.
   */
  function testFileListingPages() {
62 63
    $file_usage = $this->container->get('file.usage');
    // Users without sufficient permissions should not see file listing.
64
    $this->drupalLogin($this->baseUser);
65 66 67
    $this->drupalGet('admin/content/files');
    $this->assertResponse(403);

68
    // Log in with user with right permissions and test listing.
69
    $this->drupalLogin($this->adminUser);
70 71 72 73 74

    for ($i = 0; $i < 5; $i++) {
      $nodes[] = $this->drupalCreateNode(array('type' => 'article'));
    }

75 76 77 78 79 80 81 82 83 84 85 86 87
    $this->drupalGet('admin/content/files');
    $this->assertResponse(200);
    $this->assertText('No files available.');
    $this->drupalGet('admin/content/files');
    $this->assertResponse(200);

    // Create a file with no usage.
    $file = $this->createFile();

    $this->drupalGet('admin/content/files/usage/' . $file->id());
    $this->assertResponse(200);
    $this->assertTitle(t('File usage information for @file | Drupal', array('@file' => $file->getFilename())));

88
    foreach ($nodes as &$node) {
89
      $this->drupalGet('node/' . $node->id() . '/edit');
90 91 92
      $file = $this->getTestFile('image');

      $edit = array(
93
        'files[file_0]' => drupal_realpath($file->getFileUri()),
94
      );
95
      $this->drupalPostForm(NULL, $edit, t('Save'));
96
      $node = Node::load($node->id());
97 98 99 100 101
    }

    $this->drupalGet('admin/content/files');

    foreach ($nodes as $node) {
102
      $file = File::load($node->file->target_id);
103 104 105 106
      $this->assertText($file->getFilename());
      $this->assertLinkByHref(file_create_url($file->getFileUri()));
      $this->assertLinkByHref('admin/content/files/usage/' . $file->id());
    }
107
    $this->assertFalse(preg_match('/views-field-status priority-low\">\s*' . t('Temporary') . '/', $this->getRawContent()), 'All files are stored as permanent.');
108 109 110 111 112 113 114 115

    // Use one file two times and check usage information.
    $orphaned_file = $nodes[1]->file->target_id;
    $used_file = $nodes[0]->file->target_id;
    $nodes[1]->file->target_id = $used_file;
    $nodes[1]->save();

    $this->drupalGet('admin/content/files');
116
    $file = File::load($orphaned_file);
117
    $usage = $this->sumUsages($file_usage->listUsage($file));
118 119
    $this->assertRaw('admin/content/files/usage/' . $file->id() . '">' . $usage);

120
    $file = File::load($used_file);
121
    $usage = $this->sumUsages($file_usage->listUsage($file));
122 123 124
    $this->assertRaw('admin/content/files/usage/' . $file->id() . '">' . $usage);

    $result = $this->xpath("//td[contains(@class, 'views-field-status') and contains(text(), :value)]", array(':value' => t('Temporary')));
125
    $this->assertEqual(1, count($result), 'Unused file marked as temporary.');
126 127 128

    // Test file usage page.
    foreach ($nodes as $node) {
129
      $file = File::load($node->file->target_id);
130
      $usage = $file_usage->listUsage($file);
131 132 133 134 135 136 137 138 139 140 141 142 143 144
      $this->drupalGet('admin/content/files/usage/' . $file->id());
      $this->assertResponse(200);
      $this->assertText($node->getTitle(), 'Node title found on usage page.');
      $this->assertText('node', 'Registering entity type found on usage page.');
      $this->assertText('file', 'Registering module found on usage page.');
      foreach ($usage as $module) {
        foreach ($module as $entity_type) {
          foreach ($entity_type as $entity) {
            $this->assertText($entity, 'Usage count found on usage page.');
          }
        }
      }
      $this->assertLinkByHref('node/' . $node->id(), 0, 'Link to registering entity found on usage page.');
    }
145
  }
146 147 148 149 150 151 152 153 154

  /**
   * Creates and saves a test file.
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *  A file entity.
   */
  protected function createFile() {
    // Create a new file entity.
155
    $file = File::create([
156 157 158 159
      'uid' => 1,
      'filename' => 'druplicon.txt',
      'uri' => 'public://druplicon.txt',
      'filemime' => 'text/plain',
160 161
      'created' => 1,
      'changed' => 1,
162
      'status' => FILE_STATUS_PERMANENT,
163
    ]);
164 165 166 167 168 169 170 171
    file_put_contents($file->getFileUri(), 'hello world');

    // Save it, inserting a new record.
    $file->save();

    return $file;
  }

172
}