3 namespace Drupal\taxonomy\Tests\Views;
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;
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() {
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 $attributes = $option->attributes();
97 $tid = (string) $attributes->value;
99 $this->assertEqual($prefix . $this->terms[$i][$j]->getName(), (string) $option);
100 $this->assertEqual($this->terms[$i][$j]->id(), $tid);
104 // Ensure the autocomplete input element appears when using the 'textfield'
106 $view = View::load('test_filter_taxonomy_index_tid');
107 $display =& $view->getDisplay('default');
108 $display['display_options']['filters']['tid']['type'] = 'textfield';
110 $this->drupalGet('admin/structure/views/nojs/handler/test_filter_taxonomy_index_tid/default/filter/tid');
111 $this->assertFieldByXPath('//input[@id="edit-options-value"]');
113 // Tests \Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid::calculateDependencies().
116 'taxonomy.vocabulary.tags',
119 'taxonomy_term:tags:' . Term::load(2)->uuid(),
127 $this->assertIdentical($expected, $view->calculateDependencies()->getDependencies());
131 * Tests exposed taxonomy filters.
133 public function testExposedFilter() {
134 $node_type = $this->drupalCreateContentType(['type' => 'page']);
136 // Create the tag field itself.
137 $field_name = 'taxonomy_tags';
138 $this->createEntityReferenceField('node', $node_type->id(), $field_name, NULL, 'taxonomy_term');
140 // Create 4 nodes: 1 without a term, 2 with the same term, and 1 with a
142 $node1 = $this->drupalCreateNode();
143 $node2 = $this->drupalCreateNode([
144 $field_name => [['target_id' => $this->terms[1][0]->id()]],
146 $node3 = $this->drupalCreateNode([
147 $field_name => [['target_id' => $this->terms[1][0]->id()]],
149 $node4 = $this->drupalCreateNode([
150 $field_name => [['target_id' => $this->terms[2][0]->id()]],
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));
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][]' => [],
170 $this->drupalPostForm(NULL, [], 'Save');
172 // After switching to 'empty' operator, the node without a term should be
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));
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');
183 $this->drupalPostForm(NULL, [], 'Save');
185 // After switching to 'not empty' operator, all nodes with terms should be
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));
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.
202 'name[taxonomy_term_field_data.tid]' => TRUE,
203 'name[taxonomy_term_field_data.vid]' => TRUE
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.');