'filename' => 'druplicon.txt',
'uri' => 'public://druplicon.txt',
'filemime' => 'text/plain',
]);
$file->
setPermanent();
file_put_contents($file->
getFileUri(), 'hello world'
);
// Save it, inserting a new record.
$file->
save();
// Check that the correct hooks were called.
$this->
assertFileHooksCalled(['insert'
]);
// 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.'
);