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 LinkcheckerUnpublish404Test extends KernelTestBase {
use NodeCreationTrait;
public static $modules = [
'node',
'user',
'system',
'field',
'filter',
'text',
'dynamic_entity_reference',
'linkchecker',
'path_alias',
];
protected $checkerService;
protected $linkcheckerSetting;
protected $unpublish404Handler;
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->unpublish404Handler = $this->container
->get('plugin.manager.link_status_handler')
->createInstance('unpublish_404');
}
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>',
],
],
]);
$node
->setPublished();
$node
->save();
$fieldDefinition = $node
->get('body')
->getFieldDefinition();
$config = $fieldDefinition
->getConfig($node
->bundle());
$config
->setThirdPartySetting('linkchecker', 'scan', TRUE);
$config
->setThirdPartySetting('linkchecker', 'extractor', 'html_link_extractor');
$config
->save();
$urls = [
'https://existing.com',
'https://not-existing.com',
];
$links = [];
foreach ($urls as $url) {
$tmpLink = LinkCheckerLink::create([
'url' => $url,
'entity_id' => [
'target_id' => $node
->id(),
'target_type' => $node
->getEntityTypeId(),
],
'entity_field' => 'body',
'entity_langcode' => $node
->language()
->getId(),
]);
$tmpLink
->save();
$links[] = $tmpLink;
}
$this->linkcheckerSetting
->set('error.action_status_code_404', 0);
$this->linkcheckerSetting
->save(TRUE);
foreach ($links as $link) {
$this->unpublish404Handler
->handle($link, new Response());
$node = $this
->reloadNode($node);
$this
->assertTrue($node
->isPublished());
}
$this->linkcheckerSetting
->set('error.action_status_code_404', 2);
$this->linkcheckerSetting
->save(TRUE);
foreach ($links as $link) {
$link
->setFailCount(2);
$link
->save();
}
$node
->setPublished();
$node
->save();
$this->unpublish404Handler
->handle($links[1], new Response());
$node = $this
->reloadNode($node);
$this
->assertTrue($node
->isPublished());
$node
->setPublished();
$node
->save();
$this->unpublish404Handler
->handle($links[0], new Response());
$node = $this
->reloadNode($node);
$this
->assertTrue(!$node
->isPublished());
}
protected function reloadNode(NodeInterface $node) {
return Node::load($node
->id());
}
}