View source
<?php
namespace Drupal\Tests\field\Functional\String;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\BrowserTestBase;
class StringFieldTest extends BrowserTestBase {
protected static $modules = [
'entity_test',
'file',
];
protected $defaultTheme = 'stark';
protected $webUser;
protected function setUp() : void {
parent::setUp();
$this->webUser = $this
->drupalCreateUser([
'view test entity',
'administer entity_test content',
'access content',
]);
$this
->drupalLogin($this->webUser);
}
public function testTextfieldWidgets() {
$this
->_testTextfieldWidgets('string', 'string_textfield');
$this
->_testTextfieldWidgets('string_long', 'string_textarea');
}
public function _testTextfieldWidgets($field_type, $widget_type) {
$field_name = mb_strtolower($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();
$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();
$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,
]));
$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.');
$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);
}
}