From 04c4720ce795ebd56871a7f6b79f58835acabb5f Mon Sep 17 00:00:00 2001 From: Christoph Herr Date: Wed, 26 Sep 2018 19:35:10 -0400 Subject: [PATCH 1/2] Prevented output of empty skip link markup. --- lib/api/html/accessibility.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/api/html/accessibility.php b/lib/api/html/accessibility.php index 1cac514d..28df86b3 100644 --- a/lib/api/html/accessibility.php +++ b/lib/api/html/accessibility.php @@ -47,7 +47,11 @@ function beans_build_skip_links() { * @type string Anchor text. * } */ - $skip_links = (array) apply_filters( 'beans_skip_links_list', $skip_links ); + $skip_links = apply_filters( 'beans_skip_links_list', $skip_links ); + + if ( empty( $skip_links ) ) { + return; + } beans_output_skip_links( $skip_links ); } From f329c095c81cd7ed3c6b013d87d52631bed98f0d Mon Sep 17 00:00:00 2001 From: Christoph Herr Date: Fri, 19 Oct 2018 22:16:36 -0400 Subject: [PATCH 2/2] Added unit and integration tests --- lib/api/html/accessibility.php | 6 +-- .../api/html/beansBuildSkipLinks.php | 42 +++++++++++++++++++ .../unit/api/html/beansBuildSkipLinks.php | 22 +++++++++- 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/lib/api/html/accessibility.php b/lib/api/html/accessibility.php index 28df86b3..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(); @@ -47,10 +47,10 @@ function beans_build_skip_links() { * @type string Anchor text. * } */ - $skip_links = apply_filters( 'beans_skip_links_list', $skip_links ); + $skip_links = (array) apply_filters( 'beans_skip_links_list', $skip_links ); if ( empty( $skip_links ) ) { - return; + 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() ); + } }