-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2070 from hydephp/internationalization
[1.x] Automatically transliterate logographic inputs for slug generation hydephp/develop@464bb8b
- Loading branch information
1 parent
6d3207b
commit a9c7e9f
Showing
8 changed files
with
275 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hyde\Framework\Testing\Feature; | ||
|
||
use Hyde\Facades\Filesystem; | ||
use Hyde\Framework\Actions\CreatesNewMarkdownPostFile; | ||
use Hyde\Framework\Actions\StaticPageBuilder; | ||
use Hyde\Hyde; | ||
use Hyde\Pages\MarkdownPost; | ||
use Hyde\Testing\TestCase; | ||
|
||
/** | ||
* @coversNothing High level test to ensure the internationalization features are working. | ||
*/ | ||
class InternationalizationTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider internationalCharacterSetsProvider | ||
*/ | ||
public function testCanCreateBlogPostFilesWithInternationalCharacterSets( | ||
string $title, | ||
string $description, | ||
string $expectedSlug, | ||
string $expectedTitle | ||
) { | ||
$creator = new CreatesNewMarkdownPostFile($title, $description, 'blog', 'default', '2024-12-22 10:45'); | ||
$path = $creator->save(); | ||
|
||
$this->assertSame("_posts/$expectedSlug.md", $path); | ||
$this->assertSame($expectedSlug, $creator->getIdentifier()); | ||
$this->assertSame($creator->getIdentifier(), Hyde::makeSlug($title)); | ||
$this->assertFileExists($path); | ||
|
||
$contents = file_get_contents($path); | ||
|
||
if (str_contains($title, ' ')) { | ||
$expectedTitle = "'$expectedTitle'"; | ||
} | ||
|
||
if (str_contains($description, ' ')) { | ||
$description = "'$description'"; | ||
} | ||
|
||
$this->assertStringContainsString("title: $expectedTitle", $contents); | ||
$this->assertSame(<<<EOF | ||
--- | ||
title: {$expectedTitle} | ||
description: {$description} | ||
category: blog | ||
author: default | ||
date: '2024-12-22 10:45' | ||
--- | ||
## Write something awesome. | ||
EOF, $contents); | ||
|
||
Filesystem::unlink($path); | ||
} | ||
|
||
/** | ||
* @dataProvider internationalCharacterSetsProvider | ||
*/ | ||
public function testCanCompileBlogPostFilesWithInternationalCharacterSets( | ||
string $title, | ||
string $description, | ||
string $expectedSlug, | ||
string $expectedTitle | ||
) { | ||
$page = new MarkdownPost($expectedSlug, [ | ||
'title' => $title, | ||
'description' => $description, | ||
'category' => 'blog', | ||
'author' => 'default', | ||
'date' => '2024-12-22 10:45', | ||
]); | ||
|
||
$path = StaticPageBuilder::handle($page); | ||
|
||
$this->assertSame(Hyde::path("_site/posts/$expectedSlug.html"), $path); | ||
$this->assertFileExists($path); | ||
|
||
$contents = file_get_contents($path); | ||
|
||
$this->assertStringContainsString("<title>HydePHP - $expectedTitle</title>", $contents); | ||
$this->assertStringContainsString("<h1 itemprop=\"headline\" class=\"mb-4\">$expectedTitle</h1>", $contents); | ||
$this->assertStringContainsString("<meta name=\"description\" content=\"$description\">", $contents); | ||
|
||
Filesystem::unlink($path); | ||
} | ||
|
||
public static function internationalCharacterSetsProvider(): array | ||
{ | ||
return [ | ||
'Chinese (Simplified)' => [ | ||
'你好世界', | ||
'简短描述', | ||
'ni-hao-shi-jie', | ||
'你好世界', | ||
], | ||
'Japanese' => [ | ||
'こんにちは世界', | ||
'短い説明', | ||
'konnichihashi-jie', | ||
'こんにちは世界', | ||
], | ||
'Korean' => [ | ||
'안녕하세요 세계', | ||
'짧은 설명', | ||
'annyeonghaseyo-segye', | ||
'안녕하세요 세계', | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hyde\Framework\Testing\Unit; | ||
|
||
use Hyde\Hyde; | ||
use Hyde\Testing\UnitTestCase; | ||
|
||
class HydeHelperFacadeMakeSlugTest extends UnitTestCase | ||
{ | ||
protected static bool $needsKernel = true; | ||
|
||
public function testMakeSlugHelperConvertsTitleCaseToSlug() | ||
{ | ||
$this->assertSame('hello-world', Hyde::makeSlug('Hello World')); | ||
} | ||
|
||
public function testMakeSlugHelperConvertsKebabCaseToSlug() | ||
{ | ||
$this->assertSame('hello-world', Hyde::makeSlug('hello-world')); | ||
} | ||
|
||
public function testMakeSlugHelperConvertsSnakeCaseToSlug() | ||
{ | ||
$this->assertSame('hello-world', Hyde::makeSlug('hello_world')); | ||
} | ||
|
||
public function testMakeSlugHelperConvertsCamelCaseToSlug() | ||
{ | ||
$this->assertSame('hello-world', Hyde::makeSlug('helloWorld')); | ||
} | ||
|
||
public function testMakeSlugHelperConvertsPascalCaseToSlug() | ||
{ | ||
$this->assertSame('hello-world', Hyde::makeSlug('HelloWorld')); | ||
} | ||
|
||
public function testMakeSlugHelperHandlesMultipleSpaces() | ||
{ | ||
$this->assertSame('hello-world', Hyde::makeSlug('Hello World')); | ||
} | ||
|
||
public function testMakeSlugHelperHandlesSpecialCharacters() | ||
{ | ||
$this->assertSame('hello-world', Hyde::makeSlug('Hello & World!')); | ||
} | ||
|
||
public function testMakeSlugHelperConvertsUppercaseToLowercase() | ||
{ | ||
$this->assertSame('hello-world', Hyde::makeSlug('HELLO WORLD')); | ||
$this->assertSame('hello-world', Hyde::makeSlug('HELLO_WORLD')); | ||
} | ||
|
||
public function testMakeSlugHelperHandlesNumbers() | ||
{ | ||
$this->assertSame('hello-world-123', Hyde::makeSlug('Hello World 123')); | ||
} | ||
|
||
public function testMakeSlugHelperTransliteratesChineseCharacters() | ||
{ | ||
$this->assertSame('ni-hao-shi-jie', Hyde::makeSlug('你好世界')); | ||
} | ||
|
||
public function testMakeSlugHelperTransliteratesJapaneseCharacters() | ||
{ | ||
$this->assertSame('konnichihashi-jie', Hyde::makeSlug('こんにちは世界')); | ||
} | ||
|
||
public function testMakeSlugHelperTransliteratesKoreanCharacters() | ||
{ | ||
$this->assertSame('annyeongsegye', Hyde::makeSlug('안녕세계')); | ||
} | ||
|
||
public function testMakeSlugHelperTransliteratesArabicCharacters() | ||
{ | ||
$this->assertSame('mrhb-bllm', Hyde::makeSlug('مرحبا بالعالم')); | ||
} | ||
|
||
public function testMakeSlugHelperTransliteratesRussianCharacters() | ||
{ | ||
$this->assertSame('privet-mir', Hyde::makeSlug('Привет мир')); | ||
} | ||
|
||
public function testMakeSlugHelperTransliteratesAccentedLatinCharacters() | ||
{ | ||
$this->assertSame('hello-world', Hyde::makeSlug('hèllô wórld')); | ||
$this->assertSame('uber-strasse', Hyde::makeSlug('über straße')); | ||
} | ||
|
||
public function testMakeSlugHelperHandlesMixedScripts() | ||
{ | ||
$this->assertSame('hello-ni-hao-world', Hyde::makeSlug('Hello 你好 World')); | ||
$this->assertSame('privet-world', Hyde::makeSlug('Привет World')); | ||
} | ||
|
||
public function testMakeSlugHelperHandlesEmojis() | ||
{ | ||
$this->assertSame('hello-world', Hyde::makeSlug('Hello 👋 World')); | ||
$this->assertSame('world', Hyde::makeSlug('😊 World')); | ||
} | ||
|
||
public function testMakeSlugHelperHandlesComplexMixedInput() | ||
{ | ||
$this->assertSame( | ||
'hello-ni-hao-privet-bonjour-world-123', | ||
Hyde::makeSlug('Hello 你好 Привет Bonjóur World 123!') | ||
); | ||
} | ||
|
||
public function testMakeSlugHelperHandlesEdgeCases() | ||
{ | ||
$this->assertSame('', Hyde::makeSlug('')); | ||
$this->assertSame('at', Hyde::makeSlug('!@#$%^&*()')); | ||
$this->assertSame('', Hyde::makeSlug('... ...')); | ||
$this->assertSame('multiple-dashes', Hyde::makeSlug('multiple---dashes')); | ||
} | ||
|
||
public function testMakeSlugHelperPreservesValidCharacters() | ||
{ | ||
$this->assertSame('abc-123', Hyde::makeSlug('abc-123')); | ||
$this->assertSame('test-slug', Hyde::makeSlug('test-slug')); | ||
} | ||
|
||
public function testMakeSlugHelperHandlesWhitespace() | ||
{ | ||
$this->assertSame('trim-spaces', Hyde::makeSlug(' trim spaces ')); | ||
$this->assertSame('newline-test', Hyde::makeSlug("newline\ntest")); | ||
$this->assertSame('tab-test', Hyde::makeSlug("tab\ttest")); | ||
} | ||
} |