3 namespace Drupal\Tests\file\Kernel;
5 use Drupal\file\Entity\File;
12 class SaveTest extends FileManagedUnitTestBase {
13 public function testFileSave() {
14 // Create a new file entity.
15 $file = File::create([
17 'filename' => 'druplicon.txt',
18 'uri' => 'public://druplicon.txt',
19 'filemime' => 'text/plain',
20 'status' => FILE_STATUS_PERMANENT,
22 file_put_contents($file->getFileUri(), 'hello world');
24 // Save it, inserting a new record.
27 // Check that the correct hooks were called.
28 $this->assertFileHooksCalled(['insert']);
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.');
38 // Resave the file, updating the existing record.
40 $file->status->value = 7;
43 // Check that the correct hooks were called.
44 $this->assertFileHooksCalled(['load', 'update']);
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.');
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.
57 'filename' => 'DRUPLICON.txt',
58 'uri' => 'public://DRUPLICON.txt',
59 'filemime' => 'text/plain',
60 'status' => FILE_STATUS_PERMANENT,
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();
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(),
76 // Ensure that file URI entity queries are case sensitive.
77 $fids = \Drupal::entityQuery('file')
78 ->condition('uri', $uppercase_file->getFileUri())
81 $this->assertEqual(1, count($fids));
82 $this->assertEqual([$uppercase_file->id() => $uppercase_file->id()], $fids);