comparison core/modules/text/tests/src/Functional/TextFieldTest.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
1 <?php
2
3 namespace Drupal\Tests\text\Functional;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\entity_test\Entity\EntityTest;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\field\Entity\FieldStorageConfig;
9 use Drupal\filter\Entity\FilterFormat;
10 use Drupal\Tests\field\Functional\String\StringFieldTest;
11 use Drupal\Tests\TestFileCreationTrait;
12
13 /**
14 * Tests the creation of text fields.
15 *
16 * @group text
17 */
18 class TextFieldTest extends StringFieldTest {
19
20 use TestFileCreationTrait {
21 getTestFiles as drupalGetTestFiles;
22 }
23
24 /**
25 * A user with relevant administrative privileges.
26 *
27 * @var \Drupal\user\UserInterface
28 */
29 protected $adminUser;
30
31 protected function setUp() {
32 parent::setUp();
33
34 $this->adminUser = $this->drupalCreateUser(['administer filters']);
35 }
36
37 // Test fields.
38
39 /**
40 * Test text field validation.
41 */
42 public function testTextFieldValidation() {
43 // Create a field with settings to validate.
44 $max_length = 3;
45 $field_name = mb_strtolower($this->randomMachineName());
46 $field_storage = FieldStorageConfig::create([
47 'field_name' => $field_name,
48 'entity_type' => 'entity_test',
49 'type' => 'text',
50 'settings' => [
51 'max_length' => $max_length,
52 ],
53 ]);
54 $field_storage->save();
55 FieldConfig::create([
56 'field_storage' => $field_storage,
57 'bundle' => 'entity_test',
58 ])->save();
59
60 // Test validation with valid and invalid values.
61 $entity = EntityTest::create();
62 for ($i = 0; $i <= $max_length + 2; $i++) {
63 $entity->{$field_name}->value = str_repeat('x', $i);
64 $violations = $entity->{$field_name}->validate();
65 if ($i <= $max_length) {
66 $this->assertEqual(count($violations), 0, "Length $i does not cause validation error when max_length is $max_length");
67 }
68 else {
69 $this->assertEqual(count($violations), 1, "Length $i causes validation error when max_length is $max_length");
70 }
71 }
72 }
73
74 /**
75 * Test required long text with file upload.
76 */
77 public function testRequiredLongTextWithFileUpload() {
78 // Create a text field.
79 $text_field_name = 'text_long';
80 $field_storage = FieldStorageConfig::create([
81 'field_name' => $text_field_name,
82 'entity_type' => 'entity_test',
83 'type' => 'text_with_summary',
84 ]);
85 $field_storage->save();
86 FieldConfig::create([
87 'field_storage' => $field_storage,
88 'bundle' => 'entity_test',
89 'label' => $this->randomMachineName() . '_label',
90 'required' => TRUE,
91 ])->save();
92
93 // Create a file field.
94 $file_field_name = 'file_field';
95 $field_storage = FieldStorageConfig::create([
96 'field_name' => $file_field_name,
97 'entity_type' => 'entity_test',
98 'type' => 'file',
99 ]);
100 $field_storage->save();
101 FieldConfig::create([
102 'field_storage' => $field_storage,
103 'bundle' => 'entity_test',
104 'label' => $this->randomMachineName() . '_label',
105 ])->save();
106
107 entity_get_form_display('entity_test', 'entity_test', 'default')
108 ->setComponent($text_field_name, [
109 'type' => 'text_textarea_with_summary',
110 ])
111 ->setComponent($file_field_name, [
112 'type' => 'file_generic',
113 ])
114 ->save();
115 entity_get_display('entity_test', 'entity_test', 'full')
116 ->setComponent($text_field_name)
117 ->setComponent($file_field_name)
118 ->save();
119
120 $test_file = current($this->drupalGetTestFiles('text'));
121 $edit['files[file_field_0]'] = \Drupal::service('file_system')->realpath($test_file->uri);
122 $this->drupalPostForm('entity_test/add', $edit, 'Upload');
123 $this->assertResponse(200);
124 $edit = [
125 'text_long[0][value]' => 'Long text',
126 ];
127 $this->drupalPostForm(NULL, $edit, 'Save');
128 $this->assertResponse(200);
129 $this->drupalGet('entity_test/1');
130 $this->assertText('Long text');
131 }
132
133 /**
134 * Test widgets.
135 */
136 public function testTextfieldWidgets() {
137 $this->_testTextfieldWidgets('text', 'text_textfield');
138 $this->_testTextfieldWidgets('text_long', 'text_textarea');
139 }
140
141 /**
142 * Test widgets + 'formatted_text' setting.
143 */
144 public function testTextfieldWidgetsFormatted() {
145 $this->_testTextfieldWidgetsFormatted('text', 'text_textfield');
146 $this->_testTextfieldWidgetsFormatted('text_long', 'text_textarea');
147 }
148
149 /**
150 * Helper function for testTextfieldWidgetsFormatted().
151 */
152 public function _testTextfieldWidgetsFormatted($field_type, $widget_type) {
153 /** @var \Drupal\Core\Render\RendererInterface $renderer */
154 $renderer = $this->container->get('renderer');
155
156 // Create a field.
157 $field_name = mb_strtolower($this->randomMachineName());
158 $field_storage = FieldStorageConfig::create([
159 'field_name' => $field_name,
160 'entity_type' => 'entity_test',
161 'type' => $field_type,
162 ]);
163 $field_storage->save();
164 FieldConfig::create([
165 'field_storage' => $field_storage,
166 'bundle' => 'entity_test',
167 'label' => $this->randomMachineName() . '_label',
168 ])->save();
169 entity_get_form_display('entity_test', 'entity_test', 'default')
170 ->setComponent($field_name, [
171 'type' => $widget_type,
172 ])
173 ->save();
174 entity_get_display('entity_test', 'entity_test', 'full')
175 ->setComponent($field_name)
176 ->save();
177
178 // Disable all text formats besides the plain text fallback format.
179 $this->drupalLogin($this->adminUser);
180 foreach (filter_formats() as $format) {
181 if (!$format->isFallbackFormat()) {
182 $this->drupalPostForm('admin/config/content/formats/manage/' . $format->id() . '/disable', [], t('Disable'));
183 }
184 }
185 $this->drupalLogin($this->webUser);
186
187 // Display the creation form. Since the user only has access to one format,
188 // no format selector will be displayed.
189 $this->drupalGet('entity_test/add');
190 $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget is displayed');
191 $this->assertNoFieldByName("{$field_name}[0][format]", '', 'Format selector is not displayed');
192
193 // Submit with data that should be filtered.
194 $value = '<em>' . $this->randomMachineName() . '</em>';
195 $edit = [
196 "{$field_name}[0][value]" => $value,
197 ];
198 $this->drupalPostForm(NULL, $edit, t('Save'));
199 preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
200 $id = $match[1];
201 $this->assertText(t('entity_test @id has been created.', ['@id' => $id]), 'Entity was created');
202
203 // Display the entity.
204 $entity = EntityTest::load($id);
205 $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full');
206 $content = $display->build($entity);
207 $rendered_entity = \Drupal::service('renderer')->renderRoot($content);
208 $this->assertNotContains($value, (string) $rendered_entity);
209 $this->assertContains(Html::escape($value), (string) $rendered_entity);
210
211 // Create a new text format that does not escape HTML, and grant the user
212 // access to it.
213 $this->drupalLogin($this->adminUser);
214 $edit = [
215 'format' => mb_strtolower($this->randomMachineName()),
216 'name' => $this->randomMachineName(),
217 ];
218 $this->drupalPostForm('admin/config/content/formats/add', $edit, t('Save configuration'));
219 filter_formats_reset();
220 $format = FilterFormat::load($edit['format']);
221 $format_id = $format->id();
222 $permission = $format->getPermissionName();
223 $roles = $this->webUser->getRoles();
224 $rid = $roles[0];
225 user_role_grant_permissions($rid, [$permission]);
226 $this->drupalLogin($this->webUser);
227
228 // Display edition form.
229 // We should now have a 'text format' selector.
230 $this->drupalGet('entity_test/manage/' . $id . '/edit');
231 $this->assertFieldByName("{$field_name}[0][value]", NULL, 'Widget is displayed');
232 $this->assertFieldByName("{$field_name}[0][format]", NULL, 'Format selector is displayed');
233
234 // Edit and change the text format to the new one that was created.
235 $edit = [
236 "{$field_name}[0][format]" => $format_id,
237 ];
238 $this->drupalPostForm(NULL, $edit, t('Save'));
239 $this->assertText(t('entity_test @id has been updated.', ['@id' => $id]), 'Entity was updated');
240
241 // Display the entity.
242 $this->container->get('entity.manager')->getStorage('entity_test')->resetCache([$id]);
243 $entity = EntityTest::load($id);
244 $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full');
245 $content = $display->build($entity);
246 $rendered_entity = \Drupal::service('renderer')->renderRoot($content);
247 $this->assertContains($value, (string) $rendered_entity);
248 }
249
250 }