View source
<?php
namespace Drupal\FunctionalTests\Entity;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\TestFileCreationTrait;
class ContentEntityFormFieldValidationFilteringTest extends BrowserTestBase {
use TestFileCreationTrait;
protected $entityTypeId;
protected $fieldNameSingle;
protected $fieldNameMultiple;
protected $fieldNameFile;
protected static $modules = [
'entity_test',
'field_test',
'file',
'image',
];
protected $defaultTheme = 'classy';
protected function setUp() : void {
parent::setUp();
$web_user = $this
->drupalCreateUser([
'administer entity_test content',
]);
$this
->drupalLogin($web_user);
$this->entityTypeId = 'entity_test';
$this->fieldNameSingle = 'test_single';
$this->fieldNameMultiple = 'test_multiple';
$this->fieldNameFile = 'test_file';
FieldStorageConfig::create([
'field_name' => $this->fieldNameSingle,
'entity_type' => $this->entityTypeId,
'type' => 'test_field',
'cardinality' => 1,
])
->save();
FieldConfig::create([
'entity_type' => $this->entityTypeId,
'field_name' => $this->fieldNameSingle,
'bundle' => $this->entityTypeId,
'label' => 'Test single',
'required' => TRUE,
'translatable' => FALSE,
])
->save();
FieldStorageConfig::create([
'field_name' => $this->fieldNameMultiple,
'entity_type' => $this->entityTypeId,
'type' => 'test_field',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
])
->save();
FieldConfig::create([
'entity_type' => $this->entityTypeId,
'field_name' => $this->fieldNameMultiple,
'bundle' => $this->entityTypeId,
'label' => 'Test multiple',
'translatable' => FALSE,
])
->save();
FieldStorageConfig::create([
'field_name' => $this->fieldNameFile,
'entity_type' => $this->entityTypeId,
'type' => 'file',
'cardinality' => 1,
])
->save();
FieldConfig::create([
'entity_type' => $this->entityTypeId,
'field_name' => $this->fieldNameFile,
'bundle' => $this->entityTypeId,
'label' => 'Test file',
'translatable' => FALSE,
])
->save();
$this->container
->get('entity_display.repository')
->getFormDisplay($this->entityTypeId, $this->entityTypeId, 'default')
->setComponent($this->fieldNameSingle, [
'type' => 'test_field_widget',
])
->setComponent($this->fieldNameMultiple, [
'type' => 'test_field_widget',
])
->setComponent($this->fieldNameFile, [
'type' => 'file_generic',
])
->save();
}
public function testFieldWidgetsWithLimitedValidationErrors() {
$assert_session = $this
->assertSession();
$this
->drupalGet($this->entityTypeId . '/add');
$assert_session
->elementsCount('css', 'div#edit-test-multiple-wrapper div.form-type-textfield input', 1);
$this
->submitForm([], 'Add another item');
$assert_session
->elementsCount('css', 'div#edit-test-multiple-wrapper div.form-type-textfield input', 2);
$text_file = current($this
->getTestFiles('text'));
$edit = [
'files[test_file_0]' => \Drupal::service('file_system')
->realpath($text_file->uri),
];
$assert_session
->elementNotExists('css', 'input#edit-test-file-0-remove-button');
$this
->submitForm($edit, 'Upload');
$assert_session
->elementExists('css', 'input#edit-test-file-0-remove-button');
$field_config = FieldConfig::loadByName($this->entityTypeId, $this->entityTypeId, $this->fieldNameMultiple);
$field_config
->setRequired(TRUE);
$field_config
->save();
$this
->drupalGet($this->entityTypeId . '/add');
$this
->submitForm([], 'Add another item');
$assert_session
->pageTextContains('Test multiple (value 1) field is required.');
$this
->submitForm([], 'Save');
$assert_session
->pageTextContains('Test single field is required.');
$assert_session
->pageTextContains('Test multiple (value 1) field is required.');
}
}