|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for AddLabelToUnlabelledFormFieldsFix class |
| 4 | + * |
| 5 | + * @package AccessibilityChecker |
| 6 | + */ |
| 7 | + |
| 8 | +namespace EqualizeDigital\AccessibilityChecker\Tests\Fixes\Fix; |
| 9 | + |
| 10 | +use EqualizeDigital\AccessibilityChecker\Fixes\Fix\AddLabelToUnlabelledFormFieldsFix; |
| 11 | +use EqualizeDigital\AccessibilityChecker\Fixes\FixInterface; |
| 12 | +use WP_UnitTestCase; |
| 13 | + |
| 14 | +/** |
| 15 | + * AddLabelToUnlabelledFormFieldsFix test case |
| 16 | + */ |
| 17 | +class AddLabelToUnlabelledFormFieldsFixTest extends WP_UnitTestCase { |
| 18 | + |
| 19 | + /** |
| 20 | + * Test fix instance |
| 21 | + * |
| 22 | + * @var AddLabelToUnlabelledFormFieldsFix |
| 23 | + */ |
| 24 | + private $fix; |
| 25 | + |
| 26 | + /** |
| 27 | + * Set up test fixtures |
| 28 | + */ |
| 29 | + public function setUp(): void { |
| 30 | + parent::setUp(); |
| 31 | + $this->fix = new AddLabelToUnlabelledFormFieldsFix(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Clean up after tests |
| 36 | + */ |
| 37 | + public function tearDown(): void { |
| 38 | + // Clean up options. |
| 39 | + delete_option( 'edac_fix_add_label_to_unlabelled_form_fields' ); |
| 40 | + |
| 41 | + parent::tearDown(); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Test that the fix implements FixInterface |
| 46 | + */ |
| 47 | + public function test_implements_fix_interface() { |
| 48 | + $this->assertInstanceOf( FixInterface::class, $this->fix ); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Test get_slug method |
| 53 | + */ |
| 54 | + public function test_get_slug() { |
| 55 | + $this->assertEquals( 'add_label_to_unlabelled_form_fields', AddLabelToUnlabelledFormFieldsFix::get_slug() ); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Test get_nicename method |
| 60 | + */ |
| 61 | + public function test_get_nicename() { |
| 62 | + $this->assertEquals( 'Add Labels to Unlabelled Form Fields', AddLabelToUnlabelledFormFieldsFix::get_nicename() ); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Test get_fancyname method |
| 67 | + */ |
| 68 | + public function test_get_fancyname() { |
| 69 | + $this->assertEquals( 'Label Form Fields', AddLabelToUnlabelledFormFieldsFix::get_fancyname() ); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Test get_type method |
| 74 | + */ |
| 75 | + public function test_get_type() { |
| 76 | + $this->assertEquals( 'none', AddLabelToUnlabelledFormFieldsFix::get_type() ); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Test register method adds settings sections |
| 81 | + */ |
| 82 | + public function test_register_adds_settings_sections() { |
| 83 | + $sections = []; |
| 84 | + $this->fix->register(); |
| 85 | + |
| 86 | + $sections = apply_filters( 'edac_filter_fixes_settings_sections', $sections ); |
| 87 | + |
| 88 | + $this->assertArrayHasKey( 'add_label_to_unlabelled_form_fields', $sections ); |
| 89 | + $this->assertEquals( 'Label Form Fields', $sections['add_label_to_unlabelled_form_fields']['title'] ); |
| 90 | + $this->assertEquals( [ $this->fix, 'add_label_to_unlabelled_form_fields_section_callback' ], $sections['add_label_to_unlabelled_form_fields']['callback'] ); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Test register method adds settings fields filter |
| 95 | + */ |
| 96 | + public function test_register_adds_settings_fields_filter() { |
| 97 | + $this->fix->register(); |
| 98 | + |
| 99 | + $this->assertNotFalse( has_filter( 'edac_filter_fixes_settings_fields', [ $this->fix, 'get_fields_array' ] ) ); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Test get_fields_array method returns correct field for free version |
| 104 | + */ |
| 105 | + public function test_get_fields_array_free_version() { |
| 106 | + $fields = $this->fix->get_fields_array(); |
| 107 | + |
| 108 | + $this->assertArrayHasKey( 'edac_fix_add_label_to_unlabelled_form_fields', $fields ); |
| 109 | + $field = $fields['edac_fix_add_label_to_unlabelled_form_fields']; |
| 110 | + |
| 111 | + $this->assertEquals( 'Label Form Fields', $field['label'] ); |
| 112 | + $this->assertEquals( 'checkbox', $field['type'] ); |
| 113 | + $this->assertEquals( 'add_label_to_unlabelled_form_fields', $field['labelledby'] ); |
| 114 | + $this->assertEquals( 'Add labels to unlabelled form fields if field purpose can be determined.', $field['description'] ); |
| 115 | + $this->assertEquals( 'add_label_to_unlabelled_form_fields', $field['section'] ); |
| 116 | + $this->assertTrue( $field['upsell'] ); // Should be true for free version. |
| 117 | + $this->assertEquals( 'Add Labels to Unlabelled Form Fields', $field['group_name'] ); |
| 118 | + $this->assertEquals( 'Label Form Fields', $field['fancy_name'] ); |
| 119 | + $this->assertEquals( 'add_label_to_unlabelled_form_fields', $field['fix_slug'] ); |
| 120 | + $this->assertEquals( 8497, $field['help_id'] ); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Test get_fields_array method for pro version |
| 125 | + */ |
| 126 | + public function test_get_fields_array_pro_version() { |
| 127 | + // Create anonymous class extending the fix to simulate pro version. |
| 128 | + $pro_fix = new class() extends AddLabelToUnlabelledFormFieldsFix { |
| 129 | + public $is_pro = true; |
| 130 | + }; |
| 131 | + |
| 132 | + $fields = $pro_fix->get_fields_array(); |
| 133 | + |
| 134 | + $this->assertArrayHasKey( 'edac_fix_add_label_to_unlabelled_form_fields', $fields ); |
| 135 | + $field = $fields['edac_fix_add_label_to_unlabelled_form_fields']; |
| 136 | + |
| 137 | + $this->assertFalse( $field['upsell'] ); // Should be false for pro version. |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Test get_fields_array preserves existing fields |
| 142 | + */ |
| 143 | + public function test_get_fields_array_preserves_existing_fields() { |
| 144 | + $existing_fields = [ |
| 145 | + 'existing_field' => [ |
| 146 | + 'type' => 'text', |
| 147 | + 'label' => 'Existing Field', |
| 148 | + ], |
| 149 | + ]; |
| 150 | + |
| 151 | + $fields = $this->fix->get_fields_array( $existing_fields ); |
| 152 | + |
| 153 | + $this->assertArrayHasKey( 'existing_field', $fields ); |
| 154 | + $this->assertArrayHasKey( 'edac_fix_add_label_to_unlabelled_form_fields', $fields ); |
| 155 | + $this->assertEquals( 'text', $fields['existing_field']['type'] ); |
| 156 | + $this->assertEquals( 'Existing Field', $fields['existing_field']['label'] ); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Test run method does nothing (intentionally empty) |
| 161 | + */ |
| 162 | + public function test_run_does_nothing() { |
| 163 | + // The run method is intentionally left empty for this fix. |
| 164 | + $this->assertNull( $this->fix->run() ); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Test add_label_to_unlabelled_form_fields_section_callback method |
| 169 | + */ |
| 170 | + public function test_section_callback_outputs_description() { |
| 171 | + ob_start(); |
| 172 | + $this->fix->add_label_to_unlabelled_form_fields_section_callback(); |
| 173 | + $output = ob_get_clean(); |
| 174 | + |
| 175 | + $this->assertStringContainsString( '<p>', $output ); |
| 176 | + $this->assertStringContainsString( 'Attempt to add labels to form fields that are missing them.', $output ); |
| 177 | + $this->assertStringContainsString( '<code>.edac-generated-label</code>', $output ); |
| 178 | + $this->assertStringContainsString( 'Note: You may need to add custom styles', $output ); |
| 179 | + $this->assertStringContainsString( '</p>', $output ); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Test field structure for upsell functionality |
| 184 | + */ |
| 185 | + public function test_field_structure_upsell_functionality() { |
| 186 | + $fields = $this->fix->get_fields_array(); |
| 187 | + $field = $fields['edac_fix_add_label_to_unlabelled_form_fields']; |
| 188 | + |
| 189 | + // Test required field properties for upsell. |
| 190 | + $this->assertArrayHasKey( 'upsell', $field ); |
| 191 | + $this->assertArrayHasKey( 'group_name', $field ); |
| 192 | + $this->assertArrayHasKey( 'fancy_name', $field ); |
| 193 | + |
| 194 | + // Test that upsell is boolean. |
| 195 | + $this->assertIsBool( $field['upsell'] ); |
| 196 | + |
| 197 | + // Test that group_name and fancy_name are strings. |
| 198 | + $this->assertIsString( $field['group_name'] ); |
| 199 | + $this->assertIsString( $field['fancy_name'] ); |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * Test pro property handling |
| 204 | + */ |
| 205 | + public function test_pro_property_handling() { |
| 206 | + // Test that the property doesn't exist by default (free version). |
| 207 | + $this->assertFalse( isset( $this->fix->is_pro ) ); |
| 208 | + |
| 209 | + // Test that upsell is true when is_pro property doesn't exist. |
| 210 | + $fields = $this->fix->get_fields_array(); |
| 211 | + $field = $fields['edac_fix_add_label_to_unlabelled_form_fields']; |
| 212 | + $this->assertTrue( $field['upsell'] ); |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * Test dynamic property assignment for pro version |
| 217 | + */ |
| 218 | + public function test_dynamic_property_assignment_pro_version() { |
| 219 | + // Dynamically set is_pro property. |
| 220 | + $this->fix->is_pro = true; |
| 221 | + |
| 222 | + $fields = $this->fix->get_fields_array(); |
| 223 | + $field = $fields['edac_fix_add_label_to_unlabelled_form_fields']; |
| 224 | + |
| 225 | + $this->assertFalse( $field['upsell'] ); |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * Test dynamic property assignment for false pro value |
| 230 | + */ |
| 231 | + public function test_dynamic_property_assignment_false_pro_value() { |
| 232 | + // Set is_pro property to false explicitly. |
| 233 | + $this->fix->is_pro = false; |
| 234 | + |
| 235 | + $fields = $this->fix->get_fields_array(); |
| 236 | + $field = $fields['edac_fix_add_label_to_unlabelled_form_fields']; |
| 237 | + |
| 238 | + $this->assertTrue( $field['upsell'] ); |
| 239 | + } |
| 240 | + |
| 241 | + /** |
| 242 | + * Test field structure matches expected pattern |
| 243 | + */ |
| 244 | + public function test_field_structure_matches_pattern() { |
| 245 | + $fields = $this->fix->get_fields_array(); |
| 246 | + $field = $fields['edac_fix_add_label_to_unlabelled_form_fields']; |
| 247 | + |
| 248 | + // Test required field properties. |
| 249 | + $required_properties = [ |
| 250 | + 'label', |
| 251 | + 'type', |
| 252 | + 'labelledby', |
| 253 | + 'description', |
| 254 | + 'section', |
| 255 | + 'upsell', |
| 256 | + 'group_name', |
| 257 | + 'fancy_name', |
| 258 | + 'fix_slug', |
| 259 | + 'help_id', |
| 260 | + ]; |
| 261 | + |
| 262 | + foreach ( $required_properties as $property ) { |
| 263 | + $this->assertArrayHasKey( $property, $field, "Field missing required property: {$property}" ); |
| 264 | + } |
| 265 | + |
| 266 | + // Test field values are appropriate types. |
| 267 | + $this->assertIsString( $field['label'] ); |
| 268 | + $this->assertNotEmpty( $field['label'] ); |
| 269 | + $this->assertIsString( $field['type'] ); |
| 270 | + $this->assertNotEmpty( $field['type'] ); |
| 271 | + $this->assertIsBool( $field['upsell'] ); |
| 272 | + $this->assertIsInt( $field['help_id'] ); |
| 273 | + $this->assertGreaterThan( 0, $field['help_id'] ); |
| 274 | + } |
| 275 | + |
| 276 | + /** |
| 277 | + * Test section callback includes proper CSS class reference |
| 278 | + */ |
| 279 | + public function test_section_callback_includes_css_class_reference() { |
| 280 | + ob_start(); |
| 281 | + $this->fix->add_label_to_unlabelled_form_fields_section_callback(); |
| 282 | + $output = ob_get_clean(); |
| 283 | + |
| 284 | + $this->assertStringContainsString( '.edac-generated-label', $output ); |
| 285 | + $this->assertStringContainsString( '<code>', $output ); |
| 286 | + $this->assertStringContainsString( '</code>', $output ); |
| 287 | + } |
| 288 | + |
| 289 | + /** |
| 290 | + * Test that class has proper inheritance structure |
| 291 | + */ |
| 292 | + public function test_class_inheritance_structure() { |
| 293 | + // Verify the class implements the correct interface. |
| 294 | + $interfaces = class_implements( $this->fix ); |
| 295 | + $this->assertContains( FixInterface::class, $interfaces ); |
| 296 | + |
| 297 | + // Verify the class has required methods from interface. |
| 298 | + $this->assertTrue( method_exists( $this->fix, 'get_slug' ) ); |
| 299 | + $this->assertTrue( method_exists( $this->fix, 'get_nicename' ) ); |
| 300 | + $this->assertTrue( method_exists( $this->fix, 'get_fancyname' ) ); |
| 301 | + $this->assertTrue( method_exists( $this->fix, 'get_type' ) ); |
| 302 | + $this->assertTrue( method_exists( $this->fix, 'register' ) ); |
| 303 | + $this->assertTrue( method_exists( $this->fix, 'run' ) ); |
| 304 | + } |
| 305 | + |
| 306 | + /** |
| 307 | + * Test get_type returns 'none' indicating no frontend implementation |
| 308 | + */ |
| 309 | + public function test_get_type_indicates_no_frontend_implementation() { |
| 310 | + $this->assertEquals( 'none', $this->fix->get_type() ); |
| 311 | + } |
| 312 | + |
| 313 | + /** |
| 314 | + * Test that run method is intentionally empty |
| 315 | + */ |
| 316 | + public function test_run_method_intentionally_empty() { |
| 317 | + // Use reflection to check if run method body is empty. |
| 318 | + $reflection = new \ReflectionMethod( $this->fix, 'run' ); |
| 319 | + $start_line = $reflection->getStartLine(); |
| 320 | + $end_line = $reflection->getEndLine(); |
| 321 | + $length = $end_line - $start_line; |
| 322 | + |
| 323 | + // The method should be very short (just opening/closing braces and comment). |
| 324 | + $this->assertLessThan( 5, $length ); |
| 325 | + } |
| 326 | + |
| 327 | + /** |
| 328 | + * Test integration with WordPress hooks system |
| 329 | + */ |
| 330 | + public function test_integration_with_wordpress_hooks() { |
| 331 | + // Test that register method properly sets up hooks. |
| 332 | + $this->fix->register(); |
| 333 | + |
| 334 | + // Verify section filter is registered. |
| 335 | + $this->assertTrue( has_filter( 'edac_filter_fixes_settings_sections' ) ); |
| 336 | + |
| 337 | + // Verify fields filter is registered. |
| 338 | + $this->assertTrue( has_filter( 'edac_filter_fixes_settings_fields' ) ); |
| 339 | + } |
| 340 | + |
| 341 | + /** |
| 342 | + * Test section dynamic method name construction |
| 343 | + */ |
| 344 | + public function test_section_dynamic_method_name_construction() { |
| 345 | + $sections = []; |
| 346 | + $this->fix->register(); |
| 347 | + |
| 348 | + $sections = apply_filters( 'edac_filter_fixes_settings_sections', $sections ); |
| 349 | + |
| 350 | + // The callback should use the slug-based method name. |
| 351 | + $expected_callback = [ $this->fix, 'add_label_to_unlabelled_form_fields_section_callback' ]; |
| 352 | + $this->assertEquals( $expected_callback, $sections['add_label_to_unlabelled_form_fields']['callback'] ); |
| 353 | + } |
| 354 | +} |
0 commit comments