Backup of db before drupal security update
[yaffs-website] / web / core / modules / taxonomy / src / Tests / Views / TaxonomyIndexTidUiTest.php
1 <?php
2
3 namespace Drupal\taxonomy\Tests\Views;
4
5 use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
6 use Drupal\taxonomy\Entity\Term;
7 use Drupal\taxonomy\Entity\Vocabulary;
8 use Drupal\views\Tests\ViewTestData;
9 use Drupal\views_ui\Tests\UITestBase;
10 use Drupal\views\Entity\View;
11
12 /**
13  * Tests the taxonomy index filter handler UI.
14  *
15  * @group taxonomy
16  * @see \Drupal\taxonomy\Plugin\views\field\TaxonomyIndexTid
17  */
18 class TaxonomyIndexTidUiTest extends UITestBase {
19
20   use EntityReferenceTestTrait;
21
22   /**
23    * Views used by this test.
24    *
25    * @var array
26    */
27   public static $testViews = ['test_filter_taxonomy_index_tid', 'test_taxonomy_term_name'];
28
29   /**
30    * Modules to enable.
31    *
32    * @var array
33    */
34   public static $modules = ['node', 'taxonomy', 'views', 'views_ui', 'taxonomy_test_views'];
35
36   /**
37    * A nested array of \Drupal\taxonomy\TermInterface objects.
38    *
39    * @var \Drupal\taxonomy\TermInterface[][]
40    */
41   protected $terms = [];
42
43   /**
44    * {@inheritdoc}
45    */
46   protected function setUp() {
47     parent::setUp();
48
49     $this->adminUser = $this->drupalCreateUser(['administer taxonomy', 'administer views']);
50     $this->drupalLogin($this->adminUser);
51
52     Vocabulary::create([
53       'vid' => 'tags',
54       'name' => 'Tags',
55     ])->save();
56
57     // Setup a hierarchy which looks like this:
58     // term 0.0
59     // term 1.0
60     // - term 1.1
61     // term 2.0
62     // - term 2.1
63     // - term 2.2
64     for ($i = 0; $i < 3; $i++) {
65       for ($j = 0; $j <= $i; $j++) {
66         $this->terms[$i][$j] = $term = Term::create([
67           'vid' => 'tags',
68           'name' => "Term $i.$j",
69           'parent' => isset($terms[$i][0]) ? $terms[$i][0]->id() : 0,
70         ]);
71         $term->save();
72       }
73     }
74     ViewTestData::createTestViews(get_class($this), ['taxonomy_test_views']);
75
76     Vocabulary::create([
77       'vid' => 'empty_vocabulary',
78       'name' => 'Empty Vocabulary',
79     ])->save();
80   }
81
82   /**
83    * Tests the filter UI.
84    */
85   public function testFilterUI() {
86     $this->drupalGet('admin/structure/views/nojs/handler/test_filter_taxonomy_index_tid/default/filter/tid');
87
88     $result = $this->xpath('//select[@id="edit-options-value"]/option');
89
90     // Ensure that the expected hierarchy is available in the UI.
91     $counter = 0;
92     for ($i = 0; $i < 3; $i++) {
93       for ($j = 0; $j <= $i; $j++) {
94         $option = $result[$counter++];
95         $prefix = $this->terms[$i][$j]->parent->target_id ? '-' : '';
96         $attributes = $option->attributes();
97         $tid = (string) $attributes->value;
98
99         $this->assertEqual($prefix . $this->terms[$i][$j]->getName(), (string) $option);
100         $this->assertEqual($this->terms[$i][$j]->id(), $tid);
101       }
102     }
103
104     // Ensure the autocomplete input element appears when using the 'textfield'
105     // type.
106     $view = View::load('test_filter_taxonomy_index_tid');
107     $display =& $view->getDisplay('default');
108     $display['display_options']['filters']['tid']['type'] = 'textfield';
109     $view->save();
110     $this->drupalGet('admin/structure/views/nojs/handler/test_filter_taxonomy_index_tid/default/filter/tid');
111     $this->assertFieldByXPath('//input[@id="edit-options-value"]');
112
113     // Tests \Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid::calculateDependencies().
114     $expected = [
115       'config' => [
116         'taxonomy.vocabulary.tags',
117       ],
118       'content' => [
119         'taxonomy_term:tags:' . Term::load(2)->uuid(),
120       ],
121       'module' => [
122         'node',
123         'taxonomy',
124         'user',
125       ],
126     ];
127     $this->assertIdentical($expected, $view->calculateDependencies()->getDependencies());
128   }
129
130   /**
131    * Tests exposed taxonomy filters.
132    */
133   public function testExposedFilter() {
134     $node_type = $this->drupalCreateContentType(['type' => 'page']);
135
136     // Create the tag field itself.
137     $field_name = 'taxonomy_tags';
138     $this->createEntityReferenceField('node', $node_type->id(), $field_name, NULL, 'taxonomy_term');
139
140     // Create 4 nodes: 1 without a term, 2 with the same term, and 1 with a
141     // different term.
142     $node1 = $this->drupalCreateNode();
143     $node2 = $this->drupalCreateNode([
144       $field_name => [['target_id' => $this->terms[1][0]->id()]],
145     ]);
146     $node3 = $this->drupalCreateNode([
147       $field_name => [['target_id' => $this->terms[1][0]->id()]],
148     ]);
149     $node4 = $this->drupalCreateNode([
150       $field_name => [['target_id' => $this->terms[2][0]->id()]],
151     ]);
152
153     // Only the nodes with the selected term should be shown.
154     $this->drupalGet('test-filter-taxonomy-index-tid');
155     $xpath = $this->xpath('//div[@class="view-content"]//a');
156     $this->assertIdentical(2, count($xpath));
157     $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node2->url()]);
158     $this->assertIdentical(1, count($xpath));
159     $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node3->url()]);
160     $this->assertIdentical(1, count($xpath));
161
162     // Expose the filter.
163     $this->drupalPostForm('admin/structure/views/nojs/handler/test_filter_taxonomy_index_tid/default/filter/tid', [], 'Expose filter');
164     // Set the operator to 'empty' and remove the default term ID.
165     $this->drupalPostForm(NULL, [
166       'options[operator]' => 'empty',
167       'options[value][]' => [],
168     ], 'Apply');
169     // Save the view.
170     $this->drupalPostForm(NULL, [], 'Save');
171
172     // After switching to 'empty' operator, the node without a term should be
173     // shown.
174     $this->drupalGet('test-filter-taxonomy-index-tid');
175     $xpath = $this->xpath('//div[@class="view-content"]//a');
176     $this->assertIdentical(1, count($xpath));
177     $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node1->url()]);
178     $this->assertIdentical(1, count($xpath));
179
180     // Set the operator to 'not empty'.
181     $this->drupalPostForm('admin/structure/views/nojs/handler/test_filter_taxonomy_index_tid/default/filter/tid', ['options[operator]' => 'not empty'], 'Apply');
182     // Save the view.
183     $this->drupalPostForm(NULL, [], 'Save');
184
185     // After switching to 'not empty' operator, all nodes with terms should be
186     // shown.
187     $this->drupalGet('test-filter-taxonomy-index-tid');
188     $xpath = $this->xpath('//div[@class="view-content"]//a');
189     $this->assertIdentical(3, count($xpath));
190     $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node2->url()]);
191     $this->assertIdentical(1, count($xpath));
192     $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node3->url()]);
193     $this->assertIdentical(1, count($xpath));
194     $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node4->url()]);
195     $this->assertIdentical(1, count($xpath));
196
197     // Select 'Term ID' as the field to be displayed.
198     $edit = ['name[taxonomy_term_field_data.tid]' => TRUE];
199     $this->drupalPostForm('admin/structure/views/nojs/add-handler/test_taxonomy_term_name/default/field', $edit, 'Add and configure fields');
200     // Select 'Term' and 'Vocabulary' as filters.
201     $edit = [
202       'name[taxonomy_term_field_data.tid]' => TRUE,
203       'name[taxonomy_term_field_data.vid]' => TRUE
204     ];
205     $this->drupalPostForm('admin/structure/views/nojs/add-handler/test_taxonomy_term_name/default/filter', $edit, 'Add and configure filter criteria');
206     // Select 'Empty Vocabulary' and 'Autocomplete' from the list of options.
207     $this->drupalPostForm('admin/structure/views/nojs/handler-extra/test_taxonomy_term_name/default/filter/tid', [], 'Apply and continue');
208     // Expose the filter.
209     $edit = ['options[expose_button][checkbox][checkbox]' => TRUE];
210     $this->drupalPostForm('admin/structure/views/nojs/handler/test_taxonomy_term_name/default/filter/tid', $edit, 'Expose filter');
211     $this->drupalPostForm('admin/structure/views/nojs/handler/test_taxonomy_term_name/default/filter/tid', $edit, 'Apply');
212     // Filter 'Taxonomy terms' belonging to 'Empty Vocabulary'.
213     $edit = ['options[value][empty_vocabulary]' => TRUE];
214     $this->drupalPostForm('admin/structure/views/nojs/handler/test_taxonomy_term_name/default/filter/vid', $edit, 'Apply');
215     $this->drupalPostForm('admin/structure/views/view/test_taxonomy_term_name/edit/default', [], 'Save');
216     $this->drupalPostForm(NULL, [], t('Update preview'));
217     $preview = $this->xpath("//div[@class='view-content']");
218     $this->assertTrue(empty($preview), 'No results.');
219   }
220
221 }