3 namespace Drupal\Tests\file\Kernel;
5 use Drupal\file\Entity\File;
12 class SaveTest extends FileManagedUnitTestBase {
14 public function testFileSave() {
15 // Create a new file entity.
16 $file = File::create([
18 'filename' => 'druplicon.txt',
19 'uri' => 'public://druplicon.txt',
20 'filemime' => 'text/plain',
21 'status' => FILE_STATUS_PERMANENT,
23 file_put_contents($file->getFileUri(), 'hello world');
25 // Save it, inserting a new record.
28 // Check that the correct hooks were called.
29 $this->assertFileHooksCalled(['insert']);
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.');
39 // Resave the file, updating the existing record.
41 $file->status->value = 7;
44 // Check that the correct hooks were called.
45 $this->assertFileHooksCalled(['load', 'update']);
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.');
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.
58 'filename' => 'DRUPLICON.txt',
59 'uri' => 'public://DRUPLICON.txt',
60 'filemime' => 'text/plain',
61 'status' => FILE_STATUS_PERMANENT,
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();
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(),
77 // Ensure that file URI entity queries are case sensitive.
78 $fids = \Drupal::entityQuery('file')
79 ->condition('uri', $uppercase_file->getFileUri())
82 $this->assertEqual(1, count($fids));
83 $this->assertEqual([$uppercase_file->id() => $uppercase_file->id()], $fids);
85 // Save a file with zero bytes.
86 $file = File::create([
88 'filename' => 'no-druplicon.txt',
89 'uri' => 'public://no-druplicon.txt',
90 'filemime' => 'text/plain',
91 'status' => FILE_STATUS_PERMANENT,
94 file_put_contents($file->getFileUri(), '');
96 // Save it, inserting a new record.
99 // Check the file size was set to zero.
100 $this->assertSame(0, $file->getSize());