function FilterAPITest::testTypedDataAPI
Same name in other branches
- 8.9.x core/modules/filter/tests/src/Kernel/FilterAPITest.php \Drupal\Tests\filter\Kernel\FilterAPITest::testTypedDataAPI()
- 10 core/modules/filter/tests/src/Kernel/FilterAPITest.php \Drupal\Tests\filter\Kernel\FilterAPITest::testTypedDataAPI()
- 11.x core/modules/filter/tests/src/Kernel/FilterAPITest.php \Drupal\Tests\filter\Kernel\FilterAPITest::testTypedDataAPI()
Tests the function of the typed data type.
File
-
core/
modules/ filter/ tests/ src/ Kernel/ FilterAPITest.php, line 324
Class
- FilterAPITest
- Tests the behavior of the API of the Filter module.
Namespace
Drupal\Tests\filter\KernelCode
public function testTypedDataAPI() {
$definition = DataDefinition::create('filter_format');
$data = \Drupal::typedDataManager()->create($definition);
$this->assertInstanceOf(OptionsProviderInterface::class, $data);
$filtered_html_user = $this->createUser([
'uid' => 2,
], [
FilterFormat::load('filtered_html')->getPermissionName(),
]);
// Test with anonymous user.
$user = new AnonymousUserSession();
\Drupal::currentUser()->setAccount($user);
$expected_available_options = [
'filtered_html' => 'Filtered HTML',
'full_html' => 'Full HTML',
'filter_test' => 'Test format',
'plain_text' => 'Plain text',
];
$available_values = $data->getPossibleValues();
$this->assertEquals(array_keys($expected_available_options), $available_values);
$available_options = $data->getPossibleOptions();
$this->assertEquals($expected_available_options, $available_options);
$allowed_values = $data->getSettableValues($user);
$this->assertEquals([
'plain_text',
], $allowed_values);
$allowed_options = $data->getSettableOptions($user);
$this->assertEquals([
'plain_text' => 'Plain text',
], $allowed_options);
$data->setValue('foo');
$violations = $data->validate();
$this->assertFilterFormatViolation($violations, 'foo');
// Make sure the information provided by a violation is correct.
$violation = $violations[0];
$this->assertEquals($data, $violation->getRoot(), 'Violation root is filter format.');
$this->assertEquals('', $violation->getPropertyPath(), 'Violation property path is correct.');
$this->assertEquals('foo', $violation->getInvalidValue(), 'Violation contains invalid value.');
$data->setValue('plain_text');
$violations = $data->validate();
$this->assertCount(0, $violations, "No validation violation for format 'plain_text' found");
// Anonymous doesn't have access to the 'filtered_html' format.
$data->setValue('filtered_html');
$violations = $data->validate();
$this->assertFilterFormatViolation($violations, 'filtered_html');
// Set user with access to 'filtered_html' format.
\Drupal::currentUser()->setAccount($filtered_html_user);
$violations = $data->validate();
$this->assertCount(0, $violations, "No validation violation for accessible format 'filtered_html' found.");
$allowed_values = $data->getSettableValues($filtered_html_user);
$this->assertEquals([
'filtered_html',
'plain_text',
], $allowed_values);
$allowed_options = $data->getSettableOptions($filtered_html_user);
$expected_allowed_options = [
'filtered_html' => 'Filtered HTML',
'plain_text' => 'Plain text',
];
$this->assertEquals($expected_allowed_options, $allowed_options);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.