diff --git a/lib/api/html/accessibility.php b/lib/api/html/accessibility.php index 1cac514d..d16f8822 100644 --- a/lib/api/html/accessibility.php +++ b/lib/api/html/accessibility.php @@ -12,7 +12,7 @@ * * @since 1.5.0 * - * @return void + * @return null|bool Returns false when no skip links, otherwise null. */ function beans_build_skip_links() { $skip_links = array(); @@ -49,6 +49,10 @@ function beans_build_skip_links() { */ $skip_links = (array) apply_filters( 'beans_skip_links_list', $skip_links ); + if ( empty( $skip_links ) ) { + return false; + } + beans_output_skip_links( $skip_links ); } diff --git a/tests/phpunit/integration/api/html/beansBuildSkipLinks.php b/tests/phpunit/integration/api/html/beansBuildSkipLinks.php index 816e67f7..2bfd9329 100644 --- a/tests/phpunit/integration/api/html/beansBuildSkipLinks.php +++ b/tests/phpunit/integration/api/html/beansBuildSkipLinks.php @@ -265,4 +265,46 @@ function( $default_layout ) { $this->assertContains( $this->format_the_html( $expected ), $this->format_the_html( $actual ) ); } + + /** + * Test beans_build_skip_links() should return false when no skip links because beans_skip_links_list filter unsets the array. + */ + public function test_should_return_false_when_filter_unsets_the_array_of_skip_links() { + add_filter( + 'beans_default_layout', + function( $default_layout ) { + return 'c_sp'; + } + ); + + $this->assertEquals( beans_get_layout(), 'c_sp' ); + + add_filter( + 'beans_skip_links_list', + function( $skip_links ) { + unset( $skip_links['beans-content'], $skip_links['beans-primary-sidebar'] ); + return $skip_links; + } + ); + + $this->assertFalse( beans_build_skip_links() ); + } + + /** + * Test beans_build_skip_links() should return false when beans_skip_links_list filter returns null. + */ + public function test_should_return_false_when_filter_returns_null() { + add_filter( + 'beans_default_layout', + function( $default_layout ) { + return 'c'; + } + ); + + $this->assertEquals( beans_get_layout(), 'c' ); + + add_filter( 'beans_skip_links_list', '__return_null' ); + $this->assertFalse( beans_build_skip_links() ); + } + } diff --git a/tests/phpunit/unit/api/html/beansBuildSkipLinks.php b/tests/phpunit/unit/api/html/beansBuildSkipLinks.php index cf8e1a42..bc22e5d0 100644 --- a/tests/phpunit/unit/api/html/beansBuildSkipLinks.php +++ b/tests/phpunit/unit/api/html/beansBuildSkipLinks.php @@ -50,7 +50,7 @@ public function test_should_call_beans_has_primary_sidebar_and_beans_has_seconda /** * Test beans_build_skip_links() should not call beans_has_primary_sidebar() or beans_has_secondary_sidebar() when the layout is full-width. */ - public function test_should_not_call_beans_has_primary_sidebar_or_beans_has_secondary_sidebara_when_fullwith_layout() { + public function test_should_not_call_beans_has_primary_sidebar_or_beans_has_secondary_sidebar_when_full_width_layout() { Monkey\Functions\expect( 'beans_get_layout' )->once()->andReturn( 'c' ); Monkey\Functions\expect( 'has_nav_menu' ) ->once() @@ -66,4 +66,24 @@ public function test_should_not_call_beans_has_primary_sidebar_or_beans_has_seco $this->assertNull( beans_build_skip_links() ); } + /** + * Test beans_build_skip_links() should not call beans_output_skip_links() when there are no skip links. + */ + public function test_should_not_call_beans_output_skip_links_when_no_skip_links() { + Monkey\Functions\expect( 'beans_get_layout' )->once()->andReturn( 'c' ); + Monkey\Functions\expect( 'has_nav_menu' ) + ->once() + ->with( 'primary' ) + ->andReturn( false ); + Monkey\Functions\expect( 'beans_has_primary_sidebar' )->never(); + Monkey\Functions\expect( 'beans_has_secondary_sidebar' )->never(); + + Monkey\Filters\expectApplied( 'beans_skip_links_list' ) + ->once() + ->andReturn( null ); + + Monkey\Functions\expect( 'beans_output_skip_links' )->never(); + + $this->assertFalse( beans_build_skip_links() ); + } }