You are here

public function ArgumentValidatorTermTest::testArgumentValidatorTerm in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/taxonomy/tests/src/Kernel/Views/ArgumentValidatorTermTest.php \Drupal\Tests\taxonomy\Kernel\Views\ArgumentValidatorTermTest::testArgumentValidatorTerm()
  2. 9 core/modules/taxonomy/tests/src/Kernel/Views/ArgumentValidatorTermTest.php \Drupal\Tests\taxonomy\Kernel\Views\ArgumentValidatorTermTest::testArgumentValidatorTerm()

Tests the term argument validator plugin.

File

core/modules/taxonomy/tests/src/Kernel/Views/ArgumentValidatorTermTest.php, line 57

Class

ArgumentValidatorTermTest
Tests the plugin of the taxonomy: term argument validator.

Namespace

Drupal\Tests\taxonomy\Kernel\Views

Code

public function testArgumentValidatorTerm() {
  $view = Views::getView('test_argument_validator_term');
  $view
    ->initHandlers();

  // Test the single validator for term IDs.
  $view->argument['tid']->options['validate_options']['multiple'] = 0;

  // Pass in a single valid term.
  foreach ($this->terms as $term) {
    $this
      ->assertTrue($view->argument['tid']
      ->setArgument($term
      ->id()));
    $this
      ->assertEquals($term
      ->label(), $view->argument['tid']
      ->getTitle());
    $view->argument['tid']->validated_title = NULL;
    $view->argument['tid']->argument_validated = NULL;
  }

  // Pass in an invalid term.
  $this
    ->assertFalse($view->argument['tid']
    ->setArgument(rand(1000, 10000)));
  $this
    ->assertEmpty($view->argument['tid']
    ->getTitle());
  $view->argument['tid']->validated_title = NULL;
  $view->argument['tid']->argument_validated = NULL;

  // Test the multiple validator for term IDs.
  $view->argument['tid']->options['validate_options']['multiple'] = 1;
  $view->argument['tid']->options['break_phrase'] = TRUE;

  // Pass in a single term.
  $this
    ->assertTrue($view->argument['tid']
    ->setArgument($this->terms[0]
    ->id()));
  $this
    ->assertEquals($this->terms[0]
    ->label(), $view->argument['tid']
    ->getTitle());
  $view->argument['tid']->validated_title = NULL;
  $view->argument['tid']->argument_validated = NULL;

  // Check for multiple valid terms separated by commas.
  $this
    ->assertTrue($view->argument['tid']
    ->setArgument(implode(',', $this->ids)));
  $this
    ->assertEquals(implode(', ', $this->names), $view->argument['tid']
    ->getTitle());
  $view->argument['tid']->validated_title = NULL;
  $view->argument['tid']->argument_validated = NULL;

  // Check for multiple valid terms separated by plus signs.
  $this
    ->assertTrue($view->argument['tid']
    ->setArgument(implode('+', $this->ids)));
  $this
    ->assertEquals(implode(' + ', $this->names), $view->argument['tid']
    ->getTitle());
  $view->argument['tid']->validated_title = NULL;
  $view->argument['tid']->argument_validated = NULL;

  // Check for a single invalid term.
  $this
    ->assertFalse($view->argument['tid']
    ->setArgument(rand(1000, 10000)));
  $this
    ->assertEmpty($view->argument['tid']
    ->getTitle());
  $view->argument['tid']->validated_title = NULL;
  $view->argument['tid']->argument_validated = NULL;

  // Check for multiple invalid terms.
  $this
    ->assertFalse($view->argument['tid']
    ->setArgument(implode(',', [
    rand(1000, 10000),
    rand(1000, 10000),
  ])));
  $this
    ->assertEmpty($view->argument['tid']
    ->getTitle());
  $view->argument['tid']->validated_title = NULL;
  $view->argument['tid']->argument_validated = NULL;
}