PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/core/modules/system/lib/Drupal/system/Tests/Bootstrap/PageCacheTest.php

https://bitbucket.org/aswinvk28/smartpan-stock-drupal
PHP | 250 lines | 159 code | 39 blank | 52 comment | 0 complexity | bc9b4bebe07b2e0097a305f71db930da MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * @file
  4. * Definition of Drupal\system\Tests\Bootstrap\PageCacheTest.
  5. */
  6. namespace Drupal\system\Tests\Bootstrap;
  7. use Symfony\Component\Routing\RequestContext;
  8. use Drupal\simpletest\WebTestBase;
  9. use Drupal\Core\Cache\Cache;
  10. /**
  11. * Enables the page cache and tests it with various HTTP requests.
  12. */
  13. class PageCacheTest extends WebTestBase {
  14. protected $dumpHeaders = TRUE;
  15. /**
  16. * Modules to enable.
  17. *
  18. * @var array
  19. */
  20. public static $modules = array('test_page_test', 'system_test');
  21. public static function getInfo() {
  22. return array(
  23. 'name' => 'Page cache test',
  24. 'description' => 'Enable the page cache and test it with various HTTP requests.',
  25. 'group' => 'Bootstrap'
  26. );
  27. }
  28. function setUp() {
  29. parent::setUp();
  30. \Drupal::config('system.site')
  31. ->set('name', 'Drupal')
  32. ->set('page.front', 'test-page')
  33. ->save();
  34. }
  35. /**
  36. * Test that cache tags are properly persisted.
  37. *
  38. * Since tag based invalidation works, we know that our tag properly
  39. * persisted.
  40. */
  41. function testPageCacheTags() {
  42. $config = \Drupal::config('system.performance');
  43. $config->set('cache.page.use_internal', 1);
  44. $config->set('cache.page.max_age', 300);
  45. $config->save();
  46. $path = 'system-test/cache_tags_page';
  47. $tags = array('system_test_cache_tags_page' => TRUE);
  48. $this->drupalGet($path);
  49. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
  50. // Verify a cache hit, but also the presence of the correct cache tags.
  51. $this->drupalGet($path);
  52. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
  53. $cid_parts = array(url($path, array('absolute' => TRUE)), 'html');
  54. $cid = sha1(implode(':', $cid_parts));
  55. $cache_entry = \Drupal::cache('page')->get($cid);
  56. $this->assertIdentical($cache_entry->tags, array('content:1', 'system_test_cache_tags_page:1'));
  57. Cache::invalidateTags($tags);
  58. $this->drupalGet($path);
  59. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
  60. }
  61. /**
  62. * Tests support for different cache items with different Accept headers.
  63. */
  64. function testAcceptHeaderRequests() {
  65. $config = \Drupal::config('system.performance');
  66. $config->set('cache.page.use_internal', 1);
  67. $config->set('cache.page.max_age', 300);
  68. $config->save();
  69. $url_generator = \Drupal::urlGenerator();
  70. $url_generator->setContext(new RequestContext());
  71. $accept_header_cache_uri = $url_generator->getPathFromRoute('system_test.page_cache_accept_header');
  72. $json_accept_header = array('Accept: application/json');
  73. $this->drupalGet($accept_header_cache_uri);
  74. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'HTML page was not yet cached.');
  75. $this->drupalGet($accept_header_cache_uri);
  76. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'HTML page was cached.');
  77. $this->assertRaw('<p>oh hai this is html.</p>', 'The correct HTML response was returned.');
  78. $this->drupalGet($accept_header_cache_uri, array(), $json_accept_header);
  79. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Json response was not yet cached.');
  80. $this->drupalGet($accept_header_cache_uri, array(), $json_accept_header);
  81. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Json response was cached.');
  82. $this->assertRaw('{"content":"oh hai this is json"}', 'The correct Json response was returned.');
  83. }
  84. /**
  85. * Tests support of requests with If-Modified-Since and If-None-Match headers.
  86. */
  87. function testConditionalRequests() {
  88. $config = \Drupal::config('system.performance');
  89. $config->set('cache.page.use_internal', 1);
  90. $config->set('cache.page.max_age', 300);
  91. $config->save();
  92. // Fill the cache.
  93. $this->drupalGet('');
  94. // Verify the page is not printed twice when the cache is cold.
  95. $this->assertNoPattern('#<html.*<html#');
  96. $this->drupalHead('');
  97. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
  98. $etag = $this->drupalGetHeader('ETag');
  99. $last_modified = $this->drupalGetHeader('Last-Modified');
  100. $this->drupalGet('', array(), array('If-Modified-Since: ' . $last_modified, 'If-None-Match: ' . $etag));
  101. $this->assertResponse(304, 'Conditional request returned 304 Not Modified.');
  102. $this->drupalGet('', array(), array('If-Modified-Since: ' . gmdate(DATE_RFC822, strtotime($last_modified)), 'If-None-Match: ' . $etag));
  103. $this->assertResponse(304, 'Conditional request with obsolete If-Modified-Since date returned 304 Not Modified.');
  104. $this->drupalGet('', array(), array('If-Modified-Since: ' . gmdate(DATE_RFC850, strtotime($last_modified)), 'If-None-Match: ' . $etag));
  105. $this->assertResponse(304, 'Conditional request with obsolete If-Modified-Since date returned 304 Not Modified.');
  106. $this->drupalGet('', array(), array('If-Modified-Since: ' . $last_modified));
  107. // Verify the page is not printed twice when the cache is warm.
  108. $this->assertNoPattern('#<html.*<html#');
  109. $this->assertResponse(200, 'Conditional request without If-None-Match returned 200 OK.');
  110. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
  111. $this->drupalGet('', array(), array('If-Modified-Since: ' . gmdate(DATE_RFC1123, strtotime($last_modified) + 1), 'If-None-Match: ' . $etag));
  112. $this->assertResponse(200, 'Conditional request with new a If-Modified-Since date newer than Last-Modified returned 200 OK.');
  113. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
  114. $user = $this->drupalCreateUser();
  115. $this->drupalLogin($user);
  116. $this->drupalGet('', array(), array('If-Modified-Since: ' . $last_modified, 'If-None-Match: ' . $etag));
  117. $this->assertResponse(200, 'Conditional request returned 200 OK for authenticated user.');
  118. $this->assertFalse($this->drupalGetHeader('X-Drupal-Cache'), 'Absense of Page was not cached.');
  119. }
  120. /**
  121. * Tests cache headers.
  122. */
  123. function testPageCache() {
  124. $config = \Drupal::config('system.performance');
  125. $config->set('cache.page.use_internal', 1);
  126. $config->set('cache.page.max_age', 300);
  127. $config->set('response.gzip', 1);
  128. $config->save();
  129. // Fill the cache.
  130. $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Foo', 'value' => 'bar')));
  131. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
  132. $this->assertEqual(strtolower($this->drupalGetHeader('Vary')), 'cookie,accept-encoding', 'Vary header was sent.');
  133. // Symfony's Response logic determines a specific order for the subvalues
  134. // of the Cache-Control header, even if they are explicitly passed in to
  135. // the response header bag in a different order.
  136. $this->assertEqual($this->drupalGetHeader('Cache-Control'), 'max-age=300, public', 'Cache-Control header was sent.');
  137. $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.');
  138. $this->assertEqual($this->drupalGetHeader('Foo'), 'bar', 'Custom header was sent.');
  139. // Check cache.
  140. $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Foo', 'value' => 'bar')));
  141. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
  142. $this->assertEqual(strtolower($this->drupalGetHeader('Vary')), 'cookie,accept-encoding', 'Vary: Cookie header was sent.');
  143. $this->assertEqual($this->drupalGetHeader('Cache-Control'), 'max-age=300, public', 'Cache-Control header was sent.');
  144. $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.');
  145. $this->assertEqual($this->drupalGetHeader('Foo'), 'bar', 'Custom header was sent.');
  146. // Check replacing default headers.
  147. $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Expires', 'value' => 'Fri, 19 Nov 2008 05:00:00 GMT')));
  148. $this->assertEqual($this->drupalGetHeader('Expires'), 'Fri, 19 Nov 2008 05:00:00 GMT', 'Default header was replaced.');
  149. $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Vary', 'value' => 'User-Agent')));
  150. $this->assertEqual(strtolower($this->drupalGetHeader('Vary')), 'user-agent,accept-encoding', 'Default header was replaced.');
  151. // Check that authenticated users bypass the cache.
  152. $user = $this->drupalCreateUser();
  153. $this->drupalLogin($user);
  154. $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Foo', 'value' => 'bar')));
  155. $this->assertFalse($this->drupalGetHeader('X-Drupal-Cache'), 'Caching was bypassed.');
  156. $this->assertTrue(strpos(strtolower($this->drupalGetHeader('Vary')), 'cookie') === FALSE, 'Vary: Cookie header was not sent.');
  157. $this->assertEqual($this->drupalGetHeader('Cache-Control'), 'must-revalidate, no-cache, post-check=0, pre-check=0, private', 'Cache-Control header was sent.');
  158. $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.');
  159. $this->assertEqual($this->drupalGetHeader('Foo'), 'bar', 'Custom header was sent.');
  160. // Check the omit_vary_cookie setting.
  161. $this->drupalLogout();
  162. $settings['settings']['omit_vary_cookie'] = (object) array(
  163. 'value' => TRUE,
  164. 'required' => TRUE,
  165. );
  166. $this->writeSettings($settings);
  167. $this->drupalGet('system-test/set-header', array('query' => array('name' => 'Foo', 'value' => 'bar')));
  168. $this->assertTrue(strpos($this->drupalGetHeader('Vary'), 'Cookie') === FALSE, 'Vary: Cookie header was not sent.');
  169. }
  170. /**
  171. * Tests page compression.
  172. *
  173. * The test should pass even if zlib.output_compression is enabled in php.ini,
  174. * .htaccess or similar, or if compression is done outside PHP, e.g. by the
  175. * mod_deflate Apache module.
  176. */
  177. function testPageCompression() {
  178. $config = \Drupal::config('system.performance');
  179. $config->set('cache.page.use_internal', 1);
  180. $config->set('cache.page.max_age', 300);
  181. $config->set('response.gzip', 1);
  182. $config->save();
  183. // Fill the cache and verify that output is compressed.
  184. $this->drupalGet('', array(), array('Accept-Encoding: gzip,deflate'));
  185. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
  186. $this->drupalSetContent(gzinflate(substr($this->drupalGetContent(), 10, -8)));
  187. $this->assertRaw('</html>', 'Page was gzip compressed.');
  188. // Verify that cached output is compressed.
  189. $this->drupalGet('', array(), array('Accept-Encoding: gzip,deflate'));
  190. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
  191. $this->assertEqual($this->drupalGetHeader('Content-Encoding'), 'gzip', 'A Content-Encoding header was sent.');
  192. $this->drupalSetContent(gzinflate(substr($this->drupalGetContent(), 10, -8)));
  193. $this->assertRaw('</html>', 'Page was gzip compressed.');
  194. // Verify that a client without compression support gets an uncompressed page.
  195. $this->drupalGet('');
  196. $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
  197. $this->assertFalse($this->drupalGetHeader('Content-Encoding'), 'A Content-Encoding header was not sent.');
  198. $this->assertTitle(t('Test page | @site-name', array('@site-name' => \Drupal::config('system.site')->get('name'))), 'Site title matches.');
  199. $this->assertRaw('</html>', 'Page was not compressed.');
  200. // Disable compression mode.
  201. $config->set('response.gzip', 0);
  202. $config->save();
  203. // Verify if cached page is still available for a client with compression support.
  204. $this->drupalGet('', array(), array('Accept-Encoding: gzip,deflate'));
  205. $this->drupalSetContent(gzinflate(substr($this->drupalGetContent(), 10, -8)));
  206. $this->assertRaw('</html>', 'Page was delivered after compression mode is changed (compression support enabled).');
  207. // Verify if cached page is still available for a client without compression support.
  208. $this->drupalGet('');
  209. $this->assertRaw('</html>', 'Page was delivered after compression mode is changed (compression support disabled).');
  210. }
  211. }