From c12c8e1f28c4603327ca16c1bd3a2b951ecb22fe Mon Sep 17 00:00:00 2001
From: Yaroslav Kozak <y.kozak@dev-branch.com>
Date: Thu, 11 Apr 2024 19:44:27 +0300
Subject: [PATCH 1/3] #3247797 Fixed fatal error after enabling
 LinkStatusHandler plugins

---
 src/Plugin/LinkStatusHandlerBase.php          | 21 ++++++++++++++++--
 tests/src/Kernel/LinkcheckerRepair301Test.php | 22 ++++++++++++++++++-
 .../Kernel/LinkcheckerUnpublish404Test.php    | 22 ++++++++++++++++++-
 3 files changed, 61 insertions(+), 4 deletions(-)

diff --git a/src/Plugin/LinkStatusHandlerBase.php b/src/Plugin/LinkStatusHandlerBase.php
index 83c1894..46bfc06 100644
--- a/src/Plugin/LinkStatusHandlerBase.php
+++ b/src/Plugin/LinkStatusHandlerBase.php
@@ -175,8 +175,25 @@ abstract class LinkStatusHandlerBase extends PluginBase implements LinkStatusHan
    * Helper function to switch session.
    */
   protected function switchSession() {
-    // Switch anonymous user to an admin.
-    $this->accountSwitcher->switchTo(new UserSession(['uid' => user_load_by_name($this->linkcheckerSetting->get('error.impersonate_account'))]));
+    // Switch to the user that is configured as impersonate_account.
+    $loadedUser = user_load_by_name($this->linkcheckerSetting->get('error.impersonate_account'));
+
+    if (empty($loadedUser)) {
+      $loadedUser = user_load_by_name('admin');
+      // Theoretically admin user could be renamed, so try to load it by id.
+      if (!$loadedUser) {
+        $loadedUser = $this->entityTypeManager->getStorage('user')->load(1);
+      }
+    }
+
+    $userSessionValues = [
+      'uid' => $loadedUser->id(),
+      'name' => $loadedUser->getAccountName(),
+      'roles' => $loadedUser->getRoles(),
+      'mail' => $loadedUser->getEmail(),
+    ];
+
+    $this->accountSwitcher->switchTo(new UserSession($userSessionValues));
   }
 
   /**
diff --git a/tests/src/Kernel/LinkcheckerRepair301Test.php b/tests/src/Kernel/LinkcheckerRepair301Test.php
index 6182cc0..3b60b85 100644
--- a/tests/src/Kernel/LinkcheckerRepair301Test.php
+++ b/tests/src/Kernel/LinkcheckerRepair301Test.php
@@ -9,6 +9,7 @@ use Drupal\node\Entity\Node;
 use Drupal\node\Entity\NodeType;
 use Drupal\node\NodeInterface;
 use Drupal\Tests\node\Traits\NodeCreationTrait;
+use Drupal\Tests\user\Traits\UserCreationTrait;
 use GuzzleHttp\Psr7\Response;
 
 /**
@@ -19,6 +20,7 @@ use GuzzleHttp\Psr7\Response;
 class LinkcheckerRepair301Test extends KernelTestBase {
 
   use NodeCreationTrait;
+  use UserCreationTrait;
 
   /**
    * {@inheritdoc}
@@ -93,7 +95,14 @@ class LinkcheckerRepair301Test extends KernelTestBase {
     $this->installEntitySchema('user');
     $this->installEntitySchema('node');
     $this->installEntitySchema('linkcheckerlink');
-    $this->installConfig(['field', 'user', 'node', 'filter', 'linkchecker']);
+    $this->installConfig([
+      'field',
+      'user',
+      'node',
+      'filter',
+      'linkchecker',
+      'system',
+    ]);
 
     $this->checkerService = $this->container->get('linkchecker.checker');
     $this->linkcheckerSetting = $this->container->get('config.factory')
@@ -112,6 +121,17 @@ class LinkcheckerRepair301Test extends KernelTestBase {
       $this->httpProtocol = $this->linkcheckerSetting->get('default_url_scheme');
       $this->baseUrl = $this->httpProtocol . $this->linkcheckerSetting->get('base_path');
     }
+
+    // Since install hooks are not being called in Kernel tests - do it
+    // manually.
+    // We're calling it to set the impersonate_account in the config.
+    // @see \Drupal\KernelTests\KernelTestBase::enableModules()
+    $this->setUpCurrentUser(['uid' => 1], [
+      'administer linkchecker',
+      'access content',
+    ]);
+    $this->container->get('module_handler')->loadInclude('linckchecker', 'install');
+    linkchecker_install();
   }
 
   /**
diff --git a/tests/src/Kernel/LinkcheckerUnpublish404Test.php b/tests/src/Kernel/LinkcheckerUnpublish404Test.php
index cff3466..707bcb3 100644
--- a/tests/src/Kernel/LinkcheckerUnpublish404Test.php
+++ b/tests/src/Kernel/LinkcheckerUnpublish404Test.php
@@ -9,6 +9,7 @@ use Drupal\node\Entity\Node;
 use Drupal\node\Entity\NodeType;
 use Drupal\node\NodeInterface;
 use Drupal\Tests\node\Traits\NodeCreationTrait;
+use Drupal\Tests\user\Traits\UserCreationTrait;
 use GuzzleHttp\Psr7\Response;
 
 /**
@@ -19,6 +20,7 @@ use GuzzleHttp\Psr7\Response;
 class LinkcheckerUnpublish404Test extends KernelTestBase {
 
   use NodeCreationTrait;
+  use UserCreationTrait;
 
   /**
    * {@inheritdoc}
@@ -72,13 +74,31 @@ class LinkcheckerUnpublish404Test extends KernelTestBase {
     $this->installEntitySchema('user');
     $this->installEntitySchema('node');
     $this->installEntitySchema('linkcheckerlink');
-    $this->installConfig(['field', 'user', 'node', 'filter', 'linkchecker']);
+    $this->installConfig([
+      'field',
+      'user',
+      'node',
+      'filter',
+      'linkchecker',
+      'system',
+      ]);
 
     $this->checkerService = $this->container->get('linkchecker.checker');
     $this->linkcheckerSetting = $this->container->get('config.factory')
       ->getEditable('linkchecker.settings');
     $this->unpublish404Handler = $this->container->get('plugin.manager.link_status_handler')
       ->createInstance('unpublish_404');
+
+    // Since install hooks are not being called in Kernel tests - do it
+    // manually.
+    // We're calling it to set the impersonate_account in the config.
+    // @see \Drupal\KernelTests\KernelTestBase::enableModules()
+    $this->setUpCurrentUser(['uid' => 1], [
+      'administer linkchecker',
+      'access content',
+    ]);
+    $this->container->get('module_handler')->loadInclude('linckchecker', 'install');
+    linkchecker_install();
   }
 
   /**
-- 
GitLab


From 6fb75d5c13de8c6ca3cd52aaea7198353a6c7a3d Mon Sep 17 00:00:00 2001
From: Yaroslav Kozak <y.kozak@dev-branch.com>
Date: Fri, 12 Apr 2024 10:43:26 +0300
Subject: [PATCH 2/3] #3247797 Fixed cs error

---
 tests/src/Kernel/LinkcheckerUnpublish404Test.php | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/src/Kernel/LinkcheckerUnpublish404Test.php b/tests/src/Kernel/LinkcheckerUnpublish404Test.php
index 707bcb3..b6f4fa5 100644
--- a/tests/src/Kernel/LinkcheckerUnpublish404Test.php
+++ b/tests/src/Kernel/LinkcheckerUnpublish404Test.php
@@ -81,7 +81,7 @@ class LinkcheckerUnpublish404Test extends KernelTestBase {
       'filter',
       'linkchecker',
       'system',
-      ]);
+    ]);
 
     $this->checkerService = $this->container->get('linkchecker.checker');
     $this->linkcheckerSetting = $this->container->get('config.factory')
-- 
GitLab


From 4c5ea0205061a68c4448c0b8c62ebcc70a46b23d Mon Sep 17 00:00:00 2001
From: Yaroslav Kozak <y.kozak@dev-branch.com>
Date: Mon, 5 Aug 2024 18:35:10 +0300
Subject: [PATCH 3/3] #3247797 Removed redundant include load in tests

---
 tests/src/Kernel/LinkcheckerRepair301Test.php    | 1 -
 tests/src/Kernel/LinkcheckerUnpublish404Test.php | 1 -
 2 files changed, 2 deletions(-)

diff --git a/tests/src/Kernel/LinkcheckerRepair301Test.php b/tests/src/Kernel/LinkcheckerRepair301Test.php
index a7fc1d6..2866507 100644
--- a/tests/src/Kernel/LinkcheckerRepair301Test.php
+++ b/tests/src/Kernel/LinkcheckerRepair301Test.php
@@ -129,7 +129,6 @@ class LinkcheckerRepair301Test extends KernelTestBase {
       'administer linkchecker',
       'access content',
     ]);
-    $this->container->get('module_handler')->loadInclude('linckchecker', 'install');
     linkchecker_install();
   }
 
diff --git a/tests/src/Kernel/LinkcheckerUnpublish404Test.php b/tests/src/Kernel/LinkcheckerUnpublish404Test.php
index 5b3ac2e..bbc0692 100644
--- a/tests/src/Kernel/LinkcheckerUnpublish404Test.php
+++ b/tests/src/Kernel/LinkcheckerUnpublish404Test.php
@@ -96,7 +96,6 @@ class LinkcheckerUnpublish404Test extends KernelTestBase {
       'administer linkchecker',
       'access content',
     ]);
-    $this->container->get('module_handler')->loadInclude('linckchecker', 'install');
     linkchecker_install();
   }
 
-- 
GitLab

