Skip to content
Snippets Groups Projects
Commit 6f263d32 authored by Jeroen Tubex's avatar Jeroen Tubex Committed by Carsten Logemann
Browse files

Issue #3065028 by JeroenT, dexiecarla, Znak: Cannot clear link data and analyze content for links

parent 676c0d7d
No related branches found
No related tags found
No related merge requests found
......@@ -6,12 +6,18 @@ use Drupal\Core\Batch\BatchBuilder;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Class LinkCleanUp.
*/
class LinkCleanUp {
use DependencySerializationTrait;
use MessengerTrait;
use StringTranslationTrait;
/**
* The entity type manager.
*
......@@ -51,12 +57,11 @@ class LinkCleanUp {
$batch = new BatchBuilder();
$batch->setTitle('Remove links')
->addOperation([$this, 'processDelete'])
->addOperation([$this, 'batchProcessDelete'])
->setProgressive()
->setFinishCallback([$this, 'batchFinished']);
batch_set($batch->toArray());
}
/**
......@@ -150,4 +155,18 @@ class LinkCleanUp {
}
}
/**
* Finished callback for batch.
*/
public function batchFinished($success) {
if ($success) {
$this->messenger()
->addStatus($this->t('Links were successfully checked.'));
}
else {
$this->messenger()
->addError($this->t('Links were not checked.'));
}
}
}
<?php
namespace Drupal\Tests\linkchecker\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\linkchecker\Entity\LinkCheckerLink;
/**
* Test for linkchecker.clean_up service.
*
* @coversDefaultClass \Drupal\linkchecker\LinkCleanUp
*
* @group linkchecker
*/
class LinkcheckerCleanUpTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = [
'system',
'linkchecker',
'dynamic_entity_reference',
];
/**
* The link clean up service.
*
* @var \Drupal\linkchecker\LinkCleanUp
*/
protected $linkCleanUp;
/**
* The link checker link storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $linkCheckerLinkStorage;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->installSchema('system', 'sequences');
$this->installEntitySchema('linkcheckerlink');
$this->installConfig('linkchecker');
$this->linkCleanUp = $this->container->get('linkchecker.clean_up');
$this->linkCheckerLinkStorage = $this->container->get('entity_type.manager')
->getStorage('linkcheckerlink')
;
}
/**
* @covers ::removeAllBatch
*/
public function testRemoveAllBatch() {
$urls = [
'https://existing.com',
'https://not-existing.com',
'https://example.com/existing',
];
foreach ($urls as $url) {
$this->createDummyLink($url);
}
$this->assertCount(3, $this->linkCheckerLinkStorage->loadMultiple(NULL));
$this->linkCleanUp->removeAllBatch();
$this->runBatch();
$this->assertEmpty($this->linkCheckerLinkStorage->loadMultiple($this->linkCheckerLinkStorage->loadMultiple(NULL)));
}
/**
* Helper function for link creation.
*/
protected function createDummyLink($url) {
$link = LinkCheckerLink::create([
'url' => $url,
'entity_id' => [
'target_id' => 1,
'target_type' => 'dummy_type',
],
'entity_field' => 'dummy_field',
'entity_langcode' => 'en',
]);
$link->save();
return $link;
}
/**
* Runs the currently set batch, if any exists.
*/
protected function runBatch() {
$batch = &batch_get();
if ($batch) {
$batch['progressive'] = FALSE;
batch_process();
}
}
}
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