680d64496edc5b1cb93402ebd0bbbfad70cd441d
[yaffs-website] / web / core / modules / views / tests / src / Functional / Wizard / TaggedWithTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional\Wizard;
4
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;
9
10 /**
11  * Tests the ability of the views wizard to create views filtered by taxonomy.
12  *
13  * @group views
14  */
15 class TaggedWithTest extends WizardTestBase {
16
17   use EntityReferenceTestTrait;
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['taxonomy'];
25
26   /**
27    * Node type with an autocomplete tagging field.
28    *
29    * @var \Drupal\node\NodeTypeInterface
30    */
31   protected $nodeTypeWithTags;
32
33   /**
34    * Node type without an autocomplete tagging field.
35    *
36    * @var \Drupal\node\NodeTypeInterface
37    */
38   protected $nodeTypeWithoutTags;
39
40   /**
41    * The vocabulary used for the test tag field.
42    *
43    * @var \Drupal\taxonomy\VocabularyInterface
44    */
45   protected $tagVocabulary;
46
47   /**
48    * Holds the field storage for test tag field.
49    *
50    * @var \Drupal\field\FieldStorageConfigInterface
51    */
52   protected $tagFieldStorage;
53
54   /**
55    * Name of the test tag field.
56    *
57    * @var string
58    */
59   protected $tagFieldName;
60
61   /**
62    * Field definition for the test tag field.
63    *
64    * @var array
65    */
66   protected $tagField;
67
68   protected function setUp($import_test_views = TRUE) {
69     parent::setUp($import_test_views);
70
71     // Create two content types. One will have an autocomplete tagging field,
72     // and one won't.
73     $this->nodeTypeWithTags = $this->drupalCreateContentType();
74     $this->nodeTypeWithoutTags = $this->drupalCreateContentType();
75
76     // Create the vocabulary for the tag field.
77     $this->tagVocabulary = Vocabulary::create([
78       'name' => 'Views testing tags',
79       'vid' => 'views_testing_tags',
80     ]);
81     $this->tagVocabulary->save();
82
83     // Create the tag field itself.
84     $this->tagFieldName = 'field_views_testing_tags';
85
86     $handler_settings = [
87       'target_bundles' => [
88         $this->tagVocabulary->id() => $this->tagVocabulary->id(),
89       ],
90       'auto_create' => TRUE,
91     ];
92     $this->createEntityReferenceField('node', $this->nodeTypeWithTags->id(), $this->tagFieldName, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
93
94     entity_get_form_display('node', $this->nodeTypeWithTags->id(), 'default')
95       ->setComponent($this->tagFieldName, [
96         'type' => 'entity_reference_autocomplete_tags',
97       ])
98       ->save();
99
100     entity_get_display('node', $this->nodeTypeWithTags->id(), 'default')
101       ->setComponent($this->tagFieldName, [
102         'type' => 'entity_reference_label',
103         'weight' => 10,
104       ])
105       ->save();
106     entity_get_display('node', $this->nodeTypeWithTags->id(), 'teaser')
107       ->setComponent('field_views_testing_tags', [
108         'type' => 'entity_reference_label',
109         'weight' => 10,
110       ])
111       ->save();
112   }
113
114   /**
115    * Tests the "tagged with" functionality.
116    */
117   public function testTaggedWith() {
118     // In this test we will only create nodes that have an instance of the tag
119     // field.
120     $node_add_path = 'node/add/' . $this->nodeTypeWithTags->id();
121
122     // Create three nodes, with different tags.
123     $edit = [];
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'));
127     $edit = [];
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'));
131     $edit = [];
132     $edit['title[0][value]'] = $node_no_tags_title = $this->randomMachineName();
133     $this->drupalPostForm($node_add_path, $edit, t('Save'));
134
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".
137     $view1 = [];
138     // First select the node type and update the form so the correct tag field
139     // is used.
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);
158
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".
161     $view2 = [];
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);
178   }
179
180   /**
181    * Tests that the "tagged with" form element only shows for node types that support it.
182    */
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);
198
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(),
205       'settings' => [
206         'handler' => 'default',
207         'handler_settings' => [
208           'target_bundles' => [
209             $this->tagVocabulary->id() => $this->tagVocabulary->id(),
210           ],
211           'auto_create' => TRUE,
212         ],
213       ],
214     ])->save();
215     entity_get_form_display('node', $this->nodeTypeWithoutTags->id(), 'default')
216       ->setComponent($this->tagFieldName, [
217         'type' => 'entity_reference_autocomplete_tags',
218       ])
219       ->save();
220
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);
227   }
228
229 }