3 namespace Drupal\field\Tests\String;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\entity_test\Entity\EntityTest;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\simpletest\WebTestBase;
9 use Drupal\field\Entity\FieldStorageConfig;
12 * Tests the creation of string fields.
16 class StringFieldTest extends WebTestBase {
23 public static $modules = ['entity_test', 'file'];
26 * A user without any special permissions.
28 * @var \Drupal\user\UserInterface
32 protected function setUp() {
35 $this->webUser = $this->drupalCreateUser(['view test entity', 'administer entity_test content']);
36 $this->drupalLogin($this->webUser);
44 public function testTextfieldWidgets() {
45 $this->_testTextfieldWidgets('string', 'string_textfield');
46 $this->_testTextfieldWidgets('string_long', 'string_textarea');
50 * Helper function for testTextfieldWidgets().
52 public function _testTextfieldWidgets($field_type, $widget_type) {
54 $field_name = Unicode::strtolower($this->randomMachineName());
55 $field_storage = FieldStorageConfig::create([
56 'field_name' => $field_name,
57 'entity_type' => 'entity_test',
60 $field_storage->save();
62 'field_storage' => $field_storage,
63 'bundle' => 'entity_test',
64 'label' => $this->randomMachineName() . '_label',
66 entity_get_form_display('entity_test', 'entity_test', 'default')
67 ->setComponent($field_name, [
68 'type' => $widget_type,
70 'placeholder' => 'A placeholder on ' . $widget_type,
74 entity_get_display('entity_test', 'entity_test', 'full')
75 ->setComponent($field_name)
78 // Display creation form.
79 $this->drupalGet('entity_test/add');
80 $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget is displayed');
81 $this->assertNoFieldByName("{$field_name}[0][format]", '1', 'Format selector is not displayed');
82 $this->assertRaw(format_string('placeholder="A placeholder on @widget_type"', ['@widget_type' => $widget_type]));
84 // Submit with some value.
85 $value = $this->randomMachineName();
87 "{$field_name}[0][value]" => $value,
89 $this->drupalPostForm(NULL, $edit, t('Save'));
90 preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
92 $this->assertText(t('entity_test @id has been created.', ['@id' => $id]), 'Entity was created');
94 // Display the entity.
95 $entity = EntityTest::load($id);
96 $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full');
97 $content = $display->build($entity);
98 $this->setRawContent(\Drupal::service('renderer')->renderRoot($content));
99 $this->assertText($value, 'Filtered tags are not displayed');