View source
<?php
namespace Drupal\Tests\linkchecker\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\linkchecker\Entity\LinkCheckerLink;
use Drupal\linkchecker\LinkCheckerLinkInterface;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\node\NodeInterface;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use GuzzleHttp\Psr7\Response;
class LinkcheckerRepair301Test extends KernelTestBase {
use NodeCreationTrait;
public static $modules = [
'node',
'user',
'system',
'field',
'filter',
'text',
'dynamic_entity_reference',
'linkchecker',
'path_alias',
];
protected $checkerService;
protected $linkcheckerSetting;
protected $repair301;
protected $httpProtocol;
protected $baseUrl;
protected $request;
public function setUp() {
parent::setUp();
$this
->installSchema('system', 'sequences');
$this
->installSchema('node', 'node_access');
$this
->installSchema('linkchecker', 'linkchecker_index');
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installEntitySchema('linkcheckerlink');
$this
->installConfig([
'field',
'user',
'node',
'filter',
'linkchecker',
]);
$this->checkerService = $this->container
->get('linkchecker.checker');
$this->linkcheckerSetting = $this->container
->get('config.factory')
->getEditable('linkchecker.settings');
$this->repair301 = $this->container
->get('plugin.manager.link_status_handler')
->createInstance('repair_301');
$this->request = $this->container
->get('request_stack')
->getCurrentRequest();
if (isset($this->request)) {
$this->httpProtocol = $this->request
->getScheme() . '://';
$this->baseUrl = $this->request
->getSchemeAndHttpHost() . $this->request
->getBasePath();
}
else {
$this->httpProtocol = $this->linkcheckerSetting
->get('default_url_scheme');
$this->baseUrl = $this->httpProtocol . $this->linkcheckerSetting
->get('base_path');
}
}
public function testStatusHandling() {
$this->linkcheckerSetting
->set('check_links_types', LinkCheckerLinkInterface::TYPE_ALL);
$this->linkcheckerSetting
->save(TRUE);
$type = NodeType::create([
'name' => 'Links',
'type' => 'links',
]);
$type
->save();
node_add_body_field($type);
$node = $this
->createNode([
'type' => 'links',
'body' => [
[
'value' => '<a href="https://existing.com"></a>' . '<a href="/internal"></a>',
],
],
]);
$fieldDefinition = $node
->get('body')
->getFieldDefinition();
$config = $fieldDefinition
->getConfig($node
->bundle());
$config
->setThirdPartySetting('linkchecker', 'scan', TRUE);
$config
->setThirdPartySetting('linkchecker', 'extractor', 'html_link_extractor');
$config
->save();
$link = LinkCheckerLink::create([
'url' => 'https://existing.com',
'entity_id' => [
'target_id' => $node
->id(),
'target_type' => $node
->getEntityTypeId(),
],
'entity_field' => 'body',
'entity_langcode' => $node
->language()
->getId(),
]);
$link
->save();
$this->linkcheckerSetting
->set('error.action_status_code_301', 0);
$this->linkcheckerSetting
->save(TRUE);
$this->repair301
->handle($link, new Response(301, [
'Location' => 'https://existing.com/redirect',
]));
$this
->assertTrue($link
->isExists());
$this->linkcheckerSetting
->set('error.action_status_code_301', 2);
$this->linkcheckerSetting
->save(TRUE);
$link
->setFailCount(2);
$link
->save();
$this->repair301
->handle($link, new Response(301, [
'Location' => 'https://existing.com/redirect',
]));
$this
->assertFalse($link
->isExists());
$node = $this
->reloadNode($node);
$body = $node->body->value;
$this
->assertFalse(strpos($body, 'href="' . $link
->getUrl() . '"'));
$this
->assertNotFalse(strpos($body, 'href="https://existing.com/redirect"'));
$link = LinkCheckerLink::create([
'url' => $this->baseUrl . '/internal',
'entity_id' => [
'target_id' => $node
->id(),
'target_type' => $node
->getEntityTypeId(),
],
'entity_field' => 'body',
'entity_langcode' => $node
->language()
->getId(),
]);
$link
->setFailCount(2);
$link
->save();
$this->repair301
->handle($link, new Response(301, [
'Location' => $this->baseUrl . '/replaced',
]));
$this
->assertFalse($link
->isExists());
$node = $this
->reloadNode($node);
$body = $node->body->value;
$this
->assertFalse(strpos($body, 'href="/internal"'));
$this
->assertNotFalse(strpos($body, 'href="/replaced"'));
}
protected function reloadNode(NodeInterface $node) {
return Node::load($node
->id());
}
}