function TextFieldTest::_testTextfieldWidgetsFormatted
Same name in other branches
- 9 core/modules/text/tests/src/Functional/TextFieldTest.php \Drupal\Tests\text\Functional\TextFieldTest::_testTextfieldWidgetsFormatted()
- 8.9.x core/modules/text/tests/src/Functional/TextFieldTest.php \Drupal\Tests\text\Functional\TextFieldTest::_testTextfieldWidgetsFormatted()
- 10 core/modules/text/tests/src/Functional/TextFieldTest.php \Drupal\Tests\text\Functional\TextFieldTest::_testTextfieldWidgetsFormatted()
Helper function for testTextfieldWidgetsFormatted().
1 call to TextFieldTest::_testTextfieldWidgetsFormatted()
- TextFieldTest::testTextfieldWidgetsFormatted in core/
modules/ text/ tests/ src/ Functional/ TextFieldTest.php - Tests widgets + 'formatted_text' setting.
File
-
core/
modules/ text/ tests/ src/ Functional/ TextFieldTest.php, line 310
Class
- TextFieldTest
- Tests the creation of text fields.
Namespace
Drupal\Tests\text\FunctionalCode
public function _testTextfieldWidgetsFormatted($field_type, $widget_type) : void {
// 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,
])
->save();
$display_repository->getViewDisplay('entity_test', 'entity_test', 'full')
->setComponent($field_name)
->save();
// Disable all text formats besides the plain text fallback format.
$this->drupalLogin($this->adminUser);
foreach (filter_formats() as $format) {
if (!$format->isFallbackFormat()) {
$this->drupalGet('admin/config/content/formats/manage/' . $format->id() . '/disable');
$this->submitForm([], 'Disable');
}
}
$this->drupalLogin($this->webUser);
// Display the creation form. Since the user only has access to one format,
// no format selector will be displayed.
$this->drupalGet('entity_test/add');
$this->assertSession()
->fieldValueEquals("{$field_name}[0][value]", '');
$this->assertSession()
->fieldNotExists("{$field_name}[0][format]");
// Submit with data that should be filtered.
$value = '<em>' . $this->randomMachineName() . '</em>';
$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->assertStringNotContainsString($value, (string) $rendered_entity);
$this->assertStringContainsString(Html::escape($value), (string) $rendered_entity);
// Create a new text format that does not escape HTML, and grant the user
// access to it.
$this->drupalLogin($this->adminUser);
$edit = [
'format' => $this->randomMachineName(),
'name' => $this->randomMachineName(),
];
$this->drupalGet('admin/config/content/formats/add');
$this->submitForm($edit, 'Save configuration');
filter_formats_reset();
$format = FilterFormat::load($edit['format']);
$format_id = $format->id();
$permission = $format->getPermissionName();
$roles = $this->webUser
->getRoles();
$rid = $roles[0];
user_role_grant_permissions($rid, [
$permission,
]);
$this->drupalLogin($this->webUser);
// Display edition form.
// We should now have a 'text format' selector.
$this->drupalGet('entity_test/manage/' . $id . '/edit');
$this->assertSession()
->fieldExists("{$field_name}[0][value]");
$this->assertSession()
->fieldExists("{$field_name}[0][format]");
// Edit and change the text format to the new one that was created.
$edit = [
"{$field_name}[0][format]" => $format_id,
];
$this->submitForm($edit, 'Save');
$this->assertSession()
->pageTextContains('entity_test ' . $id . ' has been updated.');
// Display the entity.
$this->container
->get('entity_type.manager')
->getStorage('entity_test')
->resetCache([
$id,
]);
$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.