From d823d1d28299d7de013e55dc66f3d17d75495dd9 Mon Sep 17 00:00:00 2001
From: Ted Bowman <ted+git@tedbow.com>
Date: Fri, 4 Nov 2022 12:49:43 -0400
Subject: [PATCH] create assertStageEventsLogged

---
 .../EventSubscriber/EventLogSubscriber.php    |  2 +-
 .../tests/src/Build/PackageUpdateTest.php     | 27 +-------
 .../src/Build/TemplateProjectTestBase.php     | 62 +++++++++++++++++++
 3 files changed, 65 insertions(+), 26 deletions(-)

diff --git a/package_manager/tests/modules/package_manager_test_event_logger/src/EventSubscriber/EventLogSubscriber.php b/package_manager/tests/modules/package_manager_test_event_logger/src/EventSubscriber/EventLogSubscriber.php
index 0762317dbc..f94835c2ac 100644
--- a/package_manager/tests/modules/package_manager_test_event_logger/src/EventSubscriber/EventLogSubscriber.php
+++ b/package_manager/tests/modules/package_manager_test_event_logger/src/EventSubscriber/EventLogSubscriber.php
@@ -44,7 +44,7 @@ class EventLogSubscriber implements EventSubscriberInterface {
    *   The event object.
    */
   public function logEventInfo(StageEvent $event): void {
-    \Drupal::logger('package_manager')->info('Event: ' . get_class($event) . ', Stage instance of ' . get_class($event->getStage()) . ', Request time: ' . $this->time->getRequestTime());
+    \Drupal::logger('package_manager_test_event_logger')->info('package_manager_test_event_logger-start: Event: ' . get_class($event) . ', Stage instance of: ' . get_class($event->getStage()) . ':package_manager_test_event_logger-end');
   }
 
   /**
diff --git a/package_manager/tests/src/Build/PackageUpdateTest.php b/package_manager/tests/src/Build/PackageUpdateTest.php
index 24c926c499..e74ff51cdd 100644
--- a/package_manager/tests/src/Build/PackageUpdateTest.php
+++ b/package_manager/tests/src/Build/PackageUpdateTest.php
@@ -2,14 +2,7 @@
 
 namespace Drupal\Tests\package_manager\Build;
 
-use Drupal\package_manager\Event\PostApplyEvent;
-use Drupal\package_manager\Event\PostCreateEvent;
-use Drupal\package_manager\Event\PostDestroyEvent;
-use Drupal\package_manager\Event\PostRequireEvent;
-use Drupal\package_manager\Event\PreApplyEvent;
-use Drupal\package_manager\Event\PreCreateEvent;
-use Drupal\package_manager\Event\PreDestroyEvent;
-use Drupal\package_manager\Event\PreRequireEvent;
+use Drupal\package_manager\Stage;
 
 /**
  * Tests updating packages in a staging area.
@@ -77,23 +70,7 @@ class PackageUpdateTest extends TemplateProjectTestBase {
     // @see \Drupal\updated_module\PostApplySubscriber::postApply()
     $this->assertSame('Bravo!', $file_contents['bravo.txt']);
 
-    $assert_session = $mink->assertSession();
-    // There should be information for every event triggered.
-    $this->visit('/admin/reports/dblog');
-    file_put_contents("/Users/kunal.sachdev/www/test_sample.html", $mink->getSession()->getPage()->getContent());
-    $events = [
-      PreCreateEvent::class,
-      PostCreateEvent::class,
-      PreRequireEvent::class,
-      PostRequireEvent::class,
-      PreApplyEvent::class,
-      PostApplyEvent::class,
-      PreDestroyEvent::class,
-      PostDestroyEvent::class,
-    ];
-    foreach ($events as $event) {
-      $assert_session->pageTextContains('Event: ' . $event);
-    }
+    $this->assertStageEventsLogged(Stage::class);
   }
 
 }
diff --git a/package_manager/tests/src/Build/TemplateProjectTestBase.php b/package_manager/tests/src/Build/TemplateProjectTestBase.php
index d5456f6d8e..b4137d3598 100644
--- a/package_manager/tests/src/Build/TemplateProjectTestBase.php
+++ b/package_manager/tests/src/Build/TemplateProjectTestBase.php
@@ -4,6 +4,14 @@ namespace Drupal\Tests\package_manager\Build;
 
 use Drupal\BuildTests\QuickStart\QuickStartTestBase;
 use Drupal\Composer\Composer;
+use Drupal\package_manager\Event\PostApplyEvent;
+use Drupal\package_manager\Event\PostCreateEvent;
+use Drupal\package_manager\Event\PostDestroyEvent;
+use Drupal\package_manager\Event\PostRequireEvent;
+use Drupal\package_manager\Event\PreApplyEvent;
+use Drupal\package_manager\Event\PreCreateEvent;
+use Drupal\package_manager\Event\PreDestroyEvent;
+use Drupal\package_manager\Event\PreRequireEvent;
 use Drupal\Tests\package_manager\Traits\FixtureUtilityTrait;
 use Drupal\Tests\RandomGeneratorTrait;
 
@@ -422,6 +430,7 @@ END;
       ->getValue();
     if (preg_match('/^system_modules_(experimental_|non_stable_)?confirm_form$/', $form_id)) {
       $page->pressButton('Continue');
+      file_put_contents("/Users/ted.bowman/sites/test.html", $page->getContent());
       $assert_session->statusCodeEquals(200);
     }
   }
@@ -441,6 +450,58 @@ END;
     return $temp_directory;
   }
 
+  /**
+   * Asserts stage events were called in a specific order.
+   *
+   * @param string $expected_stage_class
+   *   The expected stage class for the events.
+   * @param array|null $events
+   *   (optional) The expected stage events that should have been fired in the
+   *   order in which they should have been fired. Events can be specified more
+   *   that once if they will be fired multiple times. If there are no events
+   *   specified all life cycle events from PreCreateEvent to PostDestroy will
+   *   be asserted.
+   */
+  protected function assertStageEventsLogged(string $expected_stage_class, ?array $events = NULL): void {
+    if (is_null($events)) {
+      $events = [
+        PreCreateEvent::class,
+        PostCreateEvent::class,
+        PreRequireEvent::class,
+        PostRequireEvent::class,
+        PreApplyEvent::class,
+        PostApplyEvent::class,
+        PreDestroyEvent::class,
+        PostDestroyEvent::class,
+      ];
+    }
+    else {
+      $this->assertLessThan(25, $this->count($events), 'More than 25 events may not appear on one page of the log view');
+    }
+    $assert_session = $this->getMink()->assertSession();
+    $page = $this->getMink()->getSession()->getPage();
+    $this->visit('/admin/reports/dblog');
+    $assert_session->statusCodeEquals(200);
+    $page->selectFieldOption('Type', 'package_manager_test_event_logger');
+    $page->pressButton('Filter');
+    $assert_session->statusCodeEquals(200);
+
+    // The log entries will not appear completely in the page text but they will
+    // appear in the title attribute of the links.
+    $links = $page->findAll('css', 'a[title^=package_manager_test_event_logger-start]');
+    $actual_titles = [];
+    // Loop through the links in reverse order because the most recent entries
+    // will be first.
+    foreach (array_reverse($links) as $link) {
+      $actual_titles[] = $link->getAttribute('title');
+    }
+    $expected_titles = [];
+    foreach ($events as $event) {
+      $expected_titles[] = "package_manager_test_event_logger-start: Event: $event, Stage instance of: $expected_stage_class:package_manager_test_event_logger-end";
+    }
+    $this->assertSame($expected_titles, $actual_titles);
+  }
+
   // BEGIN: DELETE FROM CORE MERGE REQUEST.
 
   /**
@@ -464,4 +525,5 @@ END;
   }
 
   // END: DELETE FROM CORE MERGE REQUEST.
+
 }
-- 
GitLab