Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
add phpunit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-gao committed Dec 3, 2021
1 parent 8bb4700 commit 09544b0
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Expand Down
28 changes: 8 additions & 20 deletions modules/tide_site_preview/src/TideSitePreviewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] : [],
];
Expand All @@ -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();
Expand All @@ -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 '';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Drupal\Tests\tide_site_preview\Kernel;

use Drupal\Core\Url;
use Drupal\Tests\token\Kernel\KernelTestBase;

/**
* Tests the TideSitePreviewHelper
*
* @group tide_site_preview
*/
class TideSitePreviewHelperTest extends KernelTestBase {

/**
* The tide_site_preview.helper service.
*
* @var \Drupal\tide_site_preview\TideSitePreviewHelper
*/
protected $tideSitePreviewHelper;

/**
* The modules to load to run the test.
*
* @var array
*/
public static $modules = [
'tide_site',
'tide_site_preview',
];

/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->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',
],
];
}

}

0 comments on commit 09544b0

Please sign in to comment.