Skip to content

Commit b4753e4

Browse files
apermolloc
andauthored
fix(archive): no home_url fallback for tax/query (#582)
* test(archive): add tests for #190 Verify taxonomy/query archives don't fall back to home_url and empty URLs are skipped in output. * fix(archive): no home_url fallback for tax/query Override get_permalink() in MslsOptionsTax and MslsOptionsQuery to return empty string instead of falling back to home_url('/'). Skip empty URLs in MslsOutput::get() and get_alternate_links(). Closes #190 --------- Co-authored-by: Dennis Ploetner <re@lloc.de>
1 parent 2310f0d commit b4753e4

File tree

6 files changed

+117
-1
lines changed

6 files changed

+117
-1
lines changed

includes/MslsOptionsQuery.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ public static function create( $id = 0 ): ?MslsOptionsQuery {
6363
return new $query_class( $sql_cache );
6464
}
6565

66+
public function get_permalink( string $language ): string {
67+
return (string) apply_filters(
68+
'msls_options_get_permalink',
69+
$this->get_postlink( $language ),
70+
$language
71+
);
72+
}
73+
6674
/**
6775
* Get postlink
6876
*

includes/MslsOptionsTax.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ public function get_postlink( $language ) {
9696
return apply_filters( MslsOptions::MSLS_GET_POSTLINK_HOOK, $post_link, $this );
9797
}
9898

99+
public function get_permalink( string $language ): string {
100+
return (string) apply_filters(
101+
'msls_options_get_permalink',
102+
$this->get_postlink( $language ),
103+
$language
104+
);
105+
}
106+
99107
/**
100108
* Get current link
101109
*

includes/MslsOutput.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public function get( ?int $display, bool $filter = false, $exists = false ): arr
7676
restore_current_blog();
7777
}
7878

79+
if ( empty( $url ) ) {
80+
continue;
81+
}
82+
7983
if ( has_filter( self::MSLS_GET_HOOK ) ) {
8084
/**
8185
* Returns HTML-link for an item of the output-arr
@@ -116,7 +120,7 @@ public function get_alternate_links() {
116120

117121
foreach ( $blogs->get_objects() as $blog ) {
118122
$url = apply_filters( self::MSLS_ALTERNATE_LINKS_HOOK, $blog->get_url( $options ), $blog );
119-
if ( is_null( $url ) ) {
123+
if ( empty( $url ) ) {
120124
continue;
121125
}
122126

tests/phpunit/TestMslsOptionsQuery.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,12 @@ public function test_non_existent_get_postlink(): void {
102102

103103
$this->assertEquals( '', ( new MslsOptionsQuery( $sql_cache ) )->get_postlink( 'fr_FR' ) );
104104
}
105+
106+
public function test_get_permalink_returns_empty_when_no_translation(): void {
107+
Functions\expect( 'get_option' )->once()->andReturn( array( 'de_DE' => 42 ) );
108+
109+
$sql_cache = \Mockery::mock( MslsSqlCacher::class );
110+
111+
$this->assertSame( '', ( new MslsOptionsQuery( $sql_cache ) )->get_permalink( 'fr_FR' ) );
112+
}
105113
}

tests/phpunit/TestMslsOptionsTax.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,33 @@ public function test_get_term_link_empty(): void {
150150
$this->assertEquals( '', $this->MslsOptionsTaxFactory()->get_term_link( 42 ) );
151151
}
152152

153+
public function test_get_permalink_returns_empty_when_no_translation(): void {
154+
$test = $this->MslsOptionsTaxFactory();
155+
156+
$this->assertSame( '', $test->get_permalink( 'fr_FR' ) );
157+
}
158+
159+
public function test_get_permalink_returns_url_when_term_link_succeeds(): void {
160+
global $wp_query;
161+
162+
$wp_query = (object) array(
163+
'tax_query' => (object) array(
164+
'queries' => array(
165+
0 => array( 'taxonomy' => 'category' ),
166+
),
167+
),
168+
);
169+
170+
$expected = 'http://example.com/category/asia/';
171+
172+
Functions\expect( 'is_woocommerce' )->once()->andReturn( false );
173+
Functions\expect( 'get_term_link' )->once()->andReturn( $expected );
174+
175+
$test = $this->MslsOptionsTaxFactory();
176+
177+
$this->assertSame( $expected, $test->get_permalink( 'de_DE' ) );
178+
}
179+
153180
public function test_get_base_option() {
154181
$this->assertEquals( '', MslsOptionsTax::get_base_option() );
155182
}

tests/phpunit/TestMslsOutput.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,67 @@ public function test_is_requirements_not_fulfilled_with_mslsoptionspost(): void
326326
$this->assertTrue( $test->is_requirements_not_fulfilled( $mydata, true, 'de_DE' ) );
327327
}
328328

329+
public function test_get_alternate_links_empty_url(): void {
330+
$blogs = array();
331+
332+
$a = \Mockery::mock( MslsBlog::class );
333+
$a->shouldReceive( 'get_alpha2' )->andReturn( 'de' );
334+
$a->shouldReceive( 'get_language' )->andReturn( 'de_DE' );
335+
$a->shouldReceive( 'get_url' )->andReturn( '' );
336+
337+
$blogs[] = $a;
338+
339+
$collection = \Mockery::mock( MslsBlogCollection::class );
340+
$collection->shouldReceive( 'get_objects' )->andReturn( $blogs );
341+
342+
Functions\expect( 'msls_blog_collection' )->once()->andReturn( $collection );
343+
Functions\expect( 'is_admin' )->once()->andReturn( false );
344+
Functions\expect( 'is_front_page' )->once()->andReturn( false );
345+
Functions\expect( 'is_search' )->once()->andReturn( false );
346+
Functions\expect( 'is_404' )->once()->andReturn( false );
347+
Functions\expect( 'is_category' )->once()->andReturn( false );
348+
Functions\expect( 'is_tag' )->once()->andReturn( false );
349+
Functions\expect( 'is_tax' )->once()->andReturn( false );
350+
Functions\expect( 'is_date' )->once()->andReturn( false );
351+
Functions\expect( 'is_author' )->once()->andReturn( false );
352+
Functions\expect( 'is_post_type_archive' )->once()->andReturn( false );
353+
Functions\expect( 'get_queried_object_id' )->once()->andReturn( 42 );
354+
Functions\expect( 'get_option' )->once()->andReturn( array() );
355+
356+
$test = $this->MslsOutputFactory();
357+
358+
$this->assertEquals( '', $test->get_alternate_links() );
359+
}
360+
361+
public function test_get_skips_empty_url(): void {
362+
$blog = \Mockery::mock( MslsBlog::class );
363+
$blog->shouldReceive( 'get_language' )->andReturn( 'de_DE' );
364+
$blog->shouldReceive( 'get_description' )->andReturn( 'Deutsch' );
365+
$blog->userblog_id = 2;
366+
367+
$options = \Mockery::mock( MslsOptions::class );
368+
$options->shouldReceive( 'get_flag_url' )->once()->andReturn( 'https://msls.co/wp-content/plugins/msls/flags/de.png' );
369+
370+
$collection = \Mockery::mock( MslsBlogCollection::class );
371+
$collection->shouldReceive( 'get_filtered' )->andReturn( array( $blog ) );
372+
$collection->shouldReceive( 'is_current_blog' )->andReturn( false );
373+
374+
Functions\expect( 'is_admin' )->atLeast()->once()->andReturn( false );
375+
Functions\expect( 'is_front_page' )->atLeast()->once()->andReturn( false );
376+
Functions\expect( 'is_search' )->andReturn( false );
377+
Functions\expect( 'is_404' )->andReturn( false );
378+
Functions\expect( 'is_category' )->atLeast()->once()->andReturn( true );
379+
Functions\expect( 'is_tag' )->andReturn( false );
380+
Functions\expect( 'is_tax' )->andReturn( false );
381+
Functions\expect( 'is_woocommerce' )->andReturn( false );
382+
Functions\expect( 'get_queried_object_id' )->atLeast()->once()->andReturn( 42 );
383+
Functions\expect( 'get_option' )->atLeast()->once()->andReturn( array() );
384+
Functions\expect( 'switch_to_blog' )->once();
385+
Functions\expect( 'restore_current_blog' )->once();
386+
387+
$this->assertEquals( array(), ( new MslsOutput( $options, $collection ) )->get( 0 ) );
388+
}
389+
329390
public function test_init(): void {
330391
Functions\expect( '_deprecated_function' )->once();
331392

0 commit comments

Comments
 (0)