From 09544b0ecb84321d231a4e6c7e3b4efa0cae0752 Mon Sep 17 00:00:00 2001 From: Vincent Gao Date: Fri, 3 Dec 2021 22:12:43 +1100 Subject: [PATCH] add phpunit tests --- .../src/Plugin/Block/PreviewLinksBlock.php | 7 +- .../src/TideSitePreviewHelper.php | 28 ++----- .../src/Kernel/TideSitePreviewHelperTest.php | 78 +++++++++++++++++++ 3 files changed, 92 insertions(+), 21 deletions(-) create mode 100644 modules/tide_site_preview/tests/src/Kernel/TideSitePreviewHelperTest.php diff --git a/modules/tide_site_preview/src/Plugin/Block/PreviewLinksBlock.php b/modules/tide_site_preview/src/Plugin/Block/PreviewLinksBlock.php index 70c8105..e53ff23 100644 --- a/modules/tide_site_preview/src/Plugin/Block/PreviewLinksBlock.php +++ b/modules/tide_site_preview/src/Plugin/Block/PreviewLinksBlock.php @@ -142,7 +142,12 @@ public function build() { if (!empty($sites['sections'][$site_id])) { $section = $this->siteHelper->getSiteById($sites['sections'][$site_id]); } - $preview_urls[$site_id] = $this->sitePreviewHelper->buildFrontendPreviewLink($this->currentNode, $site, $section, $this->getConfiguration()); + $site_base_url = $this->siteHelper->getSiteBaseUrl($site); + $url = $this->currentNode->toUrl('canonical', [ + 'absolute' => TRUE, + 'base_url' => $site_base_url, + ]); + $preview_urls[$site_id] = $this->sitePreviewHelper->buildFrontendPreviewLink($this->currentNode, $url, $site, $section, $this->getConfiguration()); } } } diff --git a/modules/tide_site_preview/src/TideSitePreviewHelper.php b/modules/tide_site_preview/src/TideSitePreviewHelper.php index 3eafc00..60313d8 100644 --- a/modules/tide_site_preview/src/TideSitePreviewHelper.php +++ b/modules/tide_site_preview/src/TideSitePreviewHelper.php @@ -49,7 +49,7 @@ public function __construct(TideSiteHelper $site_helper) { * * name: The site/section name. * * url: The absolute URL of the preview link. */ - public function buildFrontendPreviewLink(NodeInterface $node, TermInterface $site, TermInterface $section = NULL, array $configuration = []) : array { + public function buildFrontendPreviewLink(NodeInterface $node, Url $url, TermInterface $site, TermInterface $section = NULL, array $configuration = []) : array { $url_options = [ 'attributes' => !(empty($configuration['open_new_window'])) ? ['target' => '_blank'] : [], ]; @@ -68,7 +68,7 @@ public function buildFrontendPreviewLink(NodeInterface $node, TermInterface $sit $site_base_url = $this->siteHelper->getSiteBaseUrl($site); if ($node->isPublished() && $node->isDefaultRevision()) { unset($url_options['query']['section']); - $preview_link['url'] = $this->getNodeFrontendUrl($node, $site_base_url, $url_options); + $preview_link['url'] = $this->getNodeFrontendUrl($url, $url_options); } else { $revision_id = $node->getLoadedRevisionId(); @@ -84,31 +84,19 @@ public function buildFrontendPreviewLink(NodeInterface $node, TermInterface $sit /** * Get the frontend URL of a node. * - * @param \Drupal\node\NodeInterface $node + * @param \Drupal\Core\Url $url * The node. - * @param string $site_base_url - * The base URL of the frontend. - * @param array $url_options - * The extra options. * * @return \Drupal\Core\Url|string * The Url. */ - public function getNodeFrontendUrl(NodeInterface $node, $site_base_url = '', array $url_options = []) { + public function getNodeFrontendUrl(Url $url, array $url_options = []) { try { - $url = $node->toUrl('canonical', [ - 'absolute' => TRUE, - 'base_url' => $site_base_url, - ] + $url_options); - - $pattern = '/^\/site\-(\d+)\//'; - if ($site_base_url) { - $pattern = '/' . preg_quote($site_base_url, '/') . '\/site\-(\d+)\//'; - } - $clean_url = preg_replace($pattern, $site_base_url . '/', $url->toString()); + $path = $url->toString(); + $path = rtrim($path, '/'); + $clean_url = preg_replace('/\/site\-(\d+)\//', '/', $path); return $clean_url ? Url::fromUri($clean_url, $url_options) : $url; - } - catch (Exception $exception) { + } catch (Exception $exception) { watchdog_exception('tide_site_preview', $exception); } return ''; diff --git a/modules/tide_site_preview/tests/src/Kernel/TideSitePreviewHelperTest.php b/modules/tide_site_preview/tests/src/Kernel/TideSitePreviewHelperTest.php new file mode 100644 index 0000000..f5ebd7d --- /dev/null +++ b/modules/tide_site_preview/tests/src/Kernel/TideSitePreviewHelperTest.php @@ -0,0 +1,78 @@ +installConfig(['tide_site_preview']); + $this->tideSitePreviewHelper =\Drupal::service('tide_site_preview.helper'); + } + + /** + * @covers ::getNodeFrontendUrl + * @dataProvider urlDataProvider + */ + public function testgetNodeFrontendUrl($value, $expected) { + $url = Url::fromUri($value); + $url = $this->tideSitePreviewHelper->getNodeFrontendUrl($url); + $this->assertEquals($expected, $url->toString()); + } + + /** + * Data provider of test dates. + * + * @return array + * Array of values. + */ + public function urlDataProvider() { + return [ + [ + 'value' => 'https://www.vic.gov.au/site-1/hello_world', + 'expected' => 'https://www.vic.gov.au/hello_world', + ], + [ + 'value' => 'https://site-1/hello_world', + 'expected' => 'https://hello_world', + ], + [ + 'value' => 'https://site-1/', + 'expected' => 'https://site-1', + ], + [ + 'value' => 'https://site-1', + 'expected' => 'https://site-1', + ], + ]; + } + +}