3 namespace Drupal\Tests\views\Functional\Wizard;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
8 use Drupal\taxonomy\Entity\Vocabulary;
11 * Tests the ability of the views wizard to create views filtered by taxonomy.
15 class TaggedWithTest extends WizardTestBase {
17 use EntityReferenceTestTrait;
24 public static $modules = ['taxonomy'];
27 * Node type with an autocomplete tagging field.
29 * @var \Drupal\node\NodeTypeInterface
31 protected $nodeTypeWithTags;
34 * Node type without an autocomplete tagging field.
36 * @var \Drupal\node\NodeTypeInterface
38 protected $nodeTypeWithoutTags;
41 * The vocabulary used for the test tag field.
43 * @var \Drupal\taxonomy\VocabularyInterface
45 protected $tagVocabulary;
48 * Holds the field storage for test tag field.
50 * @var \Drupal\field\FieldStorageConfigInterface
52 protected $tagFieldStorage;
55 * Name of the test tag field.
59 protected $tagFieldName;
62 * Field definition for the test tag field.
68 protected function setUp($import_test_views = TRUE) {
69 parent::setUp($import_test_views);
71 // Create two content types. One will have an autocomplete tagging field,
73 $this->nodeTypeWithTags = $this->drupalCreateContentType();
74 $this->nodeTypeWithoutTags = $this->drupalCreateContentType();
76 // Create the vocabulary for the tag field.
77 $this->tagVocabulary = Vocabulary::create([
78 'name' => 'Views testing tags',
79 'vid' => 'views_testing_tags',
81 $this->tagVocabulary->save();
83 // Create the tag field itself.
84 $this->tagFieldName = 'field_views_testing_tags';
88 $this->tagVocabulary->id() => $this->tagVocabulary->id(),
90 'auto_create' => TRUE,
92 $this->createEntityReferenceField('node', $this->nodeTypeWithTags->id(), $this->tagFieldName, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
94 entity_get_form_display('node', $this->nodeTypeWithTags->id(), 'default')
95 ->setComponent($this->tagFieldName, [
96 'type' => 'entity_reference_autocomplete_tags',
100 entity_get_display('node', $this->nodeTypeWithTags->id(), 'default')
101 ->setComponent($this->tagFieldName, [
102 'type' => 'entity_reference_label',
106 entity_get_display('node', $this->nodeTypeWithTags->id(), 'teaser')
107 ->setComponent('field_views_testing_tags', [
108 'type' => 'entity_reference_label',
115 * Tests the "tagged with" functionality.
117 public function testTaggedWith() {
118 // In this test we will only create nodes that have an instance of the tag
120 $node_add_path = 'node/add/' . $this->nodeTypeWithTags->id();
122 // Create three nodes, with different tags.
124 $edit['title[0][value]'] = $node_tag1_title = $this->randomMachineName();
125 $edit[$this->tagFieldName . '[target_id]'] = 'tag1';
126 $this->drupalPostForm($node_add_path, $edit, t('Save'));
128 $edit['title[0][value]'] = $node_tag1_tag2_title = $this->randomMachineName();
129 $edit[$this->tagFieldName . '[target_id]'] = 'tag1, tag2';
130 $this->drupalPostForm($node_add_path, $edit, t('Save'));
132 $edit['title[0][value]'] = $node_no_tags_title = $this->randomMachineName();
133 $this->drupalPostForm($node_add_path, $edit, t('Save'));
135 // Create a view that filters by taxonomy term "tag1". It should show only
136 // the two nodes from above that are tagged with "tag1".
138 // First select the node type and update the form so the correct tag field
140 $view1['show[type]'] = $this->nodeTypeWithTags->id();
141 $this->drupalPostForm('admin/structure/views/add', $view1, t('Update "of type" choice'));
142 // Now resubmit the entire form to the same URL.
143 $view1['label'] = $this->randomMachineName(16);
144 $view1['id'] = strtolower($this->randomMachineName(16));
145 $view1['description'] = $this->randomMachineName(16);
146 $view1['show[tagged_with]'] = 'tag1';
147 $view1['page[create]'] = 1;
148 $view1['page[title]'] = $this->randomMachineName(16);
149 $view1['page[path]'] = $this->randomMachineName(16);
150 $this->drupalPostForm(NULL, $view1, t('Save and edit'));
151 // Visit the page and check that the nodes we expect are present and the
152 // ones we don't expect are absent.
153 $this->drupalGet($view1['page[path]']);
154 $this->assertResponse(200);
155 $this->assertText($node_tag1_title);
156 $this->assertText($node_tag1_tag2_title);
157 $this->assertNoText($node_no_tags_title);
159 // Create a view that filters by taxonomy term "tag2". It should show only
160 // the one node from above that is tagged with "tag2".
162 $view2['show[type]'] = $this->nodeTypeWithTags->id();
163 $this->drupalPostForm('admin/structure/views/add', $view2, t('Update "of type" choice'));
164 $this->assertResponse(200);
165 $view2['label'] = $this->randomMachineName(16);
166 $view2['id'] = strtolower($this->randomMachineName(16));
167 $view2['description'] = $this->randomMachineName(16);
168 $view2['show[tagged_with]'] = 'tag2';
169 $view2['page[create]'] = 1;
170 $view2['page[title]'] = $this->randomMachineName(16);
171 $view2['page[path]'] = $this->randomMachineName(16);
172 $this->drupalPostForm(NULL, $view2, t('Save and edit'));
173 $this->assertResponse(200);
174 $this->drupalGet($view2['page[path]']);
175 $this->assertNoText($node_tag1_title);
176 $this->assertText($node_tag1_tag2_title);
177 $this->assertNoText($node_no_tags_title);
181 * Tests that the "tagged with" form element only shows for node types that support it.
183 public function testTaggedWithByNodeType() {
184 // The tagging field is associated with one of our node types only. So the
185 // "tagged with" form element on the view wizard should appear on the form
186 // by default (when the wizard is configured to display all content) and
187 // also when the node type that has the tagging field is selected, but not
188 // when the node type that doesn't have the tagging field is selected.
189 $tags_xpath = '//input[@name="show[tagged_with]"]';
190 $this->drupalGet('admin/structure/views/add');
191 $this->assertFieldByXpath($tags_xpath);
192 $view['show[type]'] = $this->nodeTypeWithTags->id();
193 $this->drupalPostForm('admin/structure/views/add', $view, t('Update "of type" choice'));
194 $this->assertFieldByXpath($tags_xpath);
195 $view['show[type]'] = $this->nodeTypeWithoutTags->id();
196 $this->drupalPostForm(NULL, $view, t('Update "of type" choice (2)'));
197 $this->assertNoFieldByXpath($tags_xpath);
199 // If we add an instance of the tagging field to the second node type, the
200 // "tagged with" form element should not appear for it too.
201 FieldConfig::create([
202 'field_name' => $this->tagFieldName,
203 'entity_type' => 'node',
204 'bundle' => $this->nodeTypeWithoutTags->id(),
206 'handler' => 'default',
207 'handler_settings' => [
208 'target_bundles' => [
209 $this->tagVocabulary->id() => $this->tagVocabulary->id(),
211 'auto_create' => TRUE,
215 entity_get_form_display('node', $this->nodeTypeWithoutTags->id(), 'default')
216 ->setComponent($this->tagFieldName, [
217 'type' => 'entity_reference_autocomplete_tags',
221 $view['show[type]'] = $this->nodeTypeWithTags->id();
222 $this->drupalPostForm('admin/structure/views/add', $view, t('Update "of type" choice'));
223 $this->assertFieldByXpath($tags_xpath);
224 $view['show[type]'] = $this->nodeTypeWithoutTags->id();
225 $this->drupalPostForm(NULL, $view, t('Update "of type" choice (2)'));
226 $this->assertFieldByXpath($tags_xpath);