From 87e995edfe128765a0eb7fc07b0629f63059b19e Mon Sep 17 00:00:00 2001
From: nod_ <nod_@598310.no-reply.drupal.org>
Date: Wed, 21 Aug 2024 21:41:10 +0200
Subject: [PATCH] Issue #3469335 by catch, smustgrave: Speed up PageCacheTest

(cherry picked from commit de6e4c69e54ccef1b4ca3f12d7fc0ee7736e3789)
---
 .../tests/src/Functional/PageCacheTest.php    | 54 +++++++++++++++----
 1 file changed, 44 insertions(+), 10 deletions(-)

diff --git a/core/modules/page_cache/tests/src/Functional/PageCacheTest.php b/core/modules/page_cache/tests/src/Functional/PageCacheTest.php
index 6d12f1b09c85..0fdf598672c4 100644
--- a/core/modules/page_cache/tests/src/Functional/PageCacheTest.php
+++ b/core/modules/page_cache/tests/src/Functional/PageCacheTest.php
@@ -53,7 +53,7 @@ protected function setUp(): void {
    * Since tag based invalidation works, we know that our tag properly
    * persisted.
    */
-  public function testPageCacheTags(): void {
+  protected function testPageCacheTags(): void {
     $this->enablePageCaching();
 
     $path = 'system-test/cache_tags_page';
@@ -85,7 +85,7 @@ public function testPageCacheTags(): void {
   /**
    * Tests that the page cache doesn't depend on cacheability headers.
    */
-  public function testPageCacheTagsIndependentFromCacheabilityHeaders(): void {
+  protected function testPageCacheTagsIndependentFromCacheabilityHeaders(): void {
     // Disable the cacheability headers.
     $this->setContainerParameter('http.response.debug_cacheability_headers', FALSE);
     $this->rebuildContainer();
@@ -216,12 +216,38 @@ public function testConditionalRequests(): void {
     $this->assertSession()->statusCodeEquals(200);
     // Verify that absence of Page was not cached.
     $this->assertSession()->responseHeaderDoesNotExist('X-Drupal-Cache');
+    $this->drupalLogout();
   }
 
   /**
-   * Tests cache headers.
+   * Tests page caching.
    */
   public function testPageCache(): void {
+    $this->testCacheableWithCustomCacheControl();
+    \Drupal::service('cache.page')->deleteAll();
+    $this->testPageCacheAnonymous403404();
+    \Drupal::service('cache.page')->deleteAll();
+    $this->testCacheabilityOfRedirectResponses();
+    \Drupal::service('cache.page')->deleteAll();
+    $this->testNoUrlNormalization();
+    \Drupal::service('cache.page')->deleteAll();
+    $this->testPageCacheHeaders();
+    \Drupal::service('cache.page')->deleteAll();
+    $this->testPageCacheWithoutVaryCookie();
+    \Drupal::service('cache.page')->deleteAll();
+    $this->testPageCacheTags();
+    \Drupal::service('cache.page')->deleteAll();
+    $this->testPageCacheAnonymousRolePermissions();
+    \Drupal::service('cache.page')->deleteAll();
+    $this->testHead();
+    \Drupal::service('cache.page')->deleteAll();
+    $this->testPageCacheTagsIndependentFromCacheabilityHeaders();
+  }
+
+  /**
+   * Tests page cache headers.
+   */
+  protected function testPageCacheHeaders(): void {
     $this->enablePageCaching();
 
     // Fill the cache.
@@ -279,7 +305,7 @@ public function testPageCache(): void {
    * This test verifies that, and it verifies that it does not happen for other
    * roles.
    */
-  public function testPageCacheAnonymousRolePermissions(): void {
+  protected function testPageCacheAnonymousRolePermissions(): void {
     $this->enablePageCaching();
 
     $content_url = Url::fromRoute('system_test.permission_dependent_content');
@@ -324,12 +350,13 @@ public function testPageCacheAnonymousRolePermissions(): void {
     $this->drupalGet($route_access_url);
     $this->assertCacheContext('user.permissions');
     $this->assertSession()->responseHeaderNotContains('X-Drupal-Cache-Tags', 'config:user.role.authenticated');
+    $this->drupalLogout();
   }
 
   /**
    * Tests the 4xx-response cache tag is added and invalidated.
    */
-  public function testPageCacheAnonymous403404(): void {
+  protected function testPageCacheAnonymous403404(): void {
     $admin_url = Url::fromRoute('system.admin');
     $invalid_url = 'foo/does_not_exist';
     $tests = [
@@ -400,12 +427,18 @@ public function testPageCacheAnonymous403404(): void {
       $this->assertSession()->statusCodeEquals($code);
       $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'MISS');
     }
+    // Restore 403 and 404 caching.
+    $settings['settings']['cache_ttl_4xx'] = (object) [
+      'value' => 3600,
+      'required' => TRUE,
+    ];
+    $this->writeSettings($settings);
   }
 
   /**
    * Tests the omit_vary_cookie setting.
    */
-  public function testPageCacheWithoutVaryCookie(): void {
+  protected function testPageCacheWithoutVaryCookie(): void {
     $this->enablePageCaching();
 
     $settings['settings']['omit_vary_cookie'] = (object) [
@@ -507,7 +540,7 @@ public function testCacheableResponseResponses(): void {
   /**
    * Tests that HEAD requests are treated the same as GET requests.
    */
-  public function testHead(): void {
+  protected function testHead(): void {
     /** @var \GuzzleHttp\ClientInterface $client */
     $client = $this->getSession()->getDriver()->getClient()->getClient();
 
@@ -537,7 +570,7 @@ public function testHead(): void {
   /**
    * Tests a cacheable response with custom cache control.
    */
-  public function testCacheableWithCustomCacheControl(): void {
+  protected function testCacheableWithCustomCacheControl(): void {
     $this->enablePageCaching();
 
     $this->drupalGet('/system-test/custom-cache-control');
@@ -548,7 +581,7 @@ public function testCacheableWithCustomCacheControl(): void {
   /**
    * Tests that the Cache-Control header is added by FinishResponseSubscriber.
    */
-  public function testCacheabilityOfRedirectResponses(): void {
+  protected function testCacheabilityOfRedirectResponses(): void {
     $this->enablePageCaching();
 
     $this->getSession()->getDriver()->getClient()->followRedirects(FALSE);
@@ -561,12 +594,13 @@ public function testCacheabilityOfRedirectResponses(): void {
         $this->assertCacheMaxAge(300);
       }
     }
+    $this->getSession()->getDriver()->getClient()->followRedirects(TRUE);
   }
 
   /**
    * Tests that URLs are cached in a not normalized form.
    */
-  public function testNoUrlNormalization(): void {
+  protected function testNoUrlNormalization(): void {
     // Use absolute URLs to avoid any processing.
     $url = Url::fromRoute('<front>')->setAbsolute()->toString();
 
-- 
GitLab