Skip to content

Commit 36ac8b6

Browse files
authored
Merge pull request #7 from LibreSign/chore/cover-with-more-scenarios
chore: cover with more scenarios
2 parents 61d667d + 980bbb8 commit 36ac8b6

File tree

3 files changed

+109
-8
lines changed

3 files changed

+109
-8
lines changed

src/helpers.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ function translate_path($page, ?string $target_locale = null): string
5757
};
5858

5959
return match (true) {
60-
$target_locale === packageDefaultLocale($page) => "{$partial_path}",
61-
default => "/{$target_locale}{$partial_path}",
60+
$target_locale === packageDefaultLocale($page) => $partial_path ?: '/',
61+
default => "/{$target_locale}".($partial_path === '/' ? '' : $partial_path),
6262
};
6363
}
6464

@@ -83,13 +83,14 @@ function locale_path($page, string $partial_path, ?string $target_locale = null)
8383
$target_locale ??= current_path_locale($page);
8484

8585
$partial_path = '/'.ltrim($partial_path, '/');
86+
$partial_path = preg_replace("/^\/$target_locale\//", '/', $partial_path);
8687
if ($partial_path === '/') {
8788
$partial_path = '';
8889
}
8990

9091
return match (true) {
91-
$target_locale === packageDefaultLocale($page) => $partial_path,
92-
default => "/{$target_locale}{$partial_path}"
92+
$target_locale === packageDefaultLocale($page) => $partial_path ?: '/',
93+
default => "/{$target_locale}".($partial_path === '/' ? '' : $partial_path),
9394
};
9495
}
9596

tests/unit/LocalePathTest.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,46 @@ public function test_locale_path(string $path, ?string $locale, string $expected
1515
public static function providerLocalePath(): array
1616
{
1717
return [
18-
['', null, ''],
18+
// Handling empty paths
19+
['', null, '/'],
1920
['', 'ar', '/ar'],
21+
22+
// Root path handling
23+
['/', null, '/'],
24+
['/', 'ar', '/ar'],
25+
['/', packageDefaultLocale(), '/'],
26+
27+
// Basic path translation
2028
['blog', null, '/blog'],
2129
['blog', 'ar', '/ar/blog'],
2230
['blog/', 'ar', '/ar/blog/'],
2331
['/blog/', 'ar', '/ar/blog/'],
32+
33+
// Ensuring paths already prefixed with locale remain unchanged
34+
['/ar/blog', 'ar', '/ar/blog'],
35+
['/fr-CA/blog', 'fr-CA', '/fr-CA/blog'],
36+
37+
// Paths when `packageDefaultLocale()` should not add a locale prefix
38+
['blog', packageDefaultLocale(), '/blog'],
39+
['/', packageDefaultLocale(), '/'],
40+
41+
// Special characters handling
42+
['café', 'fr', '/fr/café'],
43+
['ação', 'pt-BR', '/pt-BR/ação'],
44+
['ümlaut', 'de', '/de/ümlaut'],
45+
46+
// Case sensitivity (if applicable)
47+
['Blog', 'es', '/es/Blog'],
48+
['NEWS', 'es', '/es/NEWS'],
49+
50+
// Multi-segment paths
51+
['blog/posts/123', 'ar', '/ar/blog/posts/123'],
52+
['es/news/article/456', 'fr-CA', '/fr-CA/es/news/article/456'],
53+
54+
// Ensuring leading and trailing slashes are handled correctly
55+
['/news', 'es', '/es/news'],
56+
['news/', 'es', '/es/news/'],
57+
['/news/', 'es', '/es/news/'],
2458
];
2559
}
2660
}

tests/unit/TranslatePathTest.php

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,87 @@ public function test_translate_path(string $path, ?string $locale, string $expec
1616
public static function providerTranslatePath(): array
1717
{
1818
return [
19+
// Translating from one locale to another
1920
['/es/blog', 'ar', '/ar/blog'],
2021
['/ar/blog', 'fr-CA', '/fr-CA/blog'],
21-
['/fr-CA/blog', 'fr-CA', '/fr-CA/blog'],
2222
['/ar/blog', 'haw-US', '/haw-US/blog'],
23-
['/haw-US/blog', 'haw-US', '/haw-US/blog'],
2423
['/fr-CA/blog', 'haw-US', '/haw-US/blog'],
2524
['/haw-US/blog', 'fr-CA', '/fr-CA/blog'],
25+
26+
// Keeping the same locale (should remain unchanged)
27+
['/fr-CA/blog', 'fr-CA', '/fr-CA/blog'],
28+
['/haw-US/blog', 'haw-US', '/haw-US/blog'],
29+
['/ar/blog', 'ar', '/ar/blog'],
30+
['/en-UK/blog', 'en-UK', '/en-UK/blog'],
31+
32+
// Translating a path without a locale to a new locale
2633
['/blog', 'ar', '/ar/blog'],
2734
['/blog', 'en-UK', '/en-UK/blog'],
2835
['/blog', 'haw-US', '/haw-US/blog'],
29-
['/blog', packageDefaultLocale(), '/blog'],
36+
37+
// Converting to the default locale (removing locale prefix)
38+
['/ar/blog', packageDefaultLocale(), '/blog'],
39+
['/fr-CA/blog', packageDefaultLocale(), '/blog'],
40+
['/haw-US/blog', packageDefaultLocale(), '/blog'],
41+
42+
// If `target_locale` is `null`, it should return to the default locale
3043
['/ar/blog', null, '/blog'],
3144
['/en-UK/blog', null, '/blog'],
3245
['/haw-US/blog', null, '/blog'],
3346
['/blog', null, '/blog'],
47+
48+
// If already in the default locale and `target_locale` is `null`
49+
['/about', null, '/about'],
50+
['/contact', null, '/contact'],
51+
52+
// If `target_locale` is the same as `current_locale`
53+
['/es', 'es', '/es'],
54+
['/es/contact', 'es', '/es/contact'],
55+
['/fr-CA/page', 'fr-CA', '/fr-CA/page'],
56+
57+
// Testing multi-segment paths
58+
['/es/blog/posts/123', 'ar', '/ar/blog/posts/123'],
59+
['/ar/news/article/456', 'fr-CA', '/fr-CA/news/article/456'],
60+
['/haw-US/shop/products/789', 'en-UK', '/en-UK/shop/products/789'],
61+
62+
// Testing `/` as a root path (edge case)
63+
['/', 'ar', '/ar'],
64+
['/', null, '/'],
65+
['/', packageDefaultLocale(), '/'],
66+
67+
// Testing paths with unrecognized prefixes (should keep them)
68+
['/custom-path/blog', 'fr-CA', '/fr-CA/custom-path/blog'],
69+
['/random/path', 'es', '/es/random/path'],
70+
71+
// Short prefixes that could cause substring errors
72+
['/a/blog', 'ar', '/ar/a/blog'],
73+
['/fr/page', 'haw-US', '/haw-US/page'],
74+
75+
// Handling an empty path (should default to `/`)
76+
['', 'ar', '/ar'],
77+
['', null, '/'],
78+
79+
// Deeply nested paths
80+
['/es/a/b/c/d/e', 'ar', '/ar/a/b/c/d/e'],
81+
['/fr-CA/deeply/nested/path/example', 'en-UK', '/en-UK/deeply/nested/path/example'],
82+
83+
// Paths with special characters
84+
['/fr-CA/à-propos', 'es', '/es/à-propos'],
85+
['/es/blog/café', 'ar', '/ar/blog/café'],
86+
['/haw-US/ümlaut', 'en-UK', '/en-UK/ümlaut'],
87+
88+
// Case sensitivity (should not modify case)
89+
['/FR/blog', 'es', '/es/FR/blog'],
90+
['/Es/blog', 'fr-CA', '/fr-CA/Es/blog'],
91+
92+
// Handling trailing slashes
93+
['/es/', 'ar', '/ar'],
94+
['/fr-CA/', null, '/'],
95+
['/blog/', 'ar', '/ar/blog'],
96+
97+
/**
98+
* @todo Unexpected locale prefixes (should behave as a normal path)
99+
*/
34100
];
35101
}
36102
}

0 commit comments

Comments
 (0)