diff --git a/linkchecker.module b/linkchecker.module index 805b5fbc5bc25fb3083739a46417bbc40f312768..5fd07c4f7e32e76cde12af358cdfd307f1d69189 100644 --- a/linkchecker.module +++ b/linkchecker.module @@ -154,8 +154,11 @@ function linkchecker_entity_insert(EntityInterface $entity) { /** @var \Drupal\linkchecker\LinkExtractorService $extractor */ $extractor = \Drupal::service('linkchecker.extractor'); $links = $extractor->extractFromEntity($entity); - $extractor->saveLinkMultiple($links); - $extractor->updateEntityExtractIndex($entity); + + if (!empty($links)) { + $extractor->saveLinkMultiple($links); + $extractor->updateEntityExtractIndex($entity); + } \Drupal::service('linkchecker.clean_up')->cleanUpForEntity($entity); } @@ -173,8 +176,11 @@ function linkchecker_entity_update(EntityInterface $entity) { /** @var \Drupal\linkchecker\LinkExtractorService $extractor */ $extractor = \Drupal::service('linkchecker.extractor'); $links = $extractor->extractFromEntity($entity); - $extractor->saveLinkMultiple($links); - $extractor->updateEntityExtractIndex($entity); + + if (!empty($links)) { + $extractor->saveLinkMultiple($links); + $extractor->updateEntityExtractIndex($entity); + } \Drupal::service('linkchecker.clean_up')->cleanUpForEntity($entity); } diff --git a/src/LinkExtractorBatch.php b/src/LinkExtractorBatch.php index 92c3807cfb47d318d39386bea133d5500c643ae8..8dafa44ceef47e5ea9dfe00e117051f17930c238 100644 --- a/src/LinkExtractorBatch.php +++ b/src/LinkExtractorBatch.php @@ -114,7 +114,8 @@ class LinkExtractorBatch { $storage = $this->entityTypeManager->getStorage($entityType->id()); foreach ($ids as $id) { $entity = $storage->load($id); - if ($entity instanceof FieldableEntityInterface){ + if ($entity instanceof FieldableEntityInterface) { + // Process the entity links. $links = $this->extractor->extractFromEntity($entity); $this->extractor->saveLinkMultiple($links); $this->extractor->updateEntityExtractIndex($entity); diff --git a/tests/src/Functional/LinkCheckerBaseTest.php b/tests/src/Functional/LinkCheckerBaseTest.php new file mode 100644 index 0000000000000000000000000000000000000000..731ecbb30bc50547f16d828a83d39f1c546d1b48 --- /dev/null +++ b/tests/src/Functional/LinkCheckerBaseTest.php @@ -0,0 +1,103 @@ + 'filtered_html', + 'name' => 'Filtered HTML', + 'weight' => 0, + ]); + $filtered_html_format->save(); + + $full_html_format = FilterFormat::create([ + 'format' => 'full_html', + 'name' => 'Full HTML', + ]); + $full_html_format->save(); + + // Create Basic page and Article node types. + $page_node_type = NodeType::create([ + 'type' => 'page', + 'name' => 'Basic page', + 'format' => 'full_html', + ]); + $page_node_type->save(); + + $article_node_type = NodeType::create([ + 'type' => 'article', + 'name' => 'Article', + 'format' => 'full_html', + ]); + $article_node_type->save(); + + // Create a body field instance for the 'page' node type. + $node_body_field = node_add_body_field($page_node_type); + $node_body_field->setThirdPartySetting('linkchecker', 'scan', TRUE); + $node_body_field->setThirdPartySetting('linkchecker', 'extractor', 'html_link_extractor'); + $node_body_field->save(); + + // Configure basic settings. + $this->config('linkchecker.settings')->set('default_url_scheme', 'http://')->save(); + $this->config('linkchecker.settings')->set('base_path', 'unexistingdomain.org/')->save(); + + $this->config('linkchecker.settings')->set('check.disable_link_check_for_urls', '')->save(); + $this->config('linkchecker.settings')->set('check_links_types', LinkCheckerLinkInterface::TYPE_ALL)->save(); + + // Core enables the URL filter for "Full HTML" by default. + // -> Blacklist / Disable URL filter for testing. + $this->config('linkchecker.settings')->set('extract.filter_blacklist', ['filter_url' => 'filter_url'])->save(); + + // Extract from all link checker supported HTML tags. + $this->config('linkchecker.settings')->set('extract.from_a', 1)->save(); + $this->config('linkchecker.settings')->set('extract.from_audio', 1)->save(); + $this->config('linkchecker.settings')->set('extract.from_embed', 1)->save(); + $this->config('linkchecker.settings')->set('extract.from_iframe', 1)->save(); + $this->config('linkchecker.settings')->set('extract.from_img', 1)->save(); + $this->config('linkchecker.settings')->set('extract.from_object', 1)->save(); + $this->config('linkchecker.settings')->set('extract.from_video', 1)->save(); + + // User to set up link checker. + $this->adminUser = $this->drupalCreateUser([ + 'administer linkchecker', + ]); + $this->drupalLogin($this->adminUser); + } + +} diff --git a/tests/src/Functional/LinkCheckerLinkExtractionStatusTest.php b/tests/src/Functional/LinkCheckerLinkExtractionStatusTest.php new file mode 100644 index 0000000000000000000000000000000000000000..6c7a2ce6f3afb475c2349d0e210dce6fe4cdfc0d --- /dev/null +++ b/tests/src/Functional/LinkCheckerLinkExtractionStatusTest.php @@ -0,0 +1,76 @@ +adminUser = $this->drupalCreateUser([ + 'administer linkchecker', + ]); + $this->drupalLogin($this->adminUser); + } + + /** + * Test that the status is correct on the status page. + */ + public function testLinkCheckerStatusCorrect() { + // Just to make sure, let's consult the service directly. + /** @var \Drupal\linkchecker\LinkExtractorBatch $batch */ + $batch = $this->container->get('linkchecker.extractor_batch'); + self::assertEquals(0, $batch->getTotalEntitiesToProcess()); + self::assertEquals(0, $batch->getNumberOfProcessedEntities()); + // Create page nodes that are processable by linkchecker. + Node::create([ + 'title' => 'node 1', + 'type' => 'page', + 'body' => [ + 'value' => 'I should give a 200 OK response.', + 'format' => 'filtered_html', + ], + ])->save(); + Node::create([ + 'title' => 'node 2', + 'type' => 'page', + 'body' => [ + 'value' => 'I contain no links, but I shall still be processed.', + 'format' => 'filtered_html', + ], + ])->save(); + $key = $this->container->get('state')->get('system.cron_key'); + $this->drupalGet('cron/' . $key); + $this->assertSession()->statusCodeEquals(204); + // The service should have updated its numbers. + self::assertEquals(2, $batch->getTotalEntitiesToProcess()); + self::assertEquals(2, $batch->getNumberOfProcessedEntities()); + // Now get the status. + $this->drupalGet('/admin/config/content/linkchecker'); + $this->assertSession()->pageTextContains('2 out of 2 items have been processed'); + $this->assertSession()->pageTextContains('100%'); + // Now create a node of the article type, which should not be processed by + // linkchecker. + Node::create([ + 'title' => 'node 3', + 'type' => 'article', + ])->save(); + // There should be no change in status. + self::assertEquals(2, $batch->getTotalEntitiesToProcess()); + self::assertEquals(2, $batch->getNumberOfProcessedEntities()); + $this->assertSession()->pageTextContains('2 out of 2 items have been processed'); + $this->assertSession()->pageTextContains('100%'); + } + +} diff --git a/tests/src/Functional/LinkCheckerLinkExtractionTest.php b/tests/src/Functional/LinkCheckerLinkExtractionTest.php index e5a90ab5082bbd7d79189db359757550c0a55687..cdcb4a97d55fd3f60939d6439f9f517b232fde33 100644 --- a/tests/src/Functional/LinkCheckerLinkExtractionTest.php +++ b/tests/src/Functional/LinkCheckerLinkExtractionTest.php @@ -3,91 +3,22 @@ namespace Drupal\Tests\linkchecker\Functional; use Drupal\Component\Render\FormattableMarkup; -use Drupal\Core\StringTranslation\StringTranslationTrait; -use Drupal\filter\Entity\FilterFormat; use Drupal\linkchecker\Entity\LinkCheckerLink; use Drupal\linkchecker\LinkCheckerLinkInterface; -use Drupal\node\Entity\NodeType; -use Drupal\Tests\BrowserTestBase; /** * Test Link checker module link extraction functionality. * * @group linkchecker */ -class LinkCheckerLinkExtractionTest extends BrowserTestBase { - - use StringTranslationTrait; - - /** - * {@inheritdoc} - */ - protected $defaultTheme = 'stark'; +class LinkCheckerLinkExtractionTest extends LinkCheckerBaseTest { /** * {@inheritdoc} */ - public static $modules = [ - 'filter', - 'linkchecker', - 'node', - 'path', - ]; - - /** - * {@inheritdoc} - */ - protected function setUp() { + protected function setUp(): void { parent::setUp(); - // Create Full HTML text format. - $filtered_html_format = FilterFormat::create([ - 'format' => 'filtered_html', - 'name' => 'Filtered HTML', - 'weight' => 0, - ]); - $filtered_html_format->save(); - - $full_html_format = FilterFormat::create([ - 'format' => 'full_html', - 'name' => 'Full HTML', - ]); - $full_html_format->save(); - - // Create Basic page and Article node types. - $node_type = NodeType::create([ - 'type' => 'page', - 'name' => 'Basic page', - 'format' => 'full_html', - ]); - $node_type->save(); - - // Create a body field instance for the 'page' node type. - $node_body_field = node_add_body_field($node_type); - $node_body_field->setThirdPartySetting('linkchecker', 'scan', TRUE); - $node_body_field->setThirdPartySetting('linkchecker', 'extractor', 'html_link_extractor'); - $node_body_field->save(); - - // Configure basic settings. - $this->config('linkchecker.settings')->set('default_url_scheme', 'http://')->save(); - $this->config('linkchecker.settings')->set('base_path', 'unexistingdomain.org/')->save(); - - $this->config('linkchecker.settings')->set('check.disable_link_check_for_urls', '')->save(); - $this->config('linkchecker.settings')->set('check_links_types', LinkCheckerLinkInterface::TYPE_ALL)->save(); - - // Core enables the URL filter for "Full HTML" by default. - // -> Blacklist / Disable URL filter for testing. - $this->config('linkchecker.settings')->set('extract.filter_blacklist', ['filter_url' => 'filter_url'])->save(); - - // Extract from all link checker supported HTML tags. - $this->config('linkchecker.settings')->set('extract.from_a', 1)->save(); - $this->config('linkchecker.settings')->set('extract.from_audio', 1)->save(); - $this->config('linkchecker.settings')->set('extract.from_embed', 1)->save(); - $this->config('linkchecker.settings')->set('extract.from_iframe', 1)->save(); - $this->config('linkchecker.settings')->set('extract.from_img', 1)->save(); - $this->config('linkchecker.settings')->set('extract.from_object', 1)->save(); - $this->config('linkchecker.settings')->set('extract.from_video', 1)->save(); - // User to set up link checker. $this->adminUser = $this->drupalCreateUser([ 'access administration pages', @@ -95,8 +26,8 @@ class LinkCheckerLinkExtractionTest extends BrowserTestBase { 'create page content', 'create url aliases', 'edit own page content', - $filtered_html_format->getPermissionName(), - $full_html_format->getPermissionName(), + 'use text format filtered_html', + 'use text format full_html', ]); $this->drupalLogin($this->adminUser); }