summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/file/tests/src/Kernel/FileItemValidationTest.php
blob: 2b843d6d3452c5dba97cc9cf3def3425f6456c11 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php

declare(strict_types=1);

namespace Drupal\Tests\file\Kernel;

use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\file\Entity\File;
use Drupal\KernelTests\KernelTestBase;
use Drupal\user\Entity\User;
use org\bovigo\vfs\vfsStream;

/**
 * Tests that files referenced in file and image fields are always validated.
 *
 * @group file
 */
class FileItemValidationTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'file',
    'image',
    'entity_test',
    'field',
    'user',
    'system',
  ];

  /**
   * A user.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $user;

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();

    $this->installEntitySchema('entity_test');
    $this->installEntitySchema('user');
    $this->installEntitySchema('file');
    $this->installSchema('file', 'file_usage');

    $this->user = User::create([
      'name' => 'username',
      'status' => 1,
    ]);
    $this->user->save();
    $this->container->get('current_user')->setAccount($this->user);
  }

  /**
   * @covers \Drupal\file\Plugin\Validation\Constraint\FileValidationConstraint
   * @covers \Drupal\file\Plugin\Validation\Constraint\FileValidationConstraintValidator
   * @dataProvider getFileTypes
   */
  public function testFileValidationConstraint($file_type): void {
    $field_storage = FieldStorageConfig::create([
      'field_name' => 'field_test_file',
      'entity_type' => 'entity_test',
      'type' => $file_type,
    ]);
    $field_storage->save();

    $field = FieldConfig::create([
      'field_name' => 'field_test_file',
      'entity_type' => 'entity_test',
      'bundle' => 'entity_test',
      'settings' => [
        'max_filesize' => '2k',
        'file_extensions' => 'jpg|png',
      ],
    ]);
    $field->save();

    vfsStream::setup('drupal_root');
    vfsStream::create([
      'sites' => [
        'default' => [
          'files' => [
            'test.txt' => str_repeat('a', 3000),
          ],
        ],
      ],
    ]);

    // Test for max filesize.
    $file = File::create([
      'uri' => 'vfs://drupal_root/sites/default/files/test.txt',
      'uid' => $this->user->id(),
    ]);
    $file->setPermanent();
    $file->save();

    $entity_test = EntityTest::create([
      'uid' => $this->user->id(),
      'field_test_file' => [
        'target_id' => $file->id(),
      ],
    ]);

    // Enforce the file to be new as file size is checked only for new files.
    $entity_test->field_test_file->entity->enforceIsNew();
    $result = $entity_test->validate();
    $this->assertCount(2, $result);
    $this->assertEquals('field_test_file.0', $result->get(0)->getPropertyPath());
    $this->assertEquals('The file is <em class="placeholder">2.93 KB</em> exceeding the maximum file size of <em class="placeholder">2 KB</em>.', (string) $result->get(0)->getMessage());
    $this->assertEquals('field_test_file.0', $result->get(1)->getPropertyPath());
    $this->assertEquals('Only files with the following extensions are allowed: <em class="placeholder">jpg|png</em>.', (string) $result->get(1)->getMessage());

    // File size is not checked for already existing files.
    $entity_test->field_test_file->entity->enforceIsNew(FALSE);
    $result = $entity_test->validate();
    $this->assertCount(1, $result);
    $this->assertEquals('field_test_file.0', $result->get(0)->getPropertyPath());
    $this->assertEquals('Only files with the following extensions are allowed: <em class="placeholder">jpg|png</em>.', (string) $result->get(0)->getMessage());

    // Refer to a file that does not exist.
    $entity_test = EntityTest::create([
      'uid' => $this->user->id(),
      'field_test_file' => [
        'target_id' => 2,
      ],
    ]);
    $result = $entity_test->validate();
    $this->assertCount(1, $result);
    $this->assertEquals('field_test_file.0.target_id', $result->get(0)->getPropertyPath());
    $this->assertEquals('The referenced entity (<em class="placeholder">file</em>: <em class="placeholder">2</em>) does not exist.', (string) $result->get(0)->getMessage());
  }

  /**
   * Provides a list of file types to test.
   */
  public static function getFileTypes() {
    return [['file'], ['image']];
  }

}