// Verify that a new file ID is set when saving a new file to the database.
$this->
assertGreaterThan(0,
$file->
id());
$loaded_file = File::
load($file->
id());
$this->
assertNotNull($loaded_file, 'Record exists in the database.'
);
$this->
assertEquals($file->
isPermanent(),
$loaded_file->
isPermanent(), 'Status was saved correctly.'
);
$this->
assertEquals(filesize($file->
getFileUri()),
$file->
getSize(), 'File size was set correctly.'
);
// Verify that the new file size was set correctly.
$this->
assertGreaterThan(1,
$file->
getChangedTime());
$this->
assertEquals('en',
$loaded_file->langcode->value, 'Langcode was defaulted correctly.'
);
// Resave the file, updating the existing record.
file_test_reset();
$file->status->value = 7;
$file->
save();
// Check that the correct hooks were called.
$this->
assertFileHooksCalled(['load', 'update'
]);
$this->
assertEquals($file->
id(),
$file->
id(), 'The file ID of an existing file is not changed when updating the database.'
);
$loaded_file = File::
load($file->
id());
// Verify that the timestamp didn't go backwards.
$this->
assertGreaterThanOrEqual($file->
getChangedTime(),
$loaded_file->
getChangedTime());
$this->
assertNotNull($loaded_file, 'Record still exists in the database.'
);