You are here

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

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

File

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

Class

FileFieldPathsGeneralTestCase
Class FileFieldPathsGeneralTestCase

Code

public function testFileUsage() {

  // Create a File field with 'node/[node:nid]' as the File path.
  $field_name = drupal_strtolower($this
    ->randomName());
  $instance_settings['filefield_paths']['file_path']['value'] = 'node/[node:nid]';
  $this
    ->createFileField($field_name, $this->content_type, array(), $instance_settings);

  // Create a node with a test file.
  $test_file = $this
    ->getTestFile('text');
  $nid = $this
    ->uploadNodeFile($test_file, $field_name, $this->content_type);

  // Get file usage for uploaded file.
  $node = node_load($nid, NULL, TRUE);
  $items = field_get_items('node', $node, $field_name);
  $file = file_load($items[0]['fid']);
  $usage = file_usage_list($file);

  // Ensure file usage count for new node is correct.
  $this
    ->assert(isset($usage['file']['node'][$nid]) && $usage['file']['node'][$nid] == 1, t('File usage count for new node is correct.'));

  // Update node.
  $this
    ->drupalPost("node/{$nid}/edit", array(), t('Save'));
  $usage = file_usage_list($file);

  // Ensure file usage count for updated node is correct.
  $this
    ->assert(isset($usage['file']['node'][$nid]) && $usage['file']['node'][$nid] == 1, t('File usage count for updated node is correct.'));

  // Update node with revision.
  $this
    ->drupalPost("node/{$nid}/edit", array(
    'revision' => TRUE,
  ), t('Save'));
  $usage = file_usage_list($file);

  // Ensure file usage count for updated node with revision is correct.
  $this
    ->assert(isset($usage['file']['node'][$nid]) && $usage['file']['node'][$nid] == 2, t('File usage count for updated node with revision is correct.'));
}