View source
<?php
namespace Drupal\Tests\linkchecker\Functional;
use Drupal\block_content\Entity\BlockContent;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Random;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\filter\Entity\FilterFormat;
use Drupal\linkchecker\Entity\LinkCheckerLink;
use Drupal\linkchecker\LinkCheckerLinkInterface;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\BrowserTestBase;
class LinkCheckerInterfaceTest extends BrowserTestBase {
use StringTranslationTrait;
protected $defaultTheme = 'stark';
public static $modules = [
'block_content',
'comment',
'filter',
'linkchecker',
'node',
'path',
];
protected function setUp() {
parent::setUp();
$full_html_format = FilterFormat::create([
'format' => 'full_html',
'name' => 'Full HTML',
]);
$full_html_format
->save();
$node_type = NodeType::create([
'type' => 'page',
'name' => 'Basic page',
'format' => 'full_html',
]);
$node_type
->save();
$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();
$block_type = BlockContentType::create([
'id' => 'block',
'label' => 'Basic block',
]);
$block_type
->save();
$block_body_field = block_content_add_body_field($block_type
->id());
$block_body_field
->setThirdPartySetting('linkchecker', 'scan', TRUE);
$block_body_field
->setThirdPartySetting('linkchecker', 'extractor', 'html_link_extractor');
$block_body_field
->save();
$this
->config('linkchecker.settings')
->set('default_url_scheme', 'http://')
->save();
$this
->config('linkchecker.settings')
->set('base_path', 'example.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();
$this
->config('linkchecker.settings')
->set('extract.filter_blacklist', [
'filter_url' => 'filter_url',
])
->save();
$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();
$permissions = [
'administer blocks',
'administer comments',
'access comments',
'post comments',
'skip comment approval',
'edit own comments',
'create page content',
'edit own page content',
'administer url aliases',
'create url aliases',
$full_html_format
->getPermissionName(),
];
$this->admin_user = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($this->admin_user);
}
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';
$random = new Random();
$folder1 = $random
->name(10);
$folder2 = $random
->name(5);
$edit = [];
$edit['title[0][value]'] = $random
->name(32);
$edit['body[0][value]'] = $body;
$edit['path[0][alias]'] = '/' . $folder1 . '/' . $folder2;
$this
->config('linkchecker.settings')
->set('check_links_types', LinkCheckerLinkInterface::TYPE_EXTERNAL)
->save();
$this
->drupalGet('node/add/page');
$this
->assertFieldByName('path[0][alias]', '', 'Path input field present on add Basic page form.');
$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);
$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,
]));
}
}
public function testLinkCheckerCreateBlockWithBrokenLinks() {
$this
->drupalGet(Url::fromRoute('entity.block_content.collection')
->toString());
$url1 = 'http://example.com/block/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';
$random = new Random();
$custom_block = [
'info[0][value]' => $random
->name(8),
'body[0][value]' => $body,
];
$this
->drupalPostForm(Url::fromRoute('block_content.add_page')
->toString(), $custom_block, 'Save');
$this
->assertText($this
->t('@type @title has been created.', [
'@type' => 'Basic block',
'@title' => $custom_block['info[0][value]'],
]), 'Custom block successfully created.');
$blocks = \Drupal::entityQuery('block_content')
->condition('info', $custom_block['info[0][value]'])
->execute();
$block = BlockContent::load(reset($blocks));
$this
->assertNotEmpty($block);
$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,
]));
}
}
protected function getLinkCheckerLinkByUrl(string $url) {
$links = \Drupal::entityTypeManager()
->getStorage('linkcheckerlink')
->loadByProperties([
'urlhash' => LinkCheckerLink::generateHash($url),
]);
return current($links);
}
}