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

Add Drupal Kernal tests to tide_site_preview module #97

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,35 @@ jobs:
INSTALL_SUGGEST: 1
BEHAT_PROFILE: "--profile=suggest"

phpunit_tests:
<<: *job-build
steps:
- attach_workspace:
at: /workspace
- checkout
- run: if [ -f "./dev-tools.sh" ] && [ ! "$DEV_TOOLS" ]; then ./dev-tools.sh; fi
- setup_remote_docker:
docker_layer_caching: true
- run: .circleci/build.sh
- run:
name: Run phpunit tests and generate coverage
command: |
.circleci/phpunit_tests.sh
.circleci/phpunit_coverage.sh
- run:
name: Copy test results
command: |
.circleci/phpunit_results.sh
when: always
- store_artifacts:
path: /tmp/phpunit
- store_test_results:
path: /tmp/phpunit

workflows:
version: 2
main:
jobs:
- build
- build_suggest
- phpunit_tests
1 change: 1 addition & 0 deletions dev-tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@
#
# Uncomment and set the Dev-Tools's commit value and commit this change.
# export GH_COMMIT=COMMIT_SHA
export GH_COMMIT=aefc0a705ec75976acdc8da4e6b89a77047fa307

bash <(curl -L https://raw.githubusercontent.com/dpc-sdp/dev-tools/master/install?"$(date +%s)") "$@"
10 changes: 8 additions & 2 deletions modules/tide_site_preview/src/Plugin/Block/PreviewLinksBlock.php
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 All @@ -154,7 +159,8 @@ public function build() {
if (!empty($sites['sections'][$primary_site->id()])) {
$primary_site_section = $this->siteHelper->getSiteById($sites['sections'][$primary_site->id()]);
}
$primary_preview_url = $this->sitePreviewHelper->buildFrontendPreviewLink($this->currentNode, $primary_site, $primary_site_section, $this->getConfiguration());
$url = $this->currentNode->toUrl();
$primary_preview_url = $this->sitePreviewHelper->buildFrontendPreviewLink($this->currentNode, $url, $primary_site, $primary_site_section, $this->getConfiguration());
unset($preview_urls[$primary_site->id()]);
array_unshift($preview_urls, $primary_preview_url);
}
Expand Down
30 changes: 16 additions & 14 deletions modules/tide_site_preview/src/TideSitePreviewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public function __construct(TideSiteHelper $site_helper) {
*
* @param \Drupal\node\NodeInterface $node
* The node.
* @param \Drupal\Core\Url $url
* The url.
* @param \Drupal\taxonomy\TermInterface $site
* The site of the preview link.
* @param \Drupal\taxonomy\TermInterface|null $section
Expand All @@ -49,7 +51,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 +70,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, $site_base_url, $url_options);
}
else {
$revision_id = $node->getLoadedRevisionId();
Expand All @@ -84,8 +86,8 @@ public function buildFrontendPreviewLink(NodeInterface $node, TermInterface $sit
/**
* Get the frontend URL of a node.
*
* @param \Drupal\node\NodeInterface $node
* The node.
* @param \Drupal\Core\Url $url
* The url.
* @param string $site_base_url
* The base URL of the frontend.
* @param array $url_options
Expand All @@ -94,19 +96,19 @@ public function buildFrontendPreviewLink(NodeInterface $node, TermInterface $sit
* @return \Drupal\Core\Url|string
* The Url.
*/
public function getNodeFrontendUrl(NodeInterface $node, $site_base_url = '', array $url_options = []) {
public function getNodeFrontendUrl(Url $url, string $site_base_url = '', array $url_options = []) {
try {
$url = $node->toUrl('canonical', [
'absolute' => TRUE,
'base_url' => $site_base_url,
] + $url_options);

$pattern = '/^\/site\-(\d+)\//';
$path = $url->toString();
$path = rtrim($path, '/');
$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) {
$pattern = '/' . preg_quote($site_base_url, '/') . '\/site\-(\d+)\//';
$clean_url = $site_base_url . $clean_url;
return $clean_url ? Url::fromUri($clean_url, $url_options) : $url;
}
$clean_url = preg_replace($pattern, $site_base_url . '/', $url->toString());
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
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
<?php

namespace Drupal\Tests\tide_site_preview\Kernel;

use Drupal\Core\Url;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\taxonomy\Entity\Term;

/**
* Tests the TideSitePreviewHelper.
*
* @coversDefaultClass \Drupal\tide_site_preview\TideSitePreviewHelper
* @group tide_site_preview
*/
class TideSitePreviewHelperTest extends EntityKernelTestBase {

/**
* 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',
'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->installConfig(['test_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',
],
];
}

/**
* @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);
}

/**
* Data provider of test dates.
*
* @return array
* Array of values.
*/
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',
],
];
}

}