You are here

public function FieldValidationTest::testValidateRequired in Mini site 8

Tests the required property on minisite field.

File

tests/src/Functional/FieldValidationTest.php, line 36

Class

FieldValidationTest
Tests the minisite field validation.

Namespace

Drupal\Tests\minisite\Functional

Code

public function testValidateRequired() {

  /** @var \Drupal\node\NodeStorageInterface $node_storage */
  $node_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('node');
  $field_name = strtolower($this
    ->randomMachineName());
  $this
    ->createMinisiteField($field_name, 'node', $this->contentType, [], [
    'required' => '1',
  ]);
  $field = FieldConfig::loadByName('node', $this->contentType, $field_name);

  // Try to post a new node without uploading a file.
  $edit = [];
  $edit['title[0][value]'] = $this
    ->randomMachineName();
  $this
    ->drupalPostForm('node/add/' . $this->contentType, $edit, $this
    ->t('Save'));
  $this
    ->assertRaw($this
    ->t('@title field is required.', [
    '@title' => $field
      ->getLabel(),
  ]));

  // Create a new node with the uploaded file.
  $test_file = $this
    ->getTestArchiveValid();
  $nid = $this
    ->uploadNodeFile($test_file, $field_name, $this->contentType);
  $this
    ->assertTrue($nid !== FALSE, new FormattableMarkup('uploadNodeFile(@test_file, @field_name, @type_name) succeeded', [
    '@test_file' => $test_file
      ->getFileUri(),
    '@field_name' => $field_name,
    '@type_name' => $this->contentType,
  ]));
  $node_storage
    ->resetCache([
    $nid,
  ]);
  $node = $node_storage
    ->load($nid);
  $node_file = File::load($node->{$field_name}->target_id);
  $this
    ->assertFileUriExists($node_file, 'File exists after uploading to the required field.');
  $this
    ->assertFileEntryExists($node_file, 'File entry exists after uploading to the required field.');
}