1 file.test FileUploadWizardTestCase::testFileUploadWizardFields()

Test the file upload wizard field step.

File

core/modules/file/tests/file.test, line 2408
Tests for file.module.

Class

FileUploadWizardTestCase
Tests creating new file entities through the file upload wizard.

Code

function testFileUploadWizardFields() {
  $test_file = $this->getTestFile('image');
  $filename = $this->randomName();
  $alt = $this->randomName();
  $title = $this->randomName();

  // Add alt and title fields to the image file type.
  //_create_file_alt_title_fields();

  $field = array('field_name' => 'field_file_image_alt_text', 'type' => 'text');
  field_create_field($field);
  $instance = array(
    'field_name' => 'field_file_image_alt_text',
    'entity_type' => 'file',
    'bundle' => 'image',
    'label' => 'Alt',
  );
  field_create_instance($instance);

  $field = array('field_name' => 'field_file_image_title_text', 'type' => 'text');
  field_create_field($field);
  $instance = array(
    'field_name' => 'field_file_image_title_text',
    'entity_type' => 'file',
    'bundle' => 'image',
    'label' => 'Title',
  );
  field_create_instance($instance);

  // Upload a basic image file.
  $edit = array();
  $edit['files[upload]'] = backdrop_realpath($test_file->uri);
  $this->backdropPost('file/add', $edit, t('Next'));

  // Enter values into the attached fields.
  $edit = array();
  $edit['filename'] = $filename;
  $edit['field_file_image_alt_text[' . LANGUAGE_NONE . '][0][value]'] = $alt;
  $edit['field_file_image_title_text[' . LANGUAGE_NONE . '][0][value]'] = $title;
  $this->backdropPost(NULL, $edit, t('Save'));

  // Check that the image file has been uploaded.
  $this->assertRaw(t('@type %name was uploaded.', array('@type' => 'Image', '%name' => $filename)), t('Image file uploaded.'));

  // Check that the file exists in the database.
  $fid = $this->getLastFileId();
  $file = file_load($fid);
  $this->assertTrue($file, t('File found in database.'));

  // Check that the alt and title text was loaded from the fields.
  $alt_value = $file->field_file_image_alt_text['und'][0]['value'];
  $this->assertEqual($alt_value, $alt, t('Alt text was stored as field data.'));
  $title_value = $file->field_file_image_title_text['und'][0]['value'];
  $this->assertEqual($title_value, $title, t('Title text was stored as field data.'));
}