SaveTest.php 3.97 KB
Newer Older
1
2
<?php

3
namespace Drupal\Tests\file\Kernel;
4

5
use Drupal\file\Entity\File;
6

7
/**
8
9
10
 * File saving tests.
 *
 * @group file
11
 */
12
class SaveTest extends FileManagedUnitTestBase {
13
  public function testFileSave() {
14
    // Create a new file entity.
15
    $file = File::create([
16
17
18
19
20
      'uid' => 1,
      'filename' => 'druplicon.txt',
      'uri' => 'public://druplicon.txt',
      'filemime' => 'text/plain',
      'status' => FILE_STATUS_PERMANENT,
21
    ]);
22
    file_put_contents($file->getFileUri(), 'hello world');
23
24
25
26
27

    // Save it, inserting a new record.
    $file->save();

    // Check that the correct hooks were called.
28
    $this->assertFileHooksCalled(['insert']);
29

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
33
34
35
    $this->assertNotNull($loaded_file, 'Record exists in the database.');
    $this->assertEqual($loaded_file->isPermanent(), $file->isPermanent(), 'Status was saved correctly.');
    $this->assertEqual($file->getSize(), filesize($file->getFileUri()), 'File size was set correctly.', 'File');
    $this->assertTrue($file->getChangedTime() > 1, 'File size was set correctly.', 'File');
36
    $this->assertEqual($loaded_file->langcode->value, 'en', 'Langcode was defaulted correctly.');
37
38
39

    // Resave the file, updating the existing record.
    file_test_reset();
40
    $file->status->value = 7;
41
42
43
    $file->save();

    // Check that the correct hooks were called.
44
    $this->assertFileHooksCalled(['load', 'update']);
45

46
47
    $this->assertEqual($file->id(), $file->id(), 'The file ID of an existing file is not changed when updating the database.', 'File');
    $this->assertTrue($file->getChangedTime() >= $file->getChangedTime(), "Timestamp didn't go backwards.", 'File');
48
    $loaded_file = File::load($file->id());
49
50
51
    $this->assertNotNull($loaded_file, 'Record still exists in the database.', 'File');
    $this->assertEqual($loaded_file->isPermanent(), $file->isPermanent(), 'Status was saved correctly.');
    $this->assertEqual($loaded_file->langcode->value, 'en', 'Langcode was saved correctly.');
52
53
54

    // 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.
55
    $uppercase_values = [
56
57
58
59
60
      'uid' => 1,
      'filename' => 'DRUPLICON.txt',
      'uri' => 'public://DRUPLICON.txt',
      'filemime' => 'text/plain',
      'status' => FILE_STATUS_PERMANENT,
61
    ];
62
    $uppercase_file = File::create($uppercase_values);
63
    file_put_contents($uppercase_file->getFileUri(), 'hello world');
64
65
    $violations = $uppercase_file->validate();
    $this->assertEqual(count($violations), 0, 'No violations when adding an URI with an existing filename in upper case.');
66
67
    $uppercase_file->save();

68
69
70
71
72
73
74
75
    // Ensure the database URI uniqueness constraint is triggered.
    $uppercase_file_duplicate = File::create($uppercase_values);
    file_put_contents($uppercase_file_duplicate->getFileUri(), 'hello world');
    $violations = $uppercase_file_duplicate->validate();
    $this->assertEqual(count($violations), 1);
    $this->assertEqual($violations[0]->getMessage(), t('The file %value already exists. Enter a unique file URI.', [
      '%value' => $uppercase_file_duplicate->getFileUri(),
    ]));
76
77
78
79
80
81
    // Ensure that file URI entity queries are case sensitive.
    $fids = \Drupal::entityQuery('file')
      ->condition('uri', $uppercase_file->getFileUri())
      ->execute();

    $this->assertEqual(1, count($fids));
82
    $this->assertEqual([$uppercase_file->id() => $uppercase_file->id()], $fids);
83

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
    // Save a file with zero bytes.
    $file = File::create([
      'uid' => 1,
      'filename' => 'no-druplicon.txt',
      'uri' => 'public://no-druplicon.txt',
      'filemime' => 'text/plain',
      'status' => FILE_STATUS_PERMANENT,
    ]);

    file_put_contents($file->getFileUri(), '');

    // Save it, inserting a new record.
    $file->save();

    // Check the file size was set to zero.
    $this->assertSame(0, $file->getSize());
100
  }
101

102
}