function FileFieldValidateTest::testFileRemoval

Same name and namespace in other branches
  1. 9 core/modules/file/tests/src/Functional/FileFieldValidateTest.php \Drupal\Tests\file\Functional\FileFieldValidateTest::testFileRemoval()
  2. 8.9.x core/modules/file/tests/src/Functional/FileFieldValidateTest.php \Drupal\Tests\file\Functional\FileFieldValidateTest::testFileRemoval()
  3. 11.x 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 174

Class

FileFieldValidateTest
Tests file field validation functions.

Namespace

Drupal\Tests\file\Functional

Code

public function testFileRemoval() : void {
  $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.');
}

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