diff --git a/.gitignore b/.gitignore index 20999c1..9aa3167 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ coverage tests/runtime coverage.xml .vagrant +.idea/ diff --git a/src/LanguageEvents/GettingLanguageByUrl.php b/src/LanguageEvents/GettingLanguageByUrl.php index 1287ddc..bf0198d 100644 --- a/src/LanguageEvents/GettingLanguageByUrl.php +++ b/src/LanguageEvents/GettingLanguageByUrl.php @@ -1,11 +1,12 @@ currentLanguageId === false) { @@ -36,7 +37,6 @@ public static function gettingLanguage(languageEvent $event) public static function afterGettingLanguage(languageEvent $event) { - $languageMatched = $event->languages[$event->multilingual->language_id]; if ($event->needRedirect === true && $languageMatched->folder) { if ($languageMatched->folder === $event->request->pathInfo) { @@ -46,10 +46,11 @@ public static function afterGettingLanguage(languageEvent $event) // no matched language and not in excluded routes - should redirect to user's regional domain with 302 \Yii::$app->urlManager->forceHostInUrl = true; $event->redirectUrl = \Yii::$app->urlManager->createUrl( - [ - $event->request->pathInfo, - 'language_id' => $event->multilingual->language_id - ] + ArrayHelper::merge( + [$event->request->pathInfo], + \Yii::$app->request->get(), + ['language_id' => $event->multilingual->language_id] + ) ); \Yii::$app->urlManager->forceHostInUrl = false; $event->redirectCode = 302; @@ -60,16 +61,13 @@ public static function afterGettingLanguage(languageEvent $event) // no matched language and not in excluded routes - should redirect to user's regional domain with 302 \Yii::$app->urlManager->forceHostInUrl = true; $event->redirectUrl = $event->sender->createUrl( - [ - $event->request->pathInfo, - 'language_id' => $event->multilingual->language_id - ] + ArrayHelper::merge( + [$event->request->pathInfo], + \Yii::$app->request->get(), + ['language_id' => $event->multilingual->language_id] + ) ); $event->redirectCode = 302; } - - } - - -} \ No newline at end of file +} diff --git a/tests/DatabaseTest.php b/tests/DatabaseTest.php index 2d3f84f..b5326af 100644 --- a/tests/DatabaseTest.php +++ b/tests/DatabaseTest.php @@ -98,7 +98,7 @@ protected function setUp() 'class' => Connection::className(), 'dsn' => 'mysql:host=localhost;dbname=multilingual.dev', 'username' => 'root', - 'password' => '7896321', + 'password' => '', ]); Yii::$app->getDb()->open(); @@ -518,6 +518,24 @@ public function testPreferredCountryByGeo() $this->assertEquals(null, $multilingual->getPreferredCity()); } + public function testGetData() + { + /** @var \DevGroup\Multilingual\Multilingual $multilingual */ + Yii::$app->trigger(Application::EVENT_BEFORE_REQUEST); + $_SERVER['SERVER_NAME'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/site/about?post=1'; + $_GET = ['post' => 1]; + try { + $this->resolve(); + } catch (ExitException $e) { + $this->assertArraySubset( + ['location' => ['http://example.ru/site/about?post=1']], + Yii::$app->response->getHeaders()->toArray() + ); + $this->assertEquals(302, Yii::$app->response->statusCode); + } + + } /** * Resets Yii2 Request component so it can handle another fake request and resolves it