You are here

public function LinkcheckerCleanUpTest::testEntityCleanup in Link checker 8

@covers ::cleanUpForEntity

File

tests/src/Kernel/LinkcheckerCleanUpTest.php, line 72

Class

LinkcheckerCleanUpTest
Test for linkchecker.clean_up service.

Namespace

Drupal\Tests\linkchecker\Kernel

Code

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));
}