| | 1 | <?php |
| | 2 | /** |
| | 3 | * @group formatting |
| | 4 | * @group color |
| | 5 | */ |
| | 6 | class Tests_Formatting_Color extends WP_UnitTestCase { |
| | 7 | /** |
| | 8 | * @ticket 27583 |
| | 9 | */ |
| | 10 | function test_valid_colors_pass_validation() { |
| | 11 | // Numeric 3 digit |
| | 12 | $color = '#333'; |
| | 13 | $this->assertTrue( is_hex_color( $color ) ); |
| | 14 | |
| | 15 | // Alphanumeric 3 character |
| | 16 | $color = '#f4A'; |
| | 17 | $this->assertTrue( is_hex_color( $color ) ); |
| | 18 | |
| | 19 | // Alpha 3 character |
| | 20 | $color = '#fff'; |
| | 21 | $this->assertTrue( is_hex_color( $color ) ); |
| | 22 | |
| | 23 | // Numeric 6 digit |
| | 24 | $color = '#333333'; |
| | 25 | $this->assertTrue( is_hex_color( $color ) ); |
| | 26 | |
| | 27 | // Alphanumeric 6 character |
| | 28 | $color = '#ff44AA'; |
| | 29 | $this->assertTrue( is_hex_color( $color ) ); |
| | 30 | |
| | 31 | // Alpha 6 character |
| | 32 | $color = '#ffffff'; |
| | 33 | $this->assertTrue( is_hex_color( $color ) ); |
| | 34 | } |
| | 35 | |
| | 36 | /** |
| | 37 | * @ticket 27583 |
| | 38 | */ |
| | 39 | function test_invalid_colors_do_not_pass() { |
| | 40 | // Too many characters |
| | 41 | $color = '#1234567'; |
| | 42 | $this->assertFalse( is_hex_color( $color ) ); |
| | 43 | |
| | 44 | // Too few characters |
| | 45 | $color = '#12'; |
| | 46 | $this->assertFalse( is_hex_color( $color ) ); |
| | 47 | |
| | 48 | // Missing # |
| | 49 | $color = 'ffffff'; |
| | 50 | $this->assertFalse( is_hex_color( $color ) ); |
| | 51 | |
| | 52 | // Invalid character |
| | 53 | $color = '#f9a%ad'; |
| | 54 | $this->assertFalse( is_hex_color( $color ) ); |
| | 55 | |
| | 56 | // Empty string |
| | 57 | $color = ''; |
| | 58 | $this->assertFalse( is_hex_color( $color ) ); |
| | 59 | |
| | 60 | // Whitespace |
| | 61 | $color = '#ff fff'; |
| | 62 | $this->assertFalse( is_hex_color( $color ) ); |
| | 63 | } |
| | 64 | |
| | 65 | function test_removal_of_hash_from_colors() { |
| | 66 | require_once ABSPATH . WPINC . '/class-wp-customize-manager.php'; |
| | 67 | |
| | 68 | // Numeric 3 digit |
| | 69 | $color = '#333'; |
| | 70 | $color_no_hash = '333'; |
| | 71 | $this->assertEquals( $color_no_hash, sanitize_hex_color_no_hash( $color ) ); |
| | 72 | |
| | 73 | // Alphanumeric 3 character |
| | 74 | $color = '#f4A'; |
| | 75 | $color_no_hash = 'f4A'; |
| | 76 | $this->assertEquals( $color_no_hash, sanitize_hex_color_no_hash( $color ) ); |
| | 77 | |
| | 78 | // Alpha 3 character |
| | 79 | $color = '#fff'; |
| | 80 | $color_no_hash = 'fff'; |
| | 81 | $this->assertEquals( $color_no_hash, sanitize_hex_color_no_hash( $color ) ); |
| | 82 | |
| | 83 | // Numeric 6 digit |
| | 84 | $color = '#333333'; |
| | 85 | $color_no_hash = '333333'; |
| | 86 | $this->assertEquals( $color_no_hash, sanitize_hex_color_no_hash( $color ) ); |
| | 87 | |
| | 88 | // Alphanumeric 6 character |
| | 89 | $color = '#ff44AA'; |
| | 90 | $color_no_hash = 'ff44AA'; |
| | 91 | $this->assertEquals( $color_no_hash, sanitize_hex_color_no_hash( $color ) ); |
| | 92 | |
| | 93 | // Alpha 6 character |
| | 94 | $color = '#ffffff'; |
| | 95 | $color_no_hash = 'ffffff'; |
| | 96 | $this->assertEquals( $color_no_hash, sanitize_hex_color_no_hash( $color ) ); |
| | 97 | } |
| | 98 | |
| | 99 | function test_prepend_hash_on_color_without_hash() { |
| | 100 | require_once ABSPATH . WPINC . '/class-wp-customize-manager.php'; |
| | 101 | |
| | 102 | // Numeric 3 digit |
| | 103 | $color = '#333'; |
| | 104 | $color_no_hash = '333'; |
| | 105 | $this->assertEquals( $color, maybe_hash_hex_color( $color_no_hash ) ); |
| | 106 | |
| | 107 | // Alphanumeric 3 character |
| | 108 | $color = '#f4A'; |
| | 109 | $color_no_hash = 'f4A'; |
| | 110 | $this->assertEquals( $color, maybe_hash_hex_color( $color_no_hash ) ); |
| | 111 | |
| | 112 | // Alpha 3 character |
| | 113 | $color = '#fff'; |
| | 114 | $color_no_hash = 'fff'; |
| | 115 | $this->assertEquals( $color, maybe_hash_hex_color( $color_no_hash ) ); |
| | 116 | |
| | 117 | // Numeric 6 digit |
| | 118 | $color = '#333333'; |
| | 119 | $color_no_hash = '333333'; |
| | 120 | $this->assertEquals( $color, maybe_hash_hex_color( $color_no_hash ) ); |
| | 121 | |
| | 122 | // Alphanumeric 6 character |
| | 123 | $color = '#ff44AA'; |
| | 124 | $color_no_hash = 'ff44AA'; |
| | 125 | $this->assertEquals( $color, maybe_hash_hex_color( $color_no_hash ) ); |
| | 126 | |
| | 127 | // Alpha 6 character |
| | 128 | $color = '#ffffff'; |
| | 129 | $color_no_hash = 'ffffff'; |
| | 130 | $this->assertEquals( $color, maybe_hash_hex_color( $color_no_hash ) ); |
| | 131 | } |
| | 132 | } |
| | 133 | No newline at end of file |