Skip to content

Commit 3a00556

Browse files
authored
Merge pull request #312 from lloc/refactoring-version-2-8
MslsOptionsTaxTerm fully tested
2 parents da357ed + 3b4f321 commit 3a00556

File tree

2 files changed

+66
-33
lines changed

2 files changed

+66
-33
lines changed

includes/MslsOptionsTaxTerm.php

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,9 @@
99
*/
1010
class MslsOptionsTaxTerm extends MslsOptionsTax {
1111

12-
/**
13-
* Base option
14-
*
15-
* @var string
16-
*/
17-
protected $base_option = 'tag_base';
12+
const BASE_OPTION = 'tag_base';
1813

19-
/**
20-
* Base definition
21-
*
22-
* @var string
23-
*/
24-
protected $base_defined = 'tag';
14+
const BASE_DEFINED = 'tag';
2515

2616
/**
2717
* Rewrite with front
@@ -33,8 +23,8 @@ class MslsOptionsTaxTerm extends MslsOptionsTax {
3323
/**
3424
* Check and correct URL
3525
*
36-
* @param string $url
37-
* @param MslsOptions $options
26+
* @param string $url
27+
* @param MslsOptionsTaxTerm $options
3828
*
3929
* @return string
4030
*/
@@ -43,32 +33,38 @@ public function check_base( $url, $options ) {
4333
return $url;
4434
}
4535

46-
global $wp_rewrite;
36+
$tax_query = $options->get_tax_query();
37+
$base_defined = self::get_base_defined( $tax_query );
38+
$base_option = self::get_base_option();
4739

48-
$base_defined = $options->base_defined;
40+
if ( $base_defined != $base_option ) {
41+
$search = '/' . $base_defined . '/';
42+
$replace = '/' . $base_option . '/';
43+
$url = str_replace( $search, $replace, $url );
44+
}
4945

50-
$permastruct = $wp_rewrite->get_extra_permastruct( $options->get_tax_query() );
46+
return $url;
47+
}
48+
49+
protected static function get_base_defined( string $tax_query ): string {
50+
global $wp_rewrite;
51+
52+
$permastruct = $wp_rewrite->get_extra_permastruct( $tax_query );
5153
if ( $permastruct ) {
5254
$permastruct = explode( '/', $permastruct );
5355
end( $permastruct );
5456
$permastruct = prev( $permastruct );
5557
if ( false !== $permastruct ) {
56-
$base_defined = $permastruct;
58+
return $permastruct;
5759
}
5860
}
5961

60-
$base_option = get_option( $options->base_option );
61-
if ( empty( $base_option ) ) {
62-
$base_option = $options->base_defined;
63-
}
62+
return self::BASE_DEFINED;
63+
}
6464

65-
if ( $base_defined != $base_option ) {
66-
$search = '/' . $base_defined . '/';
67-
$replace = '/' . $base_option . '/';
68-
$count = 1;
69-
$url = str_replace( $search, $replace, $url, $count );
70-
}
65+
protected static function get_base_option(): string {
66+
$base_option = get_option( self::BASE_OPTION, '' );
7167

72-
return $url;
68+
return $base_option ?: self::BASE_DEFINED;
7369
}
7470
}

tests/TestMslsOptionsTaxTerm.php

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,53 @@
44

55
use Brain\Monkey\Functions;
66

7+
use lloc\Msls\MslsOptions;
78
use lloc\Msls\MslsOptionsTaxTerm;
89

910
class TestMslsOptionsTaxTerm extends MslsUnitTestCase {
1011

11-
public function test_object(): void {
12-
Functions\expect( 'get_option' )->once()->andReturn( [] );
12+
protected function setUp(): void {
13+
parent::setUp();
1314

14-
$obj = new MslsOptionsTaxTerm( 0 );
15+
Functions\expect( 'get_option' )->once()->with( 'msls_term_42' )->andReturn( array( 'de_DE' => 42 ) );
1516

16-
$this->assertIsSTring( $obj->get_postlink( '' ) );
17+
$this->test = new MslsOptionsTaxTerm( 42 );
1718
}
19+
public function test_get_postlink_empty(): void {
20+
$this->assertEquals( '', $this->test->get_postlink( '' ) );
21+
}
22+
23+
public function test_check_url_empty(): void {
24+
$options = \Mockery::mock( MslsOptionsTaxTerm::class );
25+
26+
$this->assertEquals( '', $this->test->check_base( null, $options ) );
27+
$this->assertEquals( '', $this->test->check_base( '', $options ) );
28+
$this->assertEquals( '', $this->test->check_base( false, $options ) );
29+
}
30+
31+
public function test_check_url(): void {
32+
global $wp_rewrite;
33+
34+
$wp_rewrite = \Mockery::mock( 'WP_Rewrite' );
35+
$wp_rewrite->shouldReceive( 'get_extra_permastruct' )->andReturn( '/schlagwort/' );
36+
37+
$options = \Mockery::mock( MslsOptionsTaxTerm::class );
38+
$options->shouldReceive( 'get_tax_query' )->andReturn( '' );
1839

40+
$expected = 'https://example.de/tag/keyword';
41+
$this->assertEquals( $expected, $this->test->check_base( 'https://example.de/schlagwort/keyword', $options ) );
42+
}
43+
44+
public function test_check_url_permastruct_false(): void {
45+
global $wp_rewrite;
46+
47+
$wp_rewrite = \Mockery::mock( 'WP_Rewrite' );
48+
$wp_rewrite->shouldReceive( 'get_extra_permastruct' )->andReturn( false );
49+
50+
$options = \Mockery::mock( MslsOptionsTaxTerm::class );
51+
$options->shouldReceive( 'get_tax_query' )->andReturn( '' );
52+
53+
$expected = 'https://example.de/schlagwort/keyword';
54+
$this->assertEquals( $expected, $this->test->check_base( $expected, $options ) );
55+
}
1956
}

0 commit comments

Comments
 (0)