You are here

public function LinkcheckerLinkExtractorServiceTest::testIsExists in Link checker 8

Test isLinkExists method.

File

tests/src/Kernel/LinkcheckerLinkExtractorServiceTest.php, line 199

Class

LinkcheckerLinkExtractorServiceTest
Test link extractor service.

Namespace

Drupal\Tests\linkchecker\Kernel

Code

public function testIsExists() {
  $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="https://example.com/existing"></a>' . '<a href="/existing.local"></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();
  $urls = [
    'https://existing.com',
    'https://not-existing.com',
    'https://example.com/existing',
    $this->baseUrl . '/existing.local',
  ];

  /** @var \Drupal\linkchecker\LinkCheckerLinkInterface[] $links */
  $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' => 'en',
    ]);
    $tmpLink
      ->save();
    $links[] = $tmpLink;
  }

  // Extract all link with empty blacklist.
  $checkMap = [
    TRUE,
    FALSE,
    TRUE,
    TRUE,
  ];

  // Disable blacklist URLs.
  $this->linkcheckerSetting
    ->set('check.disable_link_check_for_urls', '');
  $this->linkcheckerSetting
    ->set('check_links_types', LinkCheckerLinkInterface::TYPE_ALL);
  $this->linkcheckerSetting
    ->save(TRUE);
  foreach ($links as $key => $link) {
    $this
      ->assertEquals($checkMap[$key], $this->extractorService
      ->isLinkExists($link));
  }

  // Extract all links with example.com blacklisting.
  $checkMap = [
    TRUE,
    FALSE,
    FALSE,
    TRUE,
  ];

  // Enable blacklist URLs.
  $this->linkcheckerSetting
    ->set('check.disable_link_check_for_urls', "example.com");
  $this->linkcheckerSetting
    ->save(TRUE);
  foreach ($links as $key => $link) {
    $this
      ->assertEquals($checkMap[$key], $this->extractorService
      ->isLinkExists($link));
  }

  // Extract external only.
  $checkMap = [
    TRUE,
    FALSE,
    FALSE,
    FALSE,
  ];

  // Enable blacklist URLs.
  $this->linkcheckerSetting
    ->set('check.disable_link_check_for_urls', "example.com");
  $this->linkcheckerSetting
    ->set('check_links_types', LinkCheckerLinkInterface::TYPE_EXTERNAL);
  $this->linkcheckerSetting
    ->save(TRUE);
  foreach ($links as $key => $link) {
    $this
      ->assertEquals($checkMap[$key], $this->extractorService
      ->isLinkExists($link));
  }

  // Extract internal only.
  $checkMap = [
    FALSE,
    FALSE,
    FALSE,
    TRUE,
  ];

  // Enable blacklist URLs.
  $this->linkcheckerSetting
    ->set('check.disable_link_check_for_urls', "example.com");
  $this->linkcheckerSetting
    ->set('check_links_types', LinkCheckerLinkInterface::TYPE_INTERNAL);
  $this->linkcheckerSetting
    ->save(TRUE);
  foreach ($links as $key => $link) {
    $this
      ->assertEquals($checkMap[$key], $this->extractorService
      ->isLinkExists($link));
  }

  // If parent entity was removed.
  $node
    ->delete();

  // Disable blacklist URLs.
  $this->linkcheckerSetting
    ->set('check.disable_link_check_for_urls', '');
  $this->linkcheckerSetting
    ->set('check_links_types', LinkCheckerLinkInterface::TYPE_ALL);
  $this->linkcheckerSetting
    ->save(TRUE);

  // We should reload links to clear internal runtime cache.
  foreach (LinkCheckerLink::loadMultiple() as $link) {
    $this
      ->assertFalse($this->extractorService
      ->isLinkExists($link));
  }
}