Skip to content
Snippets Groups Projects
Commit b4ee47a6 authored by Eirik Morland's avatar Eirik Morland Committed by C_Logemann
Browse files

Issue #3186356 by eiriksm, parasite: When content with a checked link changed...

Issue #3186356 by eiriksm, parasite: When content with a checked link changed to delete the URL, the link entity is not deleted
parent e08ce5df
No related branches found
No related tags found
No related merge requests found
......@@ -25,6 +25,7 @@ use Drupal\linkchecker\LinkCheckerLinkInterface;
* plural = "@count LinkChecker link types",
* ),
* handlers = {
* "storage" = "Drupal\linkchecker\LinkCheckerStorage",
* "access" = "Drupal\linkchecker\LinkCheckerLinkAccessControlHandler",
* "storage_schema" = "Drupal\linkchecker\LinkCheckerLinkStorageSchema",
* "form" = {
......
<?php
namespace Drupal\linkchecker;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\linkchecker\Entity\LinkCheckerLink;
/**
* The storage for link checker.
*/
class LinkCheckerStorage extends SqlContentEntityStorage {
/**
* Get existing IDs that matches the URL and entity.
*
* @param \Drupal\linkchecker\Entity\LinkCheckerLink $link
* The link.
*
* @return array
* An array of IDs, or an empty array if not found.
*/
public function getExistingIdsFromLink(LinkCheckerLink $link) {
$query = $this->getQuery();
$query->condition('urlhash', LinkCheckerLink::generateHash($link->getUrl()))
->condition('entity_id.target_id', $link->getParentEntity()->id())
->condition('entity_id.target_type', $link->getParentEntity()
->getEntityTypeId())
->condition('entity_field', $link->getParentEntityFieldName())
->condition('entity_langcode', $link->getParentEntityLangcode());
return $query->execute();
}
}
......@@ -121,12 +121,14 @@ class LinkCleanUp {
->load($entity->id());
$extractedIds = [];
/** @var \Drupal\linkchecker\LinkCheckerStorage $storage */
$storage = $this->entityTypeManager->getStorage('linkcheckerlink');
// If entity is not deleted, gather all links that exists in fields.
if (!$isEntityDeleted) {
$links = $this->extractor->extractFromEntity($entity);
foreach ($links as $link) {
$extractedIds[] = $link->id();
$extractedIds = array_merge($storage->getExistingIdsFromLink($link), $extractedIds);
}
}
else {
......@@ -137,8 +139,6 @@ class LinkCleanUp {
->execute();
}
$storage = $this->entityTypeManager->getStorage('linkcheckerlink');
// Get list of link IDs that should be deleted.
$query = $storage->getQuery();
$query->condition('entity_id.target_id', $entity->id());
......
......@@ -2,8 +2,13 @@
namespace Drupal\Tests\linkchecker\Kernel;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\linkchecker\Entity\LinkCheckerLink;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\node\Traits\NodeCreationTrait;
/**
* Test for linkchecker.clean_up service.
......@@ -14,6 +19,8 @@ use Drupal\linkchecker\Entity\LinkCheckerLink;
*/
class LinkcheckerCleanUpTest extends KernelTestBase {
use NodeCreationTrait;
/**
* {@inheritdoc}
*/
......@@ -21,6 +28,11 @@ class LinkcheckerCleanUpTest extends KernelTestBase {
'system',
'linkchecker',
'dynamic_entity_reference',
'node',
'user',
'field',
'filter',
'text',
];
/**
......@@ -43,13 +55,61 @@ class LinkcheckerCleanUpTest extends KernelTestBase {
public function setUp() {
parent::setUp();
$this->installSchema('system', 'sequences');
$this->installEntitySchema('node');
$this->installEntitySchema('user');
$this->installEntitySchema('linkcheckerlink');
$this->installConfig('linkchecker');
$this->installConfig(['field', 'node', 'filter', 'linkchecker']);
$this->installSchema('linkchecker', 'linkchecker_index');
$this->linkCleanUp = $this->container->get('linkchecker.clean_up');
$this->linkCheckerLinkStorage = $this->container->get('entity_type.manager')
->getStorage('linkcheckerlink')
;
->getStorage('linkcheckerlink');
}
/**
* @covers ::cleanUpForEntity
*/
public function testEntityCleanup() {
$urls = [
'http://httpstat.us/304',
'http://httpstat.us/503',
];
$node_type = NodeType::create([
'type' => 'page',
]);
$node_type->save();
node_add_body_field($node_type);
$node = $this->createNode([
'type' => 'page',
'body' => [
[
'value' => '
<a href="http://httpstat.us/304">The nightmare continues</a>'
],
],
]);
$fieldDefinition = $node->get('body')->getFieldDefinition();
/** @var \Drupal\field\Entity\FieldConfig $config */
$config = $fieldDefinition->getConfig($node->bundle());
$config->setThirdPartySetting('linkchecker', 'scan', TRUE);
$config->setThirdPartySetting('linkchecker', 'extractor', 'html_link_extractor');
$config->save();
foreach ($urls as $url) {
$link = $this->createDummyLink($url);
$link->setParentEntity($node);
$link->setParentEntityFieldName($config->getName());
$link->save();
}
// So, given we have 2 link entities that seemingly belong to this new
// entity, and then we run the cleanup function to see which links should
// really be there, we now expect it to be 1 link, since only one of them
// are found in the node body.
$this->assertCount(2, $this->linkCheckerLinkStorage->loadMultiple(NULL));
$this->linkCleanUp->cleanUpForEntity($node);
$this->assertCount(1, $this->linkCheckerLinkStorage->loadMultiple(NULL));
}
/**
......@@ -78,6 +138,7 @@ class LinkcheckerCleanUpTest extends KernelTestBase {
* Helper function for link creation.
*/
protected function createDummyLink($url) {
/** @var \Drupal\linkchecker\Entity\LinkCheckerLink $link */
$link = LinkCheckerLink::create([
'url' => $url,
'entity_id' => [
......
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