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();
68 // Test the single validator for term IDs.
69 $view->argument['tid']->validator->options['type'] = 'tid';
71 // Pass in a single valid term.
72 foreach ($this->terms as $term) {
73 $this->assertTrue($view->argument['tid']->setArgument($term->id()));
74 $this->assertEqual($view->argument['tid']->getTitle(), $term->label());
75 $view->argument['tid']->validated_title = NULL;
76 $view->argument['tid']->argument_validated = NULL;
79 // Pass in a invalid term.
80 $this->assertFalse($view->argument['tid']->setArgument(rand(1000, 10000)));
81 $this->assertEqual('', $view->argument['tid']->getTitle());
82 $view->argument['tid']->validated_title = NULL;
83 $view->argument['tid']->argument_validated = NULL;
85 // Test the multiple validator for term IDs.
86 $view->argument['tid']->validator->options['type'] = 'tids';
87 $view->argument['tid']->options['break_phrase'] = TRUE;
89 // Pass in a single term.
90 $this->assertTrue($view->argument['tid']->setArgument($this->terms[0]->id()));
91 $this->assertEqual($view->argument['tid']->getTitle(), $this->terms[0]->label());
92 $view->argument['tid']->validated_title = NULL;
93 $view->argument['tid']->argument_validated = NULL;
95 // Check for multiple valid terms separated by commas.
96 $this->assertTrue($view->argument['tid']->setArgument(implode(',', $this->ids)));
97 $this->assertEqual($view->argument['tid']->getTitle(), implode(', ', $this->names));
98 $view->argument['tid']->validated_title = NULL;
99 $view->argument['tid']->argument_validated = NULL;
101 // Check for multiple valid terms separated by plus signs.
102 $this->assertTrue($view->argument['tid']->setArgument(implode('+', $this->ids)));
103 $this->assertEqual($view->argument['tid']->getTitle(), implode(' + ', $this->names));
104 $view->argument['tid']->validated_title = NULL;
105 $view->argument['tid']->argument_validated = NULL;
107 // Check for a single invalid term.
108 $this->assertFalse($view->argument['tid']->setArgument(rand(1000, 10000)));
109 $this->assertEqual('', $view->argument['tid']->getTitle());
110 $view->argument['tid']->validated_title = NULL;
111 $view->argument['tid']->argument_validated = NULL;
113 // Check for multiple invalid terms.
114 $this->assertFalse($view->argument['tid']->setArgument(implode(',', [rand(1000, 10000), rand(1000, 10000)])));
115 $this->assertEqual('', $view->argument['tid']->getTitle());
116 $view->argument['tid']->validated_title = NULL;
117 $view->argument['tid']->argument_validated = NULL;