You are here

function SaveDataTest::testExistingError in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/file/src/Tests/SaveDataTest.php \Drupal\file\Tests\SaveDataTest::testExistingError()

Test that file_save_data() fails overwriting an existing file.

File

core/modules/file/src/Tests/SaveDataTest.php, line 123
Contains \Drupal\file\Tests\SaveDataTest.

Class

SaveDataTest
Tests the file_save_data() function.

Namespace

Drupal\file\Tests

Code

function testExistingError() {
  $contents = $this
    ->randomMachineName(8);
  $existing = $this
    ->createFile(NULL, $contents);

  // Check the overwrite error.
  $result = file_save_data('asdf', $existing
    ->getFileUri(), FILE_EXISTS_ERROR);
  $this
    ->assertFalse($result, 'Overwriting a file fails when FILE_EXISTS_ERROR is specified.');
  $this
    ->assertEqual($contents, file_get_contents($existing
    ->getFileUri()), 'Contents of existing file were unchanged.');

  // Check that no hooks were called while failing.
  $this
    ->assertFileHooksCalled(array());

  // Ensure that the existing file wasn't overwritten.
  $this
    ->assertFileUnchanged($existing, File::load($existing
    ->id()));
}