You are here

function FileFieldPathTestCase::testUploadPath in FileField 6.3

Test normal formatter display on node display.

File

tests/filefield.test, line 571

Class

FileFieldPathTestCase
Test class to check that files are uploaded to proper locations.

Code

function testUploadPath() {
  $field_name = 'field_' . strtolower($this
    ->randomName());
  $type = $this
    ->drupalCreateContentType();
  $field = $this
    ->createFileField($field_name, $type->name);
  $test_file = $this
    ->getTestFile('text');

  // Create a new node.
  $nid = $this
    ->uploadNodeFile($test_file, $field, $type->name);

  // Check that the file was uploaded to the file root.
  $node = node_load($nid, NULL, TRUE);
  $node_file = $node->{$field['field_name']}[0];
  $this
    ->assertPathMatch(file_directory_path() . '/' . $test_file->filename, $node_file['filepath'], t('The file %file was uploaded to the correct path.', array(
    '%file' => $node_file['filepath'],
  )));

  // Change the path to contain multiple subdirectories.
  $field = $this
    ->updateFileField($field['field_name'], $type->name, array(), array(
    'file_path' => 'foo/bar/baz',
  ));

  // Upload a new file into the subdirectories.
  $nid = $this
    ->uploadNodeFile($test_file, $field, $type->name);

  // Check that the file was uploaded into the subdirectory.
  $node = node_load($nid, NULL, TRUE);
  $node_file = $node->{$field['field_name']}[0];
  $this
    ->assertPathMatch(file_directory_path() . '/foo/bar/baz/' . $test_file->filename, $node_file['filepath'], t('The file %file was uploaded to the correct path.', array(
    '%file' => $node_file['filepath'],
  )));

  // Check the path when used with tokens.
  if (module_exists('token')) {

    // Change the path to contain multiple token directories.
    $field = $this
      ->updateFileField($field['field_name'], $type->name, array(), array(
      'file_path' => '[uid]/[user-raw]',
    ));

    // Upload a new file into the token subdirectories.
    $nid = $this
      ->uploadNodeFile($test_file, $field, $type->name);

    // Check that the file was uploaded into the subdirectory.
    $node = node_load($nid, NULL, TRUE);
    $node_file = $node->{$field['field_name']}[0];
    $subdirectory = token_replace('[uid]/[user-raw]', 'user', $this->admin_user);
    $this
      ->assertPathMatch(file_directory_path() . '/' . $subdirectory . '/' . $test_file->filename, $node_file['filepath'], t('The file %file was uploaded to the correct path with token replacements.', array(
      '%file' => $node_file['filepath'],
    )));
  }
  else {
    $this
      ->assertTrue(TRUE, t('File path token test skipped because the Token module is not available.'));
  }
}