|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests the empty_link rule. |
| 4 | + * |
| 5 | + * @package Accessibility_Checker |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * Some test cases for the empty_link rule. |
| 10 | + * |
| 11 | + * @group rules |
| 12 | + */ |
| 13 | +class EmptyLinkTest extends WP_UnitTestCase { |
| 14 | + |
| 15 | + /** |
| 16 | + * Test that a link with no content, no aria-label, no title, no id, no name, and no alt text throws an error. |
| 17 | + */ |
| 18 | + public function test_empty_link() { |
| 19 | + |
| 20 | + $expected_error = '<a href="http://example.com"></a>'; |
| 21 | + |
| 22 | + $not_expected_error = '<a href="http://example.com">Some content</a>'; |
| 23 | + |
| 24 | + |
| 25 | + $dom = new EDAC_Dom(); |
| 26 | + $dom->load( $expected_error . PHP_EOL . $not_expected_error ); |
| 27 | + |
| 28 | + $errors = edac_rule_empty_link( [ 'html' => $dom ], null ); |
| 29 | + |
| 30 | + $this->assertContains( $expected_error, $errors ); |
| 31 | + $this->assertNotContains( $not_expected_error, $errors ); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Test that a link with no content, no aria-label, no title, no id, no name, and no alt text throws an error. |
| 36 | + */ |
| 37 | + public function test_empty_link_with_img() { |
| 38 | + $expected_errors = [ |
| 39 | + '<a href="http://example.com"><img src="http://example.com/image.jpg" alt=""></a>', |
| 40 | + '<a href="http://example.com"><img src="http://example.com/image.jpg" alt=" "></a>', |
| 41 | + ]; |
| 42 | + |
| 43 | + $not_expected_error = '<a href="http://example.com"><img src="http://example.com/image.jpg" alt="A filled alt"></a>'; |
| 44 | + |
| 45 | + $dom = new EDAC_Dom(); |
| 46 | + $dom->load( implode( PHP_EOL, $expected_errors ) . PHP_EOL . $not_expected_error ); |
| 47 | + |
| 48 | + $errors = edac_rule_empty_link( [ 'html' => $dom ], null ); |
| 49 | + |
| 50 | + foreach ( $expected_errors as $expected_error ) { |
| 51 | + $this->assertContains( $expected_error, $errors ); |
| 52 | + } |
| 53 | + |
| 54 | + $this->assertNotContains( $not_expected_error, $errors ); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Test that a link with no content, no aria-label, no title, no id, no name, and no alt text throws an error. |
| 59 | + */ |
| 60 | + public function test_empty_link_with_input() { |
| 61 | + |
| 62 | + $expected_errors = [ |
| 63 | + '<a href="http://example.com"><input type="text"></a>', |
| 64 | + '<a href="http://example.com"><input type="text" value=""></a>', |
| 65 | + '<a href="http://example.com"><input type="text" value=" "></a>', // whitespace should be stripped, this is still empty. |
| 66 | + ]; |
| 67 | + |
| 68 | + $not_expected_error = '<a href="http://example.com"><input type="text" value="Some value"></a>'; |
| 69 | + |
| 70 | + $dom = new EDAC_Dom(); |
| 71 | + $dom->load( implode( PHP_EOL, $expected_errors ) . PHP_EOL . $not_expected_error ); |
| 72 | + |
| 73 | + $errors = edac_rule_empty_link( [ 'html' => $dom ], null ); |
| 74 | + |
| 75 | + foreach ( $expected_errors as $expected_error ) { |
| 76 | + $this->assertContains( $expected_error, $errors ); |
| 77 | + } |
| 78 | + $this->assertNotContains( $not_expected_error, $errors ); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Test that a link with no content, no aria-label, no title, no id, no name, and no alt text throws an error. |
| 83 | + */ |
| 84 | + public function test_empty_link_with_i() { |
| 85 | + $expected_errors = [ |
| 86 | + '<a href="http://example.com"><i></i></a>', |
| 87 | + '<a href="http://example.com"><i title=""></i></a>', |
| 88 | + '<a href="http://example.com"><i aria-label=""></i></a>', |
| 89 | + '<a href="http://example.com"><i title=" "></i></a>', // whitespace should be stripped, this is still empty. |
| 90 | + '<a href="http://example.com"><i aria-label=" "></i></a>', // whitespace should be stripped, this is still empty. |
| 91 | + ]; |
| 92 | + |
| 93 | + $not_expected_errors = [ |
| 94 | + '<a href="http://example.com"><i title="Some title"></i></a>', |
| 95 | + '<a href="http://example.com"><i aria-label="A label"></i></a>', |
| 96 | + '<a href="http://example.com"><i title="Some title" aria-label="A label"></i></a>', |
| 97 | + ]; |
| 98 | + |
| 99 | + $dom = new EDAC_Dom(); |
| 100 | + $dom->load( implode( PHP_EOL, $expected_errors ) . PHP_EOL . implode( PHP_EOL, $not_expected_errors ) ); |
| 101 | + |
| 102 | + $errors = edac_rule_empty_link( [ 'html' => $dom ], null ); |
| 103 | + |
| 104 | + foreach ( $expected_errors as $expected_error ) { |
| 105 | + $this->assertContains( $expected_error, $errors ); |
| 106 | + } |
| 107 | + foreach ( $not_expected_errors as $not_expected_error ) { |
| 108 | + $this->assertNotContains( $not_expected_error, $errors ); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments