View source
<?php
namespace Drupal\Tests\search\Functional;
use Drupal\Core\Cache\Cache;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
class SearchPageCacheTagsTest extends BrowserTestBase {
use AssertPageCacheContextsAndTagsTrait;
protected static $modules = [
'node',
'search',
];
protected $defaultTheme = 'stark';
protected $dumpHeaders = TRUE;
protected $searchingUser;
protected $node;
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$this->searchingUser = $this
->drupalCreateUser([
'search content',
'access user profiles',
]);
$this->node = $this
->drupalCreateNode([
'title' => 'bike shed shop',
]);
$this->node
->setOwner($this->searchingUser);
$this->node
->save();
$this->container
->get('plugin.manager.search')
->createInstance('node_search')
->updateIndex();
}
public function testSearchText() {
$this
->drupalLogin($this->searchingUser);
$this
->drupalGet('search/node');
$this
->assertCacheTag('config:search.page.node_search');
$this
->assertCacheTag('search_index:node_search');
$this
->assertCacheTag('node_list');
$edit = [];
$edit['keys'] = 'bike shed';
$this
->drupalPostForm('search/node', $edit, t('Search'));
$this
->assertText('bike shed shop');
$this
->assertCacheTag('config:search.page.node_search');
$this
->assertCacheTag('search_index');
$this
->assertCacheTag('search_index:node_search');
$this
->assertCacheTag('node:1');
$this
->assertCacheTag('user:2');
$this
->assertCacheTag('rendered');
$this
->assertCacheTag('http_response');
$this
->assertCacheTag('node_list');
$this->node->title = 'bike shop';
$this->node
->save();
$this
->drupalPostForm('search/node', $edit, t('Search'));
$this
->assertText('bike shop');
$this
->assertCacheTag('config:search.page.node_search');
$this
->assertCacheTag('search_index');
$this
->assertCacheTag('search_index:node_search');
$this
->assertCacheTag('node:1');
$this
->assertCacheTag('user:2');
$this
->assertCacheTag('rendered');
$this
->assertCacheTag('http_response');
$this
->assertCacheTag('node_list');
$this->node
->delete();
$this
->drupalPostForm('search/node', $edit, t('Search'));
$this
->assertText('Your search yielded no results.');
$this
->assertCacheTag('config:search.page.node_search');
$this
->assertCacheTag('search_index');
$this
->assertCacheTag('search_index:node_search');
$this
->assertCacheTag('node_list');
$this
->drupalGet('search/user');
$this
->assertCacheTag('config:search.page.user_search');
$this
->assertCacheTag('user_list');
$this
->assertSession()
->responseHeaderNotContains('X-Drupal-Cache-Tags', 'search_index');
$this
->assertSession()
->responseHeaderNotContains('X-Drupal-Cache-Tags', 'search_index:user_search');
$edit['keys'] = $this->searchingUser
->getAccountName();
$this
->drupalPostForm('search/user', $edit, t('Search'));
$this
->assertCacheTag('config:search.page.user_search');
$this
->assertCacheTag('user_list');
$this
->assertCacheTag('user:2');
$this
->assertSession()
->responseHeaderNotContains('X-Drupal-Cache-Tags', 'search_index');
$this
->assertSession()
->responseHeaderNotContains('X-Drupal-Cache-Tags', 'search_index:user_search');
}
public function testSearchTagsBubbling() {
$this->container
->get('module_installer')
->install([
'field_ui',
'entity_reference',
]);
$this
->resetAll();
$type_name = 'entity_reference_test';
$type = $this
->drupalCreateContentType([
'name' => $type_name,
'type' => $type_name,
]);
$bundle_path = 'admin/structure/types/manage/' . $type
->id();
$admin_user = $this
->drupalCreateUser([
'access content',
'create ' . $type_name . ' content',
'administer node fields',
'administer node display',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet($bundle_path . '/fields/add-field');
$this
->drupalPostForm(NULL, [
'label' => 'Test label',
'field_name' => 'test__ref',
'new_storage_type' => 'entity_reference',
], t('Save and continue'));
$this
->drupalPostForm(NULL, [], t('Save field settings'));
$edit = [
'title[0][value]' => 'Llama shop',
'field_test__ref[0][target_id]' => $this->node
->getTitle(),
];
$this
->drupalPostForm('node/add/' . $type
->id(), $edit, t('Save'));
$this
->drupalGet('node/2');
$this
->assertText('bike shed shop');
$this->container
->get('plugin.manager.search')
->createInstance('node_search')
->updateIndex();
$this
->drupalLogin($this->searchingUser);
$default_search_tags = [
'config:search.page.node_search',
'search_index',
'search_index:node_search',
'http_response',
'rendered',
'node_list',
];
$edit = [];
$edit['keys'] = 'shop';
$this
->drupalPostForm('search/node', $edit, t('Search'));
$this
->assertText('bike shed shop');
$this
->assertText('Llama shop');
$expected_cache_tags = Cache::mergeTags($default_search_tags, [
'node:1',
'user:2',
'node:2',
'user:3',
'node_view',
'config:filter.format.plain_text',
]);
$this
->assertCacheTags($expected_cache_tags);
$edit = [];
$edit['keys'] = 'Llama';
$this
->drupalPostForm('search/node', $edit, t('Search'));
$this
->assertText('Llama shop');
$expected_cache_tags = Cache::mergeTags($default_search_tags, [
'node:1',
'node:2',
'user:3',
'node_view',
]);
$this
->assertCacheTags($expected_cache_tags);
}
}