function StringFieldTest::_testTextfieldWidgets

Same name and namespace in other branches
  1. 9 core/modules/field/tests/src/Functional/String/StringFieldTest.php \Drupal\Tests\field\Functional\String\StringFieldTest::_testTextfieldWidgets()
  2. 8.9.x core/modules/field/tests/src/Functional/String/StringFieldTest.php \Drupal\Tests\field\Functional\String\StringFieldTest::_testTextfieldWidgets()
  3. 11.x core/modules/field/tests/src/Functional/FunctionalString/StringFieldTest.php \Drupal\Tests\field\Functional\FunctionalString\StringFieldTest::_testTextfieldWidgets()

Helper function for testTextfieldWidgets().

2 calls to StringFieldTest::_testTextfieldWidgets()
StringFieldTest::testTextfieldWidgets in core/modules/field/tests/src/Functional/FunctionalString/StringFieldTest.php
Tests widgets.
TextFieldTest::testTextfieldWidgets in core/modules/text/tests/src/Functional/TextFieldTest.php
Tests widgets.

File

core/modules/field/tests/src/Functional/FunctionalString/StringFieldTest.php, line 64

Class

StringFieldTest
Tests the creation of string fields.

Namespace

Drupal\Tests\field\Functional\FunctionalString

Code

public function _testTextfieldWidgets($field_type, $widget_type) {
  // Create a field.
  $field_name = $this->randomMachineName();
  $field_storage = FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'type' => $field_type,
  ]);
  $field_storage->save();
  FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'entity_test',
    'label' => $this->randomMachineName() . '_label',
  ])
    ->save();
  /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
  $display_repository = \Drupal::service('entity_display.repository');
  $display_repository->getFormDisplay('entity_test', 'entity_test')
    ->setComponent($field_name, [
    'type' => $widget_type,
    'settings' => [
      'placeholder' => 'A placeholder on ' . $widget_type,
    ],
  ])
    ->save();
  $display_repository->getViewDisplay('entity_test', 'entity_test', 'full')
    ->setComponent($field_name)
    ->save();
  // Display creation form.
  $this->drupalGet('entity_test/add');
  $this->assertSession()
    ->fieldValueEquals("{$field_name}[0][value]", '');
  $this->assertSession()
    ->fieldNotExists("{$field_name}[0][format]");
  $this->assertSession()
    ->responseContains(new FormattableMarkup('placeholder="A placeholder on @widget_type"', [
    '@widget_type' => $widget_type,
  ]));
  // Submit with some value.
  $value = $this->randomMachineName();
  $edit = [
    "{$field_name}[0][value]" => $value,
  ];
  $this->submitForm($edit, 'Save');
  preg_match('|entity_test/manage/(\\d+)|', $this->getUrl(), $match);
  $id = $match[1];
  $this->assertSession()
    ->pageTextContains('entity_test ' . $id . ' has been created.');
  // Display the entity.
  $entity = EntityTest::load($id);
  $display = $display_repository->getViewDisplay($entity->getEntityTypeId(), $entity->bundle(), 'full');
  $content = $display->build($entity);
  $rendered_entity = \Drupal::service('renderer')->renderRoot($content);
  $this->assertStringContainsString($value, (string) $rendered_entity);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.