You are here

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

Test File (Field) Paths on a programmatically added file.

File

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

Class

FileFieldPathsGeneralTestCase
Class FileFieldPathsGeneralTestCase

Code

public function testProgrammaticAttach() {

  // 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);

  // Create a node without an attached file.
  $node = $this
    ->drupalCreateNode(array(
    'type' => $this->content_type,
  ));

  // Create a file object.
  $test_file = $this
    ->getTestFile('text');
  $file = new stdClass();
  $file->fid = NULL;
  $file->uri = $test_file->uri;
  $file->filename = basename($file->uri);
  $file->filemime = file_get_mimetype($file->uri);
  $file->uid = $GLOBALS['user']->uid;
  $file->status = FILE_STATUS_PERMANENT;
  $file->display = TRUE;
  file_save($file);

  // Adjust timestamp to simulate real-world experience.
  $file->timestamp = REQUEST_TIME - 60;

  // Attach the file to the node.
  $node->{$field_name}[LANGUAGE_NONE][0] = (array) $file;
  node_save($node);

  // Ensure that the File path has been processed correctly.
  $node = node_load($node->nid, NULL, TRUE);
  $this
    ->assertEqual("public://node/{$node->nid}/{$node->nid}.txt", $node->{$field_name}[LANGUAGE_NONE][0]['uri'], t('The File path has been processed correctly.'));
}