3 namespace Drupal\Tests\field\Functional\String;
5 use Drupal\entity_test\Entity\EntityTest;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\Tests\BrowserTestBase;
11 * Tests the creation of string fields.
15 class StringFieldTest extends BrowserTestBase {
22 public static $modules = ['entity_test', 'file'];
25 * A user without any special permissions.
27 * @var \Drupal\user\UserInterface
31 protected function setUp() {
34 $this->webUser = $this->drupalCreateUser(['view test entity', 'administer entity_test content', 'access content']);
35 $this->drupalLogin($this->webUser);
43 public function testTextfieldWidgets() {
44 $this->_testTextfieldWidgets('string', 'string_textfield');
45 $this->_testTextfieldWidgets('string_long', 'string_textarea');
49 * Helper function for testTextfieldWidgets().
51 public function _testTextfieldWidgets($field_type, $widget_type) {
53 $field_name = mb_strtolower($this->randomMachineName());
54 $field_storage = FieldStorageConfig::create([
55 'field_name' => $field_name,
56 'entity_type' => 'entity_test',
57 'type' => $field_type,
59 $field_storage->save();
61 'field_storage' => $field_storage,
62 'bundle' => 'entity_test',
63 'label' => $this->randomMachineName() . '_label',
65 entity_get_form_display('entity_test', 'entity_test', 'default')
66 ->setComponent($field_name, [
67 'type' => $widget_type,
69 'placeholder' => 'A placeholder on ' . $widget_type,
73 entity_get_display('entity_test', 'entity_test', 'full')
74 ->setComponent($field_name)
77 // Display creation form.
78 $this->drupalGet('entity_test/add');
79 $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget is displayed');
80 $this->assertNoFieldByName("{$field_name}[0][format]", '1', 'Format selector is not displayed');
81 $this->assertRaw(format_string('placeholder="A placeholder on @widget_type"', ['@widget_type' => $widget_type]));
83 // Submit with some value.
84 $value = $this->randomMachineName();
86 "{$field_name}[0][value]" => $value,
88 $this->drupalPostForm(NULL, $edit, t('Save'));
89 preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
91 $this->assertText(t('entity_test @id has been created.', ['@id' => $id]), 'Entity was created');
93 // Display the entity.
94 $entity = EntityTest::load($id);
95 $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full');
96 $content = $display->build($entity);
97 $rendered_entity = \Drupal::service('renderer')->renderRoot($content);
98 $this->assertContains($value, (string) $rendered_entity);