You are here

function StringFieldTest::_testTextfieldWidgets in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field/src/Tests/String/StringFieldTest.php \Drupal\field\Tests\String\StringFieldTest::_testTextfieldWidgets()

Helper function for testTextfieldWidgets().

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

File

core/modules/field/src/Tests/String/StringFieldTest.php, line 54
Contains \Drupal\field\Tests\String\StringFieldTest.

Class

StringFieldTest
Tests the creation of string fields.

Namespace

Drupal\field\Tests\String

Code

function _testTextfieldWidgets($field_type, $widget_type) {

  // Create a field.
  $field_name = Unicode::strtolower($this
    ->randomMachineName());
  $field_storage = entity_create('field_storage_config', array(
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'type' => $field_type,
  ));
  $field_storage
    ->save();
  entity_create('field_config', array(
    'field_storage' => $field_storage,
    'bundle' => 'entity_test',
    'label' => $this
      ->randomMachineName() . '_label',
  ))
    ->save();
  entity_get_form_display('entity_test', 'entity_test', 'default')
    ->setComponent($field_name, array(
    'type' => $widget_type,
    'settings' => array(
      'placeholder' => 'A placeholder on ' . $widget_type,
    ),
  ))
    ->save();
  entity_get_display('entity_test', 'entity_test', 'full')
    ->setComponent($field_name)
    ->save();

  // Display creation form.
  $this
    ->drupalGet('entity_test/add');
  $this
    ->assertFieldByName("{$field_name}[0][value]", '', 'Widget is displayed');
  $this
    ->assertNoFieldByName("{$field_name}[0][format]", '1', 'Format selector is not displayed');
  $this
    ->assertRaw(format_string('placeholder="A placeholder on @widget_type"', array(
    '@widget_type' => $widget_type,
  )));

  // Submit with some value.
  $value = $this
    ->randomMachineName();
  $edit = array(
    "{$field_name}[0][value]" => $value,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  preg_match('|entity_test/manage/(\\d+)|', $this->url, $match);
  $id = $match[1];
  $this
    ->assertText(t('entity_test @id has been created.', array(
    '@id' => $id,
  )), 'Entity was created');

  // Display the entity.
  $entity = entity_load('entity_test', $id);
  $display = entity_get_display($entity
    ->getEntityTypeId(), $entity
    ->bundle(), 'full');
  $content = $display
    ->build($entity);
  $this
    ->setRawContent(\Drupal::service('renderer')
    ->renderRoot($content));
  $this
    ->assertText($value, 'Filtered tags are not displayed');
}