Skip to content

Commit

Permalink
EWPP-2022: Provide test coverage for the external links service.
Browse files Browse the repository at this point in the history
  • Loading branch information
22Alexandra committed Jun 17, 2022
1 parent d91735c commit 66d0da3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
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
// consider it external.
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'));
}

}

0 comments on commit 66d0da3

Please sign in to comment.