Replaces a file within a node.
function replaceNodeFile($file, $field_name, $nid, $new_revision = TRUE) {
$edit = array(
'files[' . $field_name . '_' . Language::LANGCODE_NOT_SPECIFIED . '_0]' => drupal_realpath($file->uri),
'revision' => (string) (int) $new_revision,
);
$this
->drupalPost('node/' . $nid . '/edit', array(), t('Remove'));
$this
->drupalPost(NULL, $edit, t('Save and keep published'));
}