TaxonomyIndexTidUiTest.php 8.35 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
40
41
42
43
44
45
  public static $modules = [
    'node',
    'taxonomy',
    'views',
    'views_ui',
    'taxonomy_test_views',
  ];
46

47
48
49
50
51
52
53
  /**
   * A nested array of \Drupal\taxonomy\TermInterface objects.
   *
   * @var \Drupal\taxonomy\TermInterface[][]
   */
  protected $terms = [];

54
55
56
  /**
   * {@inheritdoc}
   */
57
58
  protected function setUp($import_test_views = TRUE) {
    parent::setUp($import_test_views);
59

60
61
62
63
    $this->adminUser = $this->drupalCreateUser([
      'administer taxonomy',
      'administer views',
    ]);
64
65
    $this->drupalLogin($this->adminUser);

66
    Vocabulary::create([
67
68
      'vid' => 'tags',
      'name' => 'Tags',
69
    ])->save();
70
71
72
73
74
75
76
77
78
79

    // 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++) {
80
        $this->terms[$i][$j] = $term = Term::create([
81
82
          'vid' => 'tags',
          'name' => "Term $i.$j",
83
          'parent' => isset($this->terms[$i][0]) ? $this->terms[$i][0]->id() : 0,
84
        ]);
85
86
87
        $term->save();
      }
    }
88
    ViewTestData::createTestViews(get_class($this), ['taxonomy_test_views']);
89
90
91
92
93

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

96
97
98
99
  /**
   * Tests the filter UI.
   */
  public function testFilterUI() {
100
    $this->drupalGet('admin/structure/views/nojs/handler/test_filter_taxonomy_index_tid/default/filter/tid');
101
102
103
104
105
106
107
108

    $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++];
109
        $prefix = $this->terms[$i][$j]->parent->target_id ? '-' : '';
110
        $tid = $option->getAttribute('value');
111

112
        $this->assertEqual($prefix . $this->terms[$i][$j]->getName(), $option->getText());
113
        $this->assertEqual($this->terms[$i][$j]->id(), $tid);
114
115
      }
    }
116
117
118

    // Ensure the autocomplete input element appears when using the 'textfield'
    // type.
119
    $view = View::load('test_filter_taxonomy_index_tid');
120
121
122
    $display =& $view->getDisplay('default');
    $display['display_options']['filters']['tid']['type'] = 'textfield';
    $view->save();
123
    $this->drupalGet('admin/structure/views/nojs/handler/test_filter_taxonomy_index_tid/default/filter/tid');
124
    $this->assertFieldById('edit-options-value', NULL);
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

    // 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',
      ],
    ];
140
    $this->assertIdentical($expected, $view->calculateDependencies()->getDependencies());
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
  /**
   * 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');
169
    $this->assertCount(2, $xpath);
170
    $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node2->toUrl()->toString()]);
171
    $this->assertCount(1, $xpath);
172
    $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node3->toUrl()->toString()]);
173
    $this->assertCount(1, $xpath);
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

    // 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');
189
    $this->assertCount(1, $xpath);
190
    $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node1->toUrl()->toString()]);
191
    $this->assertCount(1, $xpath);
192
193
194
195
196
197
198
199
200
201

    // 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');
202
    $this->assertCount(3, $xpath);
203
    $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node2->toUrl()->toString()]);
204
    $this->assertCount(1, $xpath);
205
    $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node3->toUrl()->toString()]);
206
    $this->assertCount(1, $xpath);
207
    $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node4->toUrl()->toString()]);
208
    $this->assertCount(1, $xpath);
209
210
211
212
213
214
215

    // 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,
216
      'name[taxonomy_term_field_data.vid]' => TRUE,
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
    ];
    $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.');
232
233
  }

234
}