From 2299d55ea78a539d4e1354b625ca4a70e3f712b3 Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Sun, 26 May 2024 14:37:00 +0200 Subject: [PATCH 1/2] Optimized code --- includes/MslsCustomFilter.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/includes/MslsCustomFilter.php b/includes/MslsCustomFilter.php index 68b83c18..7655497b 100644 --- a/includes/MslsCustomFilter.php +++ b/includes/MslsCustomFilter.php @@ -49,12 +49,6 @@ function () { * @uses selected */ public function add_filter(): void { - $id = ( - filter_has_var( INPUT_GET, self::FILTER_NAME ) ? - filter_input( INPUT_GET, self::FILTER_NAME, FILTER_SANITIZE_NUMBER_INT ) : - '0' - ); - $blogs = $this->collection->get(); if ( $blogs ) { $options = array( '' => esc_html( __( 'Show all posts', 'multisite-language-switcher' ) ) ); @@ -65,6 +59,12 @@ public function add_filter(): void { ); } + $id = ( + filter_has_var( INPUT_GET, self::FILTER_NAME ) ? + filter_input( INPUT_GET, self::FILTER_NAME, FILTER_SANITIZE_NUMBER_INT ) : + '0' + ); + echo ( new Select( self::FILTER_NAME, $options, $id ) )->render(); } } @@ -84,15 +84,15 @@ public function execute_filter( \WP_Query $query ) { $id = filter_input( INPUT_GET, self::FILTER_NAME, FILTER_SANITIZE_NUMBER_INT ); $blog = $this->collection->get_object( intval( $id ) ); - if ( $blog ) { - $sql_cache = MslsSqlCacher::create( __CLASS__, __METHOD__ ); + if ( ! $blog ) { + return false; + } - // load post we need to exclude (they already have a translation) from search query - $query->query_vars['post__not_in'] = ( new TranslatedPostIdQuery( $sql_cache ) )( $blog->get_language() ); + $sql_cache = MslsSqlCacher::create( __CLASS__, __METHOD__ ); - return $query; - } + // load post we need to exclude (they already have a translation) from search query + $query->query_vars['post__not_in'] = ( new TranslatedPostIdQuery( $sql_cache ) )( $blog->get_language() ); - return false; + return $query; } } From 23e5a1a065c4efb0084152216b2a99947ca77537 Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Mon, 27 May 2024 15:49:25 +0200 Subject: [PATCH 2/2] Tests added --- phpunit.xml | 2 +- tests/MslsUnitTestCase.php | 5 +- tests/TestMslsOptionsTax.php | 94 ++++++++++++++++++++++++++++++-- tests/TestMslsPostTag.php | 1 + tests/TestMslsPostTagClassic.php | 18 +++--- tests/TestMslsTaxonomy.php | 33 ++++------- 6 files changed, 118 insertions(+), 35 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index 9930c445..755c4de7 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,5 +1,5 @@ - + ./includes/ diff --git a/tests/MslsUnitTestCase.php b/tests/MslsUnitTestCase.php index 1cd797ef..52eba307 100644 --- a/tests/MslsUnitTestCase.php +++ b/tests/MslsUnitTestCase.php @@ -19,6 +19,8 @@ protected function setUp(): void { parent::setUp(); Monkey\setUp(); + \Mockery::namedMock( 'WooCommerce', \stdClass::class ); + Functions\when( 'esc_html' )->returnArg(); Functions\when( 'esc_attr' )->returnArg(); Functions\when( 'esc_url' )->returnArg(); @@ -30,7 +32,8 @@ protected function tearDown(): void { restore_error_handler(); Monkey\tearDown(); + \Mockery::close(); + parent::tearDown(); } - } diff --git a/tests/TestMslsOptionsTax.php b/tests/TestMslsOptionsTax.php index f52cafdf..4ec0cf33 100644 --- a/tests/TestMslsOptionsTax.php +++ b/tests/TestMslsOptionsTax.php @@ -11,20 +11,66 @@ */ class TestMslsOptionsTax extends MslsUnitTestCase { + protected $woo; + protected function setUp(): void { parent::setUp(); - Functions\expect( 'get_option' )->once()->andReturn( array() ); + Functions\expect( 'get_option' )->once()->andReturn( array( 'de_DE' => 42 ) ); $this->test = new MslsOptionsTax( 0 ); } public function test_get_tax_query(): void { - $this->assertIsString( $this->test->get_tax_query() ); + Functions\expect( 'is_woocommerce' )->once()->andReturn( false ); + + $this->assertEquals( '', $this->test->get_tax_query() ); + } + + public function test_get_tax_query_woo(): void { + global $wp_query; + + $expected = 'taxonomy_query_string_1'; + $wp_query = (object) array( + 'tax_query' => (object) array( + 'queries' => array( + 1 => array( 'taxonomy' => $expected ), + ), + ), + ); + + Functions\expect( 'is_woocommerce' )->once()->andReturn( true ); + + $this->assertEquals( $expected, $this->test->get_tax_query( array() ) ); + } + + public function test_get_tax_query_set(): void { + global $wp_query; + + $expected = 'taxonomy_query_string_0'; + $wp_query = (object) array( + 'tax_query' => (object) array( + 'queries' => array( + 0 => array( 'taxonomy' => $expected ), + ), + ), + ); + + Functions\expect( 'is_woocommerce' )->once()->andReturn( false ); + + $this->assertEquals( $expected, $this->test->get_tax_query( array() ) ); } public function test_get_postlink(): void { - $this->assertIsString( $this->test->get_postlink( 'de_DE' ) ); + Functions\expect( 'is_woocommerce' )->once()->andReturn( false ); + + $this->assertEquals( '', $this->test->get_postlink( 'de_DE' ) ); + } + + public function test_get_postlink_empty(): void { + Functions\expect( 'is_woocommerce' )->never(); + + $this->assertEquals( '', $this->test->get_postlink( 'it_IT' ) ); } public function test_get_current_link(): void { @@ -32,6 +78,46 @@ public function test_get_current_link(): void { } public function test_get_term_link(): void { - $this->assertIsString( $this->test->get_term_link( 42 ) ); + global $wp_query; + + $wp_query = (object) array( + 'tax_query' => (object) array( + 'queries' => array( + 0 => array( 'taxonomy' => 'taxonomy_query_string_0' ), + ), + ), + ); + + $expected = 'http://example.com/term_link'; + + Functions\expect( 'is_woocommerce' )->once()->andReturn( false ); + Functions\expect( 'get_term_link' )->once()->andReturn( $expected ); + + $this->assertEquals( $expected, $this->test->get_term_link( 42 ) ); + } + + public function test_get_term_link_wp_error(): void { + global $wp_query; + + $wp_query = (object) array( + 'tax_query' => (object) array( + 'queries' => array( + 0 => array( 'taxonomy' => 'taxonomy_query_string_0' ), + ), + ), + ); + + $wp_error = \Mockery::mock( 'WP_Error' ); + + Functions\expect( 'is_woocommerce' )->once()->andReturn( false ); + Functions\expect( 'get_term_link' )->once()->andReturn( $wp_error ); + + $this->assertEquals( '', $this->test->get_term_link( 42 ) ); + } + + public function test_get_term_link_empty(): void { + Functions\expect( 'is_woocommerce' )->once()->andReturn( false ); + + $this->assertEquals( '', $this->test->get_term_link( 42 ) ); } } diff --git a/tests/TestMslsPostTag.php b/tests/TestMslsPostTag.php index d8749f18..388e411b 100644 --- a/tests/TestMslsPostTag.php +++ b/tests/TestMslsPostTag.php @@ -56,6 +56,7 @@ public function test_edit_input(): void { Functions\expect( 'restore_current_blog' )->atLeast(); Functions\expect( 'get_terms' )->andReturn( array() ); Functions\expect( 'plugin_dir_path' )->atLeast( 1 )->andReturn( dirname( __DIR__, 1 ) . '/' ); + Functions\expect( 'is_woocommerce' )->once()->andReturn( false ); $output = ' diff --git a/tests/TestMslsPostTagClassic.php b/tests/TestMslsPostTagClassic.php index a82bbe86..24c266fb 100644 --- a/tests/TestMslsPostTagClassic.php +++ b/tests/TestMslsPostTagClassic.php @@ -16,15 +16,17 @@ class TestMslsPostTagClassic extends MslsUnitTestCase { protected function setUp(): void { parent::setUp(); - Functions\when( 'get_option' )->justReturn( [] ); + Functions\when( 'get_option' )->justReturn( array() ); Functions\expect( 'is_admin' )->andReturn( true ); - Functions\expect( 'get_post_types' )->andReturn( [ 'post', 'page' ] ); + Functions\expect( 'get_post_types' )->andReturn( array( 'post', 'page' ) ); - foreach ( [ 'de_DE', 'en_US' ] as $locale ) { + foreach ( array( 'de_DE', 'en_US' ) as $locale ) { $blog = \Mockery::mock( MslsBlog::class ); - $blog->shouldReceive( [ - 'get_language' => $locale, - ] ); + $blog->shouldReceive( + array( + 'get_language' => $locale, + ) + ); $blogs[] = $blog; } @@ -56,8 +58,9 @@ public function test_edit_input(): void { Functions\expect( 'get_admin_url' )->andReturn( '/wp-admin/edit-tags.php' ); Functions\expect( 'switch_to_blog' )->atLeast(); Functions\expect( 'restore_current_blog' )->atLeast(); - Functions\expect( 'get_terms' )->andReturn( [] ); + Functions\expect( 'get_terms' )->andReturn( array() ); Functions\expect( 'plugin_dir_path' )->atLeast( 1 )->andReturn( dirname( __DIR__, 1 ) . '/' ); + Functions\expect( 'is_woocommerce' )->once()->andReturn( false ); $output = ' @@ -90,5 +93,4 @@ public function test_edit_input(): void { // second call should not output anything $this->test->edit_input( $tag, 'test' ); } - } diff --git a/tests/TestMslsTaxonomy.php b/tests/TestMslsTaxonomy.php index 4e2b027f..a4637b61 100644 --- a/tests/TestMslsTaxonomy.php +++ b/tests/TestMslsTaxonomy.php @@ -9,31 +9,27 @@ class TestMslsTaxonomy extends MslsUnitTestCase { - /** - * @param bool $exluded - * - * @return MslsTaxonomy - */ - public function get_test( bool $exluded = false ): MslsTaxonomy { - parent::setUp(); + /** + * @param bool $exluded + * + * @return MslsTaxonomy + */ + public function get_test( bool $exluded = false ): MslsTaxonomy { + parent::setUp(); - $options = \Mockery::mock( MslsOptions::class ); - $options->shouldReceive( 'is_excluded' )->andReturn( $exluded ); + $options = \Mockery::mock( MslsOptions::class ); + $options->shouldReceive( 'is_excluded' )->andReturn( $exluded ); - Functions\expect( 'msls_options' )->zeroOrMoreTimes()->andReturn( $options ); + Functions\expect( 'msls_options' )->zeroOrMoreTimes()->andReturn( $options ); - Functions\expect( 'apply_filters' )->atLeast()->once(); + Functions\expect( 'apply_filters' )->atLeast()->once(); - Functions\expect( 'get_taxonomies' )->atLeast()->once()->andReturn( [] ); + Functions\expect( 'get_taxonomies' )->atLeast()->once()->andReturn( array() ); Functions\expect( 'get_query_var' )->atLeast()->once()->with( 'taxonomy' )->andReturn( 'category' ); return new MslsTaxonomy(); } - /** - * @runInSeparateProcess - * @preserveGlobalState disabled - */ public function test_acl_request_included(): void { $cap = new \stdClass(); $cap->manage_terms = 'manage_categories'; @@ -46,10 +42,6 @@ public function test_acl_request_included(): void { $this->assertEquals( 'category', $this->get_test()->acl_request() ); } - /** - * @runInSeparateProcess - * @preserveGlobalState disabled - */ public function test_acl_request_excluded(): void { $this->assertEquals( '', $this->get_test( true )->acl_request() ); } @@ -69,5 +61,4 @@ public function test_is_taxonomy(): void { public function test_get_request(): void { $this->assertEquals( 'category', $this->get_test()->get_request() ); } - }