3 namespace Drupal\Tests\taxonomy\Functional\Views;
5 use Drupal\views\Views;
8 * Tests the plugin of the taxonomy: term argument validator.
11 * @see Views\taxonomy\Plugin\views\argument_validator\Term
13 class ArgumentValidatorTermTest extends TaxonomyTestBase {
16 * Stores the taxonomy term used by this test.
20 protected $terms = [];
23 * Stores the taxonomy names used by this test.
27 protected $names = [];
30 * Stores the taxonomy IDs used by this test.
41 public static $modules = ['taxonomy', 'taxonomy_test_views', 'views_test_config'];
44 * Views used by this test.
48 public static $testViews = ['test_argument_validator_term'];
50 protected function setUp($import_test_views = TRUE) {
51 parent::setUp($import_test_views);
53 // Add three terms to the 'tags' vocabulary.
54 for ($i = 0; $i < 3; $i++) {
55 $this->terms[] = $term = $this->createTerm();
56 $this->names[] = $term->label();
57 $this->ids[] = $term->id();
62 * Tests the term argument validator plugin.
64 public function testArgumentValidatorTerm() {
65 $view = Views::getView('test_argument_validator_term');
66 $view->initHandlers();
69 // Test the single validator for term IDs.
70 $view->argument['tid']->validator->options['type'] = 'tid';
72 // Pass in a single valid term.
73 foreach ($this->terms as $term) {
74 $this->assertTrue($view->argument['tid']->setArgument($term->id()));
75 $this->assertEqual($view->argument['tid']->getTitle(), $term->label());
76 $view->argument['tid']->validated_title = NULL;
77 $view->argument['tid']->argument_validated = NULL;
80 // Pass in a invalid term.
81 $this->assertFalse($view->argument['tid']->setArgument(rand(1000, 10000)));
82 $this->assertEqual('', $view->argument['tid']->getTitle());
83 $view->argument['tid']->validated_title = NULL;
84 $view->argument['tid']->argument_validated = NULL;
87 // Test the multiple validator for term IDs.
88 $view->argument['tid']->validator->options['type'] = 'tids';
89 $view->argument['tid']->options['break_phrase'] = TRUE;
91 // Pass in a single term.
92 $this->assertTrue($view->argument['tid']->setArgument($this->terms[0]->id()));
93 $this->assertEqual($view->argument['tid']->getTitle(), $this->terms[0]->label());
94 $view->argument['tid']->validated_title = NULL;
95 $view->argument['tid']->argument_validated = NULL;
97 // Check for multiple valid terms separated by commas.
98 $this->assertTrue($view->argument['tid']->setArgument(implode(',', $this->ids)));
99 $this->assertEqual($view->argument['tid']->getTitle(), implode(', ', $this->names));
100 $view->argument['tid']->validated_title = NULL;
101 $view->argument['tid']->argument_validated = NULL;
103 // Check for multiple valid terms separated by plus signs.
104 $this->assertTrue($view->argument['tid']->setArgument(implode('+', $this->ids)));
105 $this->assertEqual($view->argument['tid']->getTitle(), implode(' + ', $this->names));
106 $view->argument['tid']->validated_title = NULL;
107 $view->argument['tid']->argument_validated = NULL;
109 // Check for a single invalid term.
110 $this->assertFalse($view->argument['tid']->setArgument(rand(1000, 10000)));
111 $this->assertEqual('', $view->argument['tid']->getTitle());
112 $view->argument['tid']->validated_title = NULL;
113 $view->argument['tid']->argument_validated = NULL;
115 // Check for multiple invalid terms.
116 $this->assertFalse($view->argument['tid']->setArgument(implode(',', [rand(1000, 10000), rand(1000, 10000)])));
117 $this->assertEqual('', $view->argument['tid']->getTitle());
118 $view->argument['tid']->validated_title = NULL;
119 $view->argument['tid']->argument_validated = NULL;