3 namespace Drupal\Tests\file\Functional;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\file\Entity\File;
10 * Tests validation functions such as file type, max file size, max size per
15 class FileFieldValidateTest extends FileFieldTestBase {
18 * Tests the required property on file fields.
20 public function testRequired() {
21 $node_storage = $this->container->get('entity.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);
27 $test_file = $this->getTestFile('text');
29 // Try to post a new node without uploading a file.
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.');
35 // Create a new node with the uploaded file.
36 $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]));
39 $node_storage->resetCache([$nid]);
40 $node = $node_storage->load($nid);
42 $node_file = File::load($node->{$field_name}->target_id);
43 $this->assertFileExists($node_file, 'File exists after uploading to the required field.');
44 $this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required field.');
46 // Try again with a multiple value field.
48 $this->createFileField($field_name, 'node', $type_name, ['cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED], ['required' => '1']);
50 // Try to post a new node without uploading a file in the multivalue field.
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.');
56 // Create a new node with the uploaded file into the multivalue field.
57 $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, '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.');
66 * Tests the max file size validator.
68 public function testFileMaxSize() {
69 $node_storage = $this->container->get('entity.manager')->getStorage('node');
70 $type_name = 'article';
71 $field_name = strtolower($this->randomMachineName());
72 $this->createFileField($field_name, 'node', $type_name, [], ['required' => '1']);
75 $small_file = $this->getTestFile('text', 131072);
77 $large_file = $this->getTestFile('text', 1310720);
79 // Test uploading both a large and small file with different increments.
86 foreach ($sizes as $max_filesize => $file_limit) {
87 // Set the max file upload size.
88 $this->updateFileField($field_name, $type_name, ['max_filesize' => $max_filesize]);
90 // Create a new node with the small file, which should pass.
91 $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, 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]));
98 // Check that uploading the large file fails (1M limit).
99 $this->uploadNodeFile($large_file, $field_name, $type_name);
100 $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)]);
101 $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]));
104 // Turn off the max filesize.
105 $this->updateFileField($field_name, $type_name, ['max_filesize' => '']);
107 // Upload the big file successfully.
108 $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, 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())]));
117 * Tests file extension checking.
119 public function testFileExtension() {
120 $node_storage = $this->container->get('entity.manager')->getStorage('node');
121 $type_name = 'article';
122 $field_name = strtolower($this->randomMachineName());
123 $this->createFileField($field_name, 'node', $type_name);
125 $test_file = $this->getTestFile('image');
126 list(, $test_file_extension) = explode('.', $test_file->getFilename());
128 // Disable extension checking.
129 $this->updateFileField($field_name, $type_name, ['file_extensions' => '']);
131 // Check that the file can be uploaded with no extension checking.
132 $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, '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.');
139 // Enable extension checking for text files.
140 $this->updateFileField($field_name, $type_name, ['file_extensions' => 'txt']);
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.');
147 // Enable extension checking for text and image files.
148 $this->updateFileField($field_name, $type_name, ['file_extensions' => "txt $test_file_extension"]);
150 // Check that the file can be uploaded with extension checking.
151 $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, 'File exists after uploading a file with extension checking.');
156 $this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with extension checking.');
160 * Checks that a file can always be removed if it does not pass validation.
162 public function testFileRemoval() {
163 $node_storage = $this->container->get('entity.manager')->getStorage('node');
164 $type_name = 'article';
165 $field_name = 'file_test';
166 $this->createFileField($field_name, 'node', $type_name);
168 $test_file = $this->getTestFile('image');
170 // Disable extension checking.
171 $this->updateFileField($field_name, $type_name, ['file_extensions' => '']);
173 // Check that the file can be uploaded with no extension checking.
174 $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
175 $node_storage->resetCache([$nid]);
176 $node = $node_storage->load($nid);
177 $node_file = File::load($node->{$field_name}->target_id);
178 $this->assertFileExists($node_file, 'File exists after uploading a file with no extension checking.');
179 $this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with no extension checking.');
181 // Enable extension checking for text files.
182 $this->updateFileField($field_name, $type_name, ['file_extensions' => 'txt']);
184 // Check that the file can still be removed.
185 $this->removeNodeFile($nid);
186 $this->assertNoText('Only files with the following extensions are allowed: txt.');
187 $this->assertText('Article ' . $node->getTitle() . ' has been updated.');