View source
<?php
namespace Drupal\Tests\linkchecker\Kernel;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\link\LinkItemInterface;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\node\Traits\NodeCreationTrait;
class LinkcheckerLinkLinkExtractorTest extends KernelTestBase {
use NodeCreationTrait;
public static $modules = [
'user',
'node',
'filter',
'system',
'field',
'text',
'dynamic_entity_reference',
'link',
'linkchecker',
];
protected $linkcheckerSetting;
protected $linkLinkExtractor;
public function setUp() {
parent::setUp();
$this
->installSchema('system', 'sequences');
$this
->installSchema('linkchecker', 'linkchecker_index');
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installEntitySchema('linkcheckerlink');
$this
->installConfig([
'field',
'node',
'filter',
'linkchecker',
]);
$this->linkcheckerSetting = $this->container
->get('config.factory')
->getEditable('linkchecker.settings');
$extractorManager = $this->container
->get('plugin.manager.link_extractor');
$this->linkLinkExtractor = $extractorManager
->createInstance('link_link_extractor');
}
public function testLinkExtractor() {
$type = NodeType::create([
'name' => 'Links',
'type' => 'links',
]);
$type
->save();
FieldStorageConfig::create([
'field_name' => 'field_test',
'entity_type' => 'node',
'entity_bundle' => 'links',
'type' => 'link',
'cardinality' => -1,
])
->save();
FieldConfig::create([
'entity_type' => 'node',
'field_name' => 'field_test',
'bundle' => 'links',
'settings' => [
'link_type' => LinkItemInterface::LINK_GENERIC,
],
])
->save();
$node = $this
->createNode([
'type' => 'links',
'field_test' => [
[
$this
->getTestLinks(),
],
],
]);
$node
->set('field_test', $this
->getTestLinks());
$htmlTagConfig = [
'from_audio',
'from_embed',
'from_iframe',
'from_img',
'from_object',
'from_video',
];
foreach ($htmlTagConfig as $tagConfigName) {
$this->linkcheckerSetting
->set('extract.' . $tagConfigName, FALSE);
}
$this->linkcheckerSetting
->set('extract.from_a', TRUE);
$this->linkcheckerSetting
->save(TRUE);
$testCases = $this
->getTestUrlList();
$extractedUrls = [];
$field_values = $node
->get('field_test')
->getValue();
foreach ($field_values as $fv) {
$extractedUrls = array_merge($extractedUrls, $this->linkLinkExtractor
->extract([
$fv,
]));
}
foreach ($testCases as $url) {
$this
->assertContains($url, $extractedUrls, new FormattableMarkup('URL @url was not extracted from tag @tag!', [
'@url' => $url,
'@tag' => str_replace('from_', '', 'from_a'),
]));
}
$countTestCases = count($testCases);
$countExtractedLinks = count($extractedUrls);
$this
->assertEquals($countTestCases, $countExtractedLinks, new FormattableMarkup('Expected to extract @count but get @actual links.', [
'@count' => $countTestCases,
'@actual' => $countExtractedLinks,
]));
}
protected function getTestLinks() {
$links = $this
->getTestUrlList();
$values = [];
foreach ($links as $link) {
$values[] = [
'uri' => $link,
'title' => $link,
];
}
return $values;
}
protected function getTestUrlList() {
return [
'http://www.lagrandeepicerie.fr/#e-boutique/Les_produits_du_moment,2/coffret_vins_doux_naturels,149',
'http://wetterservice.msn.de/phclip.swf?zip=60329&ort=Frankfurt',
'http://www.msn.de/',
'http://www.adobe.com/',
'http://www.apple.com/qtactivex/qtplugin.cab',
'http://example.net/video/foo1.mov',
'http://example.net/video/foo2.mov',
'http://example.net/video/foo3.mov',
'http://example.org/video/foo1.mp4',
'http://example.org/video/foo2.mp4',
'http://example.org/video/foo3.mp4',
'http://example.org/video/foo4.mp4',
'http://example.org/video/foo5.mp4',
'http://example.org/video/foo6.mp4',
'http://example.org/video/player1.swf',
'http://example.org/video/player2.swf',
'http://example.org/video/player3.swf',
'http://example.com/iframe/',
'http://www.theora.org/cortado.jar',
'http://v2v.cc/~j/theora_testsuite/pixel_aspect_ratio.ogg',
'http://v2v.cc/~j/theora_testsuite/pixel_aspect_ratio.mov',
'http://v2v.cc/~j/theora_testsuite/320x240.ogg',
'http://example.com/foo bar/is_valid-hack.test',
'http://example.com/ajax.html#key1=value1&key2=value2',
'http://example.com/test.html#test',
'http://example.com/test.html#test%20ABC',
'mailto:test@example.com',
'javascript:foo()',
];
}
}