ExtlinkTestTarget.php in External Links 8
File
tests/src/FunctionalJavascript/ExtlinkTestTarget.php
View source
<?php
namespace Drupal\Tests\extlink\FunctionalJavascript;
class ExtlinkTestTarget extends ExtlinkTestBase {
public function testExtlinkTarget() {
$this
->config('extlink.settings')
->set('extlink_target', TRUE)
->save();
$this
->drupalLogin($this->adminUser);
$settings = [
'type' => 'page',
'title' => 'test page',
'body' => [
[
'value' => '<p><a href="http://google.com">Google!</a></p>',
'format' => $this->emptyFormat
->id(),
],
],
];
$node = $this
->drupalCreateNode($settings);
$this
->drupalGet($node
->toUrl());
$page = $this
->getSession()
->getPage();
$this
->assertTrue($page
->hasLink('Google!'));
$externalLink = $page
->find('xpath', self::EXTLINK_EXT_XPATH);
$this
->assertTrue(!is_null($externalLink) && $externalLink
->isVisible(), 'External Link does not exist.');
$link = $page
->findLink('Google!');
$this
->assertTrue($link
->getAttribute('target') === '_blank', 'ExtLink target attribute is not "_blank".');
$this
->assertTrue($link
->getAttribute('rel') === 'noopener' || $link
->getAttribute('rel') === 'noopener noreferrer' || $link
->getAttribute('rel') === 'noreferrer noopener', 'ExtLink rel attribute is not "noopener".');
}
public function testExtlinkTargetNoOverride() {
$this
->config('extlink.settings')
->set('extlink_target', TRUE)
->save();
$this
->config('extlink.settings')
->set('extlink_target_no_override', TRUE)
->save();
$this
->drupalLogin($this->adminUser);
$settings = [
'type' => 'page',
'title' => 'test page',
'body' => [
[
'value' => '<p><a href="http://google.com" target="_self">Google!</a></p>',
'format' => $this->emptyFormat
->id(),
],
],
];
$node = $this
->drupalCreateNode($settings);
$this
->drupalGet($node
->toUrl());
$page = $this
->getSession()
->getPage();
$this
->assertTrue($page
->hasLink('Google!'));
$externalLink = $page
->find('xpath', self::EXTLINK_EXT_XPATH);
$this
->assertTrue(!is_null($externalLink) && $externalLink
->isVisible(), 'External Link does not exist.');
$link = $page
->findLink('Google!');
$this
->assertTrue($link
->getAttribute('target') === '_self', 'ExtLink target attribute is not "_self".');
$this
->assertTrue($link
->getAttribute('rel') === 'noopener' || $link
->getAttribute('rel') === 'noopener noreferrer' || $link
->getAttribute('rel') === 'noreferrer noopener', 'ExtLink rel attribute is not "noopener".');
}
}