FileFieldValidateTest.php 11 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\file\Functional;
4

5
use Drupal\Core\Field\FieldStorageDefinitionInterface;
6
use Drupal\field\Entity\FieldConfig;
7
use Drupal\file\Entity\File;
8

9
/**
10 11 12 13
 * Tests validation functions such as file type, max file size, max size per
 * node, and required.
 *
 * @group file
14 15 16 17 18 19
 */
class FileFieldValidateTest extends FileFieldTestBase {

  /**
   * Tests the required property on file fields.
   */
20
  public function testRequired() {
21
    $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
22
    $type_name = 'article';
23
    $field_name = strtolower($this->randomMachineName());
24
    $storage = $this->createFileField($field_name, 'node', $type_name, [], ['required' => '1']);
25
    $field = FieldConfig::loadByName('node', $type_name, $field_name);
26 27 28 29

    $test_file = $this->getTestFile('text');

    // Try to post a new node without uploading a file.
30
    $edit = [];
31
    $edit['title[0][value]'] = $this->randomMachineName();
32
    $this->drupalPostForm('node/add/' . $type_name, $edit, t('Save'));
33
    $this->assertRaw(t('@title field is required.', ['@title' => $field->getLabel()]), 'Node save failed when required file field was empty.');
34 35 36

    // Create a new node with the uploaded file.
    $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
37
    $this->assertTrue($nid !== FALSE, format_string('uploadNodeFile(@test_file, @field_name, @type_name) succeeded', ['@test_file' => $test_file->getFileUri(), '@field_name' => $field_name, '@type_name' => $type_name]));
38

39
    $node_storage->resetCache([$nid]);
40
    $node = $node_storage->load($nid);
41

42
    $node_file = File::load($node->{$field_name}->target_id);
43
    $this->assertFileExists($node_file->getFileUri(), 'File exists after uploading to the required field.');
44
    $this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required field.');
45 46

    // Try again with a multiple value field.
47
    $storage->delete();
48
    $this->createFileField($field_name, 'node', $type_name, ['cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED], ['required' => '1']);
49 50

    // Try to post a new node without uploading a file in the multivalue field.
51
    $edit = [];
52
    $edit['title[0][value]'] = $this->randomMachineName();
53
    $this->drupalPostForm('node/add/' . $type_name, $edit, t('Save'));
54
    $this->assertRaw(t('@title field is required.', ['@title' => $field->getLabel()]), 'Node save failed when required multiple value file field was empty.');
55 56 57

    // Create a new node with the uploaded file into the multivalue field.
    $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
58
    $node_storage->resetCache([$nid]);
59
    $node = $node_storage->load($nid);
60
    $node_file = File::load($node->{$field_name}->target_id);
61
    $this->assertFileExists($node_file->getFileUri(), 'File exists after uploading to the required multiple value field.');
62
    $this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required multiple value field.');
63 64 65 66 67
  }

  /**
   * Tests the max file size validator.
   */
68
  public function testFileMaxSize() {
69
    $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
70
    $type_name = 'article';
71
    $field_name = strtolower($this->randomMachineName());
72
    $this->createFileField($field_name, 'node', $type_name, [], ['required' => '1']);
73

74 75 76 77
    // 128KB.
    $small_file = $this->getTestFile('text', 131072);
    // 1.2MB
    $large_file = $this->getTestFile('text', 1310720);
78 79

    // Test uploading both a large and small file with different increments.
80
    $sizes = [
81 82 83
      '1M' => 1048576,
      '1024K' => 1048576,
      '1048576' => 1048576,
84
    ];
85 86 87

    foreach ($sizes as $max_filesize => $file_limit) {
      // Set the max file upload size.
88
      $this->updateFileField($field_name, $type_name, ['max_filesize' => $max_filesize]);
89 90 91

      // Create a new node with the small file, which should pass.
      $nid = $this->uploadNodeFile($small_file, $field_name, $type_name);
92
      $node_storage->resetCache([$nid]);
93
      $node = $node_storage->load($nid);
94
      $node_file = File::load($node->{$field_name}->target_id);
95
      $this->assertFileExists($node_file->getFileUri(), format_string('File exists after uploading a file (%filesize) under the max limit (%maxsize).', ['%filesize' => format_size($small_file->getSize()), '%maxsize' => $max_filesize]));
96
      $this->assertFileEntryExists($node_file, format_string('File entry exists after uploading a file (%filesize) under the max limit (%maxsize).', ['%filesize' => format_size($small_file->getSize()), '%maxsize' => $max_filesize]));
97 98

      // Check that uploading the large file fails (1M limit).
99
      $this->uploadNodeFile($large_file, $field_name, $type_name);
100 101
      $error_message = t('The file is %filesize exceeding the maximum file size of %maxsize.', ['%filesize' => format_size($large_file->getSize()), '%maxsize' => format_size($file_limit)]);
      $this->assertRaw($error_message, format_string('Node save failed when file (%filesize) exceeded the max upload size (%maxsize).', ['%filesize' => format_size($large_file->getSize()), '%maxsize' => $max_filesize]));
102 103 104
    }

    // Turn off the max filesize.
105
    $this->updateFileField($field_name, $type_name, ['max_filesize' => '']);
106 107 108

    // Upload the big file successfully.
    $nid = $this->uploadNodeFile($large_file, $field_name, $type_name);
109
    $node_storage->resetCache([$nid]);
110
    $node = $node_storage->load($nid);
111
    $node_file = File::load($node->{$field_name}->target_id);
112
    $this->assertFileExists($node_file->getFileUri(), format_string('File exists after uploading a file (%filesize) with no max limit.', ['%filesize' => format_size($large_file->getSize())]));
113
    $this->assertFileEntryExists($node_file, format_string('File entry exists after uploading a file (%filesize) with no max limit.', ['%filesize' => format_size($large_file->getSize())]));
114 115 116 117 118
  }

  /**
   * Tests file extension checking.
   */
119
  public function testFileExtension() {
120
    $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
121
    $type_name = 'article';
122
    $field_name = strtolower($this->randomMachineName());
123
    $this->createFileField($field_name, 'node', $type_name);
124 125

    $test_file = $this->getTestFile('image');
126
    list(, $test_file_extension) = explode('.', $test_file->getFilename());
127 128

    // Disable extension checking.
129
    $this->updateFileField($field_name, $type_name, ['file_extensions' => '']);
130 131 132

    // Check that the file can be uploaded with no extension checking.
    $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
133
    $node_storage->resetCache([$nid]);
134
    $node = $node_storage->load($nid);
135
    $node_file = File::load($node->{$field_name}->target_id);
136
    $this->assertFileExists($node_file->getFileUri(), 'File exists after uploading a file with no extension checking.');
137
    $this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with no extension checking.');
138 139

    // Enable extension checking for text files.
140
    $this->updateFileField($field_name, $type_name, ['file_extensions' => 'txt']);
141 142

    // Check that the file with the wrong extension cannot be uploaded.
143
    $this->uploadNodeFile($test_file, $field_name, $type_name);
144
    $error_message = t('Only files with the following extensions are allowed: %files-allowed.', ['%files-allowed' => 'txt']);
145
    $this->assertRaw($error_message, 'Node save failed when file uploaded with the wrong extension.');
146 147

    // Enable extension checking for text and image files.
148
    $this->updateFileField($field_name, $type_name, ['file_extensions' => "txt $test_file_extension"]);
149 150 151

    // Check that the file can be uploaded with extension checking.
    $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
152
    $node_storage->resetCache([$nid]);
153
    $node = $node_storage->load($nid);
154
    $node_file = File::load($node->{$field_name}->target_id);
155
    $this->assertFileExists($node_file->getFileUri(), 'File exists after uploading a file with extension checking.');
156
    $this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with extension checking.');
157
  }
158

159 160 161 162
  /**
   * Checks that a file can always be removed if it does not pass validation.
   */
  public function testFileRemoval() {
163
    $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
164 165 166 167 168 169 170
    $type_name = 'article';
    $field_name = 'file_test';
    $this->createFileField($field_name, 'node', $type_name);

    $test_file = $this->getTestFile('image');

    // Disable extension checking.
171
    $this->updateFileField($field_name, $type_name, ['file_extensions' => '']);
172 173 174

    // Check that the file can be uploaded with no extension checking.
    $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
175
    $node_storage->resetCache([$nid]);
176 177
    $node = $node_storage->load($nid);
    $node_file = File::load($node->{$field_name}->target_id);
178
    $this->assertFileExists($node_file->getFileUri(), 'File exists after uploading a file with no extension checking.');
179 180 181
    $this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with no extension checking.');

    // Enable extension checking for text files.
182
    $this->updateFileField($field_name, $type_name, ['file_extensions' => 'txt']);
183 184 185 186 187 188 189

    // Check that the file can still be removed.
    $this->removeNodeFile($nid);
    $this->assertNoText('Only files with the following extensions are allowed: txt.');
    $this->assertText('Article ' . $node->getTitle() . ' has been updated.');
  }

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
  /**
   * Tests deprecation of the assertFileExists and assertFileNotExists methods.
   *
   * @group legacy
   *
   * @expectedDeprecation Passing a File entity as $file argument to FileFieldTestBase::assertFileExists is deprecated in drupal:8.8.0. It will be removed from drupal:9.0.0. Instead, pass the File entity URI via File::getFileUri(). See https://www.drupal.org/node/3057326
   * @expectedDeprecation Passing a File entity as $file argument to FileFieldTestBase::assertFileNotExists is deprecated in drupal:8.8.0. It will be removed from drupal:9.0.0. Instead, pass the File entity URI via File::getFileUri(). See https://www.drupal.org/node/3057326
   */
  public function testAssertFileExistsDeprecation() {
    $node_storage = $this->container->get('entity.manager')->getStorage('node');
    $type_name = 'article';
    $field_name = 'file_test';
    $this->createFileField($field_name, 'node', $type_name);

    $test_file = $this->getTestFile('image');

    // Disable extension checking.
    $this->updateFileField($field_name, $type_name, ['file_extensions' => '']);

    // Check that the file can be uploaded with no extension checking.
    $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
    $node_storage->resetCache([$nid]);
    $node = $node_storage->load($nid);
    $node_file = File::load($node->{$field_name}->target_id);
    $this->assertFileExists($node_file, 'File exists after uploading a file with no extension checking.');
    unlink($node_file->getFileUri());
    $this->assertFileNotExists($node_file, 'File does not exists after having been deleted.');
  }

219
}