8260565e36c6ff399210ea99fe086779847ddce8
[yaffs-website] / web / core / modules / taxonomy / tests / src / Functional / Views / ArgumentValidatorTermTest.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Functional\Views;
4
5 use Drupal\views\Views;
6
7 /**
8  * Tests the plugin of the taxonomy: term argument validator.
9  *
10  * @group taxonomy
11  * @see Views\taxonomy\Plugin\views\argument_validator\Term
12  */
13 class ArgumentValidatorTermTest extends TaxonomyTestBase {
14
15   /**
16    * Stores the taxonomy term used by this test.
17    *
18    * @var array
19    */
20   protected $terms = [];
21
22   /**
23    * Stores the taxonomy names used by this test.
24    *
25    * @var array
26    */
27   protected $names = [];
28
29   /**
30    * Stores the taxonomy IDs used by this test.
31    *
32    * @var array
33    */
34   protected $ids = [];
35
36   /**
37    * Modules to enable.
38    *
39    * @var array
40    */
41   public static $modules = ['taxonomy', 'taxonomy_test_views', 'views_test_config'];
42
43   /**
44    * Views used by this test.
45    *
46    * @var array
47    */
48   public static $testViews = ['test_argument_validator_term'];
49
50   protected function setUp($import_test_views = TRUE) {
51     parent::setUp($import_test_views);
52
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();
58     }
59   }
60
61   /**
62    * Tests the term argument validator plugin.
63    */
64   public function testArgumentValidatorTerm() {
65     $view = Views::getView('test_argument_validator_term');
66     $view->initHandlers();
67
68     // Test the single validator for term IDs.
69     $view->argument['tid']->validator->options['type'] = 'tid';
70
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;
77     }
78
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;
84
85     // Test the multiple validator for term IDs.
86     $view->argument['tid']->validator->options['type'] = 'tids';
87     $view->argument['tid']->options['break_phrase'] = TRUE;
88
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;
94
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;
100
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;
106
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;
112
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;
118   }
119
120 }