function FileManagedFileElementTest::testUnusedPermanentFileValidation

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

Verify that unused permanent files can be used.

File

core/modules/file/tests/src/Functional/FileManagedFileElementTest.php, line 207

Class

FileManagedFileElementTest
Tests the 'managed_file' element type.

Namespace

Drupal\Tests\file\Functional

Code

public function testUnusedPermanentFileValidation() {
    // Create a permanent file without usages.
    $file = $this->getTestFile('image');
    $file->setPermanent();
    $file->save();
    // By default, unused files are no longer marked temporary, and it must be
    // allowed to reference an unused file.
    $this->drupalGet('file/test/1/0/1/' . $file->id());
    $this->submitForm([], 'Save');
    $this->assertSession()
        ->pageTextNotContains('The file used in the Managed file & butter field may not be referenced.');
    $this->assertSession()
        ->pageTextContains('The file ids are ' . $file->id());
    // Enable marking unused files as temporary, unused permanent files must not
    // be referenced now.
    $this->config('file.settings')
        ->set('make_unused_managed_files_temporary', TRUE)
        ->save();
    $this->drupalGet('file/test/1/0/1/' . $file->id());
    $this->submitForm([], 'Save');
    $this->assertSession()
        ->pageTextContains('The file used in the Managed file & butter field may not be referenced.');
    $this->assertSession()
        ->pageTextNotContains('The file ids are ' . $file->id());
    // Make the file temporary, now using it is allowed.
    $file->setTemporary();
    $file->save();
    $this->drupalGet('file/test/1/0/1/' . $file->id());
    $this->submitForm([], 'Save');
    $this->assertSession()
        ->pageTextNotContains('The file used in the Managed file & butter field may not be referenced.');
    $this->assertSession()
        ->pageTextContains('The file ids are ' . $file->id());
    // Make the file permanent again and add a usage from itself, referencing is
    // still allowed.
    $file->setPermanent();
    $file->save();
    
    /** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */
    $file_usage = \Drupal::service('file.usage');
    $file_usage->add($file, 'file', 'file', $file->id());
    $this->drupalGet('file/test/1/0/1/' . $file->id());
    $this->submitForm([], 'Save');
    $this->assertSession()
        ->pageTextNotContains('The file used in the Managed file & butter field may not be referenced.');
    $this->assertSession()
        ->pageTextContains('The file ids are ' . $file->id());
}

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