Newer
Older
<?php
/**
* @file
Angie Byron
committed
* Definition of Drupal\file\Tests\SaveTest.
*/
Angie Byron
committed
namespace Drupal\file\Tests;
/**
* Tests saving files.
*/
Angie Byron
committed
class SaveTest extends FileManagedTestBase {
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
public static function getInfo() {
return array(
'name' => 'File saving',
'description' => 'File saving tests',
'group' => 'File API',
);
}
function testFileSave() {
// Create a new file entity.
$file = entity_create('file', array(
'uid' => 1,
'filename' => 'druplicon.txt',
'uri' => 'public://druplicon.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->assertTrue($file->fid > 0, t("A new file ID is set when saving a new file to the database."), 'File');
$loaded_file = db_query('SELECT * FROM {file_managed} f WHERE f.fid = :fid', array(':fid' => $file->fid))->fetchObject();
$this->assertNotNull($loaded_file, t("Record exists in the database."));
$this->assertEqual($loaded_file->status, $file->status, t("Status was saved correctly."));
$this->assertEqual($file->filesize, filesize($file->uri), t("File size was set correctly."), 'File');
$this->assertTrue($file->timestamp > 1, t("File size was set correctly."), 'File');
$this->assertEqual($loaded_file->langcode, LANGUAGE_NOT_SPECIFIED, t("Langcode was defaulted correctly."));
// 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, t("The file ID of an existing file is not changed when updating the database."), 'File');
$this->assertTrue($file->timestamp >= $file->timestamp, t("Timestamp didn't go backwards."), 'File');
$loaded_file = db_query('SELECT * FROM {file_managed} f WHERE f.fid = :fid', array(':fid' => $file->fid))->fetchObject();
$this->assertNotNull($loaded_file, t("Record still exists in the database."), 'File');
$this->assertEqual($loaded_file->status, $file->status, t("Status was saved correctly."));
$this->assertEqual($loaded_file->langcode, 'en', t("Langcode was saved correctly."));
// Try to insert a second file with the same name apart from case insensitivity
// to ensure the 'uri' index allows for filenames with different cases.
$file = entity_create('file', array(
'uid' => 1,
'filename' => 'DRUPLICON.txt',
'uri' => 'public://DRUPLICON.txt',
'filemime' => 'text/plain',
'timestamp' => 1,
'status' => FILE_STATUS_PERMANENT,
));
file_put_contents($file->uri, 'hello world');
$file->save();
}
}