function FileOnTranslatedEntityTest::testFileUsage

Same name in other branches
  1. 10 core/modules/file/tests/src/Functional/FileOnTranslatedEntityTest.php \Drupal\Tests\file\Functional\FileOnTranslatedEntityTest::testFileUsage()

Tests if file field tracks file usages correctly on translated nodes.

File

core/modules/file/tests/src/Functional/FileOnTranslatedEntityTest.php, line 217

Class

FileOnTranslatedEntityTest
Uploads files to translated nodes.

Namespace

Drupal\Tests\file\Functional

Code

public function testFileUsage() : void {
    
    /** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */
    $file_usage = \Drupal::service('file.usage');
    // Create a node and upload a file.
    $node = $this->drupalCreateNode([
        'type' => 'page',
    ]);
    $edit = [
        'files[' . $this->fieldName . '_0]' => \Drupal::service('file_system')->realpath($this->drupalGetTestFiles('text')[0]->uri),
    ];
    $this->drupalGet('node/' . $node->id() . '/edit');
    $this->submitForm($edit, 'Save');
    // Check if the file usage is correct.
    $file = File::load($this->getLastFileId());
    $this->assertEquals($file_usage->listUsage($file), [
        'file' => [
            'node' => [
                $node->id() => '1',
            ],
        ],
    ]);
    // Check if the file usage is tracked correctly when changing the original
    // language of an entity.
    $node = Node::load($node->id());
    $node->set('langcode', 'fr');
    $node->save();
    $this->assertEquals($file_usage->listUsage($file), [
        'file' => [
            'node' => [
                $node->id() => '1',
            ],
        ],
    ]);
}

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