1 file.test FileTestHelper::uploadNodeFile($file, $field_name, $nid_or_type, $new_revision = TRUE, $extras = array())

Uploads a file to a node.


File $file: File object.

string $field_name: The field name.

mixed $nid_or_type: If Node ID, update existing or create a new of node type.

bool $new_revision: If true, create a new revision.

array $extras: Extra settings for the new node.

Return value

int: Node ID.


core/modules/file/tests/file.test, line 287
Tests for file.module.


Provides methods specifically for testing File module's field handling.


function uploadNodeFile($file, $field_name, $nid_or_type, $new_revision = TRUE, $extras = array()) {
  $langcode = LANGUAGE_NONE;
  $edit = array(
    "title" => $this->randomName(),
    'revision' => (string) (int) $new_revision,

  if (is_numeric($nid_or_type)) {
    $nid = $nid_or_type;
  else {
    // Add a new node.
    $extras['type'] = $nid_or_type;
    $node = $this->backdropCreateNode($extras);
    $nid = $node->nid;
    // Save at least one revision to better simulate a real site.
    $node = node_load($nid, NULL, TRUE);
    $this->assertNotEqual($nid, $node->vid, 'Node revision exists.');

  // Attach a file to the node.
  $edit['files[' . $field_name . '_' . $langcode . '_0]'] = backdrop_realpath($file->uri);
  $this->backdropPost("node/$nid/edit", $edit, t('Save'));

  return $nid;