Commit 421f994a authored by catch's avatar catch
Browse files

Issue #3164589 by ankithashetty, paulocs, S_Bhandari, Suresh Prabhu Parkala,...

Issue #3164589 by ankithashetty, paulocs, S_Bhandari, Suresh Prabhu Parkala, mondrake, longwave: Replace assertions that use $this->getSession()->getResponseHeader() with WebAssert methods
parent 5552c61c
......@@ -211,8 +211,8 @@ public function testCacheabilityOf401Response() {
$assert_response_cacheability = function ($expected_page_cache_header_value, $expected_dynamic_page_cache_header_value) use ($session, $url) {
$this->drupalGet($url);
$this->assertSession()->statusCodeEquals(401);
$this->assertSame($expected_page_cache_header_value, $session->getResponseHeader('X-Drupal-Cache'));
$this->assertSame($expected_dynamic_page_cache_header_value, $session->getResponseHeader('X-Drupal-Dynamic-Cache'));
$this->assertSession()->responseHeaderEquals('X-Drupal-Cache', $expected_page_cache_header_value);
$this->assertSession()->responseHeaderEquals('X-Drupal-Dynamic-Cache', $expected_dynamic_page_cache_header_value);
};
// 1. First request: cold caches, both Page Cache and Dynamic Page Cache are
......
......@@ -168,28 +168,28 @@ public function testRecentNodeBlock() {
// Ensure that a page that does not have a node context can still be cached,
// the front page is the user page which is already cached from the login
// request above.
$this->assertSame('HIT', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache'));
$this->assertSession()->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'HIT');
$this->drupalGet('node/add/article');
$this->assertText($label, 'Block was displayed on the node/add/article page.');
$this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'session', 'theme', 'url.path', 'url.query_args', 'user', 'route']);
// The node/add/article page is an admin path and currently uncacheable.
$this->assertSame('UNCACHEABLE', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache'));
$this->assertSession()->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'UNCACHEABLE');
$this->drupalGet('node/' . $node1->id());
$this->assertText($label, 'Block was displayed on the node/N when node is of type article.');
$this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'url.site', 'user', 'route', 'timezone']);
$this->assertSame('MISS', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache'));
$this->assertSession()->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'MISS');
$this->drupalGet('node/' . $node1->id());
$this->assertSame('HIT', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache'));
$this->assertSession()->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'HIT');
$this->drupalGet('node/' . $node5->id());
$this->assertNoText($label, 'Block was not displayed on nodes of type page.');
$this->assertCacheContexts(['languages:language_content', 'languages:language_interface', 'theme', 'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT, 'url.site', 'user', 'route', 'timezone']);
$this->assertSame('MISS', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache'));
$this->assertSession()->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'MISS');
$this->drupalGet('node/' . $node5->id());
$this->assertSame('HIT', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache'));
$this->assertSession()->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'HIT');
// Place a block to determine which revision is provided as context
// to blocks.
......
......@@ -4,7 +4,6 @@
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
use Drupal\Component\Render\FormattableMarkup;
/**
* Provides helper methods for page cache tags tests.
......@@ -43,8 +42,7 @@ protected function setUp() {
*/
protected function verifyPageCache(Url $url, $hit_or_miss, $tags = FALSE) {
$this->drupalGet($url);
$message = new FormattableMarkup('Page cache @hit_or_miss for %path.', ['@hit_or_miss' => $hit_or_miss, '%path' => $url->toString()]);
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), $hit_or_miss, $message);
$this->assertSession()->responseHeaderEquals('X-Drupal-Cache', $hit_or_miss);
if ($hit_or_miss === 'HIT' && is_array($tags)) {
$absolute_url = $url->setAbsolute()->toString();
......@@ -68,8 +66,7 @@ protected function verifyPageCache(Url $url, $hit_or_miss, $tags = FALSE) {
*/
protected function verifyDynamicPageCache(Url $url, $hit_or_miss) {
$this->drupalGet($url);
$message = new FormattableMarkup('Dynamic page cache @hit_or_miss for %path.', ['@hit_or_miss' => $hit_or_miss, '%path' => $url->toString()]);
$this->assertSame($hit_or_miss, $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache'), $message);
$this->assertSession()->responseHeaderEquals('X-Drupal-Dynamic-Cache', $hit_or_miss);
}
}
......@@ -39,22 +39,21 @@ public function testFinishResponseSubscriber() {
$this->drupalGet('router_test/test1');
$this->assertRaw('test1', 'The correct string was returned because the route was successful.');
$session = $this->getSession();
// Check expected headers from FinishResponseSubscriber.
$headers = $session->getResponseHeaders();
$this->assertEquals($headers['X-UA-Compatible'], ['IE=edge']);
$this->assertEquals($headers['Content-language'], ['en']);
$this->assertEquals($headers['X-Content-Type-Options'], ['nosniff']);
$this->assertEquals($headers['X-Frame-Options'], ['SAMEORIGIN']);
$this->assertNull($this->drupalGetHeader('Vary'), 'Vary header is not set.');
// Check expected headers from FinishResponseSubscriber.
$this->assertSession()->responseHeaderEquals('X-UA-Compatible', 'IE=edge');
$this->assertSession()->responseHeaderEquals('Content-language', 'en');
$this->assertSession()->responseHeaderEquals('X-Content-Type-Options', 'nosniff');
$this->assertSession()->responseHeaderEquals('X-Frame-Options', 'SAMEORIGIN');
$this->assertSession()->responseHeaderEquals('Vary', NULL);
$this->drupalGet('router_test/test2');
$this->assertRaw('test2', 'The correct string was returned because the route was successful.');
// Check expected headers from FinishResponseSubscriber.
$headers = $session->getResponseHeaders();
$this->assertEqual($headers['X-Drupal-Cache-Contexts'], [implode(' ', $expected_cache_contexts)]);
$this->assertEqual($headers['X-Drupal-Cache-Tags'], ['config:user.role.anonymous http_response rendered']);
$this->assertEqual($headers['X-Drupal-Cache-Max-Age'], ['-1 (Permanent)']);
$this->assertSession()->responseHeaderEquals('X-Drupal-Cache-Contexts', implode(' ', $expected_cache_contexts));
$this->assertSession()->responseHeaderEquals('X-Drupal-Cache-Tags', 'config:user.role.anonymous http_response rendered');
$this->assertSession()->responseHeaderEquals('X-Drupal-Cache-Max-Age', '-1 (Permanent)');
// Confirm that the page wrapping is being added, so we're not getting a
// raw body returned.
$this->assertRaw('</html>', 'Page markup was found.');
......@@ -66,15 +65,13 @@ public function testFinishResponseSubscriber() {
// X-Drupal-Cache-Contexts and X-Drupal-Cache-Tags headers.
// 1. controller result: render array, globally cacheable route access.
$this->drupalGet('router_test/test18');
$headers = $session->getResponseHeaders();
$this->assertEqual($headers['X-Drupal-Cache-Contexts'], [implode(' ', Cache::mergeContexts($renderer_required_cache_contexts, ['url']))]);
$this->assertEqual($headers['X-Drupal-Cache-Tags'], ['config:user.role.anonymous foo http_response rendered']);
$this->assertEqual($headers['X-Drupal-Cache-Max-Age'], ['60']);
$this->assertSession()->responseHeaderEquals('X-Drupal-Cache-Contexts', implode(' ', Cache::mergeContexts($renderer_required_cache_contexts, ['url'])));
$this->assertSession()->responseHeaderEquals('X-Drupal-Cache-Tags', 'config:user.role.anonymous foo http_response rendered');
$this->assertSession()->responseHeaderEquals('X-Drupal-Cache-Max-Age', '60');
// 2. controller result: render array, per-role cacheable route access.
$this->drupalGet('router_test/test19');
$headers = $session->getResponseHeaders();
$this->assertEqual($headers['X-Drupal-Cache-Contexts'], [implode(' ', Cache::mergeContexts($renderer_required_cache_contexts, ['url', 'user.roles']))]);
$this->assertEqual($headers['X-Drupal-Cache-Tags'], ['config:user.role.anonymous foo http_response rendered']);
$this->assertSession()->responseHeaderEquals('X-Drupal-Cache-Contexts', implode(' ', Cache::mergeContexts($renderer_required_cache_contexts, ['url', 'user.roles'])));
$this->assertSession()->responseHeaderEquals('X-Drupal-Cache-Tags', 'config:user.role.anonymous foo http_response rendered');
// 3. controller result: Response object, globally cacheable route access.
$this->drupalGet('router_test/test1');
$headers = $session->getResponseHeaders();
......@@ -89,14 +86,12 @@ public function testFinishResponseSubscriber() {
$this->assertFalse(isset($headers['X-Drupal-Cache-Max-Age']));
// 5. controller result: CacheableResponse object, globally cacheable route access.
$this->drupalGet('router_test/test21');
$headers = $session->getResponseHeaders();
$this->assertEqual($headers['X-Drupal-Cache-Contexts'], ['']);
$this->assertEqual($headers['X-Drupal-Cache-Tags'], ['http_response']);
$this->assertSession()->responseHeaderEquals('X-Drupal-Cache-Contexts', '');
$this->assertSession()->responseHeaderEquals('X-Drupal-Cache-Tags', 'http_response');
// 6. controller result: CacheableResponse object, per-role cacheable route access.
$this->drupalGet('router_test/test22');
$headers = $session->getResponseHeaders();
$this->assertEqual($headers['X-Drupal-Cache-Contexts'], ['user.roles']);
$this->assertEqual($headers['X-Drupal-Cache-Tags'], ['http_response']);
$this->assertSession()->responseHeaderEquals('X-Drupal-Cache-Contexts', 'user.roles');
$this->assertSession()->responseHeaderEquals('X-Drupal-Cache-Tags', 'http_response');
// Finally, verify that the X-Drupal-Cache-Contexts and X-Drupal-Cache-Tags
// headers are not sent when their container parameter is set to FALSE.
......
......@@ -266,9 +266,9 @@ public function testThemeSettingsRenderCacheClear() {
// cleared by installing a theme.
$this->drupalLogout();
$this->drupalGet('');
$this->assertEquals('MISS', $this->getSession()->getResponseHeader('X-Drupal-Cache'));
$this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'MISS');
$this->drupalGet('');
$this->assertEquals('HIT', $this->getSession()->getResponseHeader('X-Drupal-Cache'));
$this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'HIT');
$this->drupalLogin($this->adminUser);
// Save Bartik's theme settings which should invalidate the 'rendered' cache
......@@ -276,7 +276,7 @@ public function testThemeSettingsRenderCacheClear() {
$this->drupalPostForm('admin/appearance/settings/bartik', [], t('Save configuration'));
$this->drupalLogout();
$this->drupalGet('');
$this->assertEquals('MISS', $this->getSession()->getResponseHeader('X-Drupal-Cache'));
$this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'MISS');
}
/**
......
......@@ -70,9 +70,9 @@ public function testCacheIntegration() {
$this->installExtraModules(['dynamic_page_cache']);
$this->drupalLogin($this->adminUser);
$this->drupalGet('test-page');
$this->assertSame('MISS', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache'));
$this->assertSession()->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'MISS');
$this->drupalGet('test-page');
$this->assertSame('HIT', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache'));
$this->assertSession()->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'HIT');
}
/**
......
......@@ -80,7 +80,7 @@ protected function drupalGet($path, array $options = [], array $headers = []) {
$url = preg_replace('|/nojs/|', '/ajax/', $url, 1);
$result = $this->drupalGet($url, $options);
$this->assertSession()->statusCodeEquals(200);
$this->assertEquals('application/json', $this->getSession()->getResponseHeader('Content-Type'));
$this->assertSession()->responseHeaderEquals('Content-Type', 'application/json');
$this->assertNotEmpty(json_decode($result), 'Ensure that the AJAX request returned valid content.');
}
......
......@@ -77,8 +77,7 @@ public function testGoTo() {
'Test-Header' => 'header value',
]);
$this->assertSession()->responseHeaderExists('Test-Header');
$returned_header = $this->getSession()->getResponseHeader('Test-Header');
$this->assertSame('header value', $returned_header);
$this->assertSession()->responseHeaderEquals('Test-Header', 'header value');
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment