You are here

public function FileFieldPathsGeneralTestCase::testUploadFile in File (Field) Paths 7

Test a basic file upload with File (Field) Paths.

File

tests/filefield_paths.general.test, line 62
Tests for the File (Field) Paths module.

Class

FileFieldPathsGeneralTestCase
Class FileFieldPathsGeneralTestCase

Code

public function testUploadFile() {
  $langcode = LANGUAGE_NONE;

  // Create a File field with 'node/[node:nid]' as the File path and
  // '[node:nid].[file:ffp-extension-original]' as the File name.
  $field_name = drupal_strtolower($this
    ->randomName());
  $instance_settings['filefield_paths']['file_path']['value'] = 'node/[node:nid]';
  $instance_settings['filefield_paths']['file_name']['value'] = '[node:nid].[file:ffp-extension-original]';
  $this
    ->createFileField($field_name, $this->content_type, array(), $instance_settings);
  $schemes = array(
    'public',
    'private',
  );
  foreach ($schemes as $scheme) {

    // Set the field URI scheme.
    $this
      ->drupalPost("admin/structure/types/manage/{$this->content_type}/fields/{$field_name}", array(
      'field[settings][uri_scheme]' => $scheme,
    ), t('Save settings'));

    // Upload a file to a node.
    $test_file = $this
      ->getTestFile('text');
    $this
      ->drupalGet("node/add/{$this->content_type}");
    $edit['title'] = $this
      ->randomName();
    $edit["files[{$field_name}_{$langcode}_0]"] = $test_file->uri;
    $this
      ->drupalPost(NULL, $edit, t('Upload'));

    // Ensure that the file was put into the Temporary file location.
    $temp_location = variable_get('filefield_paths_temp_location', 'public://filefield_paths');
    $this
      ->assertRaw(file_create_url("{$temp_location}/{$test_file->filename}"), t('File has been uploaded to the temporary file location.'));

    // Save the node.
    $this
      ->drupalPost(NULL, array(), t('Save'));

    // Get created Node ID.
    $matches = array();
    preg_match('/node\\/([0-9]+)/', $this
      ->getUrl(), $matches);
    $nid = $matches[1];

    // Ensure that the File path has been processed correctly.
    $uri = file_create_url("{$scheme}://node/{$nid}/{$nid}.txt");
    $this
      ->assertRaw($uri, t('The File path has been processed correctly.'));

    // Delete the node so we can change the URI scheme.
    node_delete($nid);
  }
}