1 file.test | FileSaveTest::testFileSave() |
File
- core/
modules/ simpletest/ tests/ file.test, line 2148 - This provides SimpleTests for the core file handling functionality. These include FileValidateTest and FileSaveTest.
Class
- FileSaveTest
- Tests saving files.
Code
function testFileSave() {
// Create a new file object.
$file = new File(array(
'uid' => 1,
'filename' => 'backdrop.txt',
'uri' => 'public://backdrop.txt',
'filemime' => 'text/plain',
'timestamp' => 1,
'status' => FILE_STATUS_PERMANENT,
));
file_put_contents($file->uri, 'hello world');
// Save it, inserting a new record.
$file->save();
// Check that the correct hooks were called.
$this->assertFileHooksCalled(array('insert'));
$this->assertNotNull($file->fid, "Saving the file should give us back a file object.", 'File');
$this->assertTrue($file->fid > 0, "A new file ID is set when saving a new file to the database.", 'File');
$loaded_file = file_load($file->fid);
$this->assertNotNull($loaded_file, "Record exists in the database.");
$this->assertEqual($loaded_file->status, $file->status, "Status was saved correctly.");
$this->assertEqual($file->filesize, filesize($file->uri), "File size was set correctly.", 'File');
$this->assertTrue($file->timestamp > 1, "File size was set correctly.", 'File');
// Resave the file, updating the existing record.
file_test_reset();
$file->status = 7;
$file->langcode = 'en';
$file->save();
// Check that the correct hooks were called.
$this->assertFileHooksCalled(array('load', 'update'));
$this->assertEqual($file->fid, $file->fid, "The file ID of an existing file is not changed when updating the database.", 'File');
$this->assertTrue($file->timestamp >= $file->timestamp, "Timestamp didn't go backwards.", 'File');
$loaded_file = db_query('SELECT * FROM {file_managed} f WHERE f.fid = :fid', array(':fid' => $file->fid))->fetch(PDO::FETCH_OBJ);
$this->assertNotNull($loaded_file, "Record still exists in the database.", 'File');
$this->assertEqual($loaded_file->status, $file->status, "Status was saved correctly.");
}