annotate core/modules/text/src/Tests/TextFieldTest.php @ 0:c75dbcec494b

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