99use EqualizeDigital \AccessibilityChecker \Fixes \Fix \HTMLLangAndDirFix ;
1010use EqualizeDigital \AccessibilityChecker \Fixes \FixInterface ;
1111
12+ require_once __DIR__ . '/FixTestTrait.php ' ;
13+
1214/**
1315 * Unit tests for the HTMLLangAndDirFix class.
1416 */
1517class HTMLLangAndDirFixTest extends WP_UnitTestCase {
1618
17- /**
18- * Test that HTMLLangAndDirFix implements FixInterface.
19- *
20- * @return void
21- */
22- public function test_implements_fix_interface () {
23- $ fix = new HTMLLangAndDirFix ();
24- $ this ->assertInstanceOf ( FixInterface::class, $ fix );
25- }
19+ use FixTestTrait;
2620
2721 /**
28- * Test get_slug returns correct slug .
22+ * Set up the test .
2923 *
3024 * @return void
3125 */
32- public function test_get_slug () {
33- $ this ->assertEquals ( 'lang_and_dir ' , HTMLLangAndDirFix::get_slug () );
26+ public function set_up () {
27+ parent ::set_up ();
28+ $ this ->fix = new HTMLLangAndDirFix ();
3429 }
3530
3631 /**
37- * Test get_nicename returns translated string .
32+ * Get the expected slug for this fix .
3833 *
39- * @return void
34+ * @return string
4035 */
41- public function test_get_nicename () {
42- $ nicename = HTMLLangAndDirFix::get_nicename ();
43- $ this ->assertIsString ( $ nicename );
44- $ this ->assertNotEmpty ( $ nicename );
45- $ this ->assertEquals ( 'Add lang & dir Attributes ' , $ nicename );
36+ protected function get_expected_slug (): string {
37+ return 'lang_and_dir ' ;
4638 }
4739
4840 /**
49- * Test get_fancyname returns translated string .
41+ * Get the expected type for this fix .
5042 *
51- * @return void
43+ * @return string
5244 */
53- public function test_get_fancyname () {
54- $ fancyname = HTMLLangAndDirFix::get_fancyname ();
55- $ this ->assertIsString ( $ fancyname );
56- $ this ->assertNotEmpty ( $ fancyname );
57- $ this ->assertEquals ( 'Set Page Language ' , $ fancyname );
45+ protected function get_expected_type (): string {
46+ return 'frontend ' ;
5847 }
5948
6049 /**
61- * Test get_type returns frontend .
50+ * Get the fix class name .
6251 *
63- * @return void
52+ * @return string
6453 */
65- public function test_get_type () {
66- $ this -> assertEquals ( ' frontend ' , HTMLLangAndDirFix::get_type () ) ;
54+ protected function get_fix_class_name (): string {
55+ return HTMLLangAndDirFix::class ;
6756 }
6857
6958 /**
@@ -72,10 +61,8 @@ public function test_get_type() {
7261 * @return void
7362 */
7463 public function test_get_fields_array () {
75- $ fix = new HTMLLangAndDirFix ();
76- $ fields = $ fix ->get_fields_array ();
64+ $ fields = $ this ->fix ->get_fields_array ();
7765
78- $ this ->assertIsArray ( $ fields );
7966 $ this ->assertArrayHasKey ( 'edac_fix_add_lang_and_dir ' , $ fields );
8067
8168 $ field = $ fields ['edac_fix_add_lang_and_dir ' ];
@@ -90,32 +77,16 @@ public function test_get_fields_array() {
9077 $ this ->assertStringContainsString ( '<html> ' , $ field ['description ' ] );
9178 }
9279
93- /**
94- * Test get_fields_array preserves existing fields.
95- *
96- * @return void
97- */
98- public function test_get_fields_array_preserves_existing_fields () {
99- $ fix = new HTMLLangAndDirFix ();
100- $ existing_fields = [ 'existing_field ' => [ 'type ' => 'text ' ] ];
101- $ fields = $ fix ->get_fields_array ( $ existing_fields );
102-
103- $ this ->assertArrayHasKey ( 'existing_field ' , $ fields );
104- $ this ->assertArrayHasKey ( 'edac_fix_add_lang_and_dir ' , $ fields );
105- }
106-
10780 /**
10881 * Test register method adds filter.
10982 *
11083 * @return void
11184 */
11285 public function test_register_adds_filter () {
113- $ fix = new HTMLLangAndDirFix ();
114-
115- $ fix ->register ();
86+ $ this ->fix ->register ();
11687
11788 // Verify that the filter was added.
118- $ this ->assertTrue ( has_filter ( 'edac_filter_fixes_settings_fields ' , [ $ fix , 'get_fields_array ' ] ) !== false );
89+ $ this ->assertTrue ( has_filter ( 'edac_filter_fixes_settings_fields ' , [ $ this -> fix , 'get_fields_array ' ] ) !== false );
11990 }
12091
12192 /**
@@ -124,15 +95,13 @@ public function test_register_adds_filter() {
12495 * @return void
12596 */
12697 public function test_run_when_disabled () {
127- $ fix = new HTMLLangAndDirFix ();
128-
12998 // Ensure option is disabled.
13099 update_option ( 'edac_fix_add_lang_and_dir ' , false );
131100
132- $ fix ->run ();
101+ $ this -> fix ->run ();
133102
134103 // Check that no filters were added.
135- $ this ->assertFalse ( has_filter ( 'language_attributes ' , [ $ fix , 'maybe_add_lang_and_dir ' ] ) );
104+ $ this ->assertFalse ( has_filter ( 'language_attributes ' , [ $ this -> fix , 'maybe_add_lang_and_dir ' ] ) );
136105 $ this ->assertFalse ( has_filter ( 'edac_filter_frontend_fixes_data ' ) );
137106 }
138107
@@ -142,15 +111,13 @@ public function test_run_when_disabled() {
142111 * @return void
143112 */
144113 public function test_run_when_enabled () {
145- $ fix = new HTMLLangAndDirFix ();
146-
147114 // Enable the option.
148115 update_option ( 'edac_fix_add_lang_and_dir ' , true );
149116
150- $ fix ->run ();
117+ $ this -> fix ->run ();
151118
152119 // Check that filters were added.
153- $ this ->assertTrue ( has_filter ( 'language_attributes ' , [ $ fix , 'maybe_add_lang_and_dir ' ] ) !== false );
120+ $ this ->assertTrue ( has_filter ( 'language_attributes ' , [ $ this -> fix , 'maybe_add_lang_and_dir ' ] ) !== false );
154121 $ this ->assertTrue ( has_filter ( 'edac_filter_frontend_fixes_data ' ) !== false );
155122 }
156123
@@ -160,12 +127,10 @@ public function test_run_when_enabled() {
160127 * @return void
161128 */
162129 public function test_frontend_data_filter () {
163- $ fix = new HTMLLangAndDirFix ();
164-
165130 // Enable the option.
166131 update_option ( 'edac_fix_add_lang_and_dir ' , true );
167132
168- $ fix ->run ();
133+ $ this -> fix ->run ();
169134
170135 // Test the filter output.
171136 $ data = apply_filters ( 'edac_filter_frontend_fixes_data ' , [] );
@@ -185,10 +150,8 @@ public function test_frontend_data_filter() {
185150 * @return void
186151 */
187152 public function test_maybe_add_lang_and_dir_missing_attributes () {
188- $ fix = new HTMLLangAndDirFix ();
189-
190153 // Test with empty output (no existing attributes).
191- $ output = $ fix ->maybe_add_lang_and_dir ( '' );
154+ $ output = $ this -> fix ->maybe_add_lang_and_dir ( '' );
192155
193156 $ this ->assertStringContainsString ( 'lang= ' , $ output );
194157 $ this ->assertStringContainsString ( 'dir= ' , $ output );
@@ -200,11 +163,9 @@ public function test_maybe_add_lang_and_dir_missing_attributes() {
200163 * @return void
201164 */
202165 public function test_maybe_add_lang_and_dir_existing_lang () {
203- $ fix = new HTMLLangAndDirFix ();
204-
205166 // Test with existing lang attribute.
206167 $ input = 'lang="en-US" ' ;
207- $ output = $ fix ->maybe_add_lang_and_dir ( $ input );
168+ $ output = $ this -> fix ->maybe_add_lang_and_dir ( $ input );
208169
209170 $ this ->assertStringContainsString ( 'lang="en-US" ' , $ output );
210171 $ this ->assertStringContainsString ( 'dir= ' , $ output );
@@ -218,11 +179,9 @@ public function test_maybe_add_lang_and_dir_existing_lang() {
218179 * @return void
219180 */
220181 public function test_maybe_add_lang_and_dir_existing_dir () {
221- $ fix = new HTMLLangAndDirFix ();
222-
223182 // Test with existing dir attribute.
224183 $ input = 'dir="ltr" ' ;
225- $ output = $ fix ->maybe_add_lang_and_dir ( $ input );
184+ $ output = $ this -> fix ->maybe_add_lang_and_dir ( $ input );
226185
227186 $ this ->assertStringContainsString ( 'dir="ltr" ' , $ output );
228187 $ this ->assertStringContainsString ( 'lang= ' , $ output );
@@ -236,11 +195,9 @@ public function test_maybe_add_lang_and_dir_existing_dir() {
236195 * @return void
237196 */
238197 public function test_maybe_add_lang_and_dir_both_exist () {
239- $ fix = new HTMLLangAndDirFix ();
240-
241198 // Test with both attributes existing.
242199 $ input = 'lang="en-US" dir="ltr" ' ;
243- $ output = $ fix ->maybe_add_lang_and_dir ( $ input );
200+ $ output = $ this -> fix ->maybe_add_lang_and_dir ( $ input );
244201
245202 // Should return unchanged.
246203 $ this ->assertEquals ( $ input , $ output );
@@ -252,9 +209,7 @@ public function test_maybe_add_lang_and_dir_both_exist() {
252209 * @return void
253210 */
254211 public function test_attribute_escaping () {
255- $ fix = new HTMLLangAndDirFix ();
256-
257- $ output = $ fix ->maybe_add_lang_and_dir ( '' );
212+ $ output = $ this ->fix ->maybe_add_lang_and_dir ( '' );
258213
259214 // Check that quotes are properly escaped in attributes.
260215 $ this ->assertStringContainsString ( 'lang=" ' , $ output );
@@ -269,9 +224,7 @@ public function test_attribute_escaping() {
269224 * @return void
270225 */
271226 public function test_direction_detection () {
272- $ fix = new HTMLLangAndDirFix ();
273-
274- $ output = $ fix ->maybe_add_lang_and_dir ( '' );
227+ $ output = $ this ->fix ->maybe_add_lang_and_dir ( '' );
275228
276229 // Since we're running in a default WordPress installation, dir should be 'ltr'.
277230 $ this ->assertStringContainsString ( 'dir="ltr" ' , $ output );
0 commit comments