Programmatically save altered file As I mentioned in my previous question I'm attempting to write tests for a patch that will, ideally, trigger the creation of a redirect if the URI of a file changes. 

My patch uses hook_file_update() which is called whenever file_save() runs. 

Currently this chunk of code:

  // Move file to new URI
  $new_uri = 'public://qwerty.png';
  file_save($test_file);
  $test_file-&gt;uri = $new_uri;
  file_save($test_file);


This calls my hook and creates the redirect, but the redirect fails because drupalGet($new_url) returns a 404.

file_save() returns the error, filesize() [function.filesize]: stat failed for public://qwerty.png.

I've tried file_save() on URIs that linked to existing files, which gives an error, I've tried it on not-yet-extant URIs (as above), which gives an error, and I've tried file_move(), which doesn't call hook_file_update(). 

How can I programmatically change the URI of a file and trigger hook_file_update() inside a test?
The way I ended up getting around this was: 


Upload my file as an unmanaged file at the new URI
$test_file-&gt;uri = $new_uri;
file_save($test_file);
file_unmanaged_delete($old_uri);


It didn't like it when I used a URI that was not yet in existence, nor one that was already in use by a managed file, but using one that goes to an unmanaged file seemed to work fine.