function FileFieldValidateTest::testRequired

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

Tests the required property on file fields.

File

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

Class

FileFieldValidateTest
Tests file field validation functions.

Namespace

Drupal\Tests\file\Functional

Code

public function testRequired() {
    $node_storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('node');
    $type_name = 'article';
    $field_name = strtolower($this->randomMachineName());
    $storage = $this->createFileField($field_name, 'node', $type_name, [], [
        'required' => '1',
    ]);
    $field = FieldConfig::loadByName('node', $type_name, $field_name);
    $test_file = $this->getTestFile('text');
    // Try to post a new node without uploading a file.
    $edit = [];
    $edit['title[0][value]'] = $this->randomMachineName();
    $this->drupalGet('node/add/' . $type_name);
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains("{$field->getLabel()} field is required.");
    // Create a new node with the uploaded file.
    $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
    $this->assertNotFalse($nid, new FormattableMarkup('uploadNodeFile(@test_file, @field_name, @type_name) succeeded', [
        '@test_file' => $test_file->getFileUri(),
        '@field_name' => $field_name,
        '@type_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 to the required field.');
    // Try again with a multiple value field.
    $storage->delete();
    $this->createFileField($field_name, 'node', $type_name, [
        'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    ], [
        'required' => '1',
    ]);
    // Try to post a new node without uploading a file in the multivalue field.
    $edit = [];
    $edit['title[0][value]'] = $this->randomMachineName();
    $this->drupalGet('node/add/' . $type_name);
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains("{$field->getLabel()} field is required.");
    // Create a new node with the uploaded file into the multivalue field.
    $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 to the required multiple value field.');
}

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