3 namespace Drupal\Tests\taxonomy\Functional\Views;
5 use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
6 use Drupal\taxonomy\Entity\Term;
7 use Drupal\taxonomy\Entity\Vocabulary;
8 use Drupal\Tests\views_ui\Functional\UITestBase;
9 use Drupal\views\Tests\ViewTestData;
10 use Drupal\views\Entity\View;
13 * Tests the taxonomy index filter handler UI.
16 * @see \Drupal\taxonomy\Plugin\views\field\TaxonomyIndexTid
18 class TaxonomyIndexTidUiTest extends UITestBase {
20 use EntityReferenceTestTrait;
23 * Views used by this test.
27 public static $testViews = ['test_filter_taxonomy_index_tid', 'test_taxonomy_term_name'];
34 public static $modules = ['node', 'taxonomy', 'views', 'views_ui', 'taxonomy_test_views'];
37 * A nested array of \Drupal\taxonomy\TermInterface objects.
39 * @var \Drupal\taxonomy\TermInterface[][]
41 protected $terms = [];
46 protected function setUp($import_test_views = TRUE) {
47 parent::setUp($import_test_views);
49 $this->adminUser = $this->drupalCreateUser(['administer taxonomy', 'administer views']);
50 $this->drupalLogin($this->adminUser);
57 // Setup a hierarchy which looks like this:
64 for ($i = 0; $i < 3; $i++) {
65 for ($j = 0; $j <= $i; $j++) {
66 $this->terms[$i][$j] = $term = Term::create([
68 'name' => "Term $i.$j",
69 'parent' => isset($terms[$i][0]) ? $terms[$i][0]->id() : 0,
74 ViewTestData::createTestViews(get_class($this), ['taxonomy_test_views']);
77 'vid' => 'empty_vocabulary',
78 'name' => 'Empty Vocabulary',
83 * Tests the filter UI.
85 public function testFilterUI() {
86 $this->drupalGet('admin/structure/views/nojs/handler/test_filter_taxonomy_index_tid/default/filter/tid');
88 $result = $this->xpath('//select[@id="edit-options-value"]/option');
90 // Ensure that the expected hierarchy is available in the UI.
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 $tid = $option->getAttribute('value');
98 $this->assertEqual($prefix . $this->terms[$i][$j]->getName(), $option->getText());
99 $this->assertEqual($this->terms[$i][$j]->id(), $tid);
103 // Ensure the autocomplete input element appears when using the 'textfield'
105 $view = View::load('test_filter_taxonomy_index_tid');
106 $display =& $view->getDisplay('default');
107 $display['display_options']['filters']['tid']['type'] = 'textfield';
109 $this->drupalGet('admin/structure/views/nojs/handler/test_filter_taxonomy_index_tid/default/filter/tid');
110 $this->assertFieldById('edit-options-value', NULL);
112 // Tests \Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid::calculateDependencies().
115 'taxonomy.vocabulary.tags',
118 'taxonomy_term:tags:' . Term::load(2)->uuid(),
126 $this->assertIdentical($expected, $view->calculateDependencies()->getDependencies());
130 * Tests exposed taxonomy filters.
132 public function testExposedFilter() {
133 $node_type = $this->drupalCreateContentType(['type' => 'page']);
135 // Create the tag field itself.
136 $field_name = 'taxonomy_tags';
137 $this->createEntityReferenceField('node', $node_type->id(), $field_name, NULL, 'taxonomy_term');
139 // Create 4 nodes: 1 without a term, 2 with the same term, and 1 with a
141 $node1 = $this->drupalCreateNode();
142 $node2 = $this->drupalCreateNode([
143 $field_name => [['target_id' => $this->terms[1][0]->id()]],
145 $node3 = $this->drupalCreateNode([
146 $field_name => [['target_id' => $this->terms[1][0]->id()]],
148 $node4 = $this->drupalCreateNode([
149 $field_name => [['target_id' => $this->terms[2][0]->id()]],
152 // Only the nodes with the selected term should be shown.
153 $this->drupalGet('test-filter-taxonomy-index-tid');
154 $xpath = $this->xpath('//div[@class="view-content"]//a');
155 $this->assertIdentical(2, count($xpath));
156 $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node2->url()]);
157 $this->assertIdentical(1, count($xpath));
158 $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node3->url()]);
159 $this->assertIdentical(1, count($xpath));
161 // Expose the filter.
162 $this->drupalPostForm('admin/structure/views/nojs/handler/test_filter_taxonomy_index_tid/default/filter/tid', [], 'Expose filter');
163 // Set the operator to 'empty' and remove the default term ID.
164 $this->drupalPostForm(NULL, [
165 'options[operator]' => 'empty',
166 'options[value][]' => [],
169 $this->drupalPostForm(NULL, [], 'Save');
171 // After switching to 'empty' operator, the node without a term should be
173 $this->drupalGet('test-filter-taxonomy-index-tid');
174 $xpath = $this->xpath('//div[@class="view-content"]//a');
175 $this->assertIdentical(1, count($xpath));
176 $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node1->url()]);
177 $this->assertIdentical(1, count($xpath));
179 // Set the operator to 'not empty'.
180 $this->drupalPostForm('admin/structure/views/nojs/handler/test_filter_taxonomy_index_tid/default/filter/tid', ['options[operator]' => 'not empty'], 'Apply');
182 $this->drupalPostForm(NULL, [], 'Save');
184 // After switching to 'not empty' operator, all nodes with terms should be
186 $this->drupalGet('test-filter-taxonomy-index-tid');
187 $xpath = $this->xpath('//div[@class="view-content"]//a');
188 $this->assertIdentical(3, count($xpath));
189 $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node2->url()]);
190 $this->assertIdentical(1, count($xpath));
191 $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node3->url()]);
192 $this->assertIdentical(1, count($xpath));
193 $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node4->url()]);
194 $this->assertIdentical(1, count($xpath));
196 // Select 'Term ID' as the field to be displayed.
197 $edit = ['name[taxonomy_term_field_data.tid]' => TRUE];
198 $this->drupalPostForm('admin/structure/views/nojs/add-handler/test_taxonomy_term_name/default/field', $edit, 'Add and configure fields');
199 // Select 'Term' and 'Vocabulary' as filters.
201 'name[taxonomy_term_field_data.tid]' => TRUE,
202 'name[taxonomy_term_field_data.vid]' => TRUE
204 $this->drupalPostForm('admin/structure/views/nojs/add-handler/test_taxonomy_term_name/default/filter', $edit, 'Add and configure filter criteria');
205 // Select 'Empty Vocabulary' and 'Autocomplete' from the list of options.
206 $this->drupalPostForm('admin/structure/views/nojs/handler-extra/test_taxonomy_term_name/default/filter/tid', [], 'Apply and continue');
207 // Expose the filter.
208 $edit = ['options[expose_button][checkbox][checkbox]' => TRUE];
209 $this->drupalPostForm('admin/structure/views/nojs/handler/test_taxonomy_term_name/default/filter/tid', $edit, 'Expose filter');
210 $this->drupalPostForm('admin/structure/views/nojs/handler/test_taxonomy_term_name/default/filter/tid', $edit, 'Apply');
211 // Filter 'Taxonomy terms' belonging to 'Empty Vocabulary'.
212 $edit = ['options[value][empty_vocabulary]' => TRUE];
213 $this->drupalPostForm('admin/structure/views/nojs/handler/test_taxonomy_term_name/default/filter/vid', $edit, 'Apply');
214 $this->drupalPostForm('admin/structure/views/view/test_taxonomy_term_name/edit/default', [], 'Save');
215 $this->drupalPostForm(NULL, [], t('Update preview'));
216 $preview = $this->xpath("//div[@class='view-content']");
217 $this->assertTrue(empty($preview), 'No results.');