You are here

public function LinkcheckerUnpublish404Test::testStatusHandling in Link checker 8

Test link checker service status handling.

File

tests/src/Kernel/LinkcheckerUnpublish404Test.php, line 82

Class

LinkcheckerUnpublish404Test
Test for Unpublish on 404 status handling.

Namespace

Drupal\Tests\linkchecker\Kernel

Code

public function testStatusHandling() {

  // Extract all links.
  $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>',
      ],
    ],
  ]);

  // Make sure that node is published.
  $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',
  ];

  /** @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' => $node
        ->language()
        ->getId(),
    ]);
    $tmpLink
      ->save();
    $links[] = $tmpLink;
  }

  // Check if this handler will not unpublish if it disabled.
  $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());
  }

  // Enable unpublish on 404 and update fail count to each link.
  $this->linkcheckerSetting
    ->set('error.action_status_code_404', 2);
  $this->linkcheckerSetting
    ->save(TRUE);
  foreach ($links as $link) {
    $link
      ->setFailCount(2);
    $link
      ->save();
  }

  // Make sure that node is published.
  $node
    ->setPublished();
  $node
    ->save();

  // Check if this handler will not unpublish
  // if link is not exists in content.
  $this->unpublish404Handler
    ->handle($links[1], new Response());
  $node = $this
    ->reloadNode($node);
  $this
    ->assertTrue($node
    ->isPublished());

  // Make sure that node is published.
  $node
    ->setPublished();
  $node
    ->save();

  // Check if this handler will unpublish
  // if link is exists in content and fail count is reached.
  $this->unpublish404Handler
    ->handle($links[0], new Response());
  $node = $this
    ->reloadNode($node);
  $this
    ->assertTrue(!$node
    ->isPublished());
}