Skip to content
Snippets Groups Projects
Commit 0a5fd70a authored by Yaroslav Kozak's avatar Yaroslav Kozak
Browse files

#3247797 Fixed fatal error after enabling LinkStatusHandler plugins

parent 5be18f3f
No related branches found
No related tags found
No related merge requests found
......@@ -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));
}
/**
......
......@@ -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}
......@@ -88,7 +90,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')
......@@ -107,6 +116,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();
}
/**
......
......@@ -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}
......@@ -67,13 +69,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();
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment