747c7b1ca546480d1fdb703eb864dd6579aec943
[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    * Test the file_save_data() function when no filename is provided.
15    */
16   public function testWithoutFilename() {
17     $contents = $this->randomMachineName(8);
18
19     $result = file_save_data($contents);
20     $this->assertTrue($result, 'Unnamed file saved correctly.');
21
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.");
27
28     // Check that the correct hooks were called.
29     $this->assertFileHooksCalled(['insert']);
30
31     // Verify that what was returned is what's in the database.
32     $this->assertFileUnchanged($result, File::load($result->id()));
33   }
34
35   /**
36    * Test the file_save_data() function when a filename is provided.
37    */
38   public function testWithFilename() {
39     $contents = $this->randomMachineName(8);
40
41     // Using filename with non-latin characters.
42     $filename = 'Текстовый файл.txt';
43
44     $result = file_save_data($contents, 'public://' . $filename);
45     $this->assertTrue($result, 'Unnamed file saved correctly.');
46
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.");
52
53     // Check that the correct hooks were called.
54     $this->assertFileHooksCalled(['insert']);
55
56     // Verify that what was returned is what's in the database.
57     $this->assertFileUnchanged($result, File::load($result->id()));
58   }
59
60   /**
61    * Test file_save_data() when renaming around an existing file.
62    */
63   public function testExistingRename() {
64     // Setup a file to overwrite.
65     $existing = $this->createFile();
66     $contents = $this->randomMachineName(8);
67
68     $result = file_save_data($contents, $existing->getFileUri(), FILE_EXISTS_RENAME);
69     $this->assertTrue($result, 'File saved successfully.');
70
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.");
76
77     // Check that the correct hooks were called.
78     $this->assertFileHooksCalled(['insert']);
79
80     // Ensure that the existing file wasn't overwritten.
81     $this->assertDifferentFile($existing, $result);
82     $this->assertFileUnchanged($existing, File::load($existing->id()));
83
84     // Verify that was returned is what's in the database.
85     $this->assertFileUnchanged($result, File::load($result->id()));
86   }
87
88   /**
89    * Test file_save_data() when replacing an existing file.
90    */
91   public function testExistingReplace() {
92     // Setup a file to overwrite.
93     $existing = $this->createFile();
94     $contents = $this->randomMachineName(8);
95
96     $result = file_save_data($contents, $existing->getFileUri(), FILE_EXISTS_REPLACE);
97     $this->assertTrue($result, 'File saved successfully.');
98
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.");
104
105     // Check that the correct hooks were called.
106     $this->assertFileHooksCalled(['load', 'update']);
107
108     // Verify that the existing file was re-used.
109     $this->assertSameFile($existing, $result);
110
111     // Verify that what was returned is what's in the database.
112     $this->assertFileUnchanged($result, File::load($result->id()));
113   }
114
115   /**
116    * Test that file_save_data() fails overwriting an existing file.
117    */
118   public function testExistingError() {
119     $contents = $this->randomMachineName(8);
120     $existing = $this->createFile(NULL, $contents);
121
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.');
126
127     // Check that no hooks were called while failing.
128     $this->assertFileHooksCalled([]);
129
130     // Ensure that the existing file wasn't overwritten.
131     $this->assertFileUnchanged($existing, File::load($existing->id()));
132   }
133
134 }