diff --git a/includes/MslsOptionsQuery.php b/includes/MslsOptionsQuery.php index 1cf93c77..fe954c1e 100644 --- a/includes/MslsOptionsQuery.php +++ b/includes/MslsOptionsQuery.php @@ -63,6 +63,14 @@ public static function create( $id = 0 ): ?MslsOptionsQuery { return new $query_class( $sql_cache ); } + public function get_permalink( string $language ): string { + return (string) apply_filters( + 'msls_options_get_permalink', + $this->get_postlink( $language ), + $language + ); + } + /** * Get postlink * diff --git a/includes/MslsOptionsTax.php b/includes/MslsOptionsTax.php index 1b5c281f..01ac0fe5 100644 --- a/includes/MslsOptionsTax.php +++ b/includes/MslsOptionsTax.php @@ -96,6 +96,14 @@ public function get_postlink( $language ) { return apply_filters( MslsOptions::MSLS_GET_POSTLINK_HOOK, $post_link, $this ); } + public function get_permalink( string $language ): string { + return (string) apply_filters( + 'msls_options_get_permalink', + $this->get_postlink( $language ), + $language + ); + } + /** * Get current link * diff --git a/includes/MslsOutput.php b/includes/MslsOutput.php index 3bbeb414..7d3061e3 100644 --- a/includes/MslsOutput.php +++ b/includes/MslsOutput.php @@ -76,6 +76,10 @@ public function get( ?int $display, bool $filter = false, $exists = false ): arr restore_current_blog(); } + if ( empty( $url ) ) { + continue; + } + if ( has_filter( self::MSLS_GET_HOOK ) ) { /** * Returns HTML-link for an item of the output-arr @@ -116,7 +120,7 @@ public function get_alternate_links() { foreach ( $blogs->get_objects() as $blog ) { $url = apply_filters( self::MSLS_ALTERNATE_LINKS_HOOK, $blog->get_url( $options ), $blog ); - if ( is_null( $url ) ) { + if ( empty( $url ) ) { continue; } diff --git a/tests/phpunit/TestMslsOptionsQuery.php b/tests/phpunit/TestMslsOptionsQuery.php index 40cffc30..e95ccfbc 100644 --- a/tests/phpunit/TestMslsOptionsQuery.php +++ b/tests/phpunit/TestMslsOptionsQuery.php @@ -102,4 +102,12 @@ public function test_non_existent_get_postlink(): void { $this->assertEquals( '', ( new MslsOptionsQuery( $sql_cache ) )->get_postlink( 'fr_FR' ) ); } + + public function test_get_permalink_returns_empty_when_no_translation(): void { + Functions\expect( 'get_option' )->once()->andReturn( array( 'de_DE' => 42 ) ); + + $sql_cache = \Mockery::mock( MslsSqlCacher::class ); + + $this->assertSame( '', ( new MslsOptionsQuery( $sql_cache ) )->get_permalink( 'fr_FR' ) ); + } } diff --git a/tests/phpunit/TestMslsOptionsTax.php b/tests/phpunit/TestMslsOptionsTax.php index 973792d8..b55a3822 100644 --- a/tests/phpunit/TestMslsOptionsTax.php +++ b/tests/phpunit/TestMslsOptionsTax.php @@ -150,6 +150,33 @@ public function test_get_term_link_empty(): void { $this->assertEquals( '', $this->MslsOptionsTaxFactory()->get_term_link( 42 ) ); } + public function test_get_permalink_returns_empty_when_no_translation(): void { + $test = $this->MslsOptionsTaxFactory(); + + $this->assertSame( '', $test->get_permalink( 'fr_FR' ) ); + } + + public function test_get_permalink_returns_url_when_term_link_succeeds(): void { + global $wp_query; + + $wp_query = (object) array( + 'tax_query' => (object) array( + 'queries' => array( + 0 => array( 'taxonomy' => 'category' ), + ), + ), + ); + + $expected = 'http://example.com/category/asia/'; + + Functions\expect( 'is_woocommerce' )->once()->andReturn( false ); + Functions\expect( 'get_term_link' )->once()->andReturn( $expected ); + + $test = $this->MslsOptionsTaxFactory(); + + $this->assertSame( $expected, $test->get_permalink( 'de_DE' ) ); + } + public function test_get_base_option() { $this->assertEquals( '', MslsOptionsTax::get_base_option() ); } diff --git a/tests/phpunit/TestMslsOutput.php b/tests/phpunit/TestMslsOutput.php index 0ad77bab..280f5f7d 100644 --- a/tests/phpunit/TestMslsOutput.php +++ b/tests/phpunit/TestMslsOutput.php @@ -326,6 +326,67 @@ public function test_is_requirements_not_fulfilled_with_mslsoptionspost(): void $this->assertTrue( $test->is_requirements_not_fulfilled( $mydata, true, 'de_DE' ) ); } + public function test_get_alternate_links_empty_url(): void { + $blogs = array(); + + $a = \Mockery::mock( MslsBlog::class ); + $a->shouldReceive( 'get_alpha2' )->andReturn( 'de' ); + $a->shouldReceive( 'get_language' )->andReturn( 'de_DE' ); + $a->shouldReceive( 'get_url' )->andReturn( '' ); + + $blogs[] = $a; + + $collection = \Mockery::mock( MslsBlogCollection::class ); + $collection->shouldReceive( 'get_objects' )->andReturn( $blogs ); + + Functions\expect( 'msls_blog_collection' )->once()->andReturn( $collection ); + Functions\expect( 'is_admin' )->once()->andReturn( false ); + Functions\expect( 'is_front_page' )->once()->andReturn( false ); + Functions\expect( 'is_search' )->once()->andReturn( false ); + Functions\expect( 'is_404' )->once()->andReturn( false ); + Functions\expect( 'is_category' )->once()->andReturn( false ); + Functions\expect( 'is_tag' )->once()->andReturn( false ); + Functions\expect( 'is_tax' )->once()->andReturn( false ); + Functions\expect( 'is_date' )->once()->andReturn( false ); + Functions\expect( 'is_author' )->once()->andReturn( false ); + Functions\expect( 'is_post_type_archive' )->once()->andReturn( false ); + Functions\expect( 'get_queried_object_id' )->once()->andReturn( 42 ); + Functions\expect( 'get_option' )->once()->andReturn( array() ); + + $test = $this->MslsOutputFactory(); + + $this->assertEquals( '', $test->get_alternate_links() ); + } + + public function test_get_skips_empty_url(): void { + $blog = \Mockery::mock( MslsBlog::class ); + $blog->shouldReceive( 'get_language' )->andReturn( 'de_DE' ); + $blog->shouldReceive( 'get_description' )->andReturn( 'Deutsch' ); + $blog->userblog_id = 2; + + $options = \Mockery::mock( MslsOptions::class ); + $options->shouldReceive( 'get_flag_url' )->once()->andReturn( 'https://msls.co/wp-content/plugins/msls/flags/de.png' ); + + $collection = \Mockery::mock( MslsBlogCollection::class ); + $collection->shouldReceive( 'get_filtered' )->andReturn( array( $blog ) ); + $collection->shouldReceive( 'is_current_blog' )->andReturn( false ); + + Functions\expect( 'is_admin' )->atLeast()->once()->andReturn( false ); + Functions\expect( 'is_front_page' )->atLeast()->once()->andReturn( false ); + Functions\expect( 'is_search' )->andReturn( false ); + Functions\expect( 'is_404' )->andReturn( false ); + Functions\expect( 'is_category' )->atLeast()->once()->andReturn( true ); + Functions\expect( 'is_tag' )->andReturn( false ); + Functions\expect( 'is_tax' )->andReturn( false ); + Functions\expect( 'is_woocommerce' )->andReturn( false ); + Functions\expect( 'get_queried_object_id' )->atLeast()->once()->andReturn( 42 ); + Functions\expect( 'get_option' )->atLeast()->once()->andReturn( array() ); + Functions\expect( 'switch_to_blog' )->once(); + Functions\expect( 'restore_current_blog' )->once(); + + $this->assertEquals( array(), ( new MslsOutput( $options, $collection ) )->get( 0 ) ); + } + public function test_init(): void { Functions\expect( '_deprecated_function' )->once();