Skip to content

Commit

Permalink
Correctly handle UTF-8 paths
Browse files Browse the repository at this point in the history
  • Loading branch information
mundschenk-at committed Jun 15, 2024
1 parent 24500ad commit a21fff3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/fixes/token-fixes/class-wrap-urls-fix.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,19 @@ private function split_domain( string $domain, Settings $settings ): string {
* Splits the given URL path.
*
* @since 6.7.0
* @since 7.0.0 Method is now protected to allow for unit testing.
*
* @param string $path A URL path.
* @param Settings $settings The settings to apply.
*
* @return string The hyphenated domain name.
*/
private function split_path( string $path, Settings $settings ): string {
protected function split_path( string $path, Settings $settings ): string {

$str_split = Strings::functions( $path )['str_split'];

// Break up the URL path to individual characters.
$path_parts = \str_split( $path, 1 ); // TODO: Does not work with non-ASCII paths.
$path_parts = $str_split( $path, 1 );
$path_count = \count( $path_parts );
$split_path = '';

Expand Down
41 changes: 38 additions & 3 deletions tests/fixes/token-fixes/class-wrap-urls-fix-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
use PHP_Typography\Fixes\Token_Fix;
use PHP_Typography\Fixes\Token_Fixes;

use Mockery as m;

/**
* Wrap_URLs_Fix unit test.
*
Expand Down Expand Up @@ -55,7 +57,7 @@ class Wrap_URLs_Fix_Test extends Token_Fix_Testcase {
protected function set_up() {
parent::set_up();

$this->fix = new Token_Fixes\Wrap_URLs_Fix();
$this->fix = m::mock( Token_Fixes\Wrap_URLs_Fix::class, [] )->shouldAllowMockingProtectedMethods()->makePartial();
}

/**
Expand All @@ -75,7 +77,7 @@ public function test_constructor() {
*
* @return array
*/
public function provide_wrap_urls_data() {
public function provide_wrap_urls_data(): array {
return [
[ 'https://example.org/', 'https://​example​.org/', 2 ],
[ 'http://example.org/', 'http://​example​.org/', 2 ],
Expand All @@ -90,8 +92,8 @@ public function provide_wrap_urls_data() {
*
* @covers ::apply
* @covers ::split_domain
* @covers ::split_path
*
* @uses ::split_path
* @uses PHP_Typography\Text_Parser
* @uses PHP_Typography\Text_Parser\Token
*
Expand Down Expand Up @@ -128,4 +130,37 @@ public function test_apply_off( $input, $result, $min_after ) {

$this->assertFixResultSame( $input, $input, false, $this->getTextnode( 'foo', $input ) );
}

/**
* Provide data for testing wrap_urls.
*
* @return array
*/
public function provide_split_path_data(): array {
return [
[ '', '', 2 ],
[ '/', '/', 2 ],
[ '/some/long/path/', '/​s​o​m​e​/​l​o​n​g​/​path/', 5 ],
[ '/Γεια/Καληνύχτα/', '/​Γ​ε​ι​α​/​Κ​α​λ​η​ν​ύχτα/', 5 ],
];
}

/**
* Test split_path.
*
* @covers ::split_path
*
* @uses PHP_Typography\Text_Parser
* @uses PHP_Typography\Text_Parser\Token
*
* @dataProvider provide_split_path_data
*
* @param string $input HTML input.
* @param string $result Expected result.
* @param int $min_after Minimum number of characters after URL wrapping.
*/
public function test_split_path( $input, $result, $min_after ) {
$this->s->set_min_after_url_wrap( $min_after );
$this->assertSame( $result, $this->clean_html( $this->fix->split_path( $input, $this->s ) ) );
}
}

0 comments on commit a21fff3

Please sign in to comment.