From cb2c9616d7d19aecdad9a64c581068bdc0ac8e2a Mon Sep 17 00:00:00 2001 From: Pierre Joye Date: Fri, 29 Jan 2021 14:48:20 +0700 Subject: [PATCH 1/8] Initialize route and raw_route, fix empty route as values --- system/src/Grav/Common/Page/Page.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index 344a7ba60c..30b6565624 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -271,7 +271,8 @@ public function translatedLanguages($onlyPublished = false) if ($exists) { $aPage = new Page(); $aPage->init(new SplFileInfo($path), $languageExtension); - + $aPage->route($this->route()); + $aPage->rawRoute($this->rawRoute()); $route = $aPage->header()->routes['default'] ?? $aPage->rawRoute(); if (!$route) { $route = $aPage->route(); From ea758e9dcafb2c75f78238f3494c81a5451c1bbf Mon Sep 17 00:00:00 2001 From: Pierre Joye Date: Sat, 30 Jan 2021 20:50:20 +0700 Subject: [PATCH 2/8] Add test for this translatedLanguages --- tests/_bootstrap.php | 4 ++++ tests/unit/Grav/Common/Page/PagesTest.php | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/tests/_bootstrap.php b/tests/_bootstrap.php index 03677dd845..c83c3d8bf9 100644 --- a/tests/_bootstrap.php +++ b/tests/_bootstrap.php @@ -12,6 +12,10 @@ $grav = Grav::instance(); $grav['config']->init(); + // This must be set first before the other init + $grav['config']->set('system.languages.supported', ['en', 'fr', 'vi']); + $grav['config']->set('system.languages.default_lang', 'en'); + foreach (array_keys($grav['setup']->getStreams()) as $stream) { @stream_wrapper_unregister($stream); } diff --git a/tests/unit/Grav/Common/Page/PagesTest.php b/tests/unit/Grav/Common/Page/PagesTest.php index b53ca8414e..2aa385cc9a 100644 --- a/tests/unit/Grav/Common/Page/PagesTest.php +++ b/tests/unit/Grav/Common/Page/PagesTest.php @@ -236,6 +236,19 @@ public function testGetList() $this->assertSame('—-▸ Blog', $list['/blog']); } + public function testTranslatedLanguages() + { + /** @var UniformResourceLocator $locator */ + $locator = $this->grav['locator']; + $folder = $locator->findResource('tests://'); + + $page = $this->pages->get($folder . '/fake/simple-site/user/pages/04.page-translated'); + $this->assertInstanceOf(PageInterface::class, $page); + $translatedLanguages = $page->translatedLanguages(); + $this->assertIsArray($translatedLanguages); + $this->assertSame(["en" => "/page-translated", "fr" => "/page-translated"], $translatedLanguages); + } + public function testGetTypes() { } From 711d7b122df9f7718216d423342f12090a5b91d3 Mon Sep 17 00:00:00 2001 From: Pierre Joye Date: Sat, 30 Jan 2021 21:22:39 +0700 Subject: [PATCH 3/8] Add fixture for translatedLanguages --- system/src/Grav/Common/Utils.php | 1 - .../user/pages/01.simple-page/default.en.md | 0 .../user/pages/01.simple-page/default.fr.md | 5 +++++ tests/unit/Grav/Common/UtilsTest.php | 4 +++- 4 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 tests/fake/single-page-translated/user/pages/01.simple-page/default.en.md create mode 100644 tests/fake/single-page-translated/user/pages/01.simple-page/default.fr.md diff --git a/system/src/Grav/Common/Utils.php b/system/src/Grav/Common/Utils.php index 4e02d5712f..013d814fe8 100644 --- a/system/src/Grav/Common/Utils.php +++ b/system/src/Grav/Common/Utils.php @@ -1196,7 +1196,6 @@ public static function pathPrefixedByLangCode($string) if (count($parts) > 0 && in_array($parts[0], $languages_enabled)) { return $parts[0]; } - return false; } diff --git a/tests/fake/single-page-translated/user/pages/01.simple-page/default.en.md b/tests/fake/single-page-translated/user/pages/01.simple-page/default.en.md new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/fake/single-page-translated/user/pages/01.simple-page/default.fr.md b/tests/fake/single-page-translated/user/pages/01.simple-page/default.fr.md new file mode 100644 index 0000000000..97156cc5e8 --- /dev/null +++ b/tests/fake/single-page-translated/user/pages/01.simple-page/default.fr.md @@ -0,0 +1,5 @@ +--- +title: Simple Page avec traduction +--- + +Simple Page Content in English \ No newline at end of file diff --git a/tests/unit/Grav/Common/UtilsTest.php b/tests/unit/Grav/Common/UtilsTest.php index adce03eddb..98c7d65a63 100644 --- a/tests/unit/Grav/Common/UtilsTest.php +++ b/tests/unit/Grav/Common/UtilsTest.php @@ -297,7 +297,9 @@ public function testPathPrefixedByLangCode() $arrayOfLanguages = ['en', 'de', 'it', 'es', 'dk', 'el']; $languagesNotEnabled = array_diff($arrayOfLanguages, $languagesEnabled); $oneLanguageNotEnabled = reset($languagesNotEnabled); - +dump($oneLanguageNotEnabled); +dump($languagesNotEnabled); +dump($languagesEnabled); if (count($languagesEnabled)) { $this->assertTrue(Utils::pathPrefixedByLangCode('/' . $languagesEnabled[0] . '/test')); } From 10be5d5a7cf4fb1f7e01c2e683c118857d1f00d3 Mon Sep 17 00:00:00 2001 From: Pierre Joye Date: Sun, 31 Jan 2021 07:14:33 +0700 Subject: [PATCH 4/8] fix text if language is found and is part as enabled languages --- tests/unit/Grav/Common/UtilsTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/unit/Grav/Common/UtilsTest.php b/tests/unit/Grav/Common/UtilsTest.php index 98c7d65a63..d27cdf64bd 100644 --- a/tests/unit/Grav/Common/UtilsTest.php +++ b/tests/unit/Grav/Common/UtilsTest.php @@ -297,11 +297,11 @@ public function testPathPrefixedByLangCode() $arrayOfLanguages = ['en', 'de', 'it', 'es', 'dk', 'el']; $languagesNotEnabled = array_diff($arrayOfLanguages, $languagesEnabled); $oneLanguageNotEnabled = reset($languagesNotEnabled); -dump($oneLanguageNotEnabled); -dump($languagesNotEnabled); -dump($languagesEnabled); + if (count($languagesEnabled)) { - $this->assertTrue(Utils::pathPrefixedByLangCode('/' . $languagesEnabled[0] . '/test')); + $languageCodePathPrefix = Utils::pathPrefixedByLangCode('/' . $languagesEnabled[0] . '/test'); + $this->assertIsString($languageCodePathPrefix); + $this->assertTrue(in_array($languageCodePathPrefix, $languagesEnabled)); } $this->assertFalse(Utils::pathPrefixedByLangCode('/' . $oneLanguageNotEnabled . '/test')); From 4ce3ff52bf6b95c98cd74e56d9a0bf230a5775d2 Mon Sep 17 00:00:00 2001 From: Pierre Joye Date: Sun, 31 Jan 2021 07:21:44 +0700 Subject: [PATCH 5/8] Add the test translated pages --- .../simple-site/user/pages/04.page-translated/default.en.md | 0 .../simple-site/user/pages/04.page-translated/default.fr.md | 5 +++++ 2 files changed, 5 insertions(+) create mode 100644 tests/fake/simple-site/user/pages/04.page-translated/default.en.md create mode 100644 tests/fake/simple-site/user/pages/04.page-translated/default.fr.md diff --git a/tests/fake/simple-site/user/pages/04.page-translated/default.en.md b/tests/fake/simple-site/user/pages/04.page-translated/default.en.md new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/fake/simple-site/user/pages/04.page-translated/default.fr.md b/tests/fake/simple-site/user/pages/04.page-translated/default.fr.md new file mode 100644 index 0000000000..97156cc5e8 --- /dev/null +++ b/tests/fake/simple-site/user/pages/04.page-translated/default.fr.md @@ -0,0 +1,5 @@ +--- +title: Simple Page avec traduction +--- + +Simple Page Content in English \ No newline at end of file From 2044a649ba68a6423b89421aa41a6c6756268289 Mon Sep 17 00:00:00 2001 From: Pierre Joye Date: Tue, 2 Feb 2021 07:25:58 +0700 Subject: [PATCH 6/8] add another test with subfolder --- .../user/pages/05.translatedlong/part2/default.en.md | 0 .../user/pages/05.translatedlong/part2/default.fr.md | 5 +++++ 2 files changed, 5 insertions(+) create mode 100644 tests/fake/simple-site/user/pages/05.translatedlong/part2/default.en.md create mode 100644 tests/fake/simple-site/user/pages/05.translatedlong/part2/default.fr.md diff --git a/tests/fake/simple-site/user/pages/05.translatedlong/part2/default.en.md b/tests/fake/simple-site/user/pages/05.translatedlong/part2/default.en.md new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/fake/simple-site/user/pages/05.translatedlong/part2/default.fr.md b/tests/fake/simple-site/user/pages/05.translatedlong/part2/default.fr.md new file mode 100644 index 0000000000..7157062348 --- /dev/null +++ b/tests/fake/simple-site/user/pages/05.translatedlong/part2/default.fr.md @@ -0,0 +1,5 @@ +--- +title: Simple Page avec traduction +--- + +Page Simple FR \ No newline at end of file From caf431d22e46919bd9eb21b336d8507a7b98b826 Mon Sep 17 00:00:00 2001 From: Pierre Joye Date: Tue, 2 Feb 2021 07:27:21 +0700 Subject: [PATCH 7/8] add faker for multiple parts with folder --- .../user/pages/01.simple-page/default.fr.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fake/single-page-translated/user/pages/01.simple-page/default.fr.md b/tests/fake/single-page-translated/user/pages/01.simple-page/default.fr.md index 97156cc5e8..7157062348 100644 --- a/tests/fake/single-page-translated/user/pages/01.simple-page/default.fr.md +++ b/tests/fake/single-page-translated/user/pages/01.simple-page/default.fr.md @@ -2,4 +2,4 @@ title: Simple Page avec traduction --- -Simple Page Content in English \ No newline at end of file +Page Simple FR \ No newline at end of file From 6c3c068485cda5e85cd34e9346091affb084048e Mon Sep 17 00:00:00 2001 From: Pierre Joye Date: Tue, 2 Feb 2021 07:27:54 +0700 Subject: [PATCH 8/8] add test for multiple parts using folder --- tests/unit/Grav/Common/Page/PagesTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/unit/Grav/Common/Page/PagesTest.php b/tests/unit/Grav/Common/Page/PagesTest.php index 887bf4af2b..edff75bc83 100644 --- a/tests/unit/Grav/Common/Page/PagesTest.php +++ b/tests/unit/Grav/Common/Page/PagesTest.php @@ -249,6 +249,18 @@ public function testTranslatedLanguages(): void $this->assertSame(["en" => "/page-translated", "fr" => "/page-translated"], $translatedLanguages); } + public function testLongPathTranslatedLanguages(): void + { + /** @var UniformResourceLocator $locator */ + $locator = $this->grav['locator']; + $folder = $locator->findResource('tests://'); + $page = $this->pages->get($folder . '/fake/simple-site/user/pages/05.translatedlong/part2'); + $this->assertInstanceOf(PageInterface::class, $page); + $translatedLanguages = $page->translatedLanguages(); + $this->assertIsArray($translatedLanguages); + $this->assertSame(["en" => "/translatedlong/part2", "fr" => "/translatedlong/part2"], $translatedLanguages); + } + public function testGetTypes(): void { }