You are here

public function LinkCheckerInterfaceTest::testLinkCheckerCreateNodeWithBrokenLinks in Link checker 8

Test node with link.

File

tests/src/Functional/LinkCheckerInterfaceTest.php, line 124

Class

LinkCheckerInterfaceTest
Test case for interface tests.

Namespace

Drupal\Tests\linkchecker\Functional

Code

public function testLinkCheckerCreateNodeWithBrokenLinks() {
  $url1 = 'http://example.com/node/broken/link';
  $body = 'Lorem ipsum dolor sit amet <a href="' . $url1 . '">broken link</a> sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat';

  // Save folder names in variables for reuse.
  $random = new Random();
  $folder1 = $random
    ->name(10);
  $folder2 = $random
    ->name(5);

  // Fill node array.
  $edit = [];
  $edit['title[0][value]'] = $random
    ->name(32);
  $edit['body[0][value]'] = $body;

  //$edit["body[0][format]"] = 'full_html';
  $edit['path[0][alias]'] = '/' . $folder1 . '/' . $folder2;

  // Extract only full qualified URLs.
  $this
    ->config('linkchecker.settings')
    ->set('check_links_types', LinkCheckerLinkInterface::TYPE_EXTERNAL)
    ->save();

  // Verify path input field appears on add "Basic page" form.
  $this
    ->drupalGet('node/add/page');

  // Verify path input is present.
  $this
    ->assertFieldByName('path[0][alias]', '', 'Path input field present on add Basic page form.');

  // Save node.
  $this
    ->drupalPostForm('node/add/page', $edit, $this
    ->t('Save'));
  $this
    ->assertText($this
    ->t('@type @title has been created.', [
    '@type' => 'Basic page',
    '@title' => $edit["title[0][value]"],
  ]), 'Node was created.');
  $node = $this
    ->drupalGetNodeByTitle($edit['title[0][value]']);
  $this
    ->assertNotEmpty($node);

  // Verify if the content link is extracted properly.
  $link = $this
    ->getLinkCheckerLinkByUrl($url1);
  if ($link) {
    $this
      ->assertIdentical($link
      ->get('url')->value, $url1, new FormattableMarkup('URL %url found.', [
      '%url' => $url1,
    ]));
  }
  else {
    $this
      ->fail(new FormattableMarkup('URL %url not found.', [
      '%url' => $url1,
    ]));
  }
}