Backup of db before drupal security update
[yaffs-website] / web / core / modules / taxonomy / tests / src / Functional / Views / TaxonomyIndexTidUiTest.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Functional\Views;
4
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;
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($import_test_views = TRUE) {
47     parent::setUp($import_test_views);
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         $tid = $option->getAttribute('value');
97
98         $this->assertEqual($prefix . $this->terms[$i][$j]->getName(), $option->getText());
99         $this->assertEqual($this->terms[$i][$j]->id(), $tid);
100       }
101     }
102
103     // Ensure the autocomplete input element appears when using the 'textfield'
104     // type.
105     $view = View::load('test_filter_taxonomy_index_tid');
106     $display =& $view->getDisplay('default');
107     $display['display_options']['filters']['tid']['type'] = 'textfield';
108     $view->save();
109     $this->drupalGet('admin/structure/views/nojs/handler/test_filter_taxonomy_index_tid/default/filter/tid');
110     $this->assertFieldById('edit-options-value', NULL);
111
112     // Tests \Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid::calculateDependencies().
113     $expected = [
114       'config' => [
115         'taxonomy.vocabulary.tags',
116       ],
117       'content' => [
118         'taxonomy_term:tags:' . Term::load(2)->uuid(),
119       ],
120       'module' => [
121         'node',
122         'taxonomy',
123         'user',
124       ],
125     ];
126     $this->assertIdentical($expected, $view->calculateDependencies()->getDependencies());
127   }
128
129   /**
130    * Tests exposed taxonomy filters.
131    */
132   public function testExposedFilter() {
133     $node_type = $this->drupalCreateContentType(['type' => 'page']);
134
135     // Create the tag field itself.
136     $field_name = 'taxonomy_tags';
137     $this->createEntityReferenceField('node', $node_type->id(), $field_name, NULL, 'taxonomy_term');
138
139     // Create 4 nodes: 1 without a term, 2 with the same term, and 1 with a
140     // different term.
141     $node1 = $this->drupalCreateNode();
142     $node2 = $this->drupalCreateNode([
143       $field_name => [['target_id' => $this->terms[1][0]->id()]],
144     ]);
145     $node3 = $this->drupalCreateNode([
146       $field_name => [['target_id' => $this->terms[1][0]->id()]],
147     ]);
148     $node4 = $this->drupalCreateNode([
149       $field_name => [['target_id' => $this->terms[2][0]->id()]],
150     ]);
151
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));
160
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][]' => [],
167     ], 'Apply');
168     // Save the view.
169     $this->drupalPostForm(NULL, [], 'Save');
170
171     // After switching to 'empty' operator, the node without a term should be
172     // shown.
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));
178
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');
181     // Save the view.
182     $this->drupalPostForm(NULL, [], 'Save');
183
184     // After switching to 'not empty' operator, all nodes with terms should be
185     // shown.
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));
195
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.
200     $edit = [
201       'name[taxonomy_term_field_data.tid]' => TRUE,
202       'name[taxonomy_term_field_data.vid]' => TRUE
203     ];
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.');
218   }
219
220 }