You are here

function FileFieldValidateTestCase::testRequired in FileField 6.3

Test required property on file fields.

File

tests/filefield.test, line 361

Class

FileFieldValidateTestCase
Test class to check for various validations.

Code

function testRequired() {
  $type = $this->node_type;
  $field = $this->field;

  // Make our field required.
  $this
    ->updateFileField($field['field_name'], $type->name, array(
    'required' => '1',
  ));
  $test_file = $this
    ->getTestFile('image');

  // Try to post a new node without uploading a file.
  $edit = array(
    'title' => $this
      ->randomName(),
  );
  $this
    ->drupalPost('node/add/' . $type->url_name, $edit, t('Save'));
  $this
    ->assertRaw(t('%title field is required.', array(
    '%title' => $field['widget']['label'],
  )), t('Node save failed when required file field was empty.'));

  // Create a new node with the uploaded file.
  $nid = $this
    ->uploadNodeFile($test_file, $field, $type->name);
  $node = node_load($nid, NULL, TRUE);
  $node_file = $node->{$field['field_name']}[0];
  $this
    ->assertFileExists($node_file, t('File exists after uploading to the required field.'));
  $this
    ->assertFileEntryExists($node_file, t('File entry exists after uploading to the required field.'));

  // Try again with a multiple value field.
  $this
    ->updateFileField($field['field_name'], $type->name, array(
    'multiple' => '0',
    'required' => '1',
  ));

  // Try to post a new node without uploading a file in the multivalue field.
  $edit = array(
    'title' => $this
      ->randomName(),
  );
  $this
    ->drupalPost('node/add/' . $type->url_name, $edit, t('Save'));
  $this
    ->assertRaw(t('%title field is required.', array(
    '%title' => $field['widget']['label'],
  )), t('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, $type->name);
  $node = node_load($nid, NULL, TRUE);
  $node_file = $node->{$field['field_name']}[0];
  $this
    ->assertFileExists($node_file, t('File exists after uploading to the required multiple value field.'));
  $this
    ->assertFileEntryExists($node_file, t('File entry exists after uploading to the required multipel value field.'));

  // Set the field back to not required.
  $this
    ->updateFileField($field['field_name'], $type->name, array(
    'multiple' => '0',
    'required' => '1',
  ));
}