Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / file / tests / src / Kernel / SaveTest.php
1 <?php
2
3 namespace Drupal\Tests\file\Kernel;
4
5 use Drupal\file\Entity\File;
6
7 /**
8  * File saving tests.
9  *
10  * @group file
11  */
12 class SaveTest extends FileManagedUnitTestBase {
13
14   public function testFileSave() {
15     // Create a new file entity.
16     $file = File::create([
17       'uid' => 1,
18       'filename' => 'druplicon.txt',
19       'uri' => 'public://druplicon.txt',
20       'filemime' => 'text/plain',
21       'status' => FILE_STATUS_PERMANENT,
22     ]);
23     file_put_contents($file->getFileUri(), 'hello world');
24
25     // Save it, inserting a new record.
26     $file->save();
27
28     // Check that the correct hooks were called.
29     $this->assertFileHooksCalled(['insert']);
30
31     $this->assertTrue($file->id() > 0, 'A new file ID is set when saving a new file to the database.', 'File');
32     $loaded_file = File::load($file->id());
33     $this->assertNotNull($loaded_file, 'Record exists in the database.');
34     $this->assertEqual($loaded_file->isPermanent(), $file->isPermanent(), 'Status was saved correctly.');
35     $this->assertEqual($file->getSize(), filesize($file->getFileUri()), 'File size was set correctly.', 'File');
36     $this->assertTrue($file->getChangedTime() > 1, 'File size was set correctly.', 'File');
37     $this->assertEqual($loaded_file->langcode->value, 'en', 'Langcode was defaulted correctly.');
38
39     // Resave the file, updating the existing record.
40     file_test_reset();
41     $file->status->value = 7;
42     $file->save();
43
44     // Check that the correct hooks were called.
45     $this->assertFileHooksCalled(['load', 'update']);
46
47     $this->assertEqual($file->id(), $file->id(), 'The file ID of an existing file is not changed when updating the database.', 'File');
48     $this->assertTrue($file->getChangedTime() >= $file->getChangedTime(), "Timestamp didn't go backwards.", 'File');
49     $loaded_file = File::load($file->id());
50     $this->assertNotNull($loaded_file, 'Record still exists in the database.', 'File');
51     $this->assertEqual($loaded_file->isPermanent(), $file->isPermanent(), 'Status was saved correctly.');
52     $this->assertEqual($loaded_file->langcode->value, 'en', 'Langcode was saved correctly.');
53
54     // Try to insert a second file with the same name apart from case insensitivity
55     // to ensure the 'uri' index allows for filenames with different cases.
56     $uppercase_values = [
57       'uid' => 1,
58       'filename' => 'DRUPLICON.txt',
59       'uri' => 'public://DRUPLICON.txt',
60       'filemime' => 'text/plain',
61       'status' => FILE_STATUS_PERMANENT,
62     ];
63     $uppercase_file = File::create($uppercase_values);
64     file_put_contents($uppercase_file->getFileUri(), 'hello world');
65     $violations = $uppercase_file->validate();
66     $this->assertEqual(count($violations), 0, 'No violations when adding an URI with an existing filename in upper case.');
67     $uppercase_file->save();
68
69     // Ensure the database URI uniqueness constraint is triggered.
70     $uppercase_file_duplicate = File::create($uppercase_values);
71     file_put_contents($uppercase_file_duplicate->getFileUri(), 'hello world');
72     $violations = $uppercase_file_duplicate->validate();
73     $this->assertEqual(count($violations), 1);
74     $this->assertEqual($violations[0]->getMessage(), t('The file %value already exists. Enter a unique file URI.', [
75       '%value' => $uppercase_file_duplicate->getFileUri(),
76     ]));
77     // Ensure that file URI entity queries are case sensitive.
78     $fids = \Drupal::entityQuery('file')
79       ->condition('uri', $uppercase_file->getFileUri())
80       ->execute();
81
82     $this->assertEqual(1, count($fids));
83     $this->assertEqual([$uppercase_file->id() => $uppercase_file->id()], $fids);
84
85     // Save a file with zero bytes.
86     $file = File::create([
87       'uid' => 1,
88       'filename' => 'no-druplicon.txt',
89       'uri' => 'public://no-druplicon.txt',
90       'filemime' => 'text/plain',
91       'status' => FILE_STATUS_PERMANENT,
92     ]);
93
94     file_put_contents($file->getFileUri(), '');
95
96     // Save it, inserting a new record.
97     $file->save();
98
99     // Check the file size was set to zero.
100     $this->assertSame(0, $file->getSize());
101   }
102
103 }