PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/core/modules/views/tests/src/Functional/Wizard/TaggedWithTest.php

http://github.com/drupal/drupal
PHP | 281 lines | 173 code | 30 blank | 78 comment | 0 complexity | 42ca0524ff8bcce76bf5725f3f95e40b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\Tests\views\Functional\Wizard;
  3. use Drupal\Core\Field\FieldStorageDefinitionInterface;
  4. use Drupal\field\Entity\FieldConfig;
  5. use Drupal\taxonomy\Entity\Term;
  6. use Drupal\taxonomy\Entity\Vocabulary;
  7. use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
  8. /**
  9. * Tests the ability of the views wizard to create views filtered by taxonomy.
  10. *
  11. * @group views
  12. */
  13. class TaggedWithTest extends WizardTestBase {
  14. use EntityReferenceTestTrait;
  15. /**
  16. * Modules to enable.
  17. *
  18. * @var array
  19. */
  20. protected static $modules = ['taxonomy'];
  21. /**
  22. * {@inheritdoc}
  23. */
  24. protected $defaultTheme = 'stark';
  25. /**
  26. * Node type with an autocomplete tagging field.
  27. *
  28. * @var \Drupal\node\NodeTypeInterface
  29. */
  30. protected $nodeTypeWithTags;
  31. /**
  32. * Node type without an autocomplete tagging field.
  33. *
  34. * @var \Drupal\node\NodeTypeInterface
  35. */
  36. protected $nodeTypeWithoutTags;
  37. /**
  38. * The vocabulary used for the test tag field.
  39. *
  40. * @var \Drupal\taxonomy\VocabularyInterface
  41. */
  42. protected $tagVocabulary;
  43. /**
  44. * Holds the field storage for test tag field.
  45. *
  46. * @var \Drupal\field\FieldStorageConfigInterface
  47. */
  48. protected $tagFieldStorage;
  49. /**
  50. * Name of the test tag field.
  51. *
  52. * @var string
  53. */
  54. protected $tagFieldName;
  55. /**
  56. * Field definition for the test tag field.
  57. *
  58. * @var array
  59. */
  60. protected $tagField;
  61. protected function setUp($import_test_views = TRUE, $modules = []): void {
  62. parent::setUp($import_test_views, $modules);
  63. // Create two content types. One will have an autocomplete tagging field,
  64. // and one won't.
  65. $this->nodeTypeWithTags = $this->drupalCreateContentType();
  66. $this->nodeTypeWithoutTags = $this->drupalCreateContentType();
  67. // Create the vocabulary for the tag field.
  68. $this->tagVocabulary = Vocabulary::create([
  69. 'name' => 'Views testing tags',
  70. 'vid' => 'views_testing_tags',
  71. ]);
  72. $this->tagVocabulary->save();
  73. // Create the tag field itself.
  74. $this->tagFieldName = 'field_views_testing_tags';
  75. $handler_settings = [
  76. 'target_bundles' => [
  77. $this->tagVocabulary->id() => $this->tagVocabulary->id(),
  78. ],
  79. 'auto_create' => TRUE,
  80. ];
  81. $this->createEntityReferenceField('node', $this->nodeTypeWithTags->id(), $this->tagFieldName, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
  82. /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
  83. $display_repository = \Drupal::service('entity_display.repository');
  84. $display_repository->getFormDisplay('node', $this->nodeTypeWithTags->id())
  85. ->setComponent($this->tagFieldName, [
  86. 'type' => 'entity_reference_autocomplete_tags',
  87. ])
  88. ->save();
  89. $display_repository->getViewDisplay('node', $this->nodeTypeWithTags->id())
  90. ->setComponent($this->tagFieldName, [
  91. 'type' => 'entity_reference_label',
  92. 'weight' => 10,
  93. ])
  94. ->save();
  95. $display_repository->getViewDisplay('node', $this->nodeTypeWithTags->id(), 'teaser')
  96. ->setComponent('field_views_testing_tags', [
  97. 'type' => 'entity_reference_label',
  98. 'weight' => 10,
  99. ])
  100. ->save();
  101. }
  102. /**
  103. * Tests the "tagged with" functionality.
  104. */
  105. public function testTaggedWith() {
  106. // In this test we will only create nodes that have an instance of the tag
  107. // field.
  108. $node_add_path = 'node/add/' . $this->nodeTypeWithTags->id();
  109. // Create three nodes, with different tags.
  110. $edit = [];
  111. $edit['title[0][value]'] = $node_tag1_title = $this->randomMachineName();
  112. $edit[$this->tagFieldName . '[target_id]'] = 'tag1';
  113. $this->drupalGet($node_add_path);
  114. $this->submitForm($edit, 'Save');
  115. $edit = [];
  116. $edit['title[0][value]'] = $node_tag1_tag2_title = $this->randomMachineName();
  117. $edit[$this->tagFieldName . '[target_id]'] = 'tag1, tag2';
  118. $this->drupalGet($node_add_path);
  119. $this->submitForm($edit, 'Save');
  120. $edit = [];
  121. $edit['title[0][value]'] = $node_no_tags_title = $this->randomMachineName();
  122. $this->drupalGet($node_add_path);
  123. $this->submitForm($edit, 'Save');
  124. // Create a view that filters by taxonomy term "tag1". It should show only
  125. // the two nodes from above that are tagged with "tag1".
  126. $view1 = [];
  127. // First select the node type and update the form so the correct tag field
  128. // is used.
  129. $view1['show[type]'] = $this->nodeTypeWithTags->id();
  130. $this->drupalGet('admin/structure/views/add');
  131. $this->submitForm($view1, 'Update "of type" choice');
  132. // Now resubmit the entire form to the same URL.
  133. $view1['label'] = $this->randomMachineName(16);
  134. $view1['id'] = strtolower($this->randomMachineName(16));
  135. $view1['description'] = $this->randomMachineName(16);
  136. $view1['show[tagged_with]'] = 'tag1';
  137. $view1['page[create]'] = 1;
  138. $view1['page[title]'] = $this->randomMachineName(16);
  139. $view1['page[path]'] = $this->randomMachineName(16);
  140. $this->submitForm($view1, 'Save and edit');
  141. // Visit the page and check that the nodes we expect are present and the
  142. // ones we don't expect are absent.
  143. $this->drupalGet($view1['page[path]']);
  144. $this->assertSession()->statusCodeEquals(200);
  145. $this->assertSession()->pageTextContains($node_tag1_title);
  146. $this->assertSession()->pageTextContains($node_tag1_tag2_title);
  147. $this->assertSession()->pageTextNotContains($node_no_tags_title);
  148. // Create a view that filters by taxonomy term "tag2". It should show only
  149. // the one node from above that is tagged with "tag2".
  150. $view2 = [];
  151. $view2['show[type]'] = $this->nodeTypeWithTags->id();
  152. $this->drupalGet('admin/structure/views/add');
  153. $this->submitForm($view2, 'Update "of type" choice');
  154. $this->assertSession()->statusCodeEquals(200);
  155. $view2['label'] = $this->randomMachineName(16);
  156. $view2['id'] = strtolower($this->randomMachineName(16));
  157. $view2['description'] = $this->randomMachineName(16);
  158. $view2['show[tagged_with]'] = 'tag2';
  159. $view2['page[create]'] = 1;
  160. $view2['page[title]'] = $this->randomMachineName(16);
  161. $view2['page[path]'] = $this->randomMachineName(16);
  162. $this->submitForm($view2, 'Save and edit');
  163. $this->assertSession()->statusCodeEquals(200);
  164. $this->drupalGet($view2['page[path]']);
  165. $this->assertSession()->pageTextNotContains($node_tag1_title);
  166. $this->assertSession()->pageTextContains($node_tag1_tag2_title);
  167. $this->assertSession()->pageTextNotContains($node_no_tags_title);
  168. }
  169. /**
  170. * Tests that the "tagged with" form element only shows for node types that support it.
  171. */
  172. public function testTaggedWithByNodeType() {
  173. // The tagging field is associated with one of our node types only. So the
  174. // "tagged with" form element on the view wizard should appear on the form
  175. // by default (when the wizard is configured to display all content) and
  176. // also when the node type that has the tagging field is selected, but not
  177. // when the node type that doesn't have the tagging field is selected.
  178. $this->drupalGet('admin/structure/views/add');
  179. $this->assertSession()->fieldExists("show[tagged_with]");
  180. $view['show[type]'] = $this->nodeTypeWithTags->id();
  181. $this->drupalGet('admin/structure/views/add');
  182. $this->submitForm($view, 'Update "of type" choice');
  183. $this->assertSession()->fieldExists("show[tagged_with]");
  184. $view['show[type]'] = $this->nodeTypeWithoutTags->id();
  185. $this->submitForm($view, 'Update "of type" choice (2)');
  186. $this->assertSession()->fieldNotExists("show[tagged_with]");
  187. // If we add an instance of the tagging field to the second node type, the
  188. // "tagged with" form element should not appear for it too.
  189. FieldConfig::create([
  190. 'field_name' => $this->tagFieldName,
  191. 'entity_type' => 'node',
  192. 'bundle' => $this->nodeTypeWithoutTags->id(),
  193. 'settings' => [
  194. 'handler' => 'default',
  195. 'handler_settings' => [
  196. 'target_bundles' => [
  197. $this->tagVocabulary->id() => $this->tagVocabulary->id(),
  198. ],
  199. 'auto_create' => TRUE,
  200. ],
  201. ],
  202. ])->save();
  203. \Drupal::service('entity_display.repository')
  204. ->getFormDisplay('node', $this->nodeTypeWithoutTags->id())
  205. ->setComponent($this->tagFieldName, [
  206. 'type' => 'entity_reference_autocomplete_tags',
  207. ])
  208. ->save();
  209. $view['show[type]'] = $this->nodeTypeWithTags->id();
  210. $this->drupalGet('admin/structure/views/add');
  211. $this->submitForm($view, 'Update "of type" choice');
  212. $this->assertSession()->fieldExists("show[tagged_with]");
  213. $view['show[type]'] = $this->nodeTypeWithoutTags->id();
  214. $this->submitForm($view, 'Update "of type" choice (2)');
  215. $this->assertSession()->fieldExists("show[tagged_with]");
  216. }
  217. /**
  218. * Tests that "tagged with" works with views entity reference.
  219. */
  220. public function testTaggedWithByViewReference() {
  221. Term::create(['name' => 'term1', 'vid' => 'views_testing_tags']);
  222. $tags_xpath = '//input[@name="show[tagged_with]"]';
  223. // If we add an instance of the tagging field to the second node type, the
  224. // "tagged with" form element should now appear for it too.
  225. FieldConfig::create([
  226. 'field_name' => $this->tagFieldName,
  227. 'entity_type' => 'node',
  228. 'bundle' => $this->nodeTypeWithoutTags->id(),
  229. 'settings' => [
  230. 'handler' => 'views',
  231. 'handler_settings' => [],
  232. ],
  233. ])->save();
  234. \Drupal::service('entity_display.repository')
  235. ->getFormDisplay('node', $this->nodeTypeWithoutTags->id())
  236. ->setComponent($this->tagFieldName, [
  237. 'type' => 'entity_reference_autocomplete_tags',
  238. ])
  239. ->save();
  240. $view['show[type]'] = $this->nodeTypeWithTags->id();
  241. $this->drupalGet('admin/structure/views/add');
  242. $this->submitForm($view, 'Update "of type" choice');
  243. $this->assertNotEmpty($this->xpath($tags_xpath));
  244. $view['show[type]'] = $this->nodeTypeWithoutTags->id();
  245. $this->submitForm($view, 'Update "of type" choice (2)');
  246. $this->assertNotEmpty($this->xpath($tags_xpath));
  247. $this->submitForm(['show[tagged_with]' => 'term1'], 'Save and edit');
  248. $this->assertSession()->statusCodeEquals(200);
  249. $this->getSession()->getPage()->hasContent('Has taxonomy term (= term1)');
  250. }
  251. }