annotate core/modules/text/tests/src/Functional/TextFieldTest.php @ 19:fa3358dc1485 tip

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