- Timestamp:
- 10/30/2023 11:21:03 PM (14 months ago)
- Location:
- branches/6.4
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/6.4
-
branches/6.4/tests/phpunit/tests/option/wpPrimeOptionCaches.php
r57016 r57030 42 42 foreach ( $options_to_prime as $option ) { 43 43 $this->assertSame( 44 "value_$option", 44 45 wp_cache_get( $option, 'options' ), 45 get_option( $option ),46 46 "$option was not primed in the 'options' cache group." 47 47 ); 48 48 49 $this->assertFalse( 50 wp_cache_get( $option, 'notoptions' ), 51 get_option( $option ), 52 "$option was primed in the 'notoptions' cache group." 49 $new_notoptions = wp_cache_get( $option, 'notoptions' ); 50 if ( ! is_array( $new_notoptions ) ) { 51 $new_notoptions = array(); 52 } 53 $this->assertArrayNotHasKey( 54 $option, 55 $new_notoptions, 56 "$option was primed in the 'notoptions' cache." 53 57 ); 54 58 } … … 63 67 64 68 /** 69 * Tests that wp_prime_option_caches() handles a mix of primed and unprimed options. 70 * 71 * @ticket 58962 72 */ 73 public function test_wp_prime_option_caches_handles_a_mix_of_primed_and_unprimed_options() { 74 global $wpdb; 75 // Create some options to prime. 76 $options_to_prime = array( 77 'option1', 78 'option2', 79 'option3', 80 ); 81 82 /* 83 * Set values for the options, 84 * clear the cache for the options, 85 * check options are not in cache initially. 86 */ 87 foreach ( $options_to_prime as $option ) { 88 update_option( $option, "value_$option", false ); 89 wp_cache_delete( $option, 'options' ); 90 $this->assertFalse( wp_cache_get( $option, 'options' ), "$option was not deleted from the cache." ); 91 } 92 93 // Add non-existent option to the options to prime. 94 $options_to_prime[] = 'option404notfound'; 95 96 // Prime the first option with a non-existent option. 97 wp_prime_option_caches( array( 'option1', 'option404notfound' ) ); 98 99 // Store the initial database query count. 100 $initial_query_count = get_num_queries(); 101 102 // Prime all the options, including the pre-primed option. 103 wp_prime_option_caches( $options_to_prime ); 104 105 // Ensure an additional database query was made. 106 $this->assertSame( 107 1, 108 get_num_queries() - $initial_query_count, 109 'Additional database queries were not made.' 110 ); 111 112 // Ensure the last query does not contain the pre-primed option. 113 $this->assertStringNotContainsString( 114 "\'option1\'", 115 $wpdb->last_query, 116 'The last query should not contain the pre-primed option.' 117 ); 118 119 // Ensure the last query does not contain the pre-primed notoption. 120 $this->assertStringNotContainsString( 121 "\'option404notfound\'", 122 $wpdb->last_query, 123 'The last query should not contain the pre-primed non-existent option.' 124 ); 125 } 126 127 /** 65 128 * Tests wp_prime_option_caches() with options that do not exist in the database. 66 129 * 67 130 * @ticket 58962 131 * @ticket 59738 68 132 */ 69 133 public function test_wp_prime_option_caches_with_nonexistent_options() { … … 97 161 $this->assertArrayHasKey( $option, $new_notoptions, "$option was not added to the notoptions cache." ); 98 162 } 163 164 // Check getting and re-priming the options does not result in additional database queries. 165 $initial_query_count = get_num_queries(); 166 foreach ( $options_to_prime as $option ) { 167 get_option( $option ); 168 $this->assertSame( 169 0, 170 get_num_queries() - $initial_query_count, 171 "Additional database queries were made getting option $option." 172 ); 173 } 174 175 wp_prime_option_caches( $options_to_prime ); 176 $this->assertSame( 177 0, 178 get_num_queries() - $initial_query_count, 179 'Additional database queries were made re-priming the options.' 180 ); 99 181 } 100 182 … … 103 185 * 104 186 * @ticket 58962 187 * @ticket 59738 105 188 */ 106 189 public function test_wp_prime_option_caches_with_empty_array() { … … 108 191 $notoptions = wp_cache_get( 'notoptions', 'options' ); 109 192 193 $initial_query_count = get_num_queries(); 110 194 wp_prime_option_caches( array() ); 111 195 112 196 $this->assertSame( $alloptions, wp_cache_get( 'alloptions', 'options' ), 'The alloptions cache was modified.' ); 113 197 $this->assertSame( $notoptions, wp_cache_get( 'notoptions', 'options' ), 'The notoptions cache was modified.' ); 198 199 // Check priming an empty array does not result in additional database queries. 200 $this->assertSame( 201 0, 202 get_num_queries() - $initial_query_count, 203 'Additional database queries were made.' 204 ); 114 205 } 115 206 … … 118 209 * 119 210 * @ticket 58962 211 * @ticket 59738 120 212 */ 121 213 public function test_wp_prime_option_caches_handles_empty_notoptions_cache() { … … 127 219 $this->assertIsArray( $notoptions, 'The notoptions cache should be an array.' ); 128 220 $this->assertArrayHasKey( 'nonexistent_option', $notoptions, 'nonexistent_option was not added to notoptions.' ); 221 222 // Check getting and re-priming the options does not result in additional database queries. 223 $initial_query_count = get_num_queries(); 224 225 get_option( 'nonexistent_option' ); 226 $this->assertSame( 227 0, 228 get_num_queries() - $initial_query_count, 229 'Additional database queries were made getting nonexistent_option.' 230 ); 231 232 wp_prime_option_caches( array( 'nonexistent_option' ) ); 233 $this->assertSame( 234 0, 235 get_num_queries() - $initial_query_count, 236 'Additional database queries were made.' 237 ); 238 } 239 240 /** 241 * Test options primed by the wp_prime_option_caches() function are identical to those primed by get_option(). 242 * 243 * @ticket 59738 244 * 245 * @dataProvider data_option_types 246 * 247 * @param mixed $option_value An option value. 248 */ 249 public function test_get_option_should_return_identical_value_when_pre_primed_by_wp_prime_option_caches( $option_value ) { 250 // As this includes a test setting the value to `(bool) false`, update_option() can not be used so add_option() is used instead. 251 add_option( 'type_of_option', $option_value, '', false ); 252 wp_cache_delete( 'type_of_option', 'options' ); 253 254 $this->assertFalse( wp_cache_get( 'type_of_option', 'options' ), 'type_of_option was not deleted from the cache for priming.' ); 255 256 // Call the wp_prime_option_caches function to prime the options. 257 wp_prime_option_caches( array( 'type_of_option' ) ); 258 $value_after_pre_priming = get_option( 'type_of_option' ); 259 260 // Clear the cache and call get_option directly. 261 wp_cache_delete( 'type_of_option', 'options' ); 262 $this->assertFalse( wp_cache_get( 'type_of_option', 'options' ), 'type_of_option was not deleted from the cache for get_option.' ); 263 $value_after_get_option = get_option( 'type_of_option' ); 264 265 /* 266 * If the option value is an object, use assertEquals() to compare the values. 267 * 268 * This is to compare the shape of the object rather than the identity of the object. 269 */ 270 if ( is_object( $option_value ) ) { 271 $this->assertEquals( $value_after_get_option, $value_after_pre_priming, 'The values should be equal.' ); 272 } else { 273 $this->assertSame( $value_after_get_option, $value_after_pre_priming, 'The values should be identical.' ); 274 } 275 } 276 277 /** 278 * Tests that wp_prime_option_caches() shapes the cache in the same fashion as get_option() 279 * 280 * @ticket 59738 281 * 282 * @dataProvider data_option_types 283 * 284 * @param mixed $option_value An option value. 285 */ 286 public function test_wp_prime_option_caches_cache_should_be_identical_to_get_option_cache( $option_value ) { 287 // As this includes a test setting the value to `(bool) false`, update_option() can not be used so add_option() is used instead. 288 add_option( 'type_of_option', $option_value, '', false ); 289 wp_cache_delete( 'type_of_option', 'options' ); 290 291 $this->assertFalse( wp_cache_get( 'type_of_option', 'options' ), 'type_of_option was not deleted from the cache for wp_prime_option_caches().' ); 292 293 // Call the wp_prime_option_caches function to prime the options. 294 wp_prime_option_caches( array( 'type_of_option' ) ); 295 $value_from_priming = wp_cache_get( 'type_of_option', 'options' ); 296 297 wp_cache_delete( 'type_of_option', 'options' ); 298 $this->assertFalse( wp_cache_get( 'type_of_option', 'options' ), 'type_of_option was not deleted from the cache for get_option().' ); 299 300 // Call get_option() to prime the options. 301 get_option( 'type_of_option' ); 302 $value_from_get_option = wp_cache_get( 'type_of_option', 'options' ); 303 304 $this->assertIsString( $value_from_get_option, 'Cache from get_option() should always be a string' ); 305 $this->assertIsString( $value_from_priming, 'Cache from wp_prime_option_caches() should always be a string' ); 306 $this->assertSame( $value_from_get_option, $value_from_priming, 'The values should be identical.' ); 307 } 308 309 /** 310 * Tests that wp_prime_option_caches() doesn't trigger DB queries on already primed options. 311 * 312 * @ticket 59738 313 * 314 * @dataProvider data_option_types 315 * 316 * @param mixed $option_value An option value. 317 */ 318 public function test_wp_prime_option_caches_does_not_trigger_db_queries_repriming_options( $option_value ) { 319 // As this includes a test setting the value to `(bool) false`, update_option() can not be used so add_option() is used instead. 320 add_option( 'double_primed_option', $option_value, '', false ); 321 wp_cache_delete( 'double_primed_option', 'options' ); 322 $options_to_prime = array( 'double_primed_option' ); 323 324 $this->assertFalse( wp_cache_get( 'double_primed_option', 'options' ), 'double_primed_option was not deleted from the cache.' ); 325 326 // Call the wp_prime_option_caches function to prime the options. 327 wp_prime_option_caches( $options_to_prime ); 328 329 // Store the initial database query count. 330 $initial_query_count = get_num_queries(); 331 332 // Check that options are only in the 'options' cache group. 333 foreach ( $options_to_prime as $option ) { 334 $this->assertNotFalse( 335 wp_cache_get( $option, 'options' ), 336 "$option was not primed in the 'options' cache group." 337 ); 338 339 $new_notoptions = wp_cache_get( $option, 'notoptions' ); 340 if ( ! is_array( $new_notoptions ) ) { 341 $new_notoptions = array(); 342 } 343 $this->assertArrayNotHasKey( 344 $option, 345 $new_notoptions, 346 "$option was primed in the 'notoptions' cache." 347 ); 348 } 349 350 // Call the wp_prime_option_caches function to prime the options. 351 wp_prime_option_caches( $options_to_prime ); 352 353 // Ensure no additional database queries were made. 354 $this->assertSame( 355 $initial_query_count, 356 get_num_queries(), 357 'Additional database queries were made.' 358 ); 359 } 360 361 /** 362 * Tests that wp_prime_option_caches() doesn't trigger DB queries for items primed in alloptions. 363 * 364 * @ticket 59738 365 * 366 * @dataProvider data_option_types 367 * 368 * @param mixed $option_value An option value. 369 */ 370 public function test_wp_prime_option_caches_does_not_trigger_db_queries_for_alloptions( $option_value ) { 371 // As this includes a test setting the value to `(bool) false`, update_option() can not be used so add_option() is used instead. 372 add_option( 'option_in_alloptions', $option_value, '', true ); 373 wp_cache_delete( 'alloptions', 'options' ); 374 wp_cache_delete( 'option_in_alloptions', 'options' ); 375 $options_to_prime = array( 'option_in_alloptions' ); 376 377 $this->assertFalse( wp_cache_get( 'option_in_alloptions', 'options' ), 'option_in_alloptions was not deleted from the cache.' ); 378 $this->assertFalse( wp_cache_get( 'alloptions', 'options' ), 'alloptions was not deleted from the cache.' ); 379 380 // Prime the alloptions cache. 381 wp_load_alloptions(); 382 383 // Store the initial database query count. 384 $initial_query_count = get_num_queries(); 385 386 // Call the wp_prime_option_caches function to reprime the option. 387 wp_prime_option_caches( $options_to_prime ); 388 389 // Check that options are in the 'alloptions' cache only. 390 foreach ( $options_to_prime as $option ) { 391 $this->assertFalse( 392 wp_cache_get( $option, 'options' ), 393 "$option was primed in the 'options' cache group." 394 ); 395 396 $new_notoptions = wp_cache_get( $option, 'notoptions' ); 397 if ( ! is_array( $new_notoptions ) ) { 398 $new_notoptions = array(); 399 } 400 $this->assertArrayNotHasKey( 401 $option, 402 $new_notoptions, 403 "$option was primed in the 'notoptions' cache." 404 ); 405 406 $new_alloptions = wp_cache_get( 'alloptions', 'options' ); 407 if ( ! is_array( $new_alloptions ) ) { 408 $new_alloptions = array(); 409 } 410 $this->assertArrayHasKey( 411 $option, 412 $new_alloptions, 413 "$option was not primed in the 'alloptions' cache." 414 ); 415 } 416 417 // Ensure no additional database queries were made. 418 $this->assertSame( 419 0, 420 get_num_queries() - $initial_query_count, 421 'Additional database queries were made.' 422 ); 423 } 424 425 /** 426 * Data provider. 427 * 428 * @return array[] 429 */ 430 public function data_option_types() { 431 return array( 432 'null' => array( null ), 433 '(bool) false' => array( false ), 434 '(bool) true' => array( true ), 435 '(int) 0' => array( 0 ), 436 '(int) -0' => array( -0 ), 437 '(int) 1' => array( 1 ), 438 '(int) -1' => array( -1 ), 439 '(float) 0.0' => array( 0.0 ), 440 '(float) -0.0' => array( -0.0 ), 441 '(float) 1.0' => array( 1.0 ), 442 'empty string' => array( '' ), 443 'string with only tabs' => array( "\t\t" ), 444 'string with only newlines' => array( "\n\n" ), 445 'string with only carriage returns' => array( "\r\r" ), 446 'string with only spaces' => array( ' ' ), 447 'populated string' => array( 'string' ), 448 'string (1)' => array( '1' ), 449 'string (0)' => array( '0' ), 450 'string (0.0)' => array( '0.0' ), 451 'string (-0)' => array( '-0' ), 452 'string (-0.0)' => array( '-0.0' ), 453 'empty array' => array( array() ), 454 'populated array' => array( array( 'string' ) ), 455 'empty object' => array( new stdClass() ), 456 'populated object' => array( (object) array( 'string' ) ), 457 'INF' => array( INF ), 458 'NAN' => array( NAN ), 459 ); 129 460 } 130 461 }
Note: See TracChangeset
for help on using the changeset viewer.