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