You are here

public function TestSubContext::fileDelete in Drupal Commons 7.3

Delete a managed Drupal file.

Parameters

$file: A file object to delete.

1 call to TestSubContext::fileDelete()
TestSubContext::cleanFiles in tests/steps/commons_test.behat.inc
Cleans up files after every scenario.

File

tests/steps/commons_test.behat.inc, line 280
Provide Behat step-definitions for generic Commons tests.

Class

TestSubContext

Code

public function fileDelete($file) {

  // Figure out if there's usage in any nodes.
  $fid = $file->fid;
  $node_usage = db_query('SELECT id AS nid FROM {file_usage} WHERE fid = (:fid) AND module = (:module) and type = (:node)', array(
    ':fid' => $fid,
    ':module' => 'media',
    ':node' => 'node',
  ))
    ->fetchAll();

  // If there is, it should be safe to unregister it, because we already know the file is owned by a current test user.
  if (!empty($node_usage)) {
    foreach ($node_usage as $nid) {
      file_usage_delete($file, 'media', 'node', $nid->nid);
    }
  }

  // See CommonsContext::fileCreate() for information on why we do this.
  $cwd = getcwd();
  chdir(DRUPAL_ROOT);
  file_delete($file);
  chdir($cwd);
}