public function LinkcheckerRepair301Test::testStatusHandling in Link checker 8
Test link checker service status handling.
File
- tests/
src/ Kernel/ LinkcheckerRepair301Test.php, line 115
Class
- LinkcheckerRepair301Test
- Test for Repair on 301 status handling.
Namespace
Drupal\Tests\linkchecker\KernelCode
public function testStatusHandling() {
// Extract all links.
$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();
// Test external link.
$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();
// Check if this handler will not replace link if it disabled.
$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());
// Enable repair on 301 and update fail count to each link.
$this->linkcheckerSetting
->set('error.action_status_code_301', 2);
$this->linkcheckerSetting
->save(TRUE);
$link
->setFailCount(2);
$link
->save();
// Check if this handler will replace link
// if link is exists in content and fail count is reached.
$this->repair301
->handle($link, new Response(301, [
'Location' => 'https://existing.com/redirect',
]));
$this
->assertFalse($link
->isExists());
$node = $this
->reloadNode($node);
$body = $node->body->value;
// Put link inside href attribute to be sure that it was replaced
// without errors.
$this
->assertFalse(strpos($body, 'href="' . $link
->getUrl() . '"'));
$this
->assertNotFalse(strpos($body, 'href="https://existing.com/redirect"'));
// Test internal link.
$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();
// Check if this handler will replace link
// if link is exists in content and fail count is reached.
$this->repair301
->handle($link, new Response(301, [
'Location' => $this->baseUrl . '/replaced',
]));
$this
->assertFalse($link
->isExists());
$node = $this
->reloadNode($node);
$body = $node->body->value;
// Put link inside href attribute to be sure that it was replaced
// without errors.
$this
->assertFalse(strpos($body, 'href="/internal"'));
$this
->assertNotFalse(strpos($body, 'href="/replaced"'));
}