| | 102 | |
| | 103 | /** |
| | 104 | * @ticket 22192 |
| | 105 | */ |
| | 106 | public function test_add_option_with_value_of_false_should_store_false_in_the_cache() { |
| | 107 | add_option( 'foo', false ); |
| | 108 | $a = wp_cache_get( 'alloptions', 'options' ); |
| | 109 | $this->assertSame( false, $a['foo'] ); |
| | 110 | } |
| | 111 | |
| | 112 | /** |
| | 113 | * @ticket 22192 |
| | 114 | * @dataProvider data_update_option_type_juggling |
| | 115 | */ |
| | 116 | public function test_update_option_should_hit_cache_when_loosely_equal_to_existing_value_and_cached_values_are_faithful_to_original_type( $old_value, $new_value ) { |
| | 117 | global $wpdb; |
| | 118 | |
| | 119 | add_option( 'foo', $old_value ); |
| | 120 | $num_queries = $wpdb->num_queries; |
| | 121 | |
| | 122 | $updated = update_option( 'foo', $new_value ); |
| | 123 | |
| | 124 | $this->assertFalse( $updated ); |
| | 125 | $this->assertSame( $num_queries, $wpdb->num_queries ); |
| | 126 | } |
| | 127 | |
| | 128 | /** |
| | 129 | * @ticket 22192 |
| | 130 | * @dataProvider data_update_option_type_juggling |
| | 131 | */ |
| | 132 | public function test_update_option_should_hit_cache_when_loosely_equal_to_existing_value_and_cached_values_are_pulled_from_the_database( $old_value, $new_value ) { |
| | 133 | global $wpdb; |
| | 134 | |
| | 135 | add_option( 'foo', $old_value ); |
| | 136 | wp_cache_delete( 'alloptions', 'options' ); |
| | 137 | wp_load_alloptions(); |
| | 138 | |
| | 139 | $num_queries = $wpdb->num_queries; |
| | 140 | |
| | 141 | $updated = update_option( 'foo', $new_value ); |
| | 142 | |
| | 143 | $this->assertFalse( $updated ); |
| | 144 | $this->assertSame( $num_queries, $wpdb->num_queries ); |
| | 145 | } |
| | 146 | |
| | 147 | public function data_update_option_type_juggling() { |
| | 148 | return array( |
| | 149 | // Truthy |
| | 150 | array( '1', '1' ), |
| | 151 | array( '1', intval( 1 ) ), |
| | 152 | array( '1', floatval( 1 ) ), |
| | 153 | array( '1', true ), |
| | 154 | array( 1, '1' ), |
| | 155 | array( 1, intval( 1 ) ), |
| | 156 | array( 1, floatval( 1 ) ), |
| | 157 | array( 1, true ), |
| | 158 | array( floatval( 1 ), '1' ), |
| | 159 | array( floatval( 1 ), intval( 1 ) ), |
| | 160 | array( floatval( 1 ), floatval( 1 ) ), |
| | 161 | array( floatval( 1 ), true ), |
| | 162 | array( true, '1' ), |
| | 163 | array( true, intval( 1 ) ), |
| | 164 | array( true, floatval( 1 ) ), |
| | 165 | array( true, true ), |
| | 166 | |
| | 167 | // Falsey |
| | 168 | array( '0', '0' ), |
| | 169 | array( '0', intval( 0 ) ), |
| | 170 | array( '0', floatval( 0 ) ), |
| | 171 | array( '0', false ), |
| | 172 | array( 0, '0' ), |
| | 173 | array( 0, intval( 0 ) ), |
| | 174 | array( 0, floatval( 0 ) ), |
| | 175 | array( 0, false ), |
| | 176 | array( floatval( 0 ), '0' ), |
| | 177 | array( floatval( 0 ), intval( 0 ) ), |
| | 178 | array( floatval( 0 ), floatval( 0 ) ), |
| | 179 | array( floatval( 0 ), false ), |
| | 180 | array( false, '0' ), |
| | 181 | array( false, intval( 0 ) ), |
| | 182 | array( false, floatval( 0 ) ), |
| | 183 | array( false, false ), |
| | 184 | ); |
| | 185 | } |