133e445bd04774214ae3e0291414024d8c570956
[yaffs-website] / web / core / tests / Drupal / FunctionalTests / Entity / ContentEntityFormFieldValidationFilteringTest.php
1 <?php
2
3 namespace Drupal\FunctionalTests\Entity;
4
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;
10
11 /**
12  * Tests field validation filtering on content entity forms.
13  *
14  * @group Entity
15  */
16 class ContentEntityFormFieldValidationFilteringTest extends BrowserTestBase {
17
18   use TestFileCreationTrait;
19
20   /**
21    * The ID of the type of the entity under test.
22    *
23    * @var string
24    */
25   protected $entityTypeId;
26
27   /**
28    * The single-valued field name being tested with the entity type.
29    *
30    * @var string
31    */
32   protected $fieldNameSingle;
33
34   /**
35    * The multi-valued field name being tested with the entity type.
36    *
37    * @var string
38    */
39   protected $fieldNameMultiple;
40
41   /**
42    * The name of the file field being tested with the entity type.
43    *
44    * @var string
45    */
46   protected $fieldNameFile;
47
48   /**
49    * {@inheritdoc}
50    */
51   public static $modules = ['entity_test', 'field_test', 'file', 'image'];
52
53   /**
54    * {@inheritdoc}
55    */
56   protected function setUp() {
57     parent::setUp();
58     $web_user = $this->drupalCreateUser(['administer entity_test content']);
59     $this->drupalLogin($web_user);
60
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';
69
70     FieldStorageConfig::create([
71       'field_name' => $this->fieldNameSingle,
72       'entity_type' => $this->entityTypeId,
73       'type' => 'test_field',
74       'cardinality' => 1,
75     ])->save();
76     FieldConfig::create([
77       'entity_type' => $this->entityTypeId,
78       'field_name' => $this->fieldNameSingle,
79       'bundle' => $this->entityTypeId,
80       'label' => 'Test single',
81       'required' => TRUE,
82       'translatable' => FALSE,
83     ])->save();
84
85     FieldStorageConfig::create([
86       'field_name' => $this->fieldNameMultiple,
87       'entity_type' => $this->entityTypeId,
88       'type' => 'test_field',
89       'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
90     ])->save();
91     FieldConfig::create([
92       'entity_type' => $this->entityTypeId,
93       'field_name' => $this->fieldNameMultiple,
94       'bundle' => $this->entityTypeId,
95       'label' => 'Test multiple',
96       'translatable' => FALSE,
97     ])->save();
98
99     // Also create a file field to test its '#limit_validation_errors'
100     // implementation.
101     FieldStorageConfig::create([
102       'field_name' => $this->fieldNameFile,
103       'entity_type' => $this->entityTypeId,
104       'type' => 'file',
105       'cardinality' => 1,
106     ])->save();
107     FieldConfig::create([
108       'entity_type' => $this->entityTypeId,
109       'field_name' => $this->fieldNameFile,
110       'bundle' => $this->entityTypeId,
111       'label' => 'Test file',
112       'translatable' => FALSE,
113     ])->save();
114
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'])
119       ->save();
120   }
121
122   /**
123    * Tests field widgets with #limit_validation_errors.
124    */
125   public function testFieldWidgetsWithLimitedValidationErrors() {
126     $assert_session = $this->assertSession();
127     $this->drupalGet($this->entityTypeId . '/add');
128
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);
135
136     // Now try to upload a file. This tests the '#limit_validation_errors'
137     // property set by
138     // \Drupal\file\Plugin\Field\FieldWidget\FileWidget::process().
139     $text_file = current($this->getTestFiles('text'));
140     $edit = [
141       'files[test_file_0]' => \Drupal::service('file_system')->realpath($text_file->uri)
142     ];
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');
146
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();
152
153     $this->drupalPostForm($this->entityTypeId . '/add', [], 'Add another item');
154     $assert_session->pageTextContains('Test multiple (value 1) field is required.');
155
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.');
161   }
162
163 }