You are here

public function FileFieldValidateTest::testFileRemoval in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/file/tests/src/Functional/FileFieldValidateTest.php \Drupal\Tests\file\Functional\FileFieldValidateTest::testFileRemoval()
  2. 10 core/modules/file/tests/src/Functional/FileFieldValidateTest.php \Drupal\Tests\file\Functional\FileFieldValidateTest::testFileRemoval()

Checks that a file can always be removed if it does not pass validation.

File

core/modules/file/tests/src/Functional/FileFieldValidateTest.php, line 170

Class

FileFieldValidateTest
Tests validation functions such as file type, max file size, max size per node, and required.

Namespace

Drupal\Tests\file\Functional

Code

public function testFileRemoval() {
  $node_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('node');
  $type_name = 'article';
  $field_name = 'file_test';
  $this
    ->createFileField($field_name, 'node', $type_name);
  $test_file = $this
    ->getTestFile('image');

  // Disable extension checking.
  $this
    ->updateFileField($field_name, $type_name, [
    'file_extensions' => '',
  ]);

  // Check that the file can be uploaded with no extension checking.
  $nid = $this
    ->uploadNodeFile($test_file, $field_name, $type_name);
  $node_storage
    ->resetCache([
    $nid,
  ]);
  $node = $node_storage
    ->load($nid);
  $node_file = File::load($node->{$field_name}->target_id);
  $this
    ->assertFileExists($node_file
    ->getFileUri());
  $this
    ->assertFileEntryExists($node_file, 'File entry exists after uploading a file with no extension checking.');

  // Enable extension checking for text files.
  $this
    ->updateFileField($field_name, $type_name, [
    'file_extensions' => 'txt',
  ]);

  // Check that the file can still be removed.
  $this
    ->removeNodeFile($nid);
  $this
    ->assertSession()
    ->pageTextNotContains('Only files with the following extensions are allowed: txt.');
  $this
    ->assertSession()
    ->pageTextContains('Article ' . $node
    ->getTitle() . ' has been updated.');
}