From 53c467a7da5d046dd4fa57b71b4c7a4c4e828c40 Mon Sep 17 00:00:00 2001 From: tuutti Date: Wed, 24 Jul 2024 09:52:50 +0300 Subject: [PATCH 1/5] UHF-10339: Default to testing in case matching environment does not exist --- .../src/ClientBuilder.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/modules/helfi_paragraphs_news_list/src/ClientBuilder.php b/modules/helfi_paragraphs_news_list/src/ClientBuilder.php index 63773b266..5a93d6c56 100644 --- a/modules/helfi_paragraphs_news_list/src/ClientBuilder.php +++ b/modules/helfi_paragraphs_news_list/src/ClientBuilder.php @@ -4,6 +4,8 @@ namespace Drupal\helfi_paragraphs_news_list; +use Drupal\helfi_api_base\Environment\Environment; +use Drupal\helfi_api_base\Environment\EnvironmentEnum; use Drupal\helfi_api_base\Environment\EnvironmentResolverInterface; use Drupal\helfi_api_base\Environment\Project; use Drupal\helfi_api_base\Environment\ServiceEnum; @@ -34,8 +36,15 @@ public function __construct( * The client. */ public function create() : Client { - $service = $this->environmentResolver - ->getEnvironment(Project::ETUSIVU, $this->environmentResolver->getActiveEnvironmentName()) + try { + $environment = $this->environmentResolver + ->getEnvironment(Project::ETUSIVU, $this->environmentResolver->getActiveEnvironmentName()); + } + catch (\InvalidArgumentException) { + $environment = $this->environmentResolver + ->getEnvironment(Project::ETUSIVU, EnvironmentEnum::Test->value); + } + $service = $environment ->getService(ServiceEnum::ElasticProxy) ->address; From 675c173386cacdb54cd541de93b4b4e26e99cfd7 Mon Sep 17 00:00:00 2001 From: tuutti Date: Wed, 24 Jul 2024 10:07:34 +0300 Subject: [PATCH 2/5] UHF-10339: Added fallback test --- .../src/ClientBuilder.php | 1 - .../tests/src/Unit/ClientBuilderTest.php | 39 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 modules/helfi_paragraphs_news_list/tests/src/Unit/ClientBuilderTest.php diff --git a/modules/helfi_paragraphs_news_list/src/ClientBuilder.php b/modules/helfi_paragraphs_news_list/src/ClientBuilder.php index 5a93d6c56..58189ad5e 100644 --- a/modules/helfi_paragraphs_news_list/src/ClientBuilder.php +++ b/modules/helfi_paragraphs_news_list/src/ClientBuilder.php @@ -4,7 +4,6 @@ namespace Drupal\helfi_paragraphs_news_list; -use Drupal\helfi_api_base\Environment\Environment; use Drupal\helfi_api_base\Environment\EnvironmentEnum; use Drupal\helfi_api_base\Environment\EnvironmentResolverInterface; use Drupal\helfi_api_base\Environment\Project; diff --git a/modules/helfi_paragraphs_news_list/tests/src/Unit/ClientBuilderTest.php b/modules/helfi_paragraphs_news_list/tests/src/Unit/ClientBuilderTest.php new file mode 100644 index 000000000..ae622775a --- /dev/null +++ b/modules/helfi_paragraphs_news_list/tests/src/Unit/ClientBuilderTest.php @@ -0,0 +1,39 @@ +getConfigFactoryStub([ + 'helfi_api_base.environment_resolver.settings' => [ + EnvironmentResolver::ENVIRONMENT_NAME_KEY => 'nonexistent', + EnvironmentResolver::PROJECT_NAME_KEY => Project::ASUMINEN, + ], + ]); + $envResolver = new EnvironmentResolver($configFactory); + $sut = new ClientBuilder($envResolver); + $this->assertInstanceOf(Client::class, $sut->create()); + } + +} From f2eda5cf2bdae5945e39565b7a91681bfc7684d1 Mon Sep 17 00:00:00 2001 From: tuutti Date: Wed, 24 Jul 2024 10:08:39 +0300 Subject: [PATCH 3/5] UHF-10339: Added comment --- modules/helfi_paragraphs_news_list/src/ClientBuilder.php | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/helfi_paragraphs_news_list/src/ClientBuilder.php b/modules/helfi_paragraphs_news_list/src/ClientBuilder.php index 58189ad5e..bf23453a2 100644 --- a/modules/helfi_paragraphs_news_list/src/ClientBuilder.php +++ b/modules/helfi_paragraphs_news_list/src/ClientBuilder.php @@ -40,6 +40,7 @@ public function create() : Client { ->getEnvironment(Project::ETUSIVU, $this->environmentResolver->getActiveEnvironmentName()); } catch (\InvalidArgumentException) { + // Use testing in case matching environment does not exist. $environment = $this->environmentResolver ->getEnvironment(Project::ETUSIVU, EnvironmentEnum::Test->value); } From a5c35b48fdcf76941a12a5130b802b2818563dfa Mon Sep 17 00:00:00 2001 From: tuutti Date: Wed, 24 Jul 2024 10:12:10 +0300 Subject: [PATCH 4/5] UHF-10339: Removed active project --- .../tests/src/Unit/ClientBuilderTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/helfi_paragraphs_news_list/tests/src/Unit/ClientBuilderTest.php b/modules/helfi_paragraphs_news_list/tests/src/Unit/ClientBuilderTest.php index ae622775a..e4a884f34 100644 --- a/modules/helfi_paragraphs_news_list/tests/src/Unit/ClientBuilderTest.php +++ b/modules/helfi_paragraphs_news_list/tests/src/Unit/ClientBuilderTest.php @@ -5,7 +5,6 @@ namespace Drupal\Tests\helfi_\Unit; use Drupal\helfi_api_base\Environment\EnvironmentResolver; -use Drupal\helfi_api_base\Environment\Project; use Drupal\helfi_paragraphs_news_list\ClientBuilder; use Drupal\Tests\helfi_api_base\Traits\EnvironmentResolverTrait; use Drupal\Tests\UnitTestCase; @@ -28,7 +27,6 @@ public function testEnvironmentFallback() : void { $configFactory = $this->getConfigFactoryStub([ 'helfi_api_base.environment_resolver.settings' => [ EnvironmentResolver::ENVIRONMENT_NAME_KEY => 'nonexistent', - EnvironmentResolver::PROJECT_NAME_KEY => Project::ASUMINEN, ], ]); $envResolver = new EnvironmentResolver($configFactory); From 729acdf84fba50ca6049078a47bd85db9da31e6d Mon Sep 17 00:00:00 2001 From: tuutti Date: Wed, 24 Jul 2024 13:23:47 +0300 Subject: [PATCH 5/5] UHF-10339: Make sure environment exists --- .../helfi_paragraphs_news_list.module | 6 +++--- modules/helfi_paragraphs_news_list/src/ClientBuilder.php | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/helfi_paragraphs_news_list/helfi_paragraphs_news_list.module b/modules/helfi_paragraphs_news_list/helfi_paragraphs_news_list.module index 7ef2ca746..c29e09550 100644 --- a/modules/helfi_paragraphs_news_list/helfi_paragraphs_news_list.module +++ b/modules/helfi_paragraphs_news_list/helfi_paragraphs_news_list.module @@ -199,12 +199,12 @@ function helfi_paragraphs_news_list_preprocess_paragraph__news_list(&$variables) $resolver = \Drupal::service('helfi_api_base.environment_resolver'); try { - $environment = $resolver->getActiveEnvironmentName(); + $environment = $resolver->getEnvironment(Project::ETUSIVU, $resolver->getActiveEnvironmentName()); } catch (\InvalidArgumentException) { - $environment = EnvironmentEnum::Prod->value; + $environment = $resolver->getEnvironment(Project::ETUSIVU, EnvironmentEnum::Prod->value); } - $instanceUrl = $resolver->getEnvironment(Project::ETUSIVU, $environment) + $instanceUrl = $environment ->getUrl($langcode); $instanceUrl = $instanceUrl . match ($langcode) { diff --git a/modules/helfi_paragraphs_news_list/src/ClientBuilder.php b/modules/helfi_paragraphs_news_list/src/ClientBuilder.php index bf23453a2..a1818bb25 100644 --- a/modules/helfi_paragraphs_news_list/src/ClientBuilder.php +++ b/modules/helfi_paragraphs_news_list/src/ClientBuilder.php @@ -40,9 +40,9 @@ public function create() : Client { ->getEnvironment(Project::ETUSIVU, $this->environmentResolver->getActiveEnvironmentName()); } catch (\InvalidArgumentException) { - // Use testing in case matching environment does not exist. + // Use prod in case matching environment does not exist. $environment = $this->environmentResolver - ->getEnvironment(Project::ETUSIVU, EnvironmentEnum::Test->value); + ->getEnvironment(Project::ETUSIVU, EnvironmentEnum::Prod->value); } $service = $environment ->getService(ServiceEnum::ElasticProxy)