function FilterAPITest::testTypedDataAPI in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/filter/src/Tests/FilterAPITest.php \Drupal\filter\Tests\FilterAPITest::testTypedDataAPI()
Tests the function of the typed data type.
File
- core/
modules/ filter/ src/ Tests/ FilterAPITest.php, line 334 - Contains \Drupal\filter\Tests\FilterAPITest.
Class
- FilterAPITest
- Tests the behavior of the API of the Filter module.
Namespace
Drupal\filter\TestsCode
function testTypedDataAPI() {
$definition = DataDefinition::create('filter_format');
$data = \Drupal::typedDataManager()
->create($definition);
$this
->assertTrue($data instanceof OptionsProviderInterface, 'Typed data object implements \\Drupal\\Core\\TypedData\\OptionsProviderInterface');
$filtered_html_user = $this
->createUser(array(
'uid' => 2,
), array(
entity_load('filter_format', 'filtered_html')
->getPermissionName(),
));
// Test with anonymous user.
$user = new AnonymousUserSession();
\Drupal::currentUser()
->setAccount($user);
$expected_available_options = array(
'filtered_html' => 'Filtered HTML',
'full_html' => 'Full HTML',
'filter_test' => 'Test format',
'plain_text' => 'Plain text',
);
$available_values = $data
->getPossibleValues();
$this
->assertEqual($available_values, array_keys($expected_available_options));
$available_options = $data
->getPossibleOptions();
$this
->assertEqual($available_options, $expected_available_options);
$allowed_values = $data
->getSettableValues($user);
$this
->assertEqual($allowed_values, array(
'plain_text',
));
$allowed_options = $data
->getSettableOptions($user);
$this
->assertEqual($allowed_options, array(
'plain_text' => 'Plain text',
));
$data
->setValue('foo');
$violations = $data
->validate();
$this
->assertFilterFormatViolation($violations, 'foo');
// Make sure the information provided by a violation is correct.
$violation = $violations[0];
$this
->assertEqual($violation
->getRoot(), $data, 'Violation root is filter format.');
$this
->assertEqual($violation
->getPropertyPath(), '', 'Violation property path is correct.');
$this
->assertEqual($violation
->getInvalidValue(), 'foo', 'Violation contains invalid value.');
$data
->setValue('plain_text');
$violations = $data
->validate();
$this
->assertEqual(count($violations), 0, "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
->assertEqual(count($violations), 0, "No validation violation for accessible format 'filtered_html' found.");
$allowed_values = $data
->getSettableValues($filtered_html_user);
$this
->assertEqual($allowed_values, array(
'filtered_html',
'plain_text',
));
$allowed_options = $data
->getSettableOptions($filtered_html_user);
$expected_allowed_options = array(
'filtered_html' => 'Filtered HTML',
'plain_text' => 'Plain text',
);
$this
->assertEqual($allowed_options, $expected_allowed_options);
}