Chris@0: container->get('entity.manager')->getStorage('node'); Chris@0: $type_name = 'article'; Chris@0: $field_name = strtolower($this->randomMachineName()); Chris@0: $storage = $this->createFileField($field_name, 'node', $type_name, [], ['required' => '1']); Chris@0: $field = FieldConfig::loadByName('node', $type_name, $field_name); Chris@0: Chris@0: $test_file = $this->getTestFile('text'); Chris@0: Chris@0: // Try to post a new node without uploading a file. Chris@0: $edit = []; Chris@0: $edit['title[0][value]'] = $this->randomMachineName(); Chris@0: $this->drupalPostForm('node/add/' . $type_name, $edit, t('Save')); Chris@0: $this->assertRaw(t('@title field is required.', ['@title' => $field->getLabel()]), 'Node save failed when required file field was empty.'); Chris@0: Chris@0: // Create a new node with the uploaded file. Chris@0: $nid = $this->uploadNodeFile($test_file, $field_name, $type_name); Chris@0: $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: Chris@0: $node_storage->resetCache([$nid]); Chris@0: $node = $node_storage->load($nid); Chris@0: Chris@0: $node_file = File::load($node->{$field_name}->target_id); Chris@0: $this->assertFileExists($node_file, 'File exists after uploading to the required field.'); Chris@0: $this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required field.'); Chris@0: Chris@0: // Try again with a multiple value field. Chris@0: $storage->delete(); Chris@0: $this->createFileField($field_name, 'node', $type_name, ['cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED], ['required' => '1']); Chris@0: Chris@0: // Try to post a new node without uploading a file in the multivalue field. Chris@0: $edit = []; Chris@0: $edit['title[0][value]'] = $this->randomMachineName(); Chris@0: $this->drupalPostForm('node/add/' . $type_name, $edit, t('Save')); Chris@0: $this->assertRaw(t('@title field is required.', ['@title' => $field->getLabel()]), 'Node save failed when required multiple value file field was empty.'); Chris@0: Chris@0: // Create a new node with the uploaded file into the multivalue field. Chris@0: $nid = $this->uploadNodeFile($test_file, $field_name, $type_name); Chris@0: $node_storage->resetCache([$nid]); Chris@0: $node = $node_storage->load($nid); Chris@0: $node_file = File::load($node->{$field_name}->target_id); Chris@0: $this->assertFileExists($node_file, 'File exists after uploading to the required multiple value field.'); Chris@0: $this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required multiple value field.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the max file size validator. Chris@0: */ Chris@0: public function testFileMaxSize() { Chris@0: $node_storage = $this->container->get('entity.manager')->getStorage('node'); Chris@0: $type_name = 'article'; Chris@0: $field_name = strtolower($this->randomMachineName()); Chris@0: $this->createFileField($field_name, 'node', $type_name, [], ['required' => '1']); Chris@0: Chris@0: // 128KB. Chris@0: $small_file = $this->getTestFile('text', 131072); Chris@0: // 1.2MB Chris@0: $large_file = $this->getTestFile('text', 1310720); Chris@0: Chris@0: // Test uploading both a large and small file with different increments. Chris@0: $sizes = [ Chris@0: '1M' => 1048576, Chris@0: '1024K' => 1048576, Chris@0: '1048576' => 1048576, Chris@0: ]; Chris@0: Chris@0: foreach ($sizes as $max_filesize => $file_limit) { Chris@0: // Set the max file upload size. Chris@0: $this->updateFileField($field_name, $type_name, ['max_filesize' => $max_filesize]); Chris@0: Chris@0: // Create a new node with the small file, which should pass. Chris@0: $nid = $this->uploadNodeFile($small_file, $field_name, $type_name); Chris@0: $node_storage->resetCache([$nid]); Chris@0: $node = $node_storage->load($nid); Chris@0: $node_file = File::load($node->{$field_name}->target_id); Chris@0: $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: $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: Chris@0: // Check that uploading the large file fails (1M limit). Chris@0: $this->uploadNodeFile($large_file, $field_name, $type_name); Chris@0: $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: $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: } Chris@0: Chris@0: // Turn off the max filesize. Chris@0: $this->updateFileField($field_name, $type_name, ['max_filesize' => '']); Chris@0: Chris@0: // Upload the big file successfully. Chris@0: $nid = $this->uploadNodeFile($large_file, $field_name, $type_name); Chris@0: $node_storage->resetCache([$nid]); Chris@0: $node = $node_storage->load($nid); Chris@0: $node_file = File::load($node->{$field_name}->target_id); Chris@0: $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: $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: } Chris@0: Chris@0: /** Chris@0: * Tests file extension checking. Chris@0: */ Chris@0: public function testFileExtension() { Chris@0: $node_storage = $this->container->get('entity.manager')->getStorage('node'); Chris@0: $type_name = 'article'; Chris@0: $field_name = strtolower($this->randomMachineName()); Chris@0: $this->createFileField($field_name, 'node', $type_name); Chris@0: Chris@0: $test_file = $this->getTestFile('image'); Chris@0: list(, $test_file_extension) = explode('.', $test_file->getFilename()); Chris@0: Chris@0: // Disable extension checking. Chris@0: $this->updateFileField($field_name, $type_name, ['file_extensions' => '']); Chris@0: Chris@0: // Check that the file can be uploaded with no extension checking. Chris@0: $nid = $this->uploadNodeFile($test_file, $field_name, $type_name); Chris@0: $node_storage->resetCache([$nid]); Chris@0: $node = $node_storage->load($nid); Chris@0: $node_file = File::load($node->{$field_name}->target_id); Chris@0: $this->assertFileExists($node_file, 'File exists after uploading a file with no extension checking.'); Chris@0: $this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with no extension checking.'); Chris@0: Chris@0: // Enable extension checking for text files. Chris@0: $this->updateFileField($field_name, $type_name, ['file_extensions' => 'txt']); Chris@0: Chris@0: // Check that the file with the wrong extension cannot be uploaded. Chris@0: $this->uploadNodeFile($test_file, $field_name, $type_name); Chris@0: $error_message = t('Only files with the following extensions are allowed: %files-allowed.', ['%files-allowed' => 'txt']); Chris@0: $this->assertRaw($error_message, 'Node save failed when file uploaded with the wrong extension.'); Chris@0: Chris@0: // Enable extension checking for text and image files. Chris@0: $this->updateFileField($field_name, $type_name, ['file_extensions' => "txt $test_file_extension"]); Chris@0: Chris@0: // Check that the file can be uploaded with extension checking. Chris@0: $nid = $this->uploadNodeFile($test_file, $field_name, $type_name); Chris@0: $node_storage->resetCache([$nid]); Chris@0: $node = $node_storage->load($nid); Chris@0: $node_file = File::load($node->{$field_name}->target_id); Chris@0: $this->assertFileExists($node_file, 'File exists after uploading a file with extension checking.'); Chris@0: $this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with extension checking.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that a file can always be removed if it does not pass validation. Chris@0: */ Chris@0: public function testFileRemoval() { Chris@0: $node_storage = $this->container->get('entity.manager')->getStorage('node'); Chris@0: $type_name = 'article'; Chris@0: $field_name = 'file_test'; Chris@0: $this->createFileField($field_name, 'node', $type_name); Chris@0: Chris@0: $test_file = $this->getTestFile('image'); Chris@0: Chris@0: // Disable extension checking. Chris@0: $this->updateFileField($field_name, $type_name, ['file_extensions' => '']); Chris@0: Chris@0: // Check that the file can be uploaded with no extension checking. Chris@0: $nid = $this->uploadNodeFile($test_file, $field_name, $type_name); Chris@0: $node_storage->resetCache([$nid]); Chris@0: $node = $node_storage->load($nid); Chris@0: $node_file = File::load($node->{$field_name}->target_id); Chris@0: $this->assertFileExists($node_file, 'File exists after uploading a file with no extension checking.'); Chris@0: $this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with no extension checking.'); Chris@0: Chris@0: // Enable extension checking for text files. Chris@0: $this->updateFileField($field_name, $type_name, ['file_extensions' => 'txt']); Chris@0: Chris@0: // Check that the file can still be removed. Chris@0: $this->removeNodeFile($nid); Chris@0: $this->assertNoText('Only files with the following extensions are allowed: txt.'); Chris@0: $this->assertText('Article ' . $node->getTitle() . ' has been updated.'); Chris@0: } Chris@0: Chris@0: }