TaxonomyIndexTidUiTest.php 8.24 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

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

36
37
38
39
40
41
42
  /**
   * A nested array of \Drupal\taxonomy\TermInterface objects.
   *
   * @var \Drupal\taxonomy\TermInterface[][]
   */
  protected $terms = [];

43
44
45
  /**
   * {@inheritdoc}
   */
46
47
  protected function setUp($import_test_views = TRUE) {
    parent::setUp($import_test_views);
48

49
50
51
    $this->adminUser = $this->drupalCreateUser(['administer taxonomy', 'administer views']);
    $this->drupalLogin($this->adminUser);

52
    Vocabulary::create([
53
54
      'vid' => 'tags',
      'name' => 'Tags',
55
    ])->save();
56
57
58
59
60
61
62
63
64
65

    // 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++) {
66
        $this->terms[$i][$j] = $term = Term::create([
67
68
69
          'vid' => 'tags',
          'name' => "Term $i.$j",
          'parent' => isset($terms[$i][0]) ? $terms[$i][0]->id() : 0,
70
        ]);
71
72
73
        $term->save();
      }
    }
74
    ViewTestData::createTestViews(get_class($this), ['taxonomy_test_views']);
75
76
77
78
79

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

82
83
84
85
  /**
   * Tests the filter UI.
   */
  public function testFilterUI() {
86
    $this->drupalGet('admin/structure/views/nojs/handler/test_filter_taxonomy_index_tid/default/filter/tid');
87
88
89
90
91
92
93
94

    $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++];
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
104

    // Ensure the autocomplete input element appears when using the 'textfield'
    // type.
105
    $view = View::load('test_filter_taxonomy_index_tid');
106
107
108
    $display =& $view->getDisplay('default');
    $display['display_options']['filters']['tid']['type'] = 'textfield';
    $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
113
114
115
116
117
118
119
120
121
122
123
124
125

    // 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',
      ],
    ];
126
    $this->assertIdentical($expected, $view->calculateDependencies()->getDependencies());
127
128
  }

129
130
131
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
  /**
   * 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));
    $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node2->url()]);
    $this->assertIdentical(1, count($xpath));
    $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node3->url()]);
    $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));
    $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node1->url()]);
    $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));
    $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node2->url()]);
    $this->assertIdentical(1, count($xpath));
    $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node3->url()]);
    $this->assertIdentical(1, count($xpath));
    $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node4->url()]);
    $this->assertIdentical(1, count($xpath));
195
196
197
198
199
200
201

    // 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,
202
      'name[taxonomy_term_field_data.vid]' => TRUE,
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
    ];
    $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.');
218
219
  }

220
}