View source
<?php
namespace Drupal\Tests\filter\Functional;
use Drupal\filter\Entity\FilterFormat;
use Drupal\Tests\BrowserTestBase;
class FilterFormTest extends BrowserTestBase {
protected static $modules = [
'filter',
'filter_test',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected $webUser;
protected function setUp() : void {
parent::setUp();
$filter_test_format = FilterFormat::load('filter_test');
$filtered_html_format = FilterFormat::load('filtered_html');
$full_html_format = FilterFormat::load('full_html');
$this->adminUser = $this
->drupalCreateUser([
'administer filters',
$filtered_html_format
->getPermissionName(),
$full_html_format
->getPermissionName(),
$filter_test_format
->getPermissionName(),
]);
$this->webUser = $this
->drupalCreateUser([
$filtered_html_format
->getPermissionName(),
$filter_test_format
->getPermissionName(),
]);
}
public function testFilterForm() {
$this
->doFilterFormTestAsAdmin();
$this
->doFilterFormTestAsNonAdmin();
\Drupal::service('module_installer')
->install([
'filter_test_plugin',
]);
}
protected function doFilterFormTestAsAdmin() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('filter-test/text-format');
$formats = [
'filtered_html',
'full_html',
'filter_test',
];
$this
->assertEnabledTextarea('edit-all-formats-no-default-value');
$this
->assertOptions('edit-all-formats-no-default-format--2', $formats, 'filtered_html');
$this
->assertEnabledTextarea('edit-all-formats-default-value');
$this
->assertOptions('edit-all-formats-default-format--2', $formats, 'filter_test');
$this
->assertEnabledTextarea('edit-all-formats-default-missing-value');
$this
->assertRequiredSelectAndOptions('edit-all-formats-default-missing-format--2', $formats);
$formats = [
'full_html',
'filter_test',
];
$this
->assertEnabledTextarea('edit-restricted-formats-no-default-value');
$this
->assertOptions('edit-restricted-formats-no-default-format--2', $formats, 'full_html');
$this
->assertEnabledTextarea('edit-restricted-formats-default-value');
$this
->assertOptions('edit-restricted-formats-default-format--2', $formats, 'full_html');
$this
->assertEnabledTextarea('edit-restricted-formats-default-missing-value');
$this
->assertRequiredSelectAndOptions('edit-restricted-formats-default-missing-format--2', $formats);
$this
->assertEnabledTextarea('edit-restricted-formats-default-disallowed-value');
$this
->assertRequiredSelectAndOptions('edit-restricted-formats-default-disallowed-format--2', $formats);
$formats = [
'filter_test',
];
$this
->assertEnabledTextarea('edit-single-format-no-default-value');
$this
->assertNoSelect('edit-single-format-no-default-format--2');
$this
->assertEnabledTextarea('edit-single-format-default-value');
$this
->assertNoSelect('edit-single-format-default-format--2');
$this
->assertEnabledTextarea('edit-single-format-default-missing-value');
$this
->assertRequiredSelectAndOptions('edit-single-format-default-missing-format--2', $formats);
$this
->assertEnabledTextarea('edit-single-format-default-disallowed-value');
$this
->assertRequiredSelectAndOptions('edit-single-format-default-disallowed-format--2', $formats);
}
protected function doFilterFormTestAsNonAdmin() {
$this
->drupalLogin($this->webUser);
$this
->drupalGet('filter-test/text-format');
$formats = [
'filtered_html',
'filter_test',
];
$this
->assertEnabledTextarea('edit-all-formats-no-default-value');
$this
->assertOptions('edit-all-formats-no-default-format--2', $formats, 'filtered_html');
$this
->assertEnabledTextarea('edit-all-formats-default-value');
$this
->assertOptions('edit-all-formats-default-format--2', $formats, 'filter_test');
$this
->assertDisabledTextarea('edit-all-formats-default-missing-value');
$this
->assertEnabledTextarea('edit-restricted-formats-no-default-value');
$this
->assertNoSelect('edit-restricted-formats-no-default-format--2');
$this
->assertDisabledTextarea('edit-restricted-formats-default-value');
$this
->assertDisabledTextarea('edit-restricted-formats-default-missing-value');
$this
->assertDisabledTextarea('edit-restricted-formats-default-disallowed-value');
$this
->assertEnabledTextarea('edit-single-format-no-default-value');
$this
->assertNoSelect('edit-single-format-no-default-format--2');
$this
->assertEnabledTextarea('edit-single-format-default-value');
$this
->assertNoSelect('edit-single-format-default-format--2');
$this
->assertDisabledTextarea('edit-single-format-default-missing-value');
$this
->assertDisabledTextarea('edit-single-format-default-disallowed-value');
}
protected function assertNoSelect($id) {
$this
->assertSession()
->elementNotExists('xpath', "//select[@id={$id}]");
}
protected function assertOptions($id, array $expected_options, $selected) {
$select = $this
->assertSession()
->selectExists($id);
$found_options = $select
->findAll('css', 'option');
$found_options = array_map(function ($item) {
return $item
->getValue();
}, $found_options);
$this
->assertEqualsCanonicalizing($expected_options, $found_options);
$this
->assertTrue($this
->assertSession()
->optionExists($id, $selected)
->isSelected());
}
protected function assertRequiredSelectAndOptions($id, array $options) {
$select = $this
->assertSession()
->selectExists($id);
$this
->assertSame('required', $select
->getAttribute('required'));
$options[] = '';
$this
->assertOptions($id, $options, '');
}
protected function assertEnabledTextarea($id) {
$textarea = $this
->assertSession()
->fieldEnabled($id);
$this
->assertSame('textarea', $textarea
->getTagName());
}
protected function assertDisabledTextarea($id) {
$textarea = $this
->assertSession()
->fieldDisabled($id);
$this
->assertSame('textarea', $textarea
->getTagName());
$this
->assertSame('This field has been disabled because you do not have sufficient permissions to edit it.', $textarea
->getText());
$select_id = str_replace('value', 'format--2', $id);
$this
->assertNoSelect($select_id);
}
}