3 namespace Drupal\FunctionalTests\Entity;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\Tests\BrowserTestBase;
9 use Drupal\Tests\TestFileCreationTrait;
12 * Tests field validation filtering on content entity forms.
16 class ContentEntityFormFieldValidationFilteringTest extends BrowserTestBase {
18 use TestFileCreationTrait;
21 * The ID of the type of the entity under test.
25 protected $entityTypeId;
28 * The single-valued field name being tested with the entity type.
32 protected $fieldNameSingle;
35 * The multi-valued field name being tested with the entity type.
39 protected $fieldNameMultiple;
42 * The name of the file field being tested with the entity type.
46 protected $fieldNameFile;
51 public static $modules = ['entity_test', 'field_test', 'file', 'image'];
56 protected function setUp() {
58 $web_user = $this->drupalCreateUser(['administer entity_test content']);
59 $this->drupalLogin($web_user);
61 // Create two fields of field type "test_field", one with single cardinality
62 // and one with unlimited cardinality on the entity type "entity_test". It
63 // is important to use this field type because its default widget has a
64 // custom \Drupal\Core\Field\WidgetInterface::errorElement() implementation.
65 $this->entityTypeId = 'entity_test';
66 $this->fieldNameSingle = 'test_single';
67 $this->fieldNameMultiple = 'test_multiple';
68 $this->fieldNameFile = 'test_file';
70 FieldStorageConfig::create([
71 'field_name' => $this->fieldNameSingle,
72 'entity_type' => $this->entityTypeId,
73 'type' => 'test_field',
77 'entity_type' => $this->entityTypeId,
78 'field_name' => $this->fieldNameSingle,
79 'bundle' => $this->entityTypeId,
80 'label' => 'Test single',
82 'translatable' => FALSE,
85 FieldStorageConfig::create([
86 'field_name' => $this->fieldNameMultiple,
87 'entity_type' => $this->entityTypeId,
88 'type' => 'test_field',
89 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
92 'entity_type' => $this->entityTypeId,
93 'field_name' => $this->fieldNameMultiple,
94 'bundle' => $this->entityTypeId,
95 'label' => 'Test multiple',
96 'translatable' => FALSE,
99 // Also create a file field to test its '#limit_validation_errors'
101 FieldStorageConfig::create([
102 'field_name' => $this->fieldNameFile,
103 'entity_type' => $this->entityTypeId,
107 FieldConfig::create([
108 'entity_type' => $this->entityTypeId,
109 'field_name' => $this->fieldNameFile,
110 'bundle' => $this->entityTypeId,
111 'label' => 'Test file',
112 'translatable' => FALSE,
115 entity_get_form_display($this->entityTypeId, $this->entityTypeId, 'default')
116 ->setComponent($this->fieldNameSingle, ['type' => 'test_field_widget'])
117 ->setComponent($this->fieldNameMultiple, ['type' => 'test_field_widget'])
118 ->setComponent($this->fieldNameFile, ['type' => 'file_generic'])
123 * Tests field widgets with #limit_validation_errors.
125 public function testFieldWidgetsWithLimitedValidationErrors() {
126 $assert_session = $this->assertSession();
127 $this->drupalGet($this->entityTypeId . '/add');
129 // The 'Test multiple' field is the only multi-valued field in the form, so
130 // try to add a new item for it. This tests the '#limit_validation_errors'
131 // property set by \Drupal\Core\Field\WidgetBase::formMultipleElements().
132 $assert_session->elementsCount('css', 'div#edit-test-multiple-wrapper div.form-type-textfield input', 1);
133 $this->drupalPostForm(NULL, [], 'Add another item');
134 $assert_session->elementsCount('css', 'div#edit-test-multiple-wrapper div.form-type-textfield input', 2);
136 // Now try to upload a file. This tests the '#limit_validation_errors'
138 // \Drupal\file\Plugin\Field\FieldWidget\FileWidget::process().
139 $text_file = current($this->getTestFiles('text'));
141 'files[test_file_0]' => \Drupal::service('file_system')->realpath($text_file->uri)
143 $assert_session->elementNotExists('css', 'input#edit-test-file-0-remove-button');
144 $this->drupalPostForm(NULL, $edit, 'Upload');
145 $assert_session->elementExists('css', 'input#edit-test-file-0-remove-button');
147 // Make the 'Test multiple' field required and check that adding another
148 // item throws a validation error.
149 $field_config = FieldConfig::loadByName($this->entityTypeId, $this->entityTypeId, $this->fieldNameMultiple);
150 $field_config->setRequired(TRUE);
151 $field_config->save();
153 $this->drupalPostForm($this->entityTypeId . '/add', [], 'Add another item');
154 $assert_session->pageTextContains('Test multiple (value 1) field is required.');
156 // Check that saving the form without entering any value for the required
157 // field still throws the proper validation errors.
158 $this->drupalPostForm(NULL, [], 'Save');
159 $assert_session->pageTextContains('Test single field is required.');
160 $assert_session->pageTextContains('Test multiple (value 1) field is required.');