function TextFieldTest::testTextfieldWidgetsAllowedFormats
Same name in other branches
- 11.x core/modules/text/tests/src/Functional/TextFieldTest.php \Drupal\Tests\text\Functional\TextFieldTest::testTextfieldWidgetsAllowedFormats()
Test widgets for fields with selected allowed formats.
File
-
core/
modules/ text/ tests/ src/ Functional/ TextFieldTest.php, line 175
Class
- TextFieldTest
- Tests the creation of text fields.
Namespace
Drupal\Tests\text\FunctionalCode
public function testTextfieldWidgetsAllowedFormats() : void {
// Create one text format.
$this->drupalLogin($this->adminUser);
$format1 = FilterFormat::create([
'format' => $this->randomMachineName(),
'name' => '1_' . $this->randomMachineName(),
]);
$format1->save();
// Create a second text format.
$format2 = FilterFormat::create([
'format' => $this->randomMachineName(),
'name' => '2_' . $this->randomMachineName(),
'filters' => [
'filter_html' => [
'status' => 1,
'settings' => [
'allowed_html' => '<strong>',
],
],
],
]);
$format2->save();
// Create a third text format.
$format3 = FilterFormat::create([
'format' => $this->randomMachineName(),
'name' => '3_' . $this->randomMachineName(),
]);
$format3->save();
// Grant access to both formats to the user.
$roles = $this->webUser
->getRoles();
$rid = $roles[0];
user_role_grant_permissions($rid, [
$format1->getPermissionName(),
$format2->getPermissionName(),
$format3->getPermissionName(),
]);
// Create a field with multiple formats allowed.
$field_name = $this->randomMachineName();
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'text',
]);
$field_storage->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'entity_test',
'label' => $this->randomMachineName() . '_label',
'settings' => [
'allowed_formats' => [],
],
]);
$field->save();
/** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository */
$entity_display_repository = $this->container
->get('entity_display.repository');
$entity_display_repository->getFormDisplay('entity_test', 'entity_test', 'default')
->setComponent($field_name, [
'type' => 'text_textfield',
])
->save();
$entity_display_repository->getViewDisplay('entity_test', 'entity_test', 'full')
->setComponent($field_name)
->save();
// Display the creation form.
$this->drupalLogin($this->webUser);
$this->drupalGet('entity_test/add');
$this->assertSession()
->fieldExists("{$field_name}[0][value]", NULL);
$this->assertSession()
->optionExists("{$field_name}[0][format]", $format1->id());
$this->assertSession()
->optionExists("{$field_name}[0][format]", $format2->id());
$this->assertSession()
->optionExists("{$field_name}[0][format]", $format3->id());
$filtered_markup = FilteredMarkup::create('<div><strong><span>Hello World</span></strong></div>');
$edit = [
"{$field_name}[0][value]" => $filtered_markup,
];
$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 = $entity_display_repository->getViewDisplay($entity->getEntityTypeId(), $entity->bundle(), 'full');
$content = $display->build($entity);
$rendered_entity = \Drupal::service('renderer')->renderRoot($content);
$this->assertStringContainsString('<div><strong><span>', (string) $rendered_entity);
// Log back in as admin.
$this->drupalLogin($this->adminUser);
// Change field to allow only one format.
$path = "entity_test/structure/entity_test/fields/entity_test.entity_test.{$field_name}";
$this->drupalGet($path);
$this->submitForm([
"settings[allowed_formats][{$format1->id()}]" => $format1->id(),
], 'Save settings');
$this->drupalGet($path);
// Display the creation form.
$this->drupalLogin($this->webUser);
// We shouldn't have the 'format' selector since only one format is allowed.
$this->drupalGet('entity_test/add');
$this->assertSession()
->fieldExists("{$field_name}[0][value]", NULL);
$this->assertSession()
->fieldNotExists("{$field_name}[0][format]");
// Retest the entity renders fine even though filter2 is disabled.
$entity = EntityTest::load($id);
$display = $entity_display_repository->getViewDisplay($entity->getEntityTypeId(), $entity->bundle(), 'full');
$content = $display->build($entity);
$rendered_entity = \Drupal::service('renderer')->renderRoot($content);
$this->assertStringContainsString('<div><strong><span>', (string) $rendered_entity);
// Test when 2 of 3 formats are selected.
$field->setSetting('allowed_formats', [
$format1->id(),
$format2->id(),
]);
$field->save();
$this->drupalGet('entity_test/add');
// We should see the 'format' selector again.
$this->assertSession()
->fieldExists("{$field_name}[0][value]", NULL);
$this->assertSession()
->optionExists("{$field_name}[0][format]", $format1->id());
$this->assertSession()
->optionExists("{$field_name}[0][format]", $format2->id());
$this->assertSession()
->optionNotExists("{$field_name}[0][format]", $format3->id());
// Change field to allow all formats by configuring none as allowed.
$field->setSetting('allowed_formats', []);
$field->save();
$this->drupalGet('entity_test/add');
// We should see the 'format' selector again.
$this->assertSession()
->fieldExists("{$field_name}[0][value]", NULL);
$this->assertSession()
->optionExists("{$field_name}[0][format]", $format1->id());
$this->assertSession()
->optionExists("{$field_name}[0][format]", $format2->id());
$this->assertSession()
->optionExists("{$field_name}[0][format]", $format3->id());
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.