Skip to content

Commit 627db7d

Browse files
committed
Tests: Cover language attributes.
Add coverage for `get_language_attributes()` and `language_attributes()`, including document types, locale normalization, text direction, filters, and displayed output. Developed in: #14534 Props rejaulalomkhan, mukesh27. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@64637 602fd350-edb4-49c9-b593-d223f7449a82
1 parent bfc5008 commit 627db7d

1 file changed

Lines changed: 168 additions & 0 deletions

File tree

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
<?php
2+
3+
/**
4+
* Tests for the `get_language_attributes()` and `language_attributes()` functions.
5+
*
6+
* @group general
7+
* @group template
8+
* @group i18n
9+
*/
10+
class Tests_General_GetLanguageAttributes extends WP_UnitTestCase {
11+
12+
/**
13+
* Original text direction of the global locale.
14+
*
15+
* @var string
16+
*/
17+
private $original_text_direction;
18+
19+
public function set_up() {
20+
parent::set_up();
21+
22+
$this->original_text_direction = $GLOBALS['wp_locale']->text_direction;
23+
}
24+
25+
public function tear_down() {
26+
$GLOBALS['wp_locale']->text_direction = $this->original_text_direction;
27+
28+
parent::tear_down();
29+
}
30+
31+
/**
32+
* Tests the attributes for each doctype and HTML type.
33+
*
34+
* @ticket 65819
35+
*
36+
* @covers ::get_language_attributes
37+
*
38+
* @dataProvider data_doctypes_and_html_types
39+
*
40+
* @param string $doctype Doctype passed to the function.
41+
* @param string $html_type Value of the html_type option.
42+
* @param string $expected Expected attributes.
43+
*/
44+
public function test_doctypes_and_html_types( $doctype, $html_type, $expected ) {
45+
update_option( 'html_type', $html_type );
46+
47+
$this->assertSame( $expected, get_language_attributes( $doctype ) );
48+
}
49+
50+
/**
51+
* Data provider for test_doctypes_and_html_types().
52+
*
53+
* @return array[]
54+
*/
55+
public static function data_doctypes_and_html_types() {
56+
return array(
57+
'html, text/html' => array( 'html', 'text/html', 'lang="en-US"' ),
58+
'xhtml, text/html' => array( 'xhtml', 'text/html', 'lang="en-US" xml:lang="en-US"' ),
59+
'html, application/xhtml+xml' => array( 'html', 'application/xhtml+xml', 'lang="en-US" xml:lang="en-US"' ),
60+
'xhtml, application/xhtml+xml' => array( 'xhtml', 'application/xhtml+xml', 'xml:lang="en-US"' ),
61+
'html, empty html_type' => array( 'html', '', 'lang="en-US"' ),
62+
'xhtml, empty html_type' => array( 'xhtml', '', 'xml:lang="en-US"' ),
63+
);
64+
}
65+
66+
/**
67+
* Tests that the default doctype is 'html'.
68+
*
69+
* @ticket 65819
70+
*
71+
* @covers ::get_language_attributes
72+
*/
73+
public function test_defaults_to_html_doctype() {
74+
update_option( 'html_type', 'application/xhtml+xml' );
75+
76+
$this->assertSame( 'lang="en-US" xml:lang="en-US"', get_language_attributes() );
77+
}
78+
79+
/**
80+
* Tests that the language comes from the site locale.
81+
*
82+
* @ticket 65819
83+
*
84+
* @covers ::get_language_attributes
85+
*/
86+
public function test_uses_site_language() {
87+
add_filter(
88+
'determine_locale',
89+
static function () {
90+
return 'pt_BR';
91+
}
92+
);
93+
94+
$this->assertSame( 'lang="pt-BR" xml:lang="pt-BR"', get_language_attributes( 'xhtml' ) );
95+
}
96+
97+
/**
98+
* Tests that the dir attribute is added for right-to-left locales.
99+
*
100+
* @ticket 65819
101+
*
102+
* @covers ::get_language_attributes
103+
*/
104+
public function test_adds_dir_for_rtl() {
105+
$GLOBALS['wp_locale']->text_direction = 'rtl';
106+
107+
$this->assertSame( 'dir="rtl" lang="en-US"', get_language_attributes() );
108+
}
109+
110+
/**
111+
* Tests the arguments passed to the 'language_attributes' filter and that its return value is used.
112+
*
113+
* @ticket 65819
114+
*
115+
* @covers ::get_language_attributes
116+
*/
117+
public function test_language_attributes_filter() {
118+
$filter = new MockAction();
119+
add_filter( 'language_attributes', array( $filter, 'filter' ), 10, 2 );
120+
add_filter(
121+
'language_attributes',
122+
static function ( $output, $doctype ) {
123+
return $output . ' data-doctype="' . $doctype . '"';
124+
},
125+
20,
126+
2
127+
);
128+
129+
$output = get_language_attributes( 'xhtml' );
130+
131+
$this->assertSame( array( 'lang="en-US" xml:lang="en-US"', 'xhtml' ), $filter->get_args()[0], 'The filter should receive the attributes and the doctype.' );
132+
$this->assertSame( 'lang="en-US" xml:lang="en-US" data-doctype="xhtml"', $output, 'The filtered attributes should be returned.' );
133+
}
134+
135+
/**
136+
* Tests that language_attributes() displays the attributes for the given doctype.
137+
*
138+
* @ticket 65819
139+
*
140+
* @covers ::language_attributes
141+
*
142+
* @dataProvider data_language_attributes_output
143+
*
144+
* @param string|null $doctype Doctype passed to the function, or null for the default.
145+
* @param string $expected Expected output.
146+
*/
147+
public function test_language_attributes_output( $doctype, $expected ) {
148+
$this->expectOutputString( $expected );
149+
150+
if ( null === $doctype ) {
151+
language_attributes();
152+
} else {
153+
language_attributes( $doctype );
154+
}
155+
}
156+
157+
/**
158+
* Data provider for test_language_attributes_output().
159+
*
160+
* @return array[]
161+
*/
162+
public static function data_language_attributes_output() {
163+
return array(
164+
'default doctype' => array( null, 'lang="en-US"' ),
165+
'xhtml doctype' => array( 'xhtml', 'lang="en-US" xml:lang="en-US"' ),
166+
);
167+
}
168+
}

0 commit comments

Comments
 (0)