function FileListingTest::testFileListingPages

Same name in other branches
  1. 8.9.x core/modules/file/tests/src/Functional/FileListingTest.php \Drupal\Tests\file\Functional\FileListingTest::testFileListingPages()
  2. 10 core/modules/file/tests/src/Functional/FileListingTest.php \Drupal\Tests\file\Functional\FileListingTest::testFileListingPages()
  3. 11.x core/modules/file/tests/src/Functional/FileListingTest.php \Drupal\Tests\file\Functional\FileListingTest::testFileListingPages()

Tests file overview with different user permissions.

File

core/modules/file/tests/src/Functional/FileListingTest.php, line 79

Class

FileListingTest
Tests file listing page functionality.

Namespace

Drupal\Tests\file\Functional

Code

public function testFileListingPages() {
    $file_usage = $this->container
        ->get('file.usage');
    // Users without sufficient permissions should not see file listing.
    $this->drupalLogin($this->baseUser);
    $this->drupalGet('admin/content/files');
    $this->assertSession()
        ->statusCodeEquals(403);
    // Log in with user with right permissions and test listing.
    $this->drupalLogin($this->adminUser);
    for ($i = 0; $i < 5; $i++) {
        $nodes[] = $this->drupalCreateNode([
            'type' => 'article',
        ]);
    }
    $this->drupalGet('admin/content/files');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->pageTextContains('No files available.');
    $this->drupalGet('admin/content/files');
    $this->assertSession()
        ->statusCodeEquals(200);
    // Create a file with no usage.
    $file = $this->createFile();
    $this->drupalGet('admin/content/files/usage/' . $file->id());
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->titleEquals('File usage information for ' . $file->getFilename() . ' | Drupal');
    foreach ($nodes as &$node) {
        $this->drupalGet('node/' . $node->id() . '/edit');
        $file = $this->getTestFile('image');
        $edit = [
            'files[file_0]' => \Drupal::service('file_system')->realpath($file->getFileUri()),
        ];
        $this->submitForm($edit, 'Save');
        $node = Node::load($node->id());
    }
    $this->drupalGet('admin/content/files');
    foreach ($nodes as $node) {
        $file = File::load($node->file->target_id);
        $this->assertSession()
            ->pageTextContains($file->getFilename());
        $this->assertSession()
            ->linkByHrefExists($file->createFileUrl());
        $this->assertSession()
            ->linkByHrefExists('admin/content/files/usage/' . $file->id());
    }
    $this->assertSession()
        ->elementTextNotContains('css', '.views-element-container table', 'Temporary');
    $this->assertSession()
        ->elementTextContains('css', '.views-element-container table', 'Permanent');
    // 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');
    $file = File::load($orphaned_file);
    $usage = $this->sumUsages($file_usage->listUsage($file));
    $this->assertSession()
        ->responseContains('admin/content/files/usage/' . $file->id() . '">' . $usage);
    $file = File::load($used_file);
    $usage = $this->sumUsages($file_usage->listUsage($file));
    $this->assertSession()
        ->responseContains('admin/content/files/usage/' . $file->id() . '">' . $usage);
    $this->assertSession()
        ->elementsCount('xpath', "//td[contains(@class, 'views-field-status') and contains(text(), 'Temporary')]", 1);
    // Test file usage page.
    foreach ($nodes as $node) {
        $file = File::load($node->file->target_id);
        $usage = $file_usage->listUsage($file);
        $this->drupalGet('admin/content/files/usage/' . $file->id());
        $this->assertSession()
            ->statusCodeEquals(200);
        $this->assertSession()
            ->pageTextContains($node->getTitle());
        // Verify that registering entity type is found on usage page.
        $this->assertSession()
            ->pageTextContains('node');
        // Verify that registering module is found on usage page.
        $this->assertSession()
            ->pageTextContains('file');
        foreach ($usage as $module) {
            foreach ($module as $entity_type) {
                foreach ($entity_type as $entity) {
                    // Verify that usage count is found on usage page.
                    $this->assertSession()
                        ->pageTextContains($entity);
                }
            }
        }
        $this->assertSession()
            ->linkByHrefExists('node/' . $node->id(), 0, 'Link to registering entity found on usage page.');
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.