From f4cb0e9966b52dcc6b24a7a38b531bd4a85faf9d Mon Sep 17 00:00:00 2001 From: Vincent Gao Date: Sat, 4 Dec 2021 12:42:01 +1100 Subject: [PATCH] add test for buildFrontendPreviewLink function --- .../src/Plugin/Block/PreviewLinksBlock.php | 3 +- .../src/TideSitePreviewHelper.php | 20 ++- .../src/Kernel/TideSitePreviewHelperTest.php | 117 +++++++++++++++++- ...taxonomy_term.sites.field_site_domains.yml | 18 +++ ...orage.taxonomy_term.field_site_domains.yml | 18 +++ .../optional/taxonomy.vocabulary.sites.yml | 8 ++ .../test_tide_site_preview.info.yml | 4 + 7 files changed, 178 insertions(+), 10 deletions(-) create mode 100644 modules/tide_site_preview/tests/test_tide_site_preview/config/optional/field.field.taxonomy_term.sites.field_site_domains.yml create mode 100644 modules/tide_site_preview/tests/test_tide_site_preview/config/optional/field.storage.taxonomy_term.field_site_domains.yml create mode 100644 modules/tide_site_preview/tests/test_tide_site_preview/config/optional/taxonomy.vocabulary.sites.yml create mode 100644 modules/tide_site_preview/tests/test_tide_site_preview/test_tide_site_preview.info.yml diff --git a/modules/tide_site_preview/src/Plugin/Block/PreviewLinksBlock.php b/modules/tide_site_preview/src/Plugin/Block/PreviewLinksBlock.php index e53ff23..5e4cc8c 100644 --- a/modules/tide_site_preview/src/Plugin/Block/PreviewLinksBlock.php +++ b/modules/tide_site_preview/src/Plugin/Block/PreviewLinksBlock.php @@ -159,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); } diff --git a/modules/tide_site_preview/src/TideSitePreviewHelper.php b/modules/tide_site_preview/src/TideSitePreviewHelper.php index 60313d8..4cb15a1 100644 --- a/modules/tide_site_preview/src/TideSitePreviewHelper.php +++ b/modules/tide_site_preview/src/TideSitePreviewHelper.php @@ -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(); @@ -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); } diff --git a/modules/tide_site_preview/tests/src/Kernel/TideSitePreviewHelperTest.php b/modules/tide_site_preview/tests/src/Kernel/TideSitePreviewHelperTest.php index f5ebd7d..455d1a7 100644 --- a/modules/tide_site_preview/tests/src/Kernel/TideSitePreviewHelperTest.php +++ b/modules/tide_site_preview/tests/src/Kernel/TideSitePreviewHelperTest.php @@ -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. @@ -27,6 +29,9 @@ class TideSitePreviewHelperTest extends KernelTestBase { public static $modules = [ 'tide_site', 'tide_site_preview', + 'node', + 'taxonomy', + 'test_tide_site_preview', ]; /** @@ -34,15 +39,18 @@ class TideSitePreviewHelperTest extends KernelTestBase { */ 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()); @@ -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', + ], + ]; + } + } diff --git a/modules/tide_site_preview/tests/test_tide_site_preview/config/optional/field.field.taxonomy_term.sites.field_site_domains.yml b/modules/tide_site_preview/tests/test_tide_site_preview/config/optional/field.field.taxonomy_term.sites.field_site_domains.yml new file mode 100644 index 0000000..fe5a930 --- /dev/null +++ b/modules/tide_site_preview/tests/test_tide_site_preview/config/optional/field.field.taxonomy_term.sites.field_site_domains.yml @@ -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 diff --git a/modules/tide_site_preview/tests/test_tide_site_preview/config/optional/field.storage.taxonomy_term.field_site_domains.yml b/modules/tide_site_preview/tests/test_tide_site_preview/config/optional/field.storage.taxonomy_term.field_site_domains.yml new file mode 100644 index 0000000..df41308 --- /dev/null +++ b/modules/tide_site_preview/tests/test_tide_site_preview/config/optional/field.storage.taxonomy_term.field_site_domains.yml @@ -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 diff --git a/modules/tide_site_preview/tests/test_tide_site_preview/config/optional/taxonomy.vocabulary.sites.yml b/modules/tide_site_preview/tests/test_tide_site_preview/config/optional/taxonomy.vocabulary.sites.yml new file mode 100644 index 0000000..7a113e3 --- /dev/null +++ b/modules/tide_site_preview/tests/test_tide_site_preview/config/optional/taxonomy.vocabulary.sites.yml @@ -0,0 +1,8 @@ +langcode: en +status: true +dependencies: { } +name: Sites +vid: sites +description: 'Sites and sections' +hierarchy: 0 +weight: 0 diff --git a/modules/tide_site_preview/tests/test_tide_site_preview/test_tide_site_preview.info.yml b/modules/tide_site_preview/tests/test_tide_site_preview/test_tide_site_preview.info.yml new file mode 100644 index 0000000..1c3b01a --- /dev/null +++ b/modules/tide_site_preview/tests/test_tide_site_preview/test_tide_site_preview.info.yml @@ -0,0 +1,4 @@ +name: 'Test tide site preview' +type: module +description: 'Support module for tide site preview testing.' +package: Testing