PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/taxonomy/src/Tests/Views/TaxonomyIndexTidUiTest.php

https://gitlab.com/reasonat/test8
PHP | 220 lines | 133 code | 28 blank | 59 comment | 4 complexity | e0df88dee217354176e433786807d3fe MD5 | raw file
  1. <?php
  2. namespace Drupal\taxonomy\Tests\Views;
  3. use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
  4. use Drupal\taxonomy\Entity\Term;
  5. use Drupal\taxonomy\Entity\Vocabulary;
  6. use Drupal\views\Tests\ViewTestData;
  7. use Drupal\views_ui\Tests\UITestBase;
  8. /**
  9. * Tests the taxonomy index filter handler UI.
  10. *
  11. * @group taxonomy
  12. * @see \Drupal\taxonomy\Plugin\views\field\TaxonomyIndexTid
  13. */
  14. class TaxonomyIndexTidUiTest extends UITestBase {
  15. use EntityReferenceTestTrait;
  16. /**
  17. * Views used by this test.
  18. *
  19. * @var array
  20. */
  21. public static $testViews = array('test_filter_taxonomy_index_tid', 'test_taxonomy_term_name');
  22. /**
  23. * Modules to enable.
  24. *
  25. * @var array
  26. */
  27. public static $modules = ['node', 'taxonomy', 'views', 'views_ui', 'taxonomy_test_views'];
  28. /**
  29. * A nested array of \Drupal\taxonomy\TermInterface objects.
  30. *
  31. * @var \Drupal\taxonomy\TermInterface[][]
  32. */
  33. protected $terms = [];
  34. /**
  35. * {@inheritdoc}
  36. */
  37. protected function setUp() {
  38. parent::setUp();
  39. $this->adminUser = $this->drupalCreateUser(['administer taxonomy', 'administer views']);
  40. $this->drupalLogin($this->adminUser);
  41. Vocabulary::create([
  42. 'vid' => 'tags',
  43. 'name' => 'Tags',
  44. ])->save();
  45. // Setup a hierarchy which looks like this:
  46. // term 0.0
  47. // term 1.0
  48. // - term 1.1
  49. // term 2.0
  50. // - term 2.1
  51. // - term 2.2
  52. for ($i = 0; $i < 3; $i++) {
  53. for ($j = 0; $j <= $i; $j++) {
  54. $this->terms[$i][$j] = $term = Term::create([
  55. 'vid' => 'tags',
  56. 'name' => "Term $i.$j",
  57. 'parent' => isset($terms[$i][0]) ? $terms[$i][0]->id() : 0,
  58. ]);
  59. $term->save();
  60. }
  61. }
  62. ViewTestData::createTestViews(get_class($this), array('taxonomy_test_views'));
  63. Vocabulary::create([
  64. 'vid' => 'empty_vocabulary',
  65. 'name' => 'Empty Vocabulary',
  66. ])->save();
  67. }
  68. /**
  69. * Tests the filter UI.
  70. */
  71. public function testFilterUI() {
  72. $this->drupalGet('admin/structure/views/nojs/handler/test_filter_taxonomy_index_tid/default/filter/tid');
  73. $result = $this->xpath('//select[@id="edit-options-value"]/option');
  74. // Ensure that the expected hierarchy is available in the UI.
  75. $counter = 0;
  76. for ($i = 0; $i < 3; $i++) {
  77. for ($j = 0; $j <= $i; $j++) {
  78. $option = $result[$counter++];
  79. $prefix = $this->terms[$i][$j]->parent->target_id ? '-' : '';
  80. $attributes = $option->attributes();
  81. $tid = (string) $attributes->value;
  82. $this->assertEqual($prefix . $this->terms[$i][$j]->getName(), (string) $option);
  83. $this->assertEqual($this->terms[$i][$j]->id(), $tid);
  84. }
  85. }
  86. // Ensure the autocomplete input element appears when using the 'textfield'
  87. // type.
  88. $view = entity_load('view', 'test_filter_taxonomy_index_tid');
  89. $display =& $view->getDisplay('default');
  90. $display['display_options']['filters']['tid']['type'] = 'textfield';
  91. $view->save();
  92. $this->drupalGet('admin/structure/views/nojs/handler/test_filter_taxonomy_index_tid/default/filter/tid');
  93. $this->assertFieldByXPath('//input[@id="edit-options-value"]');
  94. // Tests \Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid::calculateDependencies().
  95. $expected = [
  96. 'config' => [
  97. 'taxonomy.vocabulary.tags',
  98. ],
  99. 'content' => [
  100. 'taxonomy_term:tags:' . Term::load(2)->uuid(),
  101. ],
  102. 'module' => [
  103. 'node',
  104. 'taxonomy',
  105. 'user',
  106. ],
  107. ];
  108. $this->assertIdentical($expected, $view->calculateDependencies()->getDependencies());
  109. }
  110. /**
  111. * Tests exposed taxonomy filters.
  112. */
  113. public function testExposedFilter() {
  114. $node_type = $this->drupalCreateContentType(['type' => 'page']);
  115. // Create the tag field itself.
  116. $field_name = 'taxonomy_tags';
  117. $this->createEntityReferenceField('node', $node_type->id(), $field_name, NULL, 'taxonomy_term');
  118. // Create 4 nodes: 1 without a term, 2 with the same term, and 1 with a
  119. // different term.
  120. $node1 = $this->drupalCreateNode();
  121. $node2 = $this->drupalCreateNode([
  122. $field_name => [['target_id' => $this->terms[1][0]->id()]],
  123. ]);
  124. $node3 = $this->drupalCreateNode([
  125. $field_name => [['target_id' => $this->terms[1][0]->id()]],
  126. ]);
  127. $node4 = $this->drupalCreateNode([
  128. $field_name => [['target_id' => $this->terms[2][0]->id()]],
  129. ]);
  130. // Only the nodes with the selected term should be shown.
  131. $this->drupalGet('test-filter-taxonomy-index-tid');
  132. $xpath = $this->xpath('//div[@class="view-content"]//a');
  133. $this->assertIdentical(2, count($xpath));
  134. $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node2->url()]);
  135. $this->assertIdentical(1, count($xpath));
  136. $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node3->url()]);
  137. $this->assertIdentical(1, count($xpath));
  138. // Expose the filter.
  139. $this->drupalPostForm('admin/structure/views/nojs/handler/test_filter_taxonomy_index_tid/default/filter/tid', [], 'Expose filter');
  140. // Set the operator to 'empty' and remove the default term ID.
  141. $this->drupalPostForm(NULL, [
  142. 'options[operator]' => 'empty',
  143. 'options[value][]' => [],
  144. ], 'Apply');
  145. // Save the view.
  146. $this->drupalPostForm(NULL, [], 'Save');
  147. // After switching to 'empty' operator, the node without a term should be
  148. // shown.
  149. $this->drupalGet('test-filter-taxonomy-index-tid');
  150. $xpath = $this->xpath('//div[@class="view-content"]//a');
  151. $this->assertIdentical(1, count($xpath));
  152. $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node1->url()]);
  153. $this->assertIdentical(1, count($xpath));
  154. // Set the operator to 'not empty'.
  155. $this->drupalPostForm('admin/structure/views/nojs/handler/test_filter_taxonomy_index_tid/default/filter/tid', ['options[operator]' => 'not empty'], 'Apply');
  156. // Save the view.
  157. $this->drupalPostForm(NULL, [], 'Save');
  158. // After switching to 'not empty' operator, all nodes with terms should be
  159. // shown.
  160. $this->drupalGet('test-filter-taxonomy-index-tid');
  161. $xpath = $this->xpath('//div[@class="view-content"]//a');
  162. $this->assertIdentical(3, count($xpath));
  163. $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node2->url()]);
  164. $this->assertIdentical(1, count($xpath));
  165. $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node3->url()]);
  166. $this->assertIdentical(1, count($xpath));
  167. $xpath = $this->xpath('//div[@class="view-content"]//a[@href=:href]', [':href' => $node4->url()]);
  168. $this->assertIdentical(1, count($xpath));
  169. // Select 'Term ID' as the field to be displayed.
  170. $edit = ['name[taxonomy_term_field_data.tid]' => TRUE];
  171. $this->drupalPostForm('admin/structure/views/nojs/add-handler/test_taxonomy_term_name/default/field', $edit, 'Add and configure fields');
  172. // Select 'Term' and 'Vocabulary' as filters.
  173. $edit = [
  174. 'name[taxonomy_term_field_data.tid]' => TRUE,
  175. 'name[taxonomy_term_field_data.vid]' => TRUE
  176. ];
  177. $this->drupalPostForm('admin/structure/views/nojs/add-handler/test_taxonomy_term_name/default/filter', $edit, 'Add and configure filter criteria');
  178. // Select 'Empty Vocabulary' and 'Autocomplete' from the list of options.
  179. $this->drupalPostForm('admin/structure/views/nojs/handler-extra/test_taxonomy_term_name/default/filter/tid', [], 'Apply and continue');
  180. // Expose the filter.
  181. $edit = ['options[expose_button][checkbox][checkbox]' => TRUE];
  182. $this->drupalPostForm('admin/structure/views/nojs/handler/test_taxonomy_term_name/default/filter/tid', $edit, 'Expose filter');
  183. $this->drupalPostForm('admin/structure/views/nojs/handler/test_taxonomy_term_name/default/filter/tid', $edit, 'Apply');
  184. // Filter 'Taxonomy terms' belonging to 'Empty Vocabulary'.
  185. $edit = ['options[value][empty_vocabulary]' => TRUE];
  186. $this->drupalPostForm('admin/structure/views/nojs/handler/test_taxonomy_term_name/default/filter/vid', $edit, 'Apply');
  187. $this->drupalPostForm('admin/structure/views/view/test_taxonomy_term_name/edit/default', [], 'Save');
  188. $this->drupalPostForm(NULL, [], t('Update preview'));
  189. $preview = $this->xpath("//div[@class='view-content']");
  190. $this->assertTrue(empty($preview), 'No results.');
  191. }
  192. }