Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\file\Tests;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
Chris@0
|
6 use Drupal\field\Entity\FieldConfig;
|
Chris@0
|
7 use Drupal\file\Entity\File;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Tests validation functions such as file type, max file size, max size per
|
Chris@0
|
11 * node, and required.
|
Chris@0
|
12 *
|
Chris@0
|
13 * @group file
|
Chris@0
|
14 */
|
Chris@0
|
15 class FileFieldValidateTest extends FileFieldTestBase {
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * Tests the required property on file fields.
|
Chris@0
|
19 */
|
Chris@0
|
20 public function testRequired() {
|
Chris@0
|
21 $node_storage = $this->container->get('entity.manager')->getStorage('node');
|
Chris@0
|
22 $type_name = 'article';
|
Chris@0
|
23 $field_name = strtolower($this->randomMachineName());
|
Chris@0
|
24 $storage = $this->createFileField($field_name, 'node', $type_name, [], ['required' => '1']);
|
Chris@0
|
25 $field = FieldConfig::loadByName('node', $type_name, $field_name);
|
Chris@0
|
26
|
Chris@0
|
27 $test_file = $this->getTestFile('text');
|
Chris@0
|
28
|
Chris@0
|
29 // Try to post a new node without uploading a file.
|
Chris@0
|
30 $edit = [];
|
Chris@0
|
31 $edit['title[0][value]'] = $this->randomMachineName();
|
Chris@0
|
32 $this->drupalPostForm('node/add/' . $type_name, $edit, t('Save'));
|
Chris@0
|
33 $this->assertRaw(t('@title field is required.', ['@title' => $field->getLabel()]), 'Node save failed when required file field was empty.');
|
Chris@0
|
34
|
Chris@0
|
35 // Create a new node with the uploaded file.
|
Chris@0
|
36 $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
|
Chris@0
|
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]));
|
Chris@0
|
38
|
Chris@0
|
39 $node_storage->resetCache([$nid]);
|
Chris@0
|
40 $node = $node_storage->load($nid);
|
Chris@0
|
41
|
Chris@0
|
42 $node_file = File::load($node->{$field_name}->target_id);
|
Chris@0
|
43 $this->assertFileExists($node_file, 'File exists after uploading to the required field.');
|
Chris@0
|
44 $this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required field.');
|
Chris@0
|
45
|
Chris@0
|
46 // Try again with a multiple value field.
|
Chris@0
|
47 $storage->delete();
|
Chris@0
|
48 $this->createFileField($field_name, 'node', $type_name, ['cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED], ['required' => '1']);
|
Chris@0
|
49
|
Chris@0
|
50 // Try to post a new node without uploading a file in the multivalue field.
|
Chris@0
|
51 $edit = [];
|
Chris@0
|
52 $edit['title[0][value]'] = $this->randomMachineName();
|
Chris@0
|
53 $this->drupalPostForm('node/add/' . $type_name, $edit, t('Save'));
|
Chris@0
|
54 $this->assertRaw(t('@title field is required.', ['@title' => $field->getLabel()]), 'Node save failed when required multiple value file field was empty.');
|
Chris@0
|
55
|
Chris@0
|
56 // Create a new node with the uploaded file into the multivalue field.
|
Chris@0
|
57 $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
|
Chris@0
|
58 $node_storage->resetCache([$nid]);
|
Chris@0
|
59 $node = $node_storage->load($nid);
|
Chris@0
|
60 $node_file = File::load($node->{$field_name}->target_id);
|
Chris@0
|
61 $this->assertFileExists($node_file, 'File exists after uploading to the required multiple value field.');
|
Chris@0
|
62 $this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required multiple value field.');
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 /**
|
Chris@0
|
66 * Tests the max file size validator.
|
Chris@0
|
67 */
|
Chris@0
|
68 public function testFileMaxSize() {
|
Chris@0
|
69 $node_storage = $this->container->get('entity.manager')->getStorage('node');
|
Chris@0
|
70 $type_name = 'article';
|
Chris@0
|
71 $field_name = strtolower($this->randomMachineName());
|
Chris@0
|
72 $this->createFileField($field_name, 'node', $type_name, [], ['required' => '1']);
|
Chris@0
|
73
|
Chris@0
|
74 // 128KB.
|
Chris@0
|
75 $small_file = $this->getTestFile('text', 131072);
|
Chris@0
|
76 // 1.2MB
|
Chris@0
|
77 $large_file = $this->getTestFile('text', 1310720);
|
Chris@0
|
78
|
Chris@0
|
79 // Test uploading both a large and small file with different increments.
|
Chris@0
|
80 $sizes = [
|
Chris@0
|
81 '1M' => 1048576,
|
Chris@0
|
82 '1024K' => 1048576,
|
Chris@0
|
83 '1048576' => 1048576,
|
Chris@0
|
84 ];
|
Chris@0
|
85
|
Chris@0
|
86 foreach ($sizes as $max_filesize => $file_limit) {
|
Chris@0
|
87 // Set the max file upload size.
|
Chris@0
|
88 $this->updateFileField($field_name, $type_name, ['max_filesize' => $max_filesize]);
|
Chris@0
|
89
|
Chris@0
|
90 // Create a new node with the small file, which should pass.
|
Chris@0
|
91 $nid = $this->uploadNodeFile($small_file, $field_name, $type_name);
|
Chris@0
|
92 $node_storage->resetCache([$nid]);
|
Chris@0
|
93 $node = $node_storage->load($nid);
|
Chris@0
|
94 $node_file = File::load($node->{$field_name}->target_id);
|
Chris@0
|
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]));
|
Chris@0
|
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]));
|
Chris@0
|
97
|
Chris@0
|
98 // Check that uploading the large file fails (1M limit).
|
Chris@0
|
99 $this->uploadNodeFile($large_file, $field_name, $type_name);
|
Chris@0
|
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)]);
|
Chris@0
|
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]));
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 // Turn off the max filesize.
|
Chris@0
|
105 $this->updateFileField($field_name, $type_name, ['max_filesize' => '']);
|
Chris@0
|
106
|
Chris@0
|
107 // Upload the big file successfully.
|
Chris@0
|
108 $nid = $this->uploadNodeFile($large_file, $field_name, $type_name);
|
Chris@0
|
109 $node_storage->resetCache([$nid]);
|
Chris@0
|
110 $node = $node_storage->load($nid);
|
Chris@0
|
111 $node_file = File::load($node->{$field_name}->target_id);
|
Chris@0
|
112 $this->assertFileExists($node_file, format_string('File exists after uploading a file (%filesize) with no max limit.', ['%filesize' => format_size($large_file->getSize())]));
|
Chris@0
|
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())]));
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 /**
|
Chris@0
|
117 * Tests file extension checking.
|
Chris@0
|
118 */
|
Chris@0
|
119 public function testFileExtension() {
|
Chris@0
|
120 $node_storage = $this->container->get('entity.manager')->getStorage('node');
|
Chris@0
|
121 $type_name = 'article';
|
Chris@0
|
122 $field_name = strtolower($this->randomMachineName());
|
Chris@0
|
123 $this->createFileField($field_name, 'node', $type_name);
|
Chris@0
|
124
|
Chris@0
|
125 $test_file = $this->getTestFile('image');
|
Chris@0
|
126 list(, $test_file_extension) = explode('.', $test_file->getFilename());
|
Chris@0
|
127
|
Chris@0
|
128 // Disable extension checking.
|
Chris@0
|
129 $this->updateFileField($field_name, $type_name, ['file_extensions' => '']);
|
Chris@0
|
130
|
Chris@0
|
131 // Check that the file can be uploaded with no extension checking.
|
Chris@0
|
132 $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
|
Chris@0
|
133 $node_storage->resetCache([$nid]);
|
Chris@0
|
134 $node = $node_storage->load($nid);
|
Chris@0
|
135 $node_file = File::load($node->{$field_name}->target_id);
|
Chris@0
|
136 $this->assertFileExists($node_file, 'File exists after uploading a file with no extension checking.');
|
Chris@0
|
137 $this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with no extension checking.');
|
Chris@0
|
138
|
Chris@0
|
139 // Enable extension checking for text files.
|
Chris@0
|
140 $this->updateFileField($field_name, $type_name, ['file_extensions' => 'txt']);
|
Chris@0
|
141
|
Chris@0
|
142 // Check that the file with the wrong extension cannot be uploaded.
|
Chris@0
|
143 $this->uploadNodeFile($test_file, $field_name, $type_name);
|
Chris@0
|
144 $error_message = t('Only files with the following extensions are allowed: %files-allowed.', ['%files-allowed' => 'txt']);
|
Chris@0
|
145 $this->assertRaw($error_message, 'Node save failed when file uploaded with the wrong extension.');
|
Chris@0
|
146
|
Chris@0
|
147 // Enable extension checking for text and image files.
|
Chris@0
|
148 $this->updateFileField($field_name, $type_name, ['file_extensions' => "txt $test_file_extension"]);
|
Chris@0
|
149
|
Chris@0
|
150 // Check that the file can be uploaded with extension checking.
|
Chris@0
|
151 $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
|
Chris@0
|
152 $node_storage->resetCache([$nid]);
|
Chris@0
|
153 $node = $node_storage->load($nid);
|
Chris@0
|
154 $node_file = File::load($node->{$field_name}->target_id);
|
Chris@0
|
155 $this->assertFileExists($node_file, 'File exists after uploading a file with extension checking.');
|
Chris@0
|
156 $this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with extension checking.');
|
Chris@0
|
157 }
|
Chris@0
|
158
|
Chris@0
|
159 /**
|
Chris@0
|
160 * Checks that a file can always be removed if it does not pass validation.
|
Chris@0
|
161 */
|
Chris@0
|
162 public function testFileRemoval() {
|
Chris@0
|
163 $node_storage = $this->container->get('entity.manager')->getStorage('node');
|
Chris@0
|
164 $type_name = 'article';
|
Chris@0
|
165 $field_name = 'file_test';
|
Chris@0
|
166 $this->createFileField($field_name, 'node', $type_name);
|
Chris@0
|
167
|
Chris@0
|
168 $test_file = $this->getTestFile('image');
|
Chris@0
|
169
|
Chris@0
|
170 // Disable extension checking.
|
Chris@0
|
171 $this->updateFileField($field_name, $type_name, ['file_extensions' => '']);
|
Chris@0
|
172
|
Chris@0
|
173 // Check that the file can be uploaded with no extension checking.
|
Chris@0
|
174 $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
|
Chris@0
|
175 $node_storage->resetCache([$nid]);
|
Chris@0
|
176 $node = $node_storage->load($nid);
|
Chris@0
|
177 $node_file = File::load($node->{$field_name}->target_id);
|
Chris@0
|
178 $this->assertFileExists($node_file, 'File exists after uploading a file with no extension checking.');
|
Chris@0
|
179 $this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with no extension checking.');
|
Chris@0
|
180
|
Chris@0
|
181 // Enable extension checking for text files.
|
Chris@0
|
182 $this->updateFileField($field_name, $type_name, ['file_extensions' => 'txt']);
|
Chris@0
|
183
|
Chris@0
|
184 // Check that the file can still be removed.
|
Chris@0
|
185 $this->removeNodeFile($nid);
|
Chris@0
|
186 $this->assertNoText('Only files with the following extensions are allowed: txt.');
|
Chris@0
|
187 $this->assertText('Article ' . $node->getTitle() . ' has been updated.');
|
Chris@0
|
188 }
|
Chris@0
|
189
|
Chris@0
|
190 }
|