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() : void {
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
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'config:search.page.node_search');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'search_index:node_search');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'node_list');
$edit = [];
$edit['keys'] = 'bike shed';
$this
->drupalGet('search/node');
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->pageTextContains('bike shed shop');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'config:search.page.node_search');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'search_index');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'search_index:node_search');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'node:1');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'user:2');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'rendered');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'http_response');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'node_list');
$this->node->title = 'bike shop';
$this->node
->save();
$this
->drupalGet('search/node');
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->pageTextContains('bike shop');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'config:search.page.node_search');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'search_index');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'search_index:node_search');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'node:1');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'user:2');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'rendered');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'http_response');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'node_list');
$this->node
->delete();
$this
->drupalGet('search/node');
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->pageTextContains('Your search yielded no results.');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'config:search.page.node_search');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'search_index');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'search_index:node_search');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'node_list');
$this
->drupalGet('search/user');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'config:search.page.user_search');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', '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
->drupalGet('search/user');
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'config:search.page.user_search');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'user_list');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', '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',
]);
$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
->submitForm([
'label' => 'Test label',
'field_name' => 'test__ref',
'new_storage_type' => 'entity_reference',
], 'Save and continue');
$this
->submitForm([], 'Save field settings');
$edit = [
'title[0][value]' => 'Llama shop',
'field_test__ref[0][target_id]' => $this->node
->getTitle(),
];
$this
->drupalGet('node/add/' . $type
->id());
$this
->submitForm($edit, 'Save');
$this
->drupalGet('node/2');
$this
->assertSession()
->pageTextContains('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
->drupalGet('search/node');
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->pageTextContains('bike shed shop');
$this
->assertSession()
->pageTextContains('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
->drupalGet('search/node');
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->pageTextContains('Llama shop');
$expected_cache_tags = Cache::mergeTags($default_search_tags, [
'node:1',
'node:2',
'user:3',
'node_view',
]);
$this
->assertCacheTags($expected_cache_tags);
}
}