summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/file/tests/src/Kernel/SaveTest.php
blob: 674236bbd1a58aea91ab10f61b080108ff7c3737 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php

declare(strict_types=1);

namespace Drupal\Tests\file\Kernel;

use Drupal\file\Entity\File;
use Drupal\file_test\FileTestHelper;
use Drupal\Tests\user\Traits\UserCreationTrait;

/**
 * File saving tests.
 *
 * @group file
 */
class SaveTest extends FileManagedUnitTestBase {

  use UserCreationTrait;

  /**
   * Tests the saving process of file entities.
   */
  public function testFileSave(): void {
    $account = $this->createUser();
    // Create a new file entity.
    $file = File::create([
      'uid' => $account->id(),
      '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.');

    // Resave the file, updating the existing record.
    FileTestHelper::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.');
    $this->assertEquals($file->isPermanent(), $loaded_file->isPermanent(), 'Status was saved correctly.');
    $this->assertEquals('en', $loaded_file->langcode->value, '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.
    $uppercase_values = [
      'uid' => $account->id(),
      'filename' => 'DRUPLICON.txt',
      'uri' => 'public://DRUPLICON.txt',
      'filemime' => 'text/plain',
    ];
    $file->setPermanent();
    $uppercase_file = File::create($uppercase_values);
    file_put_contents($uppercase_file->getFileUri(), 'hello world');
    $violations = $uppercase_file->validate();
    $this->assertCount(0, $violations, 'No violations when adding an URI with an existing filename in upper case.');
    $uppercase_file->save();

    // 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->assertCount(1, $violations);
    $this->assertEquals(sprintf('The file %s already exists. Enter a unique file URI.', $uppercase_file_duplicate->getFileUri()), $violations[0]->getMessage());
    // Ensure that file URI entity queries are case sensitive.
    $fids = \Drupal::entityQuery('file')
      ->accessCheck(FALSE)
      ->condition('uri', $uppercase_file->getFileUri())
      ->execute();

    $this->assertCount(1, $fids);
    $this->assertEquals([$uppercase_file->id() => $uppercase_file->id()], $fids);

    // Save a file with zero bytes.
    $file = File::create([
      'uid' => $account->id(),
      'filename' => 'no-druplicon.txt',
      'uri' => 'public://no-druplicon.txt',
      'filemime' => 'text/plain',
    ]);
    $file->setPermanent();

    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());
  }

}