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

Commit

Permalink
add test for buildFrontendPreviewLink function
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-gao committed Dec 4, 2021
1 parent 09544b0 commit 47ccb91
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 9 deletions.
20 changes: 15 additions & 5 deletions modules/tide_site_preview/src/TideSitePreviewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function buildFrontendPreviewLink(NodeInterface $node, Url $url, TermInte
$site_base_url = $this->siteHelper->getSiteBaseUrl($site);
if ($node->isPublished() && $node->isDefaultRevision()) {
unset($url_options['query']['section']);
$preview_link['url'] = $this->getNodeFrontendUrl($url, $url_options);
$preview_link['url'] = $this->getNodeFrontendUrl($url, $site_base_url, $url_options);
}
else {
$revision_id = $node->getLoadedRevisionId();
Expand All @@ -86,16 +86,26 @@ public function buildFrontendPreviewLink(NodeInterface $node, Url $url, TermInte
*
* @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(Url $url, array $url_options = []) {
public function getNodeFrontendUrl(Url $url, string $site_base_url = '', array $url_options = []) {
try {
$path = $url->toString();
$path = rtrim($path, '/');
$clean_url = preg_replace('/\/site\-(\d+)\//', '/', $path);
return $clean_url ? Url::fromUri($clean_url, $url_options) : $url;
$clean_url = preg_replace('/\/site\-(\d+)\//', '/', $path,1);
if ((strpos($clean_url, '/') !== 0) && (strpos($clean_url, '#') !== 0) && (strpos($clean_url, '?') !== 0)) {
return $clean_url ? Url::fromUri($clean_url, $url_options) : $url;
}
if ($site_base_url) {
$clean_url = $site_base_url . $clean_url;
return $clean_url ? Url::fromUri($clean_url, $url_options) : $url;
}
return $clean_url ? Url::fromUserInput($clean_url, $url_options) : $url;
} catch (Exception $exception) {
watchdog_exception('tide_site_preview', $exception);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
namespace Drupal\Tests\tide_site_preview\Kernel;

use Drupal\Core\Url;
use Drupal\Tests\token\Kernel\KernelTestBase;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\taxonomy\Entity\Term;

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

/**
* The tide_site_preview.helper service.
Expand All @@ -27,22 +29,28 @@ class TideSitePreviewHelperTest extends KernelTestBase {
public static $modules = [
'tide_site',
'tide_site_preview',
'node',
'taxonomy',
'test_tide_site_preview',
];

/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installEntitySchema('node');
$this->installEntitySchema('taxonomy_term');
$this->installConfig(['tide_site_preview']);
$this->tideSitePreviewHelper =\Drupal::service('tide_site_preview.helper');
$this->installConfig(['test_tide_site_preview']);
$this->tideSitePreviewHelper = \Drupal::service('tide_site_preview.helper');
}

/**
* @covers ::getNodeFrontendUrl
* @dataProvider urlDataProvider
*/
public function testgetNodeFrontendUrl($value, $expected) {
public function testGetNodeFrontendUrl($value, $expected) {
$url = Url::fromUri($value);
$url = $this->tideSitePreviewHelper->getNodeFrontendUrl($url);
$this->assertEquals($expected, $url->toString());
Expand Down Expand Up @@ -75,4 +83,105 @@ public function urlDataProvider() {
];
}

/**
* @covers ::buildFrontendPreviewLink
* @dataProvider buildFrontendPreviewLinkDataProvider
*/
public function testBuildFrontendPreviewLink($node_values, $url, $term_values, $config, $expected) {
$node = Node::create($node_values);
$node->save();
$url = Url::fromUserInput($url);
$term = Term::create($term_values);
$term->save();

$result = $this->tideSitePreviewHelper->buildFrontendPreviewLink($node, $url, $term, NULL, $config);
$url = $result['url']->toString();
$this->assertEquals($expected, $url);
}

public function buildFrontendPreviewLinkDataProvider() {
return [
[
[
'type' => 'test',
'title' => 'test_1',
],
'/site-1/hello_world',
[
'vid' => 'sites',
'name' => 'vicgovau',
'field_site_domains' => 'www.vic.gov.au',
],
[
'id' => 'tide_site_preview_links_block',
'label' => 'Click the links below to preview this revision on frontend sites',
'provider' => 'tide_site_preview',
'label_display' => 'visible',
'open_new_window' => 1,
],
'https://www.vic.gov.au/hello_world',
],
[
[
'type' => 'test',
'title' => 'test_2',
],
'/site-2/site-4/hello_world',
[
'vid' => 'sites',
'name' => 'vicgovau',
'field_site_domains' => 'www.vic.gov.au',
],
[
'id' => 'tide_site_preview_links_block',
'label' => 'Click the links below to preview this revision on frontend sites',
'provider' => 'tide_site_preview',
'label_display' => 'visible',
'open_new_window' => 1,
],
'https://www.vic.gov.au/site-4/hello_world',
],
[
[
'type' => 'test',
'title' => 'test_3',
],
'/site-3/site-6/site-7/hello_world',
[
'vid' => 'sites',
'name' => 'vicgovau',
'field_site_domains' => 'www.vic.gov.au',
],
[
'id' => 'tide_site_preview_links_block',
'label' => 'Click the links below to preview this revision on frontend sites',
'provider' => 'tide_site_preview',
'label_display' => 'visible',
'open_new_window' => 1,
],
'https://www.vic.gov.au/site-6/site-7/hello_world',
],
[
[
'type' => 'test',
'title' => 'test_4',
],
'/site-4/hello_world',
[
'vid' => 'sites',
'name' => 'vicgovau',
'field_site_domains' => 'www.vic.gov.au',
],
[
'id' => 'tide_site_preview_links_block',
'label' => 'Click the links below to preview this revision on frontend sites',
'provider' => 'tide_site_preview',
'label_display' => 'visible',
'open_new_window' => 1,
],
'https://www.vic.gov.au/hello_world',
],
];
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
langcode: en
status: true
dependencies:
config:
- field.storage.taxonomy_term.field_site_domains
- taxonomy.vocabulary.sites
id: taxonomy_term.sites.field_site_domains
field_name: field_site_domains
entity_type: taxonomy_term
bundle: sites
label: Domains
description: 'Please enter domains for this site. One domain per line. Use * as a wildcard.'
required: true
translatable: false
default_value: { }
default_value_callback: ''
settings: { }
field_type: string_long
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
langcode: en
status: true
dependencies:
module:
- taxonomy
id: taxonomy_term.field_site_domains
field_name: field_site_domains
entity_type: taxonomy_term
type: string_long
settings:
case_sensitive: false
module: core
locked: false
cardinality: 1
translatable: true
indexes: { }
persist_with_no_fields: false
custom_storage: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
langcode: en
status: true
dependencies: { }
name: Sites
vid: sites
description: 'Sites and sections'
hierarchy: 0
weight: 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: 'Test tide site preview'
type: module
description: 'Support module for tide site preview testing.'
package: Testing

0 comments on commit 47ccb91

Please sign in to comment.