3 namespace Drupal\Tests\file\Kernel;
5 use Drupal\file\Entity\File;
8 * Tests the file_save_data() function.
12 class SaveDataTest extends FileManagedUnitTestBase {
14 * Test the file_save_data() function when no filename is provided.
16 public function testWithoutFilename() {
17 $contents = $this->randomMachineName(8);
19 $result = file_save_data($contents);
20 $this->assertTrue($result, 'Unnamed file saved correctly.');
22 $this->assertEqual(file_default_scheme(), file_uri_scheme($result->getFileUri()), "File was placed in Drupal's files directory.");
23 $this->assertEqual($result->getFilename(), drupal_basename($result->getFileUri()), "Filename was set to the file's basename.");
24 $this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of the file are correct.');
25 $this->assertEqual($result->getMimeType(), 'application/octet-stream', 'A MIME type was set.');
26 $this->assertTrue($result->isPermanent(), "The file's status was set to permanent.");
28 // Check that the correct hooks were called.
29 $this->assertFileHooksCalled(['insert']);
31 // Verify that what was returned is what's in the database.
32 $this->assertFileUnchanged($result, File::load($result->id()));
36 * Test the file_save_data() function when a filename is provided.
38 public function testWithFilename() {
39 $contents = $this->randomMachineName(8);
41 // Using filename with non-latin characters.
42 $filename = 'Текстовый файл.txt';
44 $result = file_save_data($contents, 'public://' . $filename);
45 $this->assertTrue($result, 'Unnamed file saved correctly.');
47 $this->assertEqual('public', file_uri_scheme($result->getFileUri()), "File was placed in Drupal's files directory.");
48 $this->assertEqual($filename, drupal_basename($result->getFileUri()), 'File was named correctly.');
49 $this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of the file are correct.');
50 $this->assertEqual($result->getMimeType(), 'text/plain', 'A MIME type was set.');
51 $this->assertTrue($result->isPermanent(), "The file's status was set to permanent.");
53 // Check that the correct hooks were called.
54 $this->assertFileHooksCalled(['insert']);
56 // Verify that what was returned is what's in the database.
57 $this->assertFileUnchanged($result, File::load($result->id()));
61 * Test file_save_data() when renaming around an existing file.
63 public function testExistingRename() {
64 // Setup a file to overwrite.
65 $existing = $this->createFile();
66 $contents = $this->randomMachineName(8);
68 $result = file_save_data($contents, $existing->getFileUri(), FILE_EXISTS_RENAME);
69 $this->assertTrue($result, 'File saved successfully.');
71 $this->assertEqual('public', file_uri_scheme($result->getFileUri()), "File was placed in Drupal's files directory.");
72 $this->assertEqual($result->getFilename(), $existing->getFilename(), 'Filename was set to the basename of the source, rather than that of the renamed file.');
73 $this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of the file are correct.');
74 $this->assertEqual($result->getMimeType(), 'application/octet-stream', 'A MIME type was set.');
75 $this->assertTrue($result->isPermanent(), "The file's status was set to permanent.");
77 // Check that the correct hooks were called.
78 $this->assertFileHooksCalled(['insert']);
80 // Ensure that the existing file wasn't overwritten.
81 $this->assertDifferentFile($existing, $result);
82 $this->assertFileUnchanged($existing, File::load($existing->id()));
84 // Verify that was returned is what's in the database.
85 $this->assertFileUnchanged($result, File::load($result->id()));
89 * Test file_save_data() when replacing an existing file.
91 public function testExistingReplace() {
92 // Setup a file to overwrite.
93 $existing = $this->createFile();
94 $contents = $this->randomMachineName(8);
96 $result = file_save_data($contents, $existing->getFileUri(), FILE_EXISTS_REPLACE);
97 $this->assertTrue($result, 'File saved successfully.');
99 $this->assertEqual('public', file_uri_scheme($result->getFileUri()), "File was placed in Drupal's files directory.");
100 $this->assertEqual($result->getFilename(), $existing->getFilename(), 'Filename was set to the basename of the existing file, rather than preserving the original name.');
101 $this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of the file are correct.');
102 $this->assertEqual($result->getMimeType(), 'application/octet-stream', 'A MIME type was set.');
103 $this->assertTrue($result->isPermanent(), "The file's status was set to permanent.");
105 // Check that the correct hooks were called.
106 $this->assertFileHooksCalled(['load', 'update']);
108 // Verify that the existing file was re-used.
109 $this->assertSameFile($existing, $result);
111 // Verify that what was returned is what's in the database.
112 $this->assertFileUnchanged($result, File::load($result->id()));
116 * Test that file_save_data() fails overwriting an existing file.
118 public function testExistingError() {
119 $contents = $this->randomMachineName(8);
120 $existing = $this->createFile(NULL, $contents);
122 // Check the overwrite error.
123 $result = file_save_data('asdf', $existing->getFileUri(), FILE_EXISTS_ERROR);
124 $this->assertFalse($result, 'Overwriting a file fails when FILE_EXISTS_ERROR is specified.');
125 $this->assertEqual($contents, file_get_contents($existing->getFileUri()), 'Contents of existing file were unchanged.');
127 // Check that no hooks were called while failing.
128 $this->assertFileHooksCalled([]);
130 // Ensure that the existing file wasn't overwritten.
131 $this->assertFileUnchanged($existing, File::load($existing->id()));