From 5c131679677250c501c956bb26265a6e34f34fa5 Mon Sep 17 00:00:00 2001 From: Basil Date: Tue, 7 Dec 2021 10:38:57 +0100 Subject: [PATCH] add invalid configuration hint (#2113) * add invalid configuration hint * add test * changelog --- core/CHANGELOG.md | 5 +++++ core/web/CompositionResolver.php | 14 ++++++++++---- tests/core/web/CompositionResolverTest.php | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 8c8e2e8aa..5151f18c2 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -3,6 +3,11 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). In order to read more about upgrading and BC breaks have a look at the [UPGRADE Document](UPGRADE.md). +## 2.0.2 (7. December 2021) + ++ [#2113](https://github.com/luyadev/luya/pull/2113) Improve error handling for expected composition values configuration. ++ New translations for bg, fr, hu, pt and ro + ## 2.0.1 (29. July 2021) + Forgot to adjust the LUYA Version Constant `luya\Boot::VERSION`. diff --git a/core/web/CompositionResolver.php b/core/web/CompositionResolver.php index 58c208e64..fb9227092 100644 --- a/core/web/CompositionResolver.php +++ b/core/web/CompositionResolver.php @@ -6,6 +6,7 @@ use luya\web\Request; use luya\helpers\Url; use luya\helpers\StringHelper; +use yii\base\InvalidConfigException; use yii\web\NotFoundHttpException; /** @@ -168,11 +169,16 @@ protected function getInternalResolverArray() // the validation check for validates composition values is enabled if ($this->composition->expectedValues) { - foreach ($keys as $k => $v) { - $possibleValues = $this->composition->expectedValues[$k]; + foreach ($keys as $patternKey => $expectedPatternValue) { - if (!in_array($v, $possibleValues)) { - throw new NotFoundHttpException("The requested composition key \"{$k}\" with value \"{$v}\" is not in the possible values list."); + if (!array_key_exists($patternKey, $this->composition->expectedValues)) { + throw new InvalidConfigException("Invalid configuration, the expectedValues configuration key \"{$patternKey}\" does not exists in the resolved values list."); + } + + $possibleValues = $this->composition->expectedValues[$patternKey]; + + if (!in_array($expectedPatternValue, $possibleValues)) { + throw new NotFoundHttpException("The requested composition key \"{$patternKey}\" with value \"{$expectedPatternValue}\" is not in the possible values list."); } } } diff --git a/tests/core/web/CompositionResolverTest.php b/tests/core/web/CompositionResolverTest.php index b67c6dc31..17e822d9e 100644 --- a/tests/core/web/CompositionResolverTest.php +++ b/tests/core/web/CompositionResolverTest.php @@ -6,6 +6,7 @@ use luya\web\CompositionResolver; use luya\web\Request; use luya\web\Composition; +use yii\base\InvalidConfigException; use yii\web\NotFoundHttpException; class CompositionResolverTest extends LuyaWebTestCase @@ -85,4 +86,21 @@ public function testExpectedResolverWithEmptyValuesButDefaults() $this->expectException(NotFoundHttpException::class); $resolver->resolvedValues; } + + public function testInvalidConfigurationForExpectedValues() + { + $request = new Request(); + $request->pathInfo = ''; + + $composition = new Composition($request); + $composition->pattern = '/'; + $composition->default = ['lang' => 'en', 'xyz' => 'foo']; + $composition->expectedValues = [ + 'doesnotexists' => ['en'], + ]; + + $resolver = new CompositionResolver($request, $composition); + $this->expectException(InvalidConfigException::class); + $resolver->resolvedValues; + } }