1 file.test FileUploadWizardTestCase::testFileUploadWizardStepSkipping()

Test skipping each of the file upload wizard steps.

File

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

Class

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

Code

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

  // Ensure that the file is affected by every step.
  config_set('system.core', 'file_private_path', $this->private_files_directory);

  $this->createFileType(array('type' => 'image1', 'name' => 'Image 1', 'mimetypes' => array('image/jpeg', 'image/gif', 'image/png', 'image/tiff')));
  $this->createFileType(array('type' => 'image2', 'name' => 'Image 2', 'mimetypes' => array('image/jpeg', 'image/gif', 'image/png', 'image/tiff')));

  $field_name = backdrop_strtolower($this->randomName() . '_field_name');
  $field = array('field_name' => $field_name, 'type' => 'text');
  field_create_field($field);
  $instance = array(
    'field_name' => $field_name,
    'entity_type' => 'file',
    'bundle' => 'image2',
    'label' => $this->randomName() . '_label',
  );
  field_create_instance($instance);

  // Test skipping each upload wizard step.
  $debug_i = 0;
  foreach (array('types', 'schemes', 'fields') as $skipped_step) {
    // Step to skip.
    switch ($skipped_step) {
      case 'types':
        config_set('file.settings', 'upload_wizard_skip_file_type', TRUE);
        break;
      case 'schemes':
        config_set('file.settings', 'upload_wizard_skip_scheme', TRUE);
        break;
      case 'fields':
        config_set('file.settings', 'upload_wizard_skip_fields', TRUE);
        break;
    }

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

    $debug_i++;
    debug('Step ' . $debug_i . ': ' . $skipped_step);

    // Step 1: File type selection.
    if ($skipped_step != 'types') {
      debug('Step 1: File type selection.');
      $this->assertText('File type', 'Types step confirmed.');
      $edit = array();
      $edit['type'] = 'image2';
      $this->backdropPost(NULL, $edit, t('Next'));
    }
    else {
      debug('Skipping types');
    }

    // Step 2: Scheme selection.
    if ($skipped_step != 'schemes') {
      debug('Step 2: Scheme selection.');
      $this->assertText('Private local files', 'Schemes step confirmed.');
      $edit = array();
      $edit['scheme'] = 'private';
      $this->backdropPost(NULL, $edit, t('Next'));
    }
    else {
      debug('Skipping schemes');
    }

    // Step 3: Attached fields.
    if ($skipped_step != 'fields') {
      // Skipping file type selection essentially skips this step as well
      // because the file will not be assigned a type so no fields will be
      // available.
      if ($skipped_step != 'types') {
        debug('Step 3: Attached fields.');
        $this->assertText('Configure file fields', 'Fields step confirmed.');
        $edit = array();
        $edit['filename'] = $filename;
        $edit[$field_name . '[' . LANGUAGE_NONE . '][0][value]'] = $this->randomName();
        $this->backdropPost(NULL, $edit, t('Save'));
      }
    }
    else {
      debug('Skipping fields');
    }

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

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

    // Reset 'skip' variables.
    $config = config('file.settings');
    $config->set('upload_wizard_skip_file_type', FALSE);
    $config->set('upload_wizard_skip_scheme', FALSE);
    $config->set('upload_wizard_skip_fields', FALSE);
    $config->save();
  }
}