From 09544b0ecb84321d231a4e6c7e3b4efa0cae0752 Mon Sep 17 00:00:00 2001 From: Vincent Gao Date: Fri, 3 Dec 2021 22:12:43 +1100 Subject: [PATCH 1/5] add phpunit tests --- .../src/Plugin/Block/PreviewLinksBlock.php | 7 +- .../src/TideSitePreviewHelper.php | 28 ++----- .../src/Kernel/TideSitePreviewHelperTest.php | 78 +++++++++++++++++++ 3 files changed, 92 insertions(+), 21 deletions(-) create mode 100644 modules/tide_site_preview/tests/src/Kernel/TideSitePreviewHelperTest.php diff --git a/modules/tide_site_preview/src/Plugin/Block/PreviewLinksBlock.php b/modules/tide_site_preview/src/Plugin/Block/PreviewLinksBlock.php index 70c8105..e53ff23 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()); } } } diff --git a/modules/tide_site_preview/src/TideSitePreviewHelper.php b/modules/tide_site_preview/src/TideSitePreviewHelper.php index 3eafc00..60313d8 100644 --- a/modules/tide_site_preview/src/TideSitePreviewHelper.php +++ b/modules/tide_site_preview/src/TideSitePreviewHelper.php @@ -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'] : [], ]; @@ -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(); @@ -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 ''; 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..f5ebd7d --- /dev/null +++ b/modules/tide_site_preview/tests/src/Kernel/TideSitePreviewHelperTest.php @@ -0,0 +1,78 @@ +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', + ], + ]; + } + +} From f4cb0e9966b52dcc6b24a7a38b531bd4a85faf9d Mon Sep 17 00:00:00 2001 From: Vincent Gao Date: Sat, 4 Dec 2021 12:42:01 +1100 Subject: [PATCH 2/5] 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 From 5ba2d3eb247778de8df937e64fa8efff1a0aea5a Mon Sep 17 00:00:00 2001 From: Vincent Gao Date: Tue, 7 Dec 2021 15:07:45 +1100 Subject: [PATCH 3/5] change the structure of test module --- .../tide_site_preview/src/TideSitePreviewHelper.php | 10 +++++++--- ...ld.field.taxonomy_term.sites.field_site_domains.yml | 0 .../field.storage.taxonomy_term.field_site_domains.yml | 0 .../config/optional/taxonomy.vocabulary.sites.yml | 0 .../test_tide_site_preview.info.yml | 0 .../tests/src/Kernel/TideSitePreviewHelperTest.php | 8 +++++++- 6 files changed, 14 insertions(+), 4 deletions(-) rename modules/tide_site_preview/tests/{ => modules}/test_tide_site_preview/config/optional/field.field.taxonomy_term.sites.field_site_domains.yml (100%) rename modules/tide_site_preview/tests/{ => modules}/test_tide_site_preview/config/optional/field.storage.taxonomy_term.field_site_domains.yml (100%) rename modules/tide_site_preview/tests/{ => modules}/test_tide_site_preview/config/optional/taxonomy.vocabulary.sites.yml (100%) rename modules/tide_site_preview/tests/{ => modules}/test_tide_site_preview/test_tide_site_preview.info.yml (100%) diff --git a/modules/tide_site_preview/src/TideSitePreviewHelper.php b/modules/tide_site_preview/src/TideSitePreviewHelper.php index 4cb15a1..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 @@ -85,11 +87,12 @@ public function buildFrontendPreviewLink(NodeInterface $node, Url $url, TermInte * Get the frontend URL of a node. * * @param \Drupal\Core\Url $url - * The node. + * The url. * @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. */ @@ -97,7 +100,7 @@ public function getNodeFrontendUrl(Url $url, string $site_base_url = '', array $ try { $path = $url->toString(); $path = rtrim($path, '/'); - $clean_url = preg_replace('/\/site\-(\d+)\//', '/', $path,1); + $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; } @@ -106,7 +109,8 @@ public function getNodeFrontendUrl(Url $url, string $site_base_url = '', array $ return $clean_url ? Url::fromUri($clean_url, $url_options) : $url; } return $clean_url ? Url::fromUserInput($clean_url, $url_options) : $url; - } catch (Exception $exception) { + } + catch (Exception $exception) { watchdog_exception('tide_site_preview', $exception); } return ''; 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/modules/test_tide_site_preview/config/optional/field.field.taxonomy_term.sites.field_site_domains.yml similarity index 100% rename from modules/tide_site_preview/tests/test_tide_site_preview/config/optional/field.field.taxonomy_term.sites.field_site_domains.yml rename to modules/tide_site_preview/tests/modules/test_tide_site_preview/config/optional/field.field.taxonomy_term.sites.field_site_domains.yml 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/modules/test_tide_site_preview/config/optional/field.storage.taxonomy_term.field_site_domains.yml similarity index 100% rename from modules/tide_site_preview/tests/test_tide_site_preview/config/optional/field.storage.taxonomy_term.field_site_domains.yml rename to modules/tide_site_preview/tests/modules/test_tide_site_preview/config/optional/field.storage.taxonomy_term.field_site_domains.yml diff --git a/modules/tide_site_preview/tests/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 similarity index 100% rename from modules/tide_site_preview/tests/test_tide_site_preview/config/optional/taxonomy.vocabulary.sites.yml rename to modules/tide_site_preview/tests/modules/test_tide_site_preview/config/optional/taxonomy.vocabulary.sites.yml diff --git a/modules/tide_site_preview/tests/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 similarity index 100% rename from modules/tide_site_preview/tests/test_tide_site_preview/test_tide_site_preview.info.yml rename to modules/tide_site_preview/tests/modules/test_tide_site_preview/test_tide_site_preview.info.yml diff --git a/modules/tide_site_preview/tests/src/Kernel/TideSitePreviewHelperTest.php b/modules/tide_site_preview/tests/src/Kernel/TideSitePreviewHelperTest.php index 455d1a7..41e5db3 100644 --- a/modules/tide_site_preview/tests/src/Kernel/TideSitePreviewHelperTest.php +++ b/modules/tide_site_preview/tests/src/Kernel/TideSitePreviewHelperTest.php @@ -8,7 +8,7 @@ use Drupal\taxonomy\Entity\Term; /** - * Tests the TideSitePreviewHelper + * Tests the TideSitePreviewHelper. * * @group tide_site_preview */ @@ -99,6 +99,12 @@ public function testBuildFrontendPreviewLink($node_values, $url, $term_values, $ $this->assertEquals($expected, $url); } + /** + * Data provider of test dates. + * + * @return array + * Array of values. + */ public function buildFrontendPreviewLinkDataProvider() { return [ [ From 274a9be651943d1c2d7128394e398829776b93b3 Mon Sep 17 00:00:00 2001 From: Vincent Gao Date: Tue, 7 Dec 2021 15:42:38 +1100 Subject: [PATCH 4/5] adding test coverage test coverage Update test coverage settings update Update circleci configs Update phpunit test settings Update test test update update update update update update --- .circleci/config.yml | 26 +++++++++++++++++++ .circleci/phpunit_coverage.sh | 7 +++++ .circleci/phpunit_results.sh | 6 +++++ .circleci/phpunit_tests.sh | 9 +++++++ dev-tools.sh | 1 + .../src/Kernel/TideSitePreviewHelperTest.php | 1 + 6 files changed, 50 insertions(+) create mode 100755 .circleci/phpunit_coverage.sh create mode 100755 .circleci/phpunit_results.sh create mode 100755 .circleci/phpunit_tests.sh 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/.circleci/phpunit_coverage.sh b/.circleci/phpunit_coverage.sh new file mode 100755 index 0000000..905e7bc --- /dev/null +++ b/.circleci/phpunit_coverage.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +## +# Generate coverage report +# +set -e +echo "==> Generate coverage report" +ahoy cli "phpdbg -qrr vendor/bin/phpunit ./dpc-sdp --coverage-html /app/phpunit/coverage-report" diff --git a/.circleci/phpunit_results.sh b/.circleci/phpunit_results.sh new file mode 100755 index 0000000..98cc64f --- /dev/null +++ b/.circleci/phpunit_results.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +## +# Moves phpunit results from inside container to outside. +# +set -e +docker cp $(docker-compose ps -q cli):/app/phpunit/ /tmp/ diff --git a/.circleci/phpunit_tests.sh b/.circleci/phpunit_tests.sh new file mode 100755 index 0000000..e5771b3 --- /dev/null +++ b/.circleci/phpunit_tests.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +## +# Run phpunit tests in CI. +# +set -e + +ahoy cli "mkdir -p /app/phpunit" +echo "==> Run phpunit tests" +ahoy cli "vendor/bin/phpunit ./dpc-sdp --log-junit /app/phpunit/junit.xml" 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/tests/src/Kernel/TideSitePreviewHelperTest.php b/modules/tide_site_preview/tests/src/Kernel/TideSitePreviewHelperTest.php index 41e5db3..08e7b9c 100644 --- a/modules/tide_site_preview/tests/src/Kernel/TideSitePreviewHelperTest.php +++ b/modules/tide_site_preview/tests/src/Kernel/TideSitePreviewHelperTest.php @@ -10,6 +10,7 @@ /** * Tests the TideSitePreviewHelper. * + * @coversDefaultClass \Drupal\tide_site_preview\TideSitePreviewHelper * @group tide_site_preview */ class TideSitePreviewHelperTest extends EntityKernelTestBase { From 28f8eb26776e75890d33dce30464a1023a60bddc Mon Sep 17 00:00:00 2001 From: Vincent Gao Date: Fri, 28 Jan 2022 17:23:41 +1100 Subject: [PATCH 5/5] Remove duplicate settings --- .circleci/phpunit_coverage.sh | 7 ------- .circleci/phpunit_results.sh | 6 ------ .circleci/phpunit_tests.sh | 9 --------- 3 files changed, 22 deletions(-) delete mode 100755 .circleci/phpunit_coverage.sh delete mode 100755 .circleci/phpunit_results.sh delete mode 100755 .circleci/phpunit_tests.sh diff --git a/.circleci/phpunit_coverage.sh b/.circleci/phpunit_coverage.sh deleted file mode 100755 index 905e7bc..0000000 --- a/.circleci/phpunit_coverage.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env bash -## -# Generate coverage report -# -set -e -echo "==> Generate coverage report" -ahoy cli "phpdbg -qrr vendor/bin/phpunit ./dpc-sdp --coverage-html /app/phpunit/coverage-report" diff --git a/.circleci/phpunit_results.sh b/.circleci/phpunit_results.sh deleted file mode 100755 index 98cc64f..0000000 --- a/.circleci/phpunit_results.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env bash -## -# Moves phpunit results from inside container to outside. -# -set -e -docker cp $(docker-compose ps -q cli):/app/phpunit/ /tmp/ diff --git a/.circleci/phpunit_tests.sh b/.circleci/phpunit_tests.sh deleted file mode 100755 index e5771b3..0000000 --- a/.circleci/phpunit_tests.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env bash -## -# Run phpunit tests in CI. -# -set -e - -ahoy cli "mkdir -p /app/phpunit" -echo "==> Run phpunit tests" -ahoy cli "vendor/bin/phpunit ./dpc-sdp --log-junit /app/phpunit/junit.xml"