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