|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test class for AddFileSizeAndTypeToLinkedFilesFix. |
| 4 | + * |
| 5 | + * @package accessibility-checker |
| 6 | + */ |
| 7 | + |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use EqualizeDigital\AccessibilityChecker\Fixes\Fix\AddFileSizeAndTypeToLinkedFilesFix; |
| 10 | +use EqualizeDigital\AccessibilityChecker\Fixes\FixInterface; |
| 11 | + |
| 12 | +/** |
| 13 | + * Unit tests for the AddFileSizeAndTypeToLinkedFilesFix class. |
| 14 | + */ |
| 15 | +class AddFileSizeAndTypeToLinkedFilesFixTest extends WP_UnitTestCase { |
| 16 | + |
| 17 | + /** |
| 18 | + * Set up test environment. |
| 19 | + * |
| 20 | + * @return void |
| 21 | + */ |
| 22 | + public function set_up() { |
| 23 | + parent::set_up(); |
| 24 | + // Clean up any options that might interfere with tests. |
| 25 | + delete_option( 'edac_fix_add_file_size_and_type_to_linked_files' ); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Clean up after tests. |
| 30 | + * |
| 31 | + * @return void |
| 32 | + */ |
| 33 | + public function tear_down() { |
| 34 | + // Clean up options after each test. |
| 35 | + delete_option( 'edac_fix_add_file_size_and_type_to_linked_files' ); |
| 36 | + parent::tear_down(); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Test that AddFileSizeAndTypeToLinkedFilesFix implements FixInterface. |
| 41 | + * |
| 42 | + * @return void |
| 43 | + */ |
| 44 | + public function test_implements_fix_interface() { |
| 45 | + $fix = new AddFileSizeAndTypeToLinkedFilesFix(); |
| 46 | + $this->assertInstanceOf( FixInterface::class, $fix ); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Test get_slug returns correct slug. |
| 51 | + * |
| 52 | + * @return void |
| 53 | + */ |
| 54 | + public function test_get_slug() { |
| 55 | + $this->assertEquals( 'add_file_size_and_type_to_linked_files', AddFileSizeAndTypeToLinkedFilesFix::get_slug() ); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Test get_nicename returns translated string. |
| 60 | + * |
| 61 | + * @return void |
| 62 | + */ |
| 63 | + public function test_get_nicename() { |
| 64 | + $nicename = AddFileSizeAndTypeToLinkedFilesFix::get_nicename(); |
| 65 | + $this->assertIsString( $nicename ); |
| 66 | + $this->assertNotEmpty( $nicename ); |
| 67 | + $this->assertEquals( 'Add Size & Type To File Links', $nicename ); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Test get_fancyname returns translated string. |
| 72 | + * |
| 73 | + * @return void |
| 74 | + */ |
| 75 | + public function test_get_fancyname() { |
| 76 | + $fancyname = AddFileSizeAndTypeToLinkedFilesFix::get_fancyname(); |
| 77 | + $this->assertIsString( $fancyname ); |
| 78 | + $this->assertNotEmpty( $fancyname ); |
| 79 | + $this->assertEquals( 'Add Context to Linked Files', $fancyname ); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Test get_type returns none. |
| 84 | + * |
| 85 | + * @return void |
| 86 | + */ |
| 87 | + public function test_get_type() { |
| 88 | + $this->assertEquals( 'none', AddFileSizeAndTypeToLinkedFilesFix::get_type() ); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Test register method adds filter. |
| 93 | + * |
| 94 | + * @return void |
| 95 | + */ |
| 96 | + public function test_register_adds_filter() { |
| 97 | + $fix = new AddFileSizeAndTypeToLinkedFilesFix(); |
| 98 | + |
| 99 | + // Remove any existing filters to start clean. |
| 100 | + remove_all_filters( 'edac_filter_fixes_settings_fields' ); |
| 101 | + |
| 102 | + $fix->register(); |
| 103 | + |
| 104 | + $this->assertTrue( has_filter( 'edac_filter_fixes_settings_fields' ) ); |
| 105 | + $this->assertEquals( 10, has_filter( 'edac_filter_fixes_settings_fields', [ $fix, 'get_fields_array' ] ) ); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Test get_fields_array returns correct field structure. |
| 110 | + * |
| 111 | + * @return void |
| 112 | + */ |
| 113 | + public function test_get_fields_array_structure() { |
| 114 | + $fix = new AddFileSizeAndTypeToLinkedFilesFix(); |
| 115 | + $fields = $fix->get_fields_array(); |
| 116 | + |
| 117 | + $expected_key = 'edac_fix_add_file_size_and_type_to_linked_files'; |
| 118 | + $this->assertArrayHasKey( $expected_key, $fields ); |
| 119 | + |
| 120 | + $field = $fields[ $expected_key ]; |
| 121 | + $this->assertArrayHasKey( 'type', $field ); |
| 122 | + $this->assertArrayHasKey( 'label', $field ); |
| 123 | + $this->assertArrayHasKey( 'labelledby', $field ); |
| 124 | + $this->assertArrayHasKey( 'description', $field ); |
| 125 | + $this->assertArrayHasKey( 'upsell', $field ); |
| 126 | + $this->assertArrayHasKey( 'fix_slug', $field ); |
| 127 | + $this->assertArrayHasKey( 'help_id', $field ); |
| 128 | + |
| 129 | + $this->assertEquals( 'checkbox', $field['type'] ); |
| 130 | + $this->assertEquals( 'Add File Size & Type To Links', $field['label'] ); |
| 131 | + $this->assertEquals( 'add_file_size_and_type_to_linked_files', $field['labelledby'] ); |
| 132 | + $this->assertEquals( 'Add the file size and type to linked files that may trigger a download.', $field['description'] ); |
| 133 | + $this->assertEquals( 'add_file_size_and_type_to_linked_files', $field['fix_slug'] ); |
| 134 | + $this->assertEquals( 8492, $field['help_id'] ); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Test get_fields_array preserves existing fields. |
| 139 | + * |
| 140 | + * @return void |
| 141 | + */ |
| 142 | + public function test_get_fields_array_preserves_existing_fields() { |
| 143 | + $fix = new AddFileSizeAndTypeToLinkedFilesFix(); |
| 144 | + $existing_fields = [ |
| 145 | + 'existing_field' => [ |
| 146 | + 'type' => 'text', |
| 147 | + 'label' => 'Existing Field', |
| 148 | + ], |
| 149 | + ]; |
| 150 | + |
| 151 | + $fields = $fix->get_fields_array( $existing_fields ); |
| 152 | + |
| 153 | + // Should preserve existing field. |
| 154 | + $this->assertArrayHasKey( 'existing_field', $fields ); |
| 155 | + $this->assertEquals( 'text', $fields['existing_field']['type'] ); |
| 156 | + |
| 157 | + // Should add new field. |
| 158 | + $this->assertArrayHasKey( 'edac_fix_add_file_size_and_type_to_linked_files', $fields ); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Test upsell is enabled by default (free version). |
| 163 | + * |
| 164 | + * @return void |
| 165 | + */ |
| 166 | + public function test_upsell_enabled_by_default() { |
| 167 | + $fix = new AddFileSizeAndTypeToLinkedFilesFix(); |
| 168 | + $fields = $fix->get_fields_array(); |
| 169 | + |
| 170 | + $field = $fields['edac_fix_add_file_size_and_type_to_linked_files']; |
| 171 | + $this->assertTrue( $field['upsell'] ); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Test upsell disabled when is_pro is set. |
| 176 | + * |
| 177 | + * @return void |
| 178 | + */ |
| 179 | + public function test_upsell_disabled_when_pro() { |
| 180 | + $fix = new AddFileSizeAndTypeToLinkedFilesFix(); |
| 181 | + |
| 182 | + // Set is_pro property dynamically. |
| 183 | + $fix->is_pro = true; |
| 184 | + |
| 185 | + $fields = $fix->get_fields_array(); |
| 186 | + |
| 187 | + $field = $fields['edac_fix_add_file_size_and_type_to_linked_files']; |
| 188 | + $this->assertFalse( $field['upsell'] ); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Test run method does nothing (intentionally empty). |
| 193 | + * |
| 194 | + * @return void |
| 195 | + */ |
| 196 | + public function test_run_method_is_empty() { |
| 197 | + $fix = new AddFileSizeAndTypeToLinkedFilesFix(); |
| 198 | + |
| 199 | + // Should not throw any errors and should complete successfully. |
| 200 | + $this->assertNull( $fix->run() ); |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Test field label is properly escaped. |
| 205 | + * |
| 206 | + * @return void |
| 207 | + */ |
| 208 | + public function test_field_label_is_escaped() { |
| 209 | + $fix = new AddFileSizeAndTypeToLinkedFilesFix(); |
| 210 | + $fields = $fix->get_fields_array(); |
| 211 | + |
| 212 | + $field = $fields['edac_fix_add_file_size_and_type_to_linked_files']; |
| 213 | + |
| 214 | + // The label should be a string and not contain unescaped HTML. |
| 215 | + $this->assertIsString( $field['label'] ); |
| 216 | + $this->assertEquals( 'Add File Size & Type To Links', $field['label'] ); |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Test field description is properly escaped. |
| 221 | + * |
| 222 | + * @return void |
| 223 | + */ |
| 224 | + public function test_field_description_is_escaped() { |
| 225 | + $fix = new AddFileSizeAndTypeToLinkedFilesFix(); |
| 226 | + $fields = $fix->get_fields_array(); |
| 227 | + |
| 228 | + $field = $fields['edac_fix_add_file_size_and_type_to_linked_files']; |
| 229 | + |
| 230 | + // The description should be a string and not contain unescaped HTML. |
| 231 | + $this->assertIsString( $field['description'] ); |
| 232 | + $this->assertEquals( 'Add the file size and type to linked files that may trigger a download.', $field['description'] ); |
| 233 | + } |
| 234 | + |
| 235 | + /** |
| 236 | + * Test that help_id is a positive integer. |
| 237 | + * |
| 238 | + * @return void |
| 239 | + */ |
| 240 | + public function test_help_id_is_positive_integer() { |
| 241 | + $fix = new AddFileSizeAndTypeToLinkedFilesFix(); |
| 242 | + $fields = $fix->get_fields_array(); |
| 243 | + |
| 244 | + $field = $fields['edac_fix_add_file_size_and_type_to_linked_files']; |
| 245 | + |
| 246 | + $this->assertIsInt( $field['help_id'] ); |
| 247 | + $this->assertGreaterThan( 0, $field['help_id'] ); |
| 248 | + $this->assertEquals( 8492, $field['help_id'] ); |
| 249 | + } |
| 250 | + |
| 251 | + /** |
| 252 | + * Test that fix_slug matches the class slug. |
| 253 | + * |
| 254 | + * @return void |
| 255 | + */ |
| 256 | + public function test_fix_slug_matches_class_slug() { |
| 257 | + $fix = new AddFileSizeAndTypeToLinkedFilesFix(); |
| 258 | + $fields = $fix->get_fields_array(); |
| 259 | + |
| 260 | + $field = $fields['edac_fix_add_file_size_and_type_to_linked_files']; |
| 261 | + |
| 262 | + $this->assertEquals( AddFileSizeAndTypeToLinkedFilesFix::get_slug(), $field['fix_slug'] ); |
| 263 | + } |
| 264 | + |
| 265 | + /** |
| 266 | + * Test that field key follows expected pattern. |
| 267 | + * |
| 268 | + * @return void |
| 269 | + */ |
| 270 | + public function test_field_key_follows_pattern() { |
| 271 | + $fix = new AddFileSizeAndTypeToLinkedFilesFix(); |
| 272 | + $fields = $fix->get_fields_array(); |
| 273 | + |
| 274 | + $expected_key = 'edac_fix_' . AddFileSizeAndTypeToLinkedFilesFix::get_slug(); |
| 275 | + $this->assertArrayHasKey( $expected_key, $fields ); |
| 276 | + } |
| 277 | + |
| 278 | + /** |
| 279 | + * Test labelledby field is correctly set. |
| 280 | + * |
| 281 | + * @return void |
| 282 | + */ |
| 283 | + public function test_labelledby_field() { |
| 284 | + $fix = new AddFileSizeAndTypeToLinkedFilesFix(); |
| 285 | + $fields = $fix->get_fields_array(); |
| 286 | + |
| 287 | + $field = $fields['edac_fix_add_file_size_and_type_to_linked_files']; |
| 288 | + |
| 289 | + $this->assertEquals( 'add_file_size_and_type_to_linked_files', $field['labelledby'] ); |
| 290 | + } |
| 291 | +} |
0 commit comments