Skip to content

Commit ae9c104

Browse files
authored
Merge pull request #310 from lloc/refactoring-version-2-8
Refactoring version 2 8
2 parents ca53a1b + 23e5a1a commit ae9c104

7 files changed

+131
-48
lines changed

includes/MslsCustomFilter.php

+13-13
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,6 @@ function () {
4949
* @uses selected
5050
*/
5151
public function add_filter(): void {
52-
$id = (
53-
filter_has_var( INPUT_GET, self::FILTER_NAME ) ?
54-
filter_input( INPUT_GET, self::FILTER_NAME, FILTER_SANITIZE_NUMBER_INT ) :
55-
'0'
56-
);
57-
5852
$blogs = $this->collection->get();
5953
if ( $blogs ) {
6054
$options = array( '' => esc_html( __( 'Show all posts', 'multisite-language-switcher' ) ) );
@@ -65,6 +59,12 @@ public function add_filter(): void {
6559
);
6660
}
6761

62+
$id = (
63+
filter_has_var( INPUT_GET, self::FILTER_NAME ) ?
64+
filter_input( INPUT_GET, self::FILTER_NAME, FILTER_SANITIZE_NUMBER_INT ) :
65+
'0'
66+
);
67+
6868
echo ( new Select( self::FILTER_NAME, $options, $id ) )->render();
6969
}
7070
}
@@ -84,15 +84,15 @@ public function execute_filter( \WP_Query $query ) {
8484
$id = filter_input( INPUT_GET, self::FILTER_NAME, FILTER_SANITIZE_NUMBER_INT );
8585
$blog = $this->collection->get_object( intval( $id ) );
8686

87-
if ( $blog ) {
88-
$sql_cache = MslsSqlCacher::create( __CLASS__, __METHOD__ );
87+
if ( ! $blog ) {
88+
return false;
89+
}
8990

90-
// load post we need to exclude (they already have a translation) from search query
91-
$query->query_vars['post__not_in'] = ( new TranslatedPostIdQuery( $sql_cache ) )( $blog->get_language() );
91+
$sql_cache = MslsSqlCacher::create( __CLASS__, __METHOD__ );
9292

93-
return $query;
94-
}
93+
// load post we need to exclude (they already have a translation) from search query
94+
$query->query_vars['post__not_in'] = ( new TranslatedPostIdQuery( $sql_cache ) )( $blog->get_language() );
9595

96-
return false;
96+
return $query;
9797
}
9898
}

phpunit.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="true" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
33
<coverage>
44
<include>
55
<directory suffix=".php">./includes/</directory>

tests/MslsUnitTestCase.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ protected function setUp(): void {
1919
parent::setUp();
2020
Monkey\setUp();
2121

22+
\Mockery::namedMock( 'WooCommerce', \stdClass::class );
23+
2224
Functions\when( 'esc_html' )->returnArg();
2325
Functions\when( 'esc_attr' )->returnArg();
2426
Functions\when( 'esc_url' )->returnArg();
@@ -30,7 +32,8 @@ protected function tearDown(): void {
3032
restore_error_handler();
3133

3234
Monkey\tearDown();
35+
\Mockery::close();
36+
3337
parent::tearDown();
3438
}
35-
3639
}

tests/TestMslsOptionsTax.php

+90-4
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,113 @@
1111
*/
1212
class TestMslsOptionsTax extends MslsUnitTestCase {
1313

14+
protected $woo;
15+
1416
protected function setUp(): void {
1517
parent::setUp();
1618

17-
Functions\expect( 'get_option' )->once()->andReturn( array() );
19+
Functions\expect( 'get_option' )->once()->andReturn( array( 'de_DE' => 42 ) );
1820

1921
$this->test = new MslsOptionsTax( 0 );
2022
}
2123

2224
public function test_get_tax_query(): void {
23-
$this->assertIsString( $this->test->get_tax_query() );
25+
Functions\expect( 'is_woocommerce' )->once()->andReturn( false );
26+
27+
$this->assertEquals( '', $this->test->get_tax_query() );
28+
}
29+
30+
public function test_get_tax_query_woo(): void {
31+
global $wp_query;
32+
33+
$expected = 'taxonomy_query_string_1';
34+
$wp_query = (object) array(
35+
'tax_query' => (object) array(
36+
'queries' => array(
37+
1 => array( 'taxonomy' => $expected ),
38+
),
39+
),
40+
);
41+
42+
Functions\expect( 'is_woocommerce' )->once()->andReturn( true );
43+
44+
$this->assertEquals( $expected, $this->test->get_tax_query( array() ) );
45+
}
46+
47+
public function test_get_tax_query_set(): void {
48+
global $wp_query;
49+
50+
$expected = 'taxonomy_query_string_0';
51+
$wp_query = (object) array(
52+
'tax_query' => (object) array(
53+
'queries' => array(
54+
0 => array( 'taxonomy' => $expected ),
55+
),
56+
),
57+
);
58+
59+
Functions\expect( 'is_woocommerce' )->once()->andReturn( false );
60+
61+
$this->assertEquals( $expected, $this->test->get_tax_query( array() ) );
2462
}
2563

2664
public function test_get_postlink(): void {
27-
$this->assertIsString( $this->test->get_postlink( 'de_DE' ) );
65+
Functions\expect( 'is_woocommerce' )->once()->andReturn( false );
66+
67+
$this->assertEquals( '', $this->test->get_postlink( 'de_DE' ) );
68+
}
69+
70+
public function test_get_postlink_empty(): void {
71+
Functions\expect( 'is_woocommerce' )->never();
72+
73+
$this->assertEquals( '', $this->test->get_postlink( 'it_IT' ) );
2874
}
2975

3076
public function test_get_current_link(): void {
3177
$this->assertIsString( $this->test->get_current_link() );
3278
}
3379

3480
public function test_get_term_link(): void {
35-
$this->assertIsString( $this->test->get_term_link( 42 ) );
81+
global $wp_query;
82+
83+
$wp_query = (object) array(
84+
'tax_query' => (object) array(
85+
'queries' => array(
86+
0 => array( 'taxonomy' => 'taxonomy_query_string_0' ),
87+
),
88+
),
89+
);
90+
91+
$expected = 'http://example.com/term_link';
92+
93+
Functions\expect( 'is_woocommerce' )->once()->andReturn( false );
94+
Functions\expect( 'get_term_link' )->once()->andReturn( $expected );
95+
96+
$this->assertEquals( $expected, $this->test->get_term_link( 42 ) );
97+
}
98+
99+
public function test_get_term_link_wp_error(): void {
100+
global $wp_query;
101+
102+
$wp_query = (object) array(
103+
'tax_query' => (object) array(
104+
'queries' => array(
105+
0 => array( 'taxonomy' => 'taxonomy_query_string_0' ),
106+
),
107+
),
108+
);
109+
110+
$wp_error = \Mockery::mock( 'WP_Error' );
111+
112+
Functions\expect( 'is_woocommerce' )->once()->andReturn( false );
113+
Functions\expect( 'get_term_link' )->once()->andReturn( $wp_error );
114+
115+
$this->assertEquals( '', $this->test->get_term_link( 42 ) );
116+
}
117+
118+
public function test_get_term_link_empty(): void {
119+
Functions\expect( 'is_woocommerce' )->once()->andReturn( false );
120+
121+
$this->assertEquals( '', $this->test->get_term_link( 42 ) );
36122
}
37123
}

tests/TestMslsPostTag.php

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public function test_edit_input(): void {
5656
Functions\expect( 'restore_current_blog' )->atLeast();
5757
Functions\expect( 'get_terms' )->andReturn( array() );
5858
Functions\expect( 'plugin_dir_path' )->atLeast( 1 )->andReturn( dirname( __DIR__, 1 ) . '/' );
59+
Functions\expect( 'is_woocommerce' )->once()->andReturn( false );
5960

6061
$output = '<tr>
6162
<th colspan="2">

tests/TestMslsPostTagClassic.php

+10-8
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ class TestMslsPostTagClassic extends MslsUnitTestCase {
1616
protected function setUp(): void {
1717
parent::setUp();
1818

19-
Functions\when( 'get_option' )->justReturn( [] );
19+
Functions\when( 'get_option' )->justReturn( array() );
2020
Functions\expect( 'is_admin' )->andReturn( true );
21-
Functions\expect( 'get_post_types' )->andReturn( [ 'post', 'page' ] );
21+
Functions\expect( 'get_post_types' )->andReturn( array( 'post', 'page' ) );
2222

23-
foreach ( [ 'de_DE', 'en_US' ] as $locale ) {
23+
foreach ( array( 'de_DE', 'en_US' ) as $locale ) {
2424
$blog = \Mockery::mock( MslsBlog::class );
25-
$blog->shouldReceive( [
26-
'get_language' => $locale,
27-
] );
25+
$blog->shouldReceive(
26+
array(
27+
'get_language' => $locale,
28+
)
29+
);
2830

2931
$blogs[] = $blog;
3032
}
@@ -56,8 +58,9 @@ public function test_edit_input(): void {
5658
Functions\expect( 'get_admin_url' )->andReturn( '/wp-admin/edit-tags.php' );
5759
Functions\expect( 'switch_to_blog' )->atLeast();
5860
Functions\expect( 'restore_current_blog' )->atLeast();
59-
Functions\expect( 'get_terms' )->andReturn( [] );
61+
Functions\expect( 'get_terms' )->andReturn( array() );
6062
Functions\expect( 'plugin_dir_path' )->atLeast( 1 )->andReturn( dirname( __DIR__, 1 ) . '/' );
63+
Functions\expect( 'is_woocommerce' )->once()->andReturn( false );
6164

6265
$output = '<tr>
6366
<th colspan="2">
@@ -90,5 +93,4 @@ public function test_edit_input(): void {
9093
// second call should not output anything
9194
$this->test->edit_input( $tag, 'test' );
9295
}
93-
9496
}

tests/TestMslsTaxonomy.php

+12-21
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,27 @@
99

1010
class TestMslsTaxonomy extends MslsUnitTestCase {
1111

12-
/**
13-
* @param bool $exluded
14-
*
15-
* @return MslsTaxonomy
16-
*/
17-
public function get_test( bool $exluded = false ): MslsTaxonomy {
18-
parent::setUp();
12+
/**
13+
* @param bool $exluded
14+
*
15+
* @return MslsTaxonomy
16+
*/
17+
public function get_test( bool $exluded = false ): MslsTaxonomy {
18+
parent::setUp();
1919

20-
$options = \Mockery::mock( MslsOptions::class );
21-
$options->shouldReceive( 'is_excluded' )->andReturn( $exluded );
20+
$options = \Mockery::mock( MslsOptions::class );
21+
$options->shouldReceive( 'is_excluded' )->andReturn( $exluded );
2222

23-
Functions\expect( 'msls_options' )->zeroOrMoreTimes()->andReturn( $options );
23+
Functions\expect( 'msls_options' )->zeroOrMoreTimes()->andReturn( $options );
2424

25-
Functions\expect( 'apply_filters' )->atLeast()->once();
25+
Functions\expect( 'apply_filters' )->atLeast()->once();
2626

27-
Functions\expect( 'get_taxonomies' )->atLeast()->once()->andReturn( [] );
27+
Functions\expect( 'get_taxonomies' )->atLeast()->once()->andReturn( array() );
2828
Functions\expect( 'get_query_var' )->atLeast()->once()->with( 'taxonomy' )->andReturn( 'category' );
2929

3030
return new MslsTaxonomy();
3131
}
3232

33-
/**
34-
* @runInSeparateProcess
35-
* @preserveGlobalState disabled
36-
*/
3733
public function test_acl_request_included(): void {
3834
$cap = new \stdClass();
3935
$cap->manage_terms = 'manage_categories';
@@ -46,10 +42,6 @@ public function test_acl_request_included(): void {
4642
$this->assertEquals( 'category', $this->get_test()->acl_request() );
4743
}
4844

49-
/**
50-
* @runInSeparateProcess
51-
* @preserveGlobalState disabled
52-
*/
5345
public function test_acl_request_excluded(): void {
5446
$this->assertEquals( '', $this->get_test( true )->acl_request() );
5547
}
@@ -69,5 +61,4 @@ public function test_is_taxonomy(): void {
6961
public function test_get_request(): void {
7062
$this->assertEquals( 'category', $this->get_test()->get_request() );
7163
}
72-
7364
}

0 commit comments

Comments
 (0)