Backup of db before drupal security update
[yaffs-website] / web / core / modules / taxonomy / src / Tests / Views / ArgumentValidatorTermTest.php
1 <?php
2
3 namespace Drupal\taxonomy\Tests\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() {
51     parent::setUp();
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
69     // Test the single validator for term IDs.
70     $view->argument['tid']->validator->options['type'] = 'tid';
71
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;
78     }
79
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;
85
86
87     // Test the multiple validator for term IDs.
88     $view->argument['tid']->validator->options['type'] = 'tids';
89     $view->argument['tid']->options['break_phrase'] = TRUE;
90
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;
96
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;
102
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;
108
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;
114
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;
120   }
121
122 }