Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions includes/MslsOptionsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
8 changes: 8 additions & 0 deletions includes/MslsOptionsTax.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
6 changes: 5 additions & 1 deletion includes/MslsOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
8 changes: 8 additions & 0 deletions tests/phpunit/TestMslsOptionsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
}
}
27 changes: 27 additions & 0 deletions tests/phpunit/TestMslsOptionsTax.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
}
Expand Down
61 changes: 61 additions & 0 deletions tests/phpunit/TestMslsOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Loading