View source
<?php
namespace Drupal\Tests\minisite\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\field\Entity\FieldConfig;
use Drupal\file\Entity\File;
use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
class FieldValidationTest extends MinisiteTestBase {
use FieldUiTestTrait;
protected static $modules = [
'block',
];
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('system_breadcrumb_block');
}
public function testValidateRequired() {
$node_storage = $this->container
->get('entity_type.manager')
->getStorage('node');
$field_name = strtolower($this
->randomMachineName());
$this
->createMinisiteField($field_name, 'node', $this->contentType, [], [
'required' => '1',
]);
$field = FieldConfig::loadByName('node', $this->contentType, $field_name);
$edit = [];
$edit['title[0][value]'] = $this
->randomMachineName();
$this
->drupalPostForm('node/add/' . $this->contentType, $edit, $this
->t('Save'));
$this
->assertRaw($this
->t('@title field is required.', [
'@title' => $field
->getLabel(),
]));
$test_file = $this
->getTestArchiveValid();
$nid = $this
->uploadNodeFile($test_file, $field_name, $this->contentType);
$this
->assertTrue($nid !== FALSE, new FormattableMarkup('uploadNodeFile(@test_file, @field_name, @type_name) succeeded', [
'@test_file' => $test_file
->getFileUri(),
'@field_name' => $field_name,
'@type_name' => $this->contentType,
]));
$node_storage
->resetCache([
$nid,
]);
$node = $node_storage
->load($nid);
$node_file = File::load($node->{$field_name}->target_id);
$this
->assertFileUriExists($node_file, 'File exists after uploading to the required field.');
$this
->assertFileEntryExists($node_file, 'File entry exists after uploading to the required field.');
}
public function testValidateArchiveFormat() {
$field_name = strtolower($this
->randomMachineName());
$this
->createMinisiteField($field_name, 'node', $this->contentType, [], [
'required' => '1',
]);
$test_file = $this
->getTestArchiveInvalidFormat();
$this
->uploadNodeFile($test_file, $field_name, $this->contentType);
$this
->assertRaw($this
->t('The specified file %filename could not be uploaded.', [
'%filename' => $test_file
->getFilename(),
]));
$this
->assertRaw($this
->t('File @filename is not an archive file.', [
'@filename' => $test_file
->getFilename(),
]));
}
public function testFieldAllowedExtensions() {
$field_name = strtolower($this
->randomMachineName());
$this
->createMinisiteField($field_name, 'node', $this->contentType);
$path = 'admin/structure/types/manage/' . $this->contentType . '/fields/node.' . $this->contentType . '.' . $field_name;
$allowed_extensions = 'html, htm, js, css, png';
$edit['settings[minisite_extensions]'] = $allowed_extensions;
$this
->drupalPostForm($path, $edit, $this
->t('Save settings'));
$this
->assertRaw($this
->t('Saved %field configuration.', [
'%field' => $field_name,
]));
$denied_extensions = 'scr';
$edit['settings[minisite_extensions]'] = $allowed_extensions . ' ' . $denied_extensions;
$this
->drupalPostForm($path, $edit, $this
->t('Save settings'));
$this
->assertRaw($this
->t('The list of allowed extensions is not valid, be sure to not include %ext extension(s).', [
'%ext' => $denied_extensions,
]));
$denied_extensions = 'scr exe';
$edit['settings[minisite_extensions]'] = $allowed_extensions . ' ' . $denied_extensions;
$this
->drupalPostForm($path, $edit, $this
->t('Save settings'));
$this
->assertRaw($this
->t('The list of allowed extensions is not valid, be sure to not include %ext extension(s).', [
'%ext' => str_replace(' ', ', ', $denied_extensions),
]));
}
}