You are here

public function ArgumentValidatorTermTest::testArgumentValidatorTerm in Zircon Profile 8

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

Tests the term argument validator plugin.

File

core/modules/taxonomy/src/Tests/Views/ArgumentValidatorTermTest.php, line 69
Contains \Drupal\taxonomy\Tests\Views\ArgumentValidatorTermTest.

Class

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

Namespace

Drupal\taxonomy\Tests\Views

Code

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

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

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

  // Pass in a invalid term.
  $this
    ->assertFalse($view->argument['tid']
    ->setArgument(rand(1000, 10000)));
  $this
    ->assertEqual('', $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']->validator->options['type'] = 'tids';
  $view->argument['tid']->options['break_phrase'] = TRUE;

  // Pass in a single term.
  $this
    ->assertTrue($view->argument['tid']
    ->setArgument($this->terms[0]
    ->id()));
  $this
    ->assertEqual($view->argument['tid']
    ->getTitle(), $this->terms[0]
    ->label());
  $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
    ->assertEqual($view->argument['tid']
    ->getTitle(), implode(', ', $this->names));
  $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
    ->assertEqual($view->argument['tid']
    ->getTitle(), implode(' + ', $this->names));
  $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
    ->assertEqual('', $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
    ->assertEqual('', $view->argument['tid']
    ->getTitle());
  $view->argument['tid']->validated_title = NULL;
  $view->argument['tid']->argument_validated = NULL;
}