You are here

function MediaFileFieldValidateTestCase::testRequired in D7 Media 7.3

Same name and namespace in other branches
  1. 7.4 tests/media.test \MediaFileFieldValidateTestCase::testRequired()
  2. 7.2 tests/media.test \MediaFileFieldValidateTestCase::testRequired()

Tests the required property on file fields.

File

tests/media.test, line 1162
Tests for media.module.

Class

MediaFileFieldValidateTestCase
Tests various validations.

Code

function testRequired() {
  $type_name = 'article';
  $field_name = strtolower($this
    ->randomName());
  $this
    ->createFileField($field_name, $type_name, array(), array(
    'required' => '1',
  ));
  $field = field_info_field($field_name);
  $instance = field_info_instance('node', $field_name, $type_name);
  $test_file = $this
    ->getTestFile('text');
  $test_file->uid = $this->admin_user->uid;
  $test_file = file_save($test_file);

  // Try to post a new node without attaching a file.
  $langcode = LANGUAGE_NONE;
  $edit = array(
    "title" => $this
      ->randomName(),
  );
  $this
    ->drupalPost('node/add/' . $type_name, $edit, t('Save'));
  $this
    ->assertRaw(t('!title field is required.', array(
    '!title' => $instance['label'],
  )), 'Node save failed when required file field was empty.');

  // Create a new node with the attached file.
  $nid = $this
    ->attachNodeFile($test_file, $field_name, $type_name);
  $this
    ->assertTrue($nid !== FALSE, format_string('attachNodeFile(@test_file, @field_name, @type_name) succeeded', array(
    '@test_file' => $test_file->uri,
    '@field_name' => $field_name,
    '@type_name' => $type_name,
  )));
  $node = node_load($nid, NULL, TRUE);
  $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
  $this
    ->assertFileExists($node_file, 'File exists after attaching to the required field.');
  $this
    ->assertFileEntryExists($node_file, 'File entry exists after attaching to the required field.');

  // Try again with a multiple value field.
  field_delete_field($field_name);
  $this
    ->createFileField($field_name, $type_name, array(
    'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  ), array(
    'required' => '1',
  ));

  // Try to post a new node without attaching a file in the multivalue field.
  $edit = array(
    'title' => $this
      ->randomName(),
  );
  $this
    ->drupalPost('node/add/' . $type_name, $edit, t('Save'));
  $this
    ->assertRaw(t('!title field is required.', array(
    '!title' => $instance['label'],
  )), 'Node save failed when required multiple value file field was empty.');

  // Create a new node with the attached file into the multivalue field.
  $nid = $this
    ->attachNodeFile($test_file, $field_name, $type_name);
  $node = node_load($nid, NULL, TRUE);
  $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
  $this
    ->assertFileExists($node_file, 'File exists after attaching to the required multiple value field.');
  $this
    ->assertFileEntryExists($node_file, 'File entry exists after attaching to the required multiple value field.');

  // Remove our file field.
  field_delete_field($field_name);
}