comparison core/modules/file/src/Tests/FileFieldValidateTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\file\Tests;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\file\Entity\File;
8
9 /**
10 * Tests validation functions such as file type, max file size, max size per
11 * node, and required.
12 *
13 * @group file
14 */
15 class FileFieldValidateTest extends FileFieldTestBase {
16
17 /**
18 * Tests the required property on file fields.
19 */
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);
26
27 $test_file = $this->getTestFile('text');
28
29 // 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 // 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]));
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, '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 // 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.');
63 }
64
65 /**
66 * Tests the max file size validator.
67 */
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']);
73
74 // 128KB.
75 $small_file = $this->getTestFile('text', 131072);
76 // 1.2MB
77 $large_file = $this->getTestFile('text', 1310720);
78
79 // Test uploading both a large and small file with different increments.
80 $sizes = [
81 '1M' => 1048576,
82 '1024K' => 1048576,
83 '1048576' => 1048576,
84 ];
85
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]);
89
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]));
97
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]));
102 }
103
104 // Turn off the max filesize.
105 $this->updateFileField($field_name, $type_name, ['max_filesize' => '']);
106
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())]));
114 }
115
116 /**
117 * Tests file extension checking.
118 */
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);
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 // 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.');
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 // 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.');
157 }
158
159 /**
160 * Checks that a file can always be removed if it does not pass validation.
161 */
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);
167
168 $test_file = $this->getTestFile('image');
169
170 // Disable extension checking.
171 $this->updateFileField($field_name, $type_name, ['file_extensions' => '']);
172
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.');
180
181 // Enable extension checking for text files.
182 $this->updateFileField($field_name, $type_name, ['file_extensions' => 'txt']);
183
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.');
188 }
189
190 }