public function SaveDataTest::testExistingError in Drupal 9
Same name and namespace in other branches
- 8 core/modules/file/tests/src/Kernel/SaveDataTest.php \Drupal\Tests\file\Kernel\SaveDataTest::testExistingError()
Tests that file_save_data() fails overwriting an existing file.
File
- core/
modules/ file/ tests/ src/ Kernel/ SaveDataTest.php, line 130
Class
- SaveDataTest
- Tests the file_save_data() function.
Namespace
Drupal\Tests\file\KernelCode
public function testExistingError() {
$contents = $this
->randomMachineName(8);
$existing = $this
->createFile(NULL, $contents);
// Check the overwrite error.
$result = file_save_data('asdf', $existing
->getFileUri(), FileSystemInterface::EXISTS_ERROR);
$this
->assertFalse($result, 'Overwriting a file fails when FileSystemInterface::EXISTS_ERROR is specified.');
$this
->assertEquals($contents, file_get_contents($existing
->getFileUri()), 'Contents of existing file were unchanged.');
// Check that no hooks were called while failing.
$this
->assertFileHooksCalled([]);
// Ensure that the existing file wasn't overwritten.
$this
->assertFileUnchanged($existing, File::load($existing
->id()));
}