Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EWPP-2022: Provide test coverage for the external links service. #1125

Merged
merged 1 commit into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions modules/oe_theme_helper/src/ExternalLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
class ExternalLinks implements ExternalLinksInterface {

/**
* The configuration factory.
* The internal domain regex.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
* @var string
*/
protected $configFactory;
protected string $internalDomainExpression;

/**
* Constructs an ExternalLinks object.
Expand All @@ -27,13 +27,18 @@ class ExternalLinks implements ExternalLinksInterface {
* The configuration factory.
*/
public function __construct(ConfigFactoryInterface $config_factory) {
$this->configFactory = $config_factory;
$this->internalDomainExpression = $config_factory->get('oe_theme_helper.internal_domains')->get('internal_domain') ?? '';
}

/**
* {@inheritdoc}
*/
public function isExternalLink($url): bool {
public function isExternalLink($url = NULL): bool {
// If no value is provided or the value si not a proper link, we'll
// return FALSE as it can't be evaluated.
if (!$url) {
return FALSE;
}
if ($url instanceof Url) {
$external = $url->isExternal();
$path = UrlHelper::parse($url->toString())['path'];
Expand All @@ -47,11 +52,10 @@ public function isExternalLink($url): bool {
}

// If it's an external link, make sure its domain is not internal.
$internal_domain_expression = $this->configFactory->get('oe_theme_helper.internal_domains')->get('internal_domain');
if (!$internal_domain_expression) {
if (!$this->internalDomainExpression) {
return $external;
}
return !preg_match_all($internal_domain_expression, $path);
return !preg_match_all($this->internalDomainExpression, $path);
}

}
37 changes: 37 additions & 0 deletions modules/oe_theme_helper/tests/src/Kernel/ExternalLinksTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types = 1);

namespace Drupal\Tests\oe_theme_helper\Kernel;

use Drupal\Core\Url;
use Drupal\Tests\oe_theme\Kernel\AbstractKernelTestBase;

/**
* Provides test coverage for the ExternalLinks service.
*
* @group batch1
*/
class ExternalLinksTest extends AbstractKernelTestBase {

/**
* Covers isExternalLink method.
*/
public function testIsExternalLink(): void {
$external_links = $this->container->get('oe_theme_helper.external_links');
// Assert internal URL.
$this->assertFalse($external_links->isExternalLink(Url::fromUserInput('/user')));
// Assert external string path.
$this->assertTrue($external_links->isExternalLink('https://example.com'));
// Assert external string path under EU domain.
$this->assertFalse($external_links->isExternalLink('https://example.europa.eu'));
$this->assertFalse($external_links->isExternalLink('www.ec.europa.eu/info'));
// Assert null and empty value.
$this->assertFalse($external_links->isExternalLink());
$this->assertFalse($external_links->isExternalLink(''));
// Assert incorrect paths.
$this->assertFalse($external_links->isExternalLink('www. incorrect . com'));
$this->assertFalse($external_links->isExternalLink('www. ec . europa . eu'));
}

}