TaxonomyIndexTidUiTest.php 8.41 KB
Newer Older
1
2
<?php

3
namespace Drupal\Tests\taxonomy\Functional\Views;
4

5
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
6
7
use Drupal\taxonomy\Entity\Term;
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
14

/**
 * Tests the taxonomy index filter handler UI.
 *
15
 * @group taxonomy
16
17
18
19
 * @see \Drupal\taxonomy\Plugin\views\field\TaxonomyIndexTid
 */
class TaxonomyIndexTidUiTest extends UITestBase {

20
21
  use EntityReferenceTestTrait;

22
23
24
25
26
  /**
   * Views used by this test.
   *
   * @var array
   */
27
  public static $testViews = ['test_filter_taxonomy_index_tid', 'test_taxonomy_term_name'];
28

29
30
31
32
33
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'classy';

34
35
36
37
38
  /**
   * Modules to enable.
   *
   * @var array
   */
39
  public static $modules = ['node', 'taxonomy', 'views', 'views_ui', 'taxonomy_test_views'];
40

41
42
43
44
45
46
47
  /**
   * A nested array of \Drupal\taxonomy\TermInterface objects.
   *
   * @var \Drupal\taxonomy\TermInterface[][]
   */
  protected $terms = [];

48
49
50
  /**
   * {@inheritdoc}
   */
51
  protected function setUp($import_test_views = TRUE): void {
52
    parent::setUp($import_test_views);
53

54
55
56
    $this->adminUser = $this->drupalCreateUser(['administer taxonomy', 'administer views']);
    $this->drupalLogin($this->adminUser);

57
    Vocabulary::create([
58
59
      'vid' => 'tags',
      'name' => 'Tags',
60
    ])->save();
61
62
63
64
65
66
67
68
69
70

    // Setup a hierarchy which looks like this:
    // term 0.0
    // term 1.0
    // - term 1.1
    // term 2.0
    // - term 2.1
    // - term 2.2
    for ($i = 0; $i < 3; $i++) {
      for ($j = 0; $j <= $i; $j++) {
71
        $this->terms[$i][$j] = $term = Term::create([
72
73
          'vid' => 'tags',
          'name' => "Term $i.$j",
74
          'parent' => isset($this->terms[$i][0]) ? $this->terms[$i][0]->id() : 0,
75
        ]);
76
77
78
        $term->save();
      }
    }
79
    ViewTestData::createTestViews(get_class($this), ['taxonomy_test_views']);
80
81
82
83
84

    Vocabulary::create([
      'vid' => 'empty_vocabulary',
      'name' => 'Empty Vocabulary',
    ])->save();
85
  }
86

87
88
89
90
  /**
   * Tests the filter UI.
   */
  public function testFilterUI() {
91
    $this->drupalGet('admin/structure/views/nojs/handler/test_filter_taxonomy_index_tid/default/filter/tid');
92
93
94
95
96
97
98
99

    $result = $this->xpath('//select[@id="edit-options-value"]/option');

    // Ensure that the expected hierarchy is available in the UI.
    $counter = 0;
    for ($i = 0; $i < 3; $i++) {
      for ($j = 0; $j <= $i; $j++) {
        $option = $result[$counter++];
100
        $prefix = $this->terms[$i][$j]->parent->target_id ? '-' : '';
101
        $tid = $option->getAttribute('value');
102

103
        $this->assertEqual($prefix . $this->terms[$i][$j]->getName(), $option->getText());
104
        $this->assertEqual($this->terms[$i][$j]->id(), $tid);
105
106
      }
    }
107
108
109

    // Ensure the autocomplete input element appears when using the 'textfield'
    // type.
110
    $view = View::load('test_filter_taxonomy_index_tid');
111
112
113
    $display =& $view->getDisplay('default');
    $display['display_options']['filters']['tid']['type'] = 'textfield';
    $view->save();
114
    $this->drupalGet('admin/structure/views/nojs/handler/test_filter_taxonomy_index_tid/default/filter/tid');
115
    $this->assertFieldById('edit-options-value', NULL);
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

    // Tests \Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid::calculateDependencies().
    $expected = [
      'config' => [
        'taxonomy.vocabulary.tags',
      ],
      'content' => [
        'taxonomy_term:tags:' . Term::load(2)->uuid(),
      ],
      'module' => [
        'node',
        'taxonomy',
        'user',
      ],
    ];
131
    $this->assertIdentical($expected, $view->calculateDependencies()->getDependencies());
132
133
  }

134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  /**
   * Tests exposed taxonomy filters.
   */
  public function testExposedFilter() {
    $node_type = $this->drupalCreateContentType(['type' => 'page']);

    // Create the tag field itself.
    $field_name = 'taxonomy_tags';
    $this->createEntityReferenceField('node', $node_type->id(), $field_name, NULL, 'taxonomy_term');

    // Create 4 nodes: 1 without a term, 2 with the same term, and 1 with a
    // different term.
    $node1 = $this->drupalCreateNode();
    $node2 = $this->drupalCreateNode([
      $field_name => [['target_id' => $this->terms[1][0]->id()]],
    ]);
    $node3 = $this->drupalCreateNode([
      $field_name => [['target_id' => $this->terms[1][0]->id()]],
    ]);
    $node4 = $this->drupalCreateNode([
      $field_name => [['target_id' => $this->terms[2][0]->id()]],
    ]);

    // Only the nodes with the selected term should be shown.
    $this->drupalGet('test-filter-taxonomy-index-tid');
    $xpath = $this->xpath('//div[@class="view-content"]//a');
    $this->assertIdentical(2, count($xpath));
161
    $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node2->toUrl()->toString()]);
162
    $this->assertIdentical(1, count($xpath));
163
    $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node3->toUrl()->toString()]);
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
    $this->assertIdentical(1, count($xpath));

    // Expose the filter.
    $this->drupalPostForm('admin/structure/views/nojs/handler/test_filter_taxonomy_index_tid/default/filter/tid', [], 'Expose filter');
    // Set the operator to 'empty' and remove the default term ID.
    $this->drupalPostForm(NULL, [
      'options[operator]' => 'empty',
      'options[value][]' => [],
    ], 'Apply');
    // Save the view.
    $this->drupalPostForm(NULL, [], 'Save');

    // After switching to 'empty' operator, the node without a term should be
    // shown.
    $this->drupalGet('test-filter-taxonomy-index-tid');
    $xpath = $this->xpath('//div[@class="view-content"]//a');
    $this->assertIdentical(1, count($xpath));
181
    $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node1->toUrl()->toString()]);
182
183
184
185
186
187
188
189
190
191
192
193
    $this->assertIdentical(1, count($xpath));

    // Set the operator to 'not empty'.
    $this->drupalPostForm('admin/structure/views/nojs/handler/test_filter_taxonomy_index_tid/default/filter/tid', ['options[operator]' => 'not empty'], 'Apply');
    // Save the view.
    $this->drupalPostForm(NULL, [], 'Save');

    // After switching to 'not empty' operator, all nodes with terms should be
    // shown.
    $this->drupalGet('test-filter-taxonomy-index-tid');
    $xpath = $this->xpath('//div[@class="view-content"]//a');
    $this->assertIdentical(3, count($xpath));
194
    $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node2->toUrl()->toString()]);
195
    $this->assertIdentical(1, count($xpath));
196
    $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node3->toUrl()->toString()]);
197
    $this->assertIdentical(1, count($xpath));
198
    $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node4->toUrl()->toString()]);
199
    $this->assertIdentical(1, count($xpath));
200
201
202
203
204
205
206

    // Select 'Term ID' as the field to be displayed.
    $edit = ['name[taxonomy_term_field_data.tid]' => TRUE];
    $this->drupalPostForm('admin/structure/views/nojs/add-handler/test_taxonomy_term_name/default/field', $edit, 'Add and configure fields');
    // Select 'Term' and 'Vocabulary' as filters.
    $edit = [
      'name[taxonomy_term_field_data.tid]' => TRUE,
207
      'name[taxonomy_term_field_data.vid]' => TRUE,
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
    ];
    $this->drupalPostForm('admin/structure/views/nojs/add-handler/test_taxonomy_term_name/default/filter', $edit, 'Add and configure filter criteria');
    // Select 'Empty Vocabulary' and 'Autocomplete' from the list of options.
    $this->drupalPostForm('admin/structure/views/nojs/handler-extra/test_taxonomy_term_name/default/filter/tid', [], 'Apply and continue');
    // Expose the filter.
    $edit = ['options[expose_button][checkbox][checkbox]' => TRUE];
    $this->drupalPostForm('admin/structure/views/nojs/handler/test_taxonomy_term_name/default/filter/tid', $edit, 'Expose filter');
    $this->drupalPostForm('admin/structure/views/nojs/handler/test_taxonomy_term_name/default/filter/tid', $edit, 'Apply');
    // Filter 'Taxonomy terms' belonging to 'Empty Vocabulary'.
    $edit = ['options[value][empty_vocabulary]' => TRUE];
    $this->drupalPostForm('admin/structure/views/nojs/handler/test_taxonomy_term_name/default/filter/vid', $edit, 'Apply');
    $this->drupalPostForm('admin/structure/views/view/test_taxonomy_term_name/edit/default', [], 'Save');
    $this->drupalPostForm(NULL, [], t('Update preview'));
    $preview = $this->xpath("//div[@class='view-content']");
    $this->assertTrue(empty($preview), 'No results.');
223
224
  }

225
}