1<?php
2
3namespace Drupal\Tests\taxonomy\Kernel\Views;
4
5use Drupal\views\Views;
6
7/**
8 * Tests the plugin of the taxonomy: term argument validator.
9 *
10 * @group taxonomy
11 *
12 * @see \Drupal\taxonomy\Plugin\views\argument_validator\Term
13 */
14class ArgumentValidatorTermTest extends TaxonomyTestBase {
15
16  /**
17   * Stores the taxonomy term used by this test.
18   *
19   * @var array
20   */
21  protected $terms = [];
22
23  /**
24   * Stores the taxonomy names used by this test.
25   *
26   * @var array
27   */
28  protected $names = [];
29
30  /**
31   * Stores the taxonomy IDs used by this test.
32   *
33   * @var array
34   */
35  protected $ids = [];
36
37  /**
38   * {@inheritdoc}
39   */
40  public static $testViews = ['test_argument_validator_term'];
41
42  /**
43   * {@inheritdoc}
44   */
45  protected function setUp($import_test_views = TRUE) {
46    parent::setUp($import_test_views);
47
48    // Add three terms to the 'tags' vocabulary.
49    for ($i = 0; $i < 3; $i++) {
50      $this->terms[] = $term = $this->createTerm();
51      $this->names[] = $term->label();
52      $this->ids[] = $term->id();
53    }
54  }
55
56  /**
57   * Tests the term argument validator plugin.
58   */
59  public function testArgumentValidatorTerm() {
60    $view = Views::getView('test_argument_validator_term');
61    $view->initHandlers();
62
63    // Test the single validator for term IDs.
64    $view->argument['tid']->options['validate_options']['multiple'] = 0;
65
66    // Pass in a single valid term.
67    foreach ($this->terms as $term) {
68      $this->assertTrue($view->argument['tid']->setArgument($term->id()));
69      $this->assertEquals($term->label(), $view->argument['tid']->getTitle());
70      $view->argument['tid']->validated_title = NULL;
71      $view->argument['tid']->argument_validated = NULL;
72    }
73
74    // Pass in a invalid term.
75    $this->assertFalse($view->argument['tid']->setArgument(rand(1000, 10000)));
76    $this->assertEmpty($view->argument['tid']->getTitle());
77    $view->argument['tid']->validated_title = NULL;
78    $view->argument['tid']->argument_validated = NULL;
79
80    // Test the multiple validator for term IDs.
81    $view->argument['tid']->options['validate_options']['multiple'] = 1;
82    $view->argument['tid']->options['break_phrase'] = TRUE;
83
84    // Pass in a single term.
85    $this->assertTrue($view->argument['tid']->setArgument($this->terms[0]->id()));
86    $this->assertEquals($this->terms[0]->label(), $view->argument['tid']->getTitle());
87    $view->argument['tid']->validated_title = NULL;
88    $view->argument['tid']->argument_validated = NULL;
89
90    // Check for multiple valid terms separated by commas.
91    $this->assertTrue($view->argument['tid']->setArgument(implode(',', $this->ids)));
92    $this->assertEquals(implode(', ', $this->names), $view->argument['tid']->getTitle());
93    $view->argument['tid']->validated_title = NULL;
94    $view->argument['tid']->argument_validated = NULL;
95
96    // Check for multiple valid terms separated by plus signs.
97    $this->assertTrue($view->argument['tid']->setArgument(implode('+', $this->ids)));
98    $this->assertEquals(implode(' + ', $this->names), $view->argument['tid']->getTitle());
99    $view->argument['tid']->validated_title = NULL;
100    $view->argument['tid']->argument_validated = NULL;
101
102    // Check for a single invalid term.
103    $this->assertFalse($view->argument['tid']->setArgument(rand(1000, 10000)));
104    $this->assertEmpty($view->argument['tid']->getTitle());
105    $view->argument['tid']->validated_title = NULL;
106    $view->argument['tid']->argument_validated = NULL;
107
108    // Check for multiple invalid terms.
109    $this->assertFalse($view->argument['tid']->setArgument(implode(',', [rand(1000, 10000), rand(1000, 10000)])));
110    $this->assertEmpty($view->argument['tid']->getTitle());
111    $view->argument['tid']->validated_title = NULL;
112    $view->argument['tid']->argument_validated = NULL;
113  }
114
115}
116