diff --git a/.circleci/config.yml b/.circleci/config.yml index f35e1f0..2c919b7 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -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 diff --git a/dev-tools.sh b/dev-tools.sh index d7573df..281696b 100755 --- a/dev-tools.sh +++ b/dev-tools.sh @@ -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)") "$@" diff --git a/modules/tide_site_preview/src/Plugin/Block/PreviewLinksBlock.php b/modules/tide_site_preview/src/Plugin/Block/PreviewLinksBlock.php index 70c8105..5e4cc8c 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()); } } } @@ -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); } diff --git a/modules/tide_site_preview/src/TideSitePreviewHelper.php b/modules/tide_site_preview/src/TideSitePreviewHelper.php index 3eafc00..cb596f6 100644 --- a/modules/tide_site_preview/src/TideSitePreviewHelper.php +++ b/modules/tide_site_preview/src/TideSitePreviewHelper.php @@ -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 @@ -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'] : [], ]; @@ -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(); @@ -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 @@ -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); diff --git a/modules/tide_site_preview/tests/modules/test_tide_site_preview/config/optional/field.field.taxonomy_term.sites.field_site_domains.yml b/modules/tide_site_preview/tests/modules/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/modules/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/modules/test_tide_site_preview/config/optional/field.storage.taxonomy_term.field_site_domains.yml b/modules/tide_site_preview/tests/modules/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/modules/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/modules/test_tide_site_preview/config/optional/taxonomy.vocabulary.sites.yml b/modules/tide_site_preview/tests/modules/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/modules/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/modules/test_tide_site_preview/test_tide_site_preview.info.yml b/modules/tide_site_preview/tests/modules/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/modules/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 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..08e7b9c --- /dev/null +++ b/modules/tide_site_preview/tests/src/Kernel/TideSitePreviewHelperTest.php @@ -0,0 +1,194 @@ +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', + ], + ]; + } + +}