function FileListingTest::testFileListingUsageNoLink

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

Tests file listing usage page for entities with no canonical link template.

File

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

Class

FileListingTest
Tests file listing page functionality.

Namespace

Drupal\Tests\file\Functional

Code

public function testFileListingUsageNoLink() : void {
    // Login with user with right permissions and test listing.
    $this->drupalLogin($this->adminUser);
    // Create a bundle and attach a File field to the bundle.
    $bundle = $this->randomMachineName();
    entity_test_create_bundle($bundle, NULL, 'entity_test_constraints');
    $this->createFileField('field_test_file', 'entity_test_constraints', $bundle, [], [
        'file_extensions' => 'txt png',
    ]);
    // Create file to attach to entity.
    $file = File::create([
        'filename' => 'druplicon.txt',
        'uri' => 'public://druplicon.txt',
        'filemime' => 'text/plain',
    ]);
    $file->setPermanent();
    file_put_contents($file->getFileUri(), 'hello world');
    $file->save();
    // Create entity and attach the created file.
    $entity_name = $this->randomMachineName();
    $entity = EntityTestConstraints::create([
        'uid' => 1,
        'name' => $entity_name,
        'type' => $bundle,
        'field_test_file' => [
            'target_id' => $file->id(),
        ],
    ]);
    $entity->save();
    // Create node entity and attach the created file.
    $node = $this->drupalCreateNode([
        'type' => 'article',
        'file' => $file,
    ]);
    $node->save();
    // Load the file usage page for the created and attached file.
    $this->drupalGet('admin/content/files/usage/' . $file->id());
    $this->assertSession()
        ->statusCodeEquals(200);
    // Entity name should be displayed, but not linked if Entity::toUrl
    // throws an exception
    $this->assertSession()
        ->pageTextContains($entity_name);
    $this->assertSession()
        ->linkNotExists($entity_name, 'Linked entity name not added to file usage listing.');
    $this->assertSession()
        ->linkExists($node->getTitle());
}

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