From 66d0da39c6a1f81f00a87af77474fd81ee2c3f84 Mon Sep 17 00:00:00 2001 From: 22Alexandra Date: Fri, 17 Jun 2022 12:51:18 +0300 Subject: [PATCH] EWPP-2022: Provide test coverage for the external links service. --- modules/oe_theme_helper/src/ExternalLinks.php | 20 ++++++---- .../tests/src/Kernel/ExternalLinksTest.php | 37 +++++++++++++++++++ 2 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 modules/oe_theme_helper/tests/src/Kernel/ExternalLinksTest.php diff --git a/modules/oe_theme_helper/src/ExternalLinks.php b/modules/oe_theme_helper/src/ExternalLinks.php index 77e5752488..c61b675629 100644 --- a/modules/oe_theme_helper/src/ExternalLinks.php +++ b/modules/oe_theme_helper/src/ExternalLinks.php @@ -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. @@ -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']; @@ -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); } } diff --git a/modules/oe_theme_helper/tests/src/Kernel/ExternalLinksTest.php b/modules/oe_theme_helper/tests/src/Kernel/ExternalLinksTest.php new file mode 100644 index 0000000000..fcdcf10a9b --- /dev/null +++ b/modules/oe_theme_helper/tests/src/Kernel/ExternalLinksTest.php @@ -0,0 +1,37 @@ +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')); + } + +}