You are here

function FileFieldTestCase::uploadNodeFile in FileField 6.3

Upload a file to a node.

7 calls to FileFieldTestCase::uploadNodeFile()
FileFieldDisplayTestCase::testNodeDisplay in tests/filefield.test
Test normal formatter display on node display.
FileFieldPathTestCase::testUploadPath in tests/filefield.test
Test normal formatter display on node display.
FileFieldRevisionTestCase::testRevisions in tests/filefield.test
Test creating multiple revisions of a node and managing the attached files.
FileFieldValidateTestCase::testFileExtension in tests/filefield.test
Test the file extension, do additional checks if mimedetect is installed.
FileFieldValidateTestCase::testFileMaxSize in tests/filefield.test
Test the max file size validator.

... See full list

File

tests/filefield.test, line 90

Class

FileFieldTestCase

Code

function uploadNodeFile($file, $field, $nid_or_type, $new_revision = TRUE) {
  $field_name = $field['field_name'];
  $edit = array(
    'title' => $this
      ->randomName(),
    'revision' => (string) (int) $new_revision,
  );
  if (is_numeric($nid_or_type)) {
    $node = node_load($nid_or_type);
    $delta = isset($node->{$field_name}) ? count($node->{$field_name}) : 0;
    $edit['files[' . $field_name . '_' . $delta . ']'] = realpath($file->filepath);
    $this
      ->drupalPost('node/' . $nid_or_type . '/edit', $edit, t('Save'));
  }
  else {
    $delta = '0';
    $edit['files[' . $field_name . '_' . $delta . ']'] = realpath($file->filepath);
    $type = str_replace('_', '-', $nid_or_type);
    $this
      ->drupalPost('node/add/' . $type, $edit, t('Save'));
  }
  $matches = array();
  preg_match('/node\\/([0-9]+)/', $this
    ->getUrl(), $matches);
  $nid = isset($matches[1]) ? $matches[1] : FALSE;

  // Edit the node and add a description if possible.
  if ($nid && $field['description_field']) {
    $edit = array(
      'revision' => 0,
      $field_name . '[' . $delta . '][data][description]' => $this
        ->randomString(),
    );
    $this
      ->drupalPost('node/' . $nid . '/edit', $edit, t('Save'));
  }
  return $nid;
}