Skip to content
Snippets Groups Projects
Commit 40698c6c authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2870457 by ApacheEx, andypost, dawehner, naveenvalecha, vaplas, nlisgo,...

Issue #2870457 by ApacheEx, andypost, dawehner, naveenvalecha, vaplas, nlisgo, Wim Leers, Lendude, alexpott: Convert web tests to browser tests for page_cache module
parent 5d01c252
Branches
Tags
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
<?php <?php
namespace Drupal\page_cache\Tests; namespace Drupal\Tests\page_cache\Functional;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber; use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageInterface;
use Drupal\simpletest\WebTestBase;
use Drupal\node\NodeInterface; use Drupal\node\NodeInterface;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait; use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
use Drupal\Tests\BrowserTestBase;
/** /**
* Enables the page cache and tests its cache tags in various scenarios. * Enables the page cache and tests its cache tags in various scenarios.
* *
* @group Cache * @group Cache
* @see \Drupal\page_cache\Tests\PageCacheTest * @see \Drupal\Tests\page_cache\Functional\PageCacheTest
* @see \Drupal\node\Tests\NodePageCacheTest * @see \Drupal\node\Tests\NodePageCacheTest
* @see \Drupal\menu_ui\Tests\MenuTest::testMenuBlockPageCacheTags() * @see \Drupal\menu_ui\Tests\MenuTest::testMenuBlockPageCacheTags()
*/ */
class PageCacheTagsIntegrationTest extends WebTestBase { class PageCacheTagsIntegrationTest extends BrowserTestBase {
use AssertPageCacheContextsAndTagsTrait; use AssertPageCacheContextsAndTagsTrait;
......
<?php <?php
namespace Drupal\page_cache\Tests; namespace Drupal\Tests\page_cache\Functional;
use Drupal\Component\Datetime\DateTimePlus; use Drupal\Component\Datetime\DateTimePlus;
use Drupal\Core\Site\Settings; use Drupal\Core\Site\Settings;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\entity_test\Entity\EntityTest; use Drupal\entity_test\Entity\EntityTest;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\Cache;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
use Drupal\user\RoleInterface; use Drupal\user\RoleInterface;
/** /**
...@@ -15,7 +16,9 @@ ...@@ -15,7 +16,9 @@
* *
* @group page_cache * @group page_cache
*/ */
class PageCacheTest extends WebTestBase { class PageCacheTest extends BrowserTestBase {
use AssertPageCacheContextsAndTagsTrait;
protected $dumpHeaders = TRUE; protected $dumpHeaders = TRUE;
...@@ -79,7 +82,10 @@ public function testPageCacheTags() { ...@@ -79,7 +82,10 @@ public function testPageCacheTags() {
* Test that the page cache doesn't depend on cacheability headers. * Test that the page cache doesn't depend on cacheability headers.
*/ */
public function testPageCacheTagsIndependentFromCacheabilityHeaders() { public function testPageCacheTagsIndependentFromCacheabilityHeaders() {
$this->setHttpResponseDebugCacheabilityHeaders(FALSE); // Disable the cacheability headers.
$this->setContainerParameter('http.response.debug_cacheability_headers', FALSE);
$this->rebuildContainer();
$this->resetAll();
$path = 'system-test/cache_tags_page'; $path = 'system-test/cache_tags_page';
$tags = ['system_test_cache_tags_page']; $tags = ['system_test_cache_tags_page'];
...@@ -185,33 +191,42 @@ public function testConditionalRequests() { ...@@ -185,33 +191,42 @@ public function testConditionalRequests() {
// Verify the page is not printed twice when the cache is cold. // Verify the page is not printed twice when the cache is cold.
$this->assertNoPattern('#<html.*<html#'); $this->assertNoPattern('#<html.*<html#');
$this->drupalHead(''); $this->drupalGet('');
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.'); $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
$etag = $this->drupalGetHeader('ETag'); $etag = $this->drupalGetHeader('ETag');
$last_modified = $this->drupalGetHeader('Last-Modified'); $last_modified = $this->drupalGetHeader('Last-Modified');
$this->drupalGet('', [], ['If-Modified-Since: ' . $last_modified, 'If-None-Match: ' . $etag]); $this->drupalGet('', [], ['If-Modified-Since' => $last_modified, 'If-None-Match' => $etag]);
$this->assertResponse(304, 'Conditional request returned 304 Not Modified.'); $this->assertResponse(304, 'Conditional request returned 304 Not Modified.');
$this->drupalGet('', [], ['If-Modified-Since: ' . gmdate(DATE_RFC822, strtotime($last_modified)), 'If-None-Match: ' . $etag]); $this->drupalGet('', [], [
'If-Modified-Since' => gmdate(DATE_RFC822, strtotime($last_modified)),
'If-None-Match' => $etag,
]);
$this->assertResponse(304, 'Conditional request with obsolete If-Modified-Since date returned 304 Not Modified.'); $this->assertResponse(304, 'Conditional request with obsolete If-Modified-Since date returned 304 Not Modified.');
$this->drupalGet('', [], ['If-Modified-Since: ' . gmdate(DATE_RFC850, strtotime($last_modified)), 'If-None-Match: ' . $etag]); $this->drupalGet('', [], [
'If-Modified-Since' => gmdate(DATE_RFC850, strtotime($last_modified)),
'If-None-Match' => $etag,
]);
$this->assertResponse(304, 'Conditional request with obsolete If-Modified-Since date returned 304 Not Modified.'); $this->assertResponse(304, 'Conditional request with obsolete If-Modified-Since date returned 304 Not Modified.');
$this->drupalGet('', [], ['If-Modified-Since: ' . $last_modified]); $this->drupalGet('', [], ['If-Modified-Since' => $last_modified, 'If-None-Match' => NULL]);
// Verify the page is not printed twice when the cache is warm. // Verify the page is not printed twice when the cache is warm.
$this->assertNoPattern('#<html.*<html#'); $this->assertNoPattern('#<html.*<html#');
$this->assertResponse(200, 'Conditional request without If-None-Match returned 200 OK.'); $this->assertResponse(200, 'Conditional request without If-None-Match returned 200 OK.');
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.'); $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
$this->drupalGet('', [], ['If-Modified-Since: ' . gmdate(DateTimePlus::RFC7231, strtotime($last_modified) + 1), 'If-None-Match: ' . $etag]); $this->drupalGet('', [], [
'If-Modified-Since' => gmdate(DateTimePlus::RFC7231, strtotime($last_modified) + 1),
'If-None-Match' => $etag,
]);
$this->assertResponse(200, 'Conditional request with new a If-Modified-Since date newer than Last-Modified returned 200 OK.'); $this->assertResponse(200, 'Conditional request with new a If-Modified-Since date newer than Last-Modified returned 200 OK.');
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.'); $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
$user = $this->drupalCreateUser(); $user = $this->drupalCreateUser();
$this->drupalLogin($user); $this->drupalLogin($user);
$this->drupalGet('', [], ['If-Modified-Since: ' . $last_modified, 'If-None-Match: ' . $etag]); $this->drupalGet('', [], ['If-Modified-Since' => $last_modified, 'If-None-Match' => $etag]);
$this->assertResponse(200, 'Conditional request returned 200 OK for authenticated user.'); $this->assertResponse(200, 'Conditional request returned 200 OK for authenticated user.');
$this->assertFalse($this->drupalGetHeader('X-Drupal-Cache'), 'Absence of Page was not cached.'); $this->assertFalse($this->drupalGetHeader('X-Drupal-Cache'), 'Absence of Page was not cached.');
} }
...@@ -515,24 +530,27 @@ public function testCacheableResponseResponses() { ...@@ -515,24 +530,27 @@ public function testCacheableResponseResponses() {
* Tests that HEAD requests are treated the same as GET requests. * Tests that HEAD requests are treated the same as GET requests.
*/ */
public function testHead() { public function testHead() {
/** @var \GuzzleHttp\ClientInterface $client */
$client = $this->getSession()->getDriver()->getClient()->getClient();
// GET, then HEAD. // GET, then HEAD.
$url_a = $this->buildUrl('system-test/set-header', ['query' => ['name' => 'Foo', 'value' => 'bar']]); $url_a = $this->buildUrl('system-test/set-header', ['query' => ['name' => 'Foo', 'value' => 'bar']]);
$response_body = $this->curlExec([CURLOPT_HTTPGET => TRUE, CURLOPT_URL => $url_a, CURLOPT_CUSTOMREQUEST => 'GET', CURLOPT_NOBODY => FALSE]); $response_body = $this->drupalGet($url_a);
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.'); $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
$this->assertEqual($this->drupalGetHeader('Foo'), 'bar', 'Custom header was sent.'); $this->assertEqual($this->drupalGetHeader('Foo'), 'bar', 'Custom header was sent.');
$this->assertEqual('The following header was set: <em class="placeholder">Foo</em>: <em class="placeholder">bar</em>', $response_body); $this->assertEqual('The following header was set: <em class="placeholder">Foo</em>: <em class="placeholder">bar</em>', $response_body);
$response_body = $this->curlExec([CURLOPT_HTTPGET => FALSE, CURLOPT_URL => $url_a, CURLOPT_CUSTOMREQUEST => 'HEAD', CURLOPT_NOBODY => FALSE]); $response = $client->request('HEAD', $url_a);
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.'); $this->assertEqual($response->getHeaderLine('X-Drupal-Cache'), 'HIT', 'Page was cached.');
$this->assertEqual($this->drupalGetHeader('Foo'), 'bar', 'Custom header was sent.'); $this->assertEqual($response->getHeaderLine('Foo'), 'bar', 'Custom header was sent.');
$this->assertEqual('', $response_body); $this->assertEqual('', $response->getBody()->getContents());
// HEAD, then GET. // HEAD, then GET.
$url_b = $this->buildUrl('system-test/set-header', ['query' => ['name' => 'Foo', 'value' => 'baz']]); $url_b = $this->buildUrl('system-test/set-header', ['query' => ['name' => 'Foo', 'value' => 'baz']]);
$response_body = $this->curlExec([CURLOPT_HTTPGET => FALSE, CURLOPT_URL => $url_b, CURLOPT_CUSTOMREQUEST => 'HEAD', CURLOPT_NOBODY => FALSE]); $response = $client->request('HEAD', $url_b);
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.'); $this->assertEqual($response->getHeaderLine('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
$this->assertEqual($this->drupalGetHeader('Foo'), 'baz', 'Custom header was sent.'); $this->assertEqual($response->getHeaderLine('Foo'), 'baz', 'Custom header was sent.');
$this->assertEqual('', $response_body); $this->assertEqual('', $response->getBody()->getContents());
$response_body = $this->curlExec([CURLOPT_HTTPGET => TRUE, CURLOPT_URL => $url_b, CURLOPT_CUSTOMREQUEST => 'GET', CURLOPT_NOBODY => FALSE]); $response_body = $this->drupalGet($url_b);
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.'); $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
$this->assertEqual($this->drupalGetHeader('Foo'), 'baz', 'Custom header was sent.'); $this->assertEqual($this->drupalGetHeader('Foo'), 'baz', 'Custom header was sent.');
$this->assertEqual('The following header was set: <em class="placeholder">Foo</em>: <em class="placeholder">baz</em>', $response_body); $this->assertEqual('The following header was set: <em class="placeholder">Foo</em>: <em class="placeholder">baz</em>', $response_body);
...@@ -577,18 +595,52 @@ public function testNoUrlNormalization() { ...@@ -577,18 +595,52 @@ public function testNoUrlNormalization() {
]; ];
foreach ($tests as list($url_raw, $url_normalized)) { foreach ($tests as list($url_raw, $url_normalized)) {
// Initialize cache on raw URL. // Initialize cache on raw URL.
$this->drupalGet($url_raw); $headers = $this->getHeaders($url_raw);
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS'); $this->assertEquals('MISS', $headers['X-Drupal-Cache']);
// Ensure cache was set. // Ensure cache was set.
$this->drupalGet($url_raw); $headers = $this->getHeaders($url_raw);
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', "Cache was set for {$url_raw} URL."); $this->assertEquals('HIT', $headers['X-Drupal-Cache'], "Cache was set for {$url_raw} URL.");
// Check if the normalized URL is not cached. // Check if the normalized URL is not cached.
$this->drupalGet($url_normalized); $headers = $this->getHeaders($url_normalized);
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', "Cache is missing for {$url_normalized} URL."); $this->assertEquals('MISS', $headers['X-Drupal-Cache'], "Cache is missing for {$url_normalized} URL.");
} }
} }
/**
* Retrieves only the headers for an absolute path.
*
* Executes a cURL request without any modifications to the given URL.
* Note that Guzzle always normalizes URLs which prevents testing all
* possible edge cases.
*
* @param string $url
* URL to request.
*
* @return array
* Array of headers.
*/
protected function getHeaders($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, drupal_generate_test_ua($this->databasePrefix));
$output = curl_exec($ch);
curl_close($ch);
$headers = [];
foreach (explode("\n", $output) as $header) {
if (strpos($header, ':')) {
list($key, $value) = explode(':', $header, 2);
$headers[trim($key)] = trim($value);
}
}
return $headers;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment