d11304bb4b66780b29d8528419e128255eb34176
[yaffs-website] / web / core / modules / search / tests / src / Functional / SearchPageCacheTagsTest.php
1 <?php
2
3 namespace Drupal\Tests\search\Functional;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
8
9 /**
10  * Tests the search_page entity cache tags on the search results pages.
11  *
12  * @group search
13  */
14 class SearchPageCacheTagsTest extends BrowserTestBase {
15
16   use AssertPageCacheContextsAndTagsTrait;
17
18   /**
19    * {@inheritdoc}
20    */
21   protected static $modules = ['node', 'search'];
22
23   /**
24    * {@inheritdoc}
25    */
26   protected $dumpHeaders = TRUE;
27
28   /**
29    * A user with permission to search content.
30    *
31    * @var \Drupal\user\UserInterface
32    */
33   protected $searchingUser;
34
35   /**
36    * A node that is indexed by the search module.
37    *
38    * @var \Drupal\node\NodeInterface
39    */
40   protected $node;
41
42   /**
43    * {@inheritdoc}
44    */
45   protected function setUp() {
46     parent::setUp();
47
48     $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
49
50     // Create user.
51     $this->searchingUser = $this->drupalCreateUser(['search content', 'access user profiles']);
52
53     // Create a node and update the search index.
54     $this->node = $this->drupalCreateNode(['title' => 'bike shed shop']);
55     $this->node->setOwner($this->searchingUser);
56     $this->node->save();
57     $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
58     search_update_totals();
59   }
60
61   /**
62    * Tests the presence of the expected cache tag in various situations.
63    */
64   public function testSearchText() {
65     $this->drupalLogin($this->searchingUser);
66
67     // Initial page for searching nodes.
68     $this->drupalGet('search/node');
69     $this->assertCacheTag('config:search.page.node_search');
70     $this->assertCacheTag('search_index:node_search');
71     $this->assertCacheTag('node_list');
72
73     // Node search results.
74     $edit = [];
75     $edit['keys'] = 'bike shed';
76     $this->drupalPostForm('search/node', $edit, t('Search'));
77     $this->assertText('bike shed shop');
78     $this->assertCacheTag('config:search.page.node_search');
79     $this->assertCacheTag('search_index');
80     $this->assertCacheTag('search_index:node_search');
81     $this->assertCacheTag('node:1');
82     $this->assertCacheTag('user:2');
83     $this->assertCacheTag('rendered');
84     $this->assertCacheTag('http_response');
85     $this->assertCacheTag('node_list');
86
87     // Updating a node should invalidate the search plugin's index cache tag.
88     $this->node->title = 'bike shop';
89     $this->node->save();
90     $this->drupalPostForm('search/node', $edit, t('Search'));
91     $this->assertText('bike shop');
92     $this->assertCacheTag('config:search.page.node_search');
93     $this->assertCacheTag('search_index');
94     $this->assertCacheTag('search_index:node_search');
95     $this->assertCacheTag('node:1');
96     $this->assertCacheTag('user:2');
97     $this->assertCacheTag('rendered');
98     $this->assertCacheTag('http_response');
99     $this->assertCacheTag('node_list');
100
101     // Deleting a node should invalidate the search plugin's index cache tag.
102     $this->node->delete();
103     $this->drupalPostForm('search/node', $edit, t('Search'));
104     $this->assertText('Your search yielded no results.');
105     $this->assertCacheTag('config:search.page.node_search');
106     $this->assertCacheTag('search_index');
107     $this->assertCacheTag('search_index:node_search');
108     $this->assertCacheTag('node_list');
109
110     // Initial page for searching users.
111     $this->drupalGet('search/user');
112     $this->assertCacheTag('config:search.page.user_search');
113     $this->assertCacheTag('user_list');
114     $this->assertSession()->responseHeaderNotContains('X-Drupal-Cache-Tags', 'search_index');
115     $this->assertSession()->responseHeaderNotContains('X-Drupal-Cache-Tags', 'search_index:user_search');
116
117     // User search results.
118     $edit['keys'] = $this->searchingUser->getUsername();
119     $this->drupalPostForm('search/user', $edit, t('Search'));
120     $this->assertCacheTag('config:search.page.user_search');
121     $this->assertCacheTag('user_list');
122     $this->assertCacheTag('user:2');
123     $this->assertSession()->responseHeaderNotContains('X-Drupal-Cache-Tags', 'search_index');
124     $this->assertSession()->responseHeaderNotContains('X-Drupal-Cache-Tags', 'search_index:user_search');
125   }
126
127   /**
128    * Tests the presence of expected cache tags with referenced entities.
129    */
130   public function testSearchTagsBubbling() {
131
132     // Install field UI and entity reference modules.
133     $this->container->get('module_installer')->install(['field_ui', 'entity_reference']);
134     $this->resetAll();
135
136     // Creates a new content type that will have an entity reference.
137     $type_name = 'entity_reference_test';
138     $type = $this->drupalCreateContentType(['name' => $type_name, 'type' => $type_name]);
139
140     $bundle_path = 'admin/structure/types/manage/' . $type->id();
141
142     // Create test user.
143     $admin_user = $this->drupalCreateUser([
144       'access content',
145       'create ' . $type_name . ' content',
146       'administer node fields',
147       'administer node display',
148
149     ]);
150     $this->drupalLogin($admin_user);
151
152     // First step: 'Add new field' on the 'Manage fields' page.
153     $this->drupalGet($bundle_path . '/fields/add-field');
154     $this->drupalPostForm(NULL, [
155       'label' => 'Test label',
156       'field_name' => 'test__ref',
157       'new_storage_type' => 'entity_reference',
158     ], t('Save and continue'));
159
160     // Second step: 'Field settings' form.
161     $this->drupalPostForm(NULL, [], t('Save field settings'));
162
163     // Create a new node of our newly created node type and fill in the entity
164     // reference field.
165     $edit = [
166       'title[0][value]' => 'Llama shop',
167       'field_test__ref[0][target_id]' => $this->node->getTitle(),
168     ];
169     $this->drupalPostForm('node/add/' . $type->id(), $edit, t('Save'));
170
171     // Test that the value of the entity reference field is shown.
172     $this->drupalGet('node/2');
173     $this->assertText('bike shed shop');
174
175     // Refresh the search index.
176     $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
177     search_update_totals();
178
179     // Log in with searching user again.
180     $this->drupalLogin($this->searchingUser);
181
182     // Default search cache tags.
183     $default_search_tags = [
184       'config:search.page.node_search',
185       'search_index',
186       'search_index:node_search',
187       'http_response',
188       'rendered',
189       'node_list',
190     ];
191
192     // Node search results for shop, should return node:1 (bike shed shop) and
193     // node:2 (Llama shop). The related authors cache tags should be visible as
194     // well.
195     $edit = [];
196     $edit['keys'] = 'shop';
197     $this->drupalPostForm('search/node', $edit, t('Search'));
198     $this->assertText('bike shed shop');
199     $this->assertText('Llama shop');
200     $expected_cache_tags = Cache::mergeTags($default_search_tags, [
201       'node:1',
202       'user:2',
203       'node:2',
204       'user:3',
205       'node_view',
206       'config:filter.format.plain_text',
207     ]);
208     $this->assertCacheTags($expected_cache_tags);
209
210     // Only get the new node in the search results, should result in node:1,
211     // node:2 and user:3 as cache tags even though only node:1 is shown. This is
212     // because node:2 is reference in node:1 as an entity reference.
213     $edit = [];
214     $edit['keys'] = 'Llama';
215     $this->drupalPostForm('search/node', $edit, t('Search'));
216     $this->assertText('Llama shop');
217     $expected_cache_tags = Cache::mergeTags($default_search_tags, [
218       'node:1',
219       'node:2',
220       'user:3',
221       'node_view',
222     ]);
223     $this->assertCacheTags($expected_cache_tags);
224   }
225
226 }