Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / file / tests / src / Kernel / SaveDataTest.php
1 <?php
2
3 namespace Drupal\Tests\file\Kernel;
4
5 use Drupal\file\Entity\File;
6
7 /**
8  * Tests the file_save_data() function.
9  *
10  * @group file
11  */
12 class SaveDataTest extends FileManagedUnitTestBase {
13
14   /**
15    * Test the file_save_data() function when no filename is provided.
16    */
17   public function testWithoutFilename() {
18     $contents = $this->randomMachineName(8);
19
20     $result = file_save_data($contents);
21     $this->assertTrue($result, 'Unnamed file saved correctly.');
22
23     $this->assertEqual(file_default_scheme(), file_uri_scheme($result->getFileUri()), "File was placed in Drupal's files directory.");
24     $this->assertEqual($result->getFilename(), drupal_basename($result->getFileUri()), "Filename was set to the file's basename.");
25     $this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of the file are correct.');
26     $this->assertEqual($result->getMimeType(), 'application/octet-stream', 'A MIME type was set.');
27     $this->assertTrue($result->isPermanent(), "The file's status was set to permanent.");
28
29     // Check that the correct hooks were called.
30     $this->assertFileHooksCalled(['insert']);
31
32     // Verify that what was returned is what's in the database.
33     $this->assertFileUnchanged($result, File::load($result->id()));
34   }
35
36   /**
37    * Test the file_save_data() function when a filename is provided.
38    */
39   public function testWithFilename() {
40     $contents = $this->randomMachineName(8);
41
42     // Using filename with non-latin characters.
43     $filename = 'Текстовый файл.txt';
44
45     $result = file_save_data($contents, 'public://' . $filename);
46     $this->assertTrue($result, 'Unnamed file saved correctly.');
47
48     $this->assertEqual('public', file_uri_scheme($result->getFileUri()), "File was placed in Drupal's files directory.");
49     $this->assertEqual($filename, drupal_basename($result->getFileUri()), 'File was named correctly.');
50     $this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of the file are correct.');
51     $this->assertEqual($result->getMimeType(), 'text/plain', 'A MIME type was set.');
52     $this->assertTrue($result->isPermanent(), "The file's status was set to permanent.");
53
54     // Check that the correct hooks were called.
55     $this->assertFileHooksCalled(['insert']);
56
57     // Verify that what was returned is what's in the database.
58     $this->assertFileUnchanged($result, File::load($result->id()));
59   }
60
61   /**
62    * Test file_save_data() when renaming around an existing file.
63    */
64   public function testExistingRename() {
65     // Setup a file to overwrite.
66     $existing = $this->createFile();
67     $contents = $this->randomMachineName(8);
68
69     $result = file_save_data($contents, $existing->getFileUri(), FILE_EXISTS_RENAME);
70     $this->assertTrue($result, 'File saved successfully.');
71
72     $this->assertEqual('public', file_uri_scheme($result->getFileUri()), "File was placed in Drupal's files directory.");
73     $this->assertEqual($result->getFilename(), $existing->getFilename(), 'Filename was set to the basename of the source, rather than that of the renamed file.');
74     $this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of the file are correct.');
75     $this->assertEqual($result->getMimeType(), 'application/octet-stream', 'A MIME type was set.');
76     $this->assertTrue($result->isPermanent(), "The file's status was set to permanent.");
77
78     // Check that the correct hooks were called.
79     $this->assertFileHooksCalled(['insert']);
80
81     // Ensure that the existing file wasn't overwritten.
82     $this->assertDifferentFile($existing, $result);
83     $this->assertFileUnchanged($existing, File::load($existing->id()));
84
85     // Verify that was returned is what's in the database.
86     $this->assertFileUnchanged($result, File::load($result->id()));
87   }
88
89   /**
90    * Test file_save_data() when replacing an existing file.
91    */
92   public function testExistingReplace() {
93     // Setup a file to overwrite.
94     $existing = $this->createFile();
95     $contents = $this->randomMachineName(8);
96
97     $result = file_save_data($contents, $existing->getFileUri(), FILE_EXISTS_REPLACE);
98     $this->assertTrue($result, 'File saved successfully.');
99
100     $this->assertEqual('public', file_uri_scheme($result->getFileUri()), "File was placed in Drupal's files directory.");
101     $this->assertEqual($result->getFilename(), $existing->getFilename(), 'Filename was set to the basename of the existing file, rather than preserving the original name.');
102     $this->assertEqual($contents, file_get_contents($result->getFileUri()), 'Contents of the file are correct.');
103     $this->assertEqual($result->getMimeType(), 'application/octet-stream', 'A MIME type was set.');
104     $this->assertTrue($result->isPermanent(), "The file's status was set to permanent.");
105
106     // Check that the correct hooks were called.
107     $this->assertFileHooksCalled(['load', 'update']);
108
109     // Verify that the existing file was re-used.
110     $this->assertSameFile($existing, $result);
111
112     // Verify that what was returned is what's in the database.
113     $this->assertFileUnchanged($result, File::load($result->id()));
114   }
115
116   /**
117    * Test that file_save_data() fails overwriting an existing file.
118    */
119   public function testExistingError() {
120     $contents = $this->randomMachineName(8);
121     $existing = $this->createFile(NULL, $contents);
122
123     // Check the overwrite error.
124     $result = file_save_data('asdf', $existing->getFileUri(), FILE_EXISTS_ERROR);
125     $this->assertFalse($result, 'Overwriting a file fails when FILE_EXISTS_ERROR is specified.');
126     $this->assertEqual($contents, file_get_contents($existing->getFileUri()), 'Contents of existing file were unchanged.');
127
128     // Check that no hooks were called while failing.
129     $this->assertFileHooksCalled([]);
130
131     // Ensure that the existing file wasn't overwritten.
132     $this->assertFileUnchanged($existing, File::load($existing->id()));
133   }
134
135 }