Skip to content

Commit a4ad96e

Browse files
Tests: Cover _navigation_markup().
Verify: - Default navigation markup structure, CSS class, aria-label, screen reader text, and links insertion. - Custom arguments for CSS class, screen reader heading, and `aria-label`. - Fallback of `aria-label` to screen reader text when empty. - The `navigation_markup_template` filter receiving both template and CSS class arguments. - Sanitization of CSS classes via `sanitize_html_class()`. Developed in #15739. Props devmuhib, huzaifaalmesbah. See #66757. git-svn-id: https://develop.svn.wordpress.org/trunk@65805 602fd350-edb4-49c9-b593-d223f7449a82
1 parent f608a2c commit a4ad96e

1 file changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
/**
4+
* Tests for the `_navigation_markup()` function.
5+
*
6+
* @group link
7+
* @group template
8+
*
9+
* @covers ::_navigation_markup
10+
*/
11+
class Tests_Link_NavigationMarkup extends WP_UnitTestCase {
12+
13+
/**
14+
* Tests default markup structure and attributes.
15+
*
16+
* @ticket 66612
17+
*/
18+
public function test_navigation_markup_default_structure() {
19+
$links = '<span class="page-numbers current">1</span><a class="page-numbers" href="#2">2</a>';
20+
$actual = _navigation_markup( $links );
21+
22+
$this->assertStringContainsString( 'class="navigation posts-navigation"', $actual );
23+
$this->assertStringContainsString( 'aria-label="Posts navigation"', $actual );
24+
$this->assertStringContainsString( '<h2 class="screen-reader-text">Posts navigation</h2>', $actual );
25+
$this->assertStringContainsString( '<div class="nav-links">' . $links . '</div>', $actual );
26+
}
27+
28+
/**
29+
* Tests navigation markup with custom class, screen reader text, and aria label.
30+
*
31+
* @ticket 66612
32+
*/
33+
public function test_navigation_markup_custom_arguments() {
34+
$links = '<a href="#next">Next</a>';
35+
$actual = _navigation_markup( $links, 'custom-class', 'Custom Reader Text', 'Custom Aria Label' );
36+
37+
$this->assertStringContainsString( 'class="navigation custom-class"', $actual );
38+
$this->assertStringContainsString( 'aria-label="Custom Aria Label"', $actual );
39+
$this->assertStringContainsString( '<h2 class="screen-reader-text">Custom Reader Text</h2>', $actual );
40+
$this->assertStringContainsString( '<div class="nav-links">' . $links . '</div>', $actual );
41+
}
42+
43+
/**
44+
* Tests that `aria-label` defaults to screen reader text when empty.
45+
*
46+
* @ticket 66612
47+
*/
48+
public function test_navigation_markup_aria_label_falls_back_to_screen_reader_text() {
49+
$links = '<a href="#prev">Previous</a>';
50+
$actual = _navigation_markup( $links, 'pagination', 'Pagination Heading', '' );
51+
52+
$this->assertStringContainsString( 'aria-label="Pagination Heading"', $actual );
53+
$this->assertStringContainsString( '<h2 class="screen-reader-text">Pagination Heading</h2>', $actual );
54+
}
55+
56+
/**
57+
* Tests that `navigation_markup_template` filter modifies the markup and receives class parameter.
58+
*
59+
* @ticket 66612
60+
*/
61+
public function test_navigation_markup_template_filter() {
62+
$passed_template = '';
63+
$passed_css_class = '';
64+
65+
add_filter(
66+
'navigation_markup_template',
67+
static function ( $template, $css_class ) use ( &$passed_template, &$passed_css_class ) {
68+
$passed_template = $template;
69+
$passed_css_class = $css_class;
70+
71+
return '<nav role="navigation" class="%1$s"><p>%2$s</p><ul class="links">%3$s</ul></nav>';
72+
},
73+
10,
74+
2
75+
);
76+
77+
$links = '<li>1</li>';
78+
$actual = _navigation_markup( $links, 'my-nav', 'My Nav' );
79+
80+
$this->assertStringContainsString( 'class="navigation %1$s"', $passed_template );
81+
$this->assertSame( 'my-nav', $passed_css_class );
82+
$this->assertSame( '<nav role="navigation" class="my-nav"><p>My Nav</p><ul class="links"><li>1</li></ul></nav>', $actual );
83+
}
84+
85+
/**
86+
* Tests that CSS class is sanitized with sanitize_html_class().
87+
*
88+
* @ticket 66612
89+
*/
90+
public function test_navigation_markup_sanitizes_html_class() {
91+
$links = '<a href="#page">Page</a>';
92+
$actual = _navigation_markup( $links, 'invalid class with spaces & <chars>' );
93+
94+
$this->assertStringContainsString( 'class="navigation invalidclasswithspaceschars"', $actual );
95+
}
96+
}

0 commit comments

Comments
 (0)