public function LinkcheckerHtmlLinkExtractorTest::testHtmlExtractor in Link checker 8
Test HTML extractor.
File
- tests/
src/ Kernel/ LinkcheckerHtmlLinkExtractorTest.php, line 70
Class
- LinkcheckerHtmlLinkExtractorTest
- Test html link extractor.
Namespace
Drupal\Tests\linkchecker\KernelCode
public function testHtmlExtractor() {
$type = NodeType::create([
'name' => 'Links',
'type' => 'links',
]);
$type
->save();
node_add_body_field($type);
$node = $this
->createNode([
'type' => 'links',
'body' => [
[
'value' => $this
->getTestBody(),
],
],
]);
$htmlTagConfig = [
'from_a',
'from_audio',
'from_embed',
'from_iframe',
'from_img',
'from_object',
'from_video',
];
// First disable extraction from each tag.
foreach ($htmlTagConfig as $tagConfigName) {
$this->linkcheckerSetting
->set('extract.' . $tagConfigName, FALSE);
}
$this->linkcheckerSetting
->save(TRUE);
// Test extraction for each HTML-tag.
// In this case we will check if config conditions works well.
foreach ($htmlTagConfig as $tagConfigName) {
$this->linkcheckerSetting
->set('extract.' . $tagConfigName, TRUE);
$this->linkcheckerSetting
->save(TRUE);
$testCases = array_filter($this
->getTestUrlList(), function ($item) use ($tagConfigName) {
return $item == $tagConfigName;
});
$testCases = array_keys($testCases);
$extractedUrls = $this->htmlLinkExtractor
->extract($node
->get('body')
->getValue());
foreach ($testCases as $url) {
$this
->assertTrue(in_array($url, $extractedUrls), new FormattableMarkup('URL @url was not extracted from tag @tag!', [
'@url' => $url,
'@tag' => str_replace('from_', '', $tagConfigName),
]));
}
$countTestCases = count($testCases);
$countExtractedLinks = count($extractedUrls);
$this
->assertEquals($countTestCases, $countExtractedLinks, new FormattableMarkup('Expected to extract @count but get @actual links.', [
'@count' => $countTestCases,
'@actual' => $countExtractedLinks,
]));
$this->linkcheckerSetting
->set('extract.' . $tagConfigName, FALSE);
$this->linkcheckerSetting
->save(TRUE);
}
}