You are here

public function FilterAPITest::testTypedDataAPI in Drupal 8

Same name and namespace in other branches
  1. 9 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 334

Class

FilterAPITest
Tests the behavior of the API of the Filter module.

Namespace

Drupal\Tests\filter\Kernel

Code

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
    ->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, [
    'plain_text',
  ]);
  $allowed_options = $data
    ->getSettableOptions($user);
  $this
    ->assertEqual($allowed_options, [
    '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
    ->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
    ->assertEqual($allowed_values, [
    'filtered_html',
    'plain_text',
  ]);
  $allowed_options = $data
    ->getSettableOptions($filtered_html_user);
  $expected_allowed_options = [
    'filtered_html' => 'Filtered HTML',
    'plain_text' => 'Plain text',
  ];
  $this
    ->assertEqual($allowed_options, $expected_allowed_options);
}