annotate core/modules/text/tests/src/Functional/TextFieldTest.php @ 5:12f9dff5fda9 tip

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