You are here

function FileFieldValidateTest::testRequired in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/file/src/Tests/FileFieldValidateTest.php \Drupal\file\Tests\FileFieldValidateTest::testRequired()

Tests the required property on file fields.

File

core/modules/file/src/Tests/FileFieldValidateTest.php, line 25
Contains \Drupal\file\Tests\FileFieldValidateTest.

Class

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

Namespace

Drupal\file\Tests

Code

function testRequired() {
  $node_storage = $this->container
    ->get('entity.manager')
    ->getStorage('node');
  $type_name = 'article';
  $field_name = strtolower($this
    ->randomMachineName());
  $storage = $this
    ->createFileField($field_name, 'node', $type_name, array(), array(
    '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 = array();
  $edit['title[0][value]'] = $this
    ->randomMachineName();
  $this
    ->drupalPostForm('node/add/' . $type_name, $edit, t('Save and publish'));
  $this
    ->assertRaw(t('@title field is required.', array(
    '@title' => $field
      ->getLabel(),
  )), 'Node save failed when required file field was empty.');

  // Create a new node with the uploaded file.
  $nid = $this
    ->uploadNodeFile($test_file, $field_name, $type_name);
  $this
    ->assertTrue($nid !== FALSE, format_string('uploadNodeFile(@test_file, @field_name, @type_name) succeeded', array(
    '@test_file' => $test_file
      ->getFileUri(),
    '@field_name' => $field_name,
    '@type_name' => $type_name,
  )));
  $node_storage
    ->resetCache(array(
    $nid,
  ));
  $node = $node_storage
    ->load($nid);
  $node_file = File::load($node->{$field_name}->target_id);
  $this
    ->assertFileExists($node_file, 'File exists after uploading to the required field.');
  $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, array(
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  ), array(
    'required' => '1',
  ));

  // Try to post a new node without uploading a file in the multivalue field.
  $edit = array();
  $edit['title[0][value]'] = $this
    ->randomMachineName();
  $this
    ->drupalPostForm('node/add/' . $type_name, $edit, t('Save and publish'));
  $this
    ->assertRaw(t('@title field is required.', array(
    '@title' => $field
      ->getLabel(),
  )), 'Node save failed when required multiple value file field was empty.');

  // Create a new node with the uploaded file into the multivalue field.
  $nid = $this
    ->uploadNodeFile($test_file, $field_name, $type_name);
  $node_storage
    ->resetCache(array(
    $nid,
  ));
  $node = $node_storage
    ->load($nid);
  $node_file = File::load($node->{$field_name}->target_id);
  $this
    ->assertFileExists($node_file, 'File exists after uploading to the required multiple value field.');
  $this
    ->assertFileEntryExists($node_file, 'File entry exists after uploading to the required multiple value field.');
}