You are here

public function ArgumentValidatorTermNameTest::testArgumentValidatorTermName in Drupal 10

Tests the term name argument validator plugin.

File

core/modules/taxonomy/tests/src/Kernel/Views/ArgumentValidatorTermNameTest.php, line 81

Class

ArgumentValidatorTermNameTest
Tests the plugin of the taxonomy: taxonomy_term_name argument validator.

Namespace

Drupal\Tests\taxonomy\Kernel\Views

Code

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

  // Test with name that does not correspond to any term.
  $this
    ->assertFalse($view->argument['name']
    ->setArgument('not a term name'));
  $view->argument['name']->validated_title = NULL;
  $view->argument['name']->argument_validated = NULL;

  // Test with term in the wrong vocabulary.
  $this
    ->assertFalse($view->argument['name']
    ->setArgument($this->names[4]));
  $view->argument['name']->validated_title = NULL;
  $view->argument['name']->argument_validated = NULL;

  // Test with a couple valid names.
  $this
    ->assertTrue($view->argument['name']
    ->setArgument($this->names[0]));
  $this
    ->assertEquals($this->names[0], $view->argument['name']
    ->getTitle());
  $view->argument['name']->validated_title = NULL;
  $view->argument['name']->argument_validated = NULL;
  $this
    ->assertTrue($view->argument['name']
    ->setArgument($this->names[1]));
  $this
    ->assertEquals($this->names[1], $view->argument['name']
    ->getTitle());
  $view->argument['name']->validated_title = NULL;
  $view->argument['name']->argument_validated = NULL;

  // Test that multiple valid terms don't validate because multiple arguments
  // are currently not supported.
  $multiple_terms = $this->names[0] . '+' . $this->names[1];
  $this
    ->assertFalse($view->argument['name']
    ->setArgument($multiple_terms));
  $view->argument['name']->validated_title = NULL;
  $view->argument['name']->argument_validated = NULL;

  // Test term whose name is shared by term in disallowed bundle.
  $this
    ->assertTrue($view->argument['name']
    ->setArgument($this->names[2]));
  $this
    ->assertEquals($this->names[2], $view->argument['name']
    ->getTitle());
  $view->argument['name']->validated_title = NULL;
  $view->argument['name']->argument_validated = NULL;

  // Add the second vocabulary as an allowed bundle.
  $view->argument['name']->options['validate_options']['bundles']['views_testing_tags_2'] = 'views_testing_tags_2';

  // Test that an array of bundles is handled by passing terms with unique
  // names in each bundle.
  $this
    ->assertTrue($view->argument['name']
    ->setArgument($this->names[0]));
  $this
    ->assertEquals($this->names[0], $view->argument['name']
    ->getTitle());
  $view->argument['name']->validated_title = NULL;
  $view->argument['name']->argument_validated = NULL;
  $this
    ->assertTrue($view->argument['name']
    ->setArgument($this->names[4]));
  $this
    ->assertEquals($this->names[4], $view->argument['name']
    ->getTitle());
  $view->argument['name']->validated_title = NULL;
  $view->argument['name']->argument_validated = NULL;

  // Allow any and all bundles.
  $view->argument['name']->options['validate_options']['bundles'] = [];

  // Test that an empty array of bundles is handled by testing terms with
  // unique names in each bundle.
  $this
    ->assertTrue($view->argument['name']
    ->setArgument($this->names[0]));
  $this
    ->assertEquals($this->names[0], $view->argument['name']
    ->getTitle());
  $view->argument['name']->validated_title = NULL;
  $view->argument['name']->argument_validated = NULL;
  $this
    ->assertTrue($view->argument['name']
    ->setArgument($this->names[4]));
  $this
    ->assertEquals($this->names[4], $view->argument['name']
    ->getTitle());
}