Changeset 56946 for trunk/tests/phpunit/tests/option/networkOption.php
- Timestamp:
- 10/16/2023 08:35:05 PM (14 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/option/networkOption.php
r56814 r56946 229 229 $this->assertSame( $num_queries_pre_update, get_num_queries() ); 230 230 } 231 232 /**233 * Tests that update_network_option() triggers one additional query and returns true234 * for some loosely equal old and new values when the old value is retrieved from the cache.235 *236 * The additional query is triggered to update the value in the database.237 *238 * If the old value is false, the additional queries are triggered to:239 * 1. get the old value from the database via get_network_option() -> get_option().240 * 2. (Single Site only) get the old value from the database via update_network_option() -> update_option() -> get_option().241 * 3. update the value in the database via update_network_options() -> update_option().242 *243 * @ticket 59360244 *245 * @covers ::update_network_option246 *247 * @dataProvider data_loosely_equal_values_that_should_update248 *249 * @param mixed $old_value The old value.250 * @param mixed $new_value The new value to try to set.251 */252 public function test_update_network_option_should_update_some_loosely_equal_values_from_cache( $old_value, $new_value ) {253 add_network_option( null, 'foo', $old_value );254 255 $num_queries = get_num_queries();256 257 // Comparison will happen against value cached during add_network_option() above.258 $updated = update_network_option( null, 'foo', $new_value );259 260 $expected_queries = 1;261 262 if ( false === $old_value ) {263 $expected_queries = is_multisite() ? 2 : 3;264 }265 266 $this->assertSame( $expected_queries, get_num_queries() - $num_queries, "The number of queries should have increased by $expected_queries." );267 $this->assertTrue( $updated, 'update_network_option() should have returned true.' );268 }269 270 /**271 * Tests that update_network_option() triggers two additional queries and returns true272 * for some loosely equal old and new values when the old value is retrieved from the database.273 *274 * The two additional queries are triggered to:275 * 1. retrieve the old value from the database, as the option does not exist in the cache.276 * 2. update the value in the database.277 *278 * On Single Site, if the old value is false, the four additional queries are triggered to:279 * 1. get the old value from the database via get_network_option() -> get_option().280 * 2. get the alloptions cache via get_network_option() -> get_option().281 * 3. get the old value from the database via update_network_option() -> update_option() -> get_option().282 * 4. update the value in the database via update_network_options() -> update_option().283 *284 * @ticket 59360285 *286 * @covers ::update_network_option287 *288 * @dataProvider data_loosely_equal_values_that_should_update289 *290 * @param mixed $old_value The old value.291 * @param mixed $new_value The new value to try to set.292 */293 public function test_update_network_option_should_update_some_loosely_equal_values_from_db( $old_value, $new_value ) {294 add_network_option( null, 'foo', $old_value );295 296 $num_queries = get_num_queries();297 298 // Delete cache.299 $network_cache_key = get_current_network_id() . ':foo';300 wp_cache_delete( $network_cache_key, 'site-options' );301 wp_cache_delete( 'alloptions', 'options' );302 303 $updated = update_network_option( null, 'foo', $new_value );304 305 $expected_queries = false === $old_value && ! is_multisite() ? 4 : 2;306 307 $this->assertSame( $expected_queries, get_num_queries() - $num_queries, "The number of queries should have increased by $expected_queries." );308 $this->assertTrue( $updated, 'update_network_option() should have returned true.' );309 }310 311 /**312 * Tests that update_network_option() triggers one additional query and returns true313 * for some loosely equal old and new values when the old value is retrieved from a refreshed cache.314 *315 * The additional query is triggered to update the value in the database.316 *317 * If the old value is false, the additional queries are triggered to:318 * 1. get the old value from the database via get_network_option() -> get_option().319 * 2. get the old value from the database via update_network_option() -> update_option() -> get_option().320 * 3. update the value in the database via update_network_options() -> update_option().321 *322 * @ticket 59360323 *324 * @covers ::update_network_option325 *326 * @dataProvider data_loosely_equal_values_that_should_update327 *328 * @param mixed $old_value The old value.329 * @param mixed $new_value The new value to try to set.330 */331 public function test_update_network_option_should_update_some_loosely_equal_values_from_refreshed_cache( $old_value, $new_value ) {332 add_network_option( null, 'foo', $old_value );333 334 // Delete and refresh cache from DB.335 wp_cache_delete( 'alloptions', 'options' );336 wp_load_alloptions();337 338 $num_queries = get_num_queries();339 $updated = update_network_option( null, 'foo', $new_value );340 341 $expected_queries = 1;342 343 if ( false === $old_value ) {344 $expected_queries = is_multisite() ? 2 : 3;345 }346 347 $this->assertSame( $expected_queries, get_num_queries() - $num_queries, "The number of queries should have increased by $expected_queries." );348 $this->assertTrue( $updated, 'update_network_option() should have returned true.' );349 }350 351 /**352 * Data provider.353 *354 * @return array355 */356 public function data_loosely_equal_values_that_should_update() {357 return array(358 // Falsey values.359 '(string) "0" to false' => array( '0', false ),360 'empty string to (int) 0' => array( '', 0 ),361 'empty string to (float) 0.0' => array( '', 0.0 ),362 '(int) 0 to empty string' => array( 0, '' ),363 '(int) 0 to false' => array( 0, false ),364 '(float) 0.0 to empty string' => array( 0.0, '' ),365 '(float) 0.0 to false' => array( 0.0, false ),366 'false to (string) "0"' => array( false, '0' ),367 'false to (int) 0' => array( false, 0 ),368 'false to (float) 0.0' => array( false, 0.0 ),369 370 // Non-scalar values.371 'false to array()' => array( false, array() ),372 '(string) "false" to array()' => array( 'false', array() ),373 'empty string to array()' => array( '', array() ),374 '(int 0) to array()' => array( 0, array() ),375 '(string) "0" to array()' => array( '0', array() ),376 '(string) "false" to null' => array( 'false', null ),377 '(int) 0 to null' => array( 0, null ),378 '(string) "0" to null' => array( '0', null ),379 'array() to false' => array( array(), false ),380 'array() to (string) "false"' => array( array(), 'false' ),381 'array() to empty string' => array( array(), '' ),382 'array() to (int) 0' => array( array(), 0 ),383 'array() to (string) "0"' => array( array(), '0' ),384 'array() to null' => array( array(), null ),385 );386 }387 388 /**389 * Tests that update_network_option() triggers no additional queries and returns false390 * for some values when the old value is retrieved from the cache.391 *392 * @ticket 59360393 *394 * @covers ::update_network_option395 *396 * @dataProvider data_loosely_equal_values_that_should_not_update397 * @dataProvider data_strictly_equal_values398 *399 * @param mixed $old_value The old value.400 * @param mixed $new_value The new value to try to set.401 */402 public function test_update_network_option_should_not_update_some_values_from_cache( $old_value, $new_value ) {403 add_network_option( null, 'foo', $old_value );404 405 $num_queries = get_num_queries();406 407 // Comparison will happen against value cached during add_option() above.408 $updated = update_network_option( null, 'foo', $new_value );409 410 $this->assertSame( $num_queries, get_num_queries(), 'No additional queries should have run.' );411 $this->assertFalse( $updated, 'update_network_option() should have returned false.' );412 }413 414 /**415 * Tests that update_network_option() triggers one additional query and returns false416 * for some values when the old value is retrieved from the database.417 *418 * The additional query is triggered to retrieve the old value from the database.419 *420 * @ticket 59360421 *422 * @covers ::update_network_option423 *424 * @dataProvider data_loosely_equal_values_that_should_not_update425 * @dataProvider data_strictly_equal_values426 *427 * @param mixed $old_value The old value.428 * @param mixed $new_value The new value to try to set.429 */430 public function test_update_network_option_should_not_update_some_values_from_db( $old_value, $new_value ) {431 add_network_option( null, 'foo', $old_value );432 433 $num_queries = get_num_queries();434 435 // Delete cache.436 $network_cache_key = get_current_network_id() . ':foo';437 wp_cache_delete( $network_cache_key, 'site-options' );438 wp_cache_delete( 'alloptions', 'options' );439 440 $updated = update_network_option( null, 'foo', $new_value );441 442 $this->assertSame( 1, get_num_queries() - $num_queries, 'One additional query should have run to update the value.' );443 $this->assertFalse( $updated, 'update_network_option() should have returned false.' );444 }445 446 /**447 * Tests that update_network_option() triggers no additional queries and returns false448 * for some values when the old value is retrieved from a refreshed cache.449 *450 * @ticket 59360451 *452 * @covers ::update_network_option453 *454 * @dataProvider data_loosely_equal_values_that_should_not_update455 * @dataProvider data_strictly_equal_values456 *457 * @param mixed $old_value The old value.458 * @param mixed $new_value The new value to try to set.459 */460 public function test_update_network_option_should_not_update_some_values_from_refreshed_cache( $old_value, $new_value ) {461 add_network_option( null, 'foo', $old_value );462 463 // Delete and refresh cache from DB.464 wp_cache_delete( 'alloptions', 'options' );465 wp_load_alloptions();466 467 $num_queries = get_num_queries();468 $updated = update_network_option( null, 'foo', $new_value );469 470 /*471 * Strictly equal old and new values will cause an early return472 * with no additional queries.473 */474 $this->assertSame( $num_queries, get_num_queries(), 'No additional queries should have run.' );475 $this->assertFalse( $updated, 'update_network_option() should have returned false.' );476 }477 478 /**479 * Data provider.480 *481 * @return array[]482 */483 public function data_loosely_equal_values_that_should_not_update() {484 return array(485 // Truthy values.486 '(string) "1" to (int) 1' => array( '1', 1 ),487 '(string) "1" to (float) 1.0' => array( '1', 1.0 ),488 '(string) "1" to true' => array( '1', true ),489 '(int) 1 to (string) "1"' => array( 1, '1' ),490 '1 to (float) 1.0' => array( 1, 1.0 ),491 '(int) 1 to true' => array( 1, true ),492 '(float) 1.0 to (string) "1"' => array( 1.0, '1' ),493 '(float) 1.0 to (int) 1' => array( 1.0, 1 ),494 '1.0 to true' => array( 1.0, true ),495 'true to (string) "1"' => array( true, '1' ),496 'true to 1' => array( true, 1 ),497 'true to (float) 1.0' => array( true, 1.0 ),498 499 // Falsey values.500 '(string) "0" to (int) 0' => array( '0', 0 ),501 '(string) "0" to (float) 0.0' => array( '0', 0.0 ),502 '(int) 0 to (string) "0"' => array( 0, '0' ),503 '(int) 0 to (float) 0.0' => array( 0, 0.0 ),504 '(float) 0.0 to (string) "0"' => array( 0.0, '0' ),505 '(float) 0.0 to (int) 0' => array( 0.0, 0 ),506 'empty string to false' => array( '', false ),507 508 /*509 * null as an initial value behaves differently by triggering510 * a query, so it is not included in these datasets.511 *512 * See data_stored_as_empty_string() and its related test.513 */514 );515 }516 517 /**518 * Data provider.519 *520 * @return array521 */522 public function data_strictly_equal_values() {523 $obj = new stdClass();524 525 return array(526 // Truthy values.527 '(string) "1"' => array( '1', '1' ),528 '(int) 1' => array( 1, 1 ),529 '(float) 1.0' => array( 1.0, 1.0 ),530 'true' => array( true, true ),531 'string with spaces' => array( ' ', ' ' ),532 'non-empty array' => array( array( 'false' ), array( 'false' ) ),533 'object' => array( $obj, $obj ),534 535 // Falsey values.536 '(string) "0"' => array( '0', '0' ),537 'empty string' => array( '', '' ),538 '(int) 0' => array( 0, 0 ),539 '(float) 0.0' => array( 0.0, 0.0 ),540 'empty array' => array( array(), array() ),541 542 /*543 * false and null are not included in these datasets544 * because false is the default value, which triggers545 * a call to add_network_option().546 *547 * See data_stored_as_empty_string() and its related test.548 */549 );550 }551 552 /**553 * Tests that update_network_option() handles a null new value when the new value554 * is retrieved from the cache.555 *556 * On Single Site, this will result in no additional queries as557 * the option_value database field is not nullable.558 *559 * On Multisite, this will result in one additional query as560 * the meta_value database field is nullable.561 *562 * @ticket 59360563 *564 * @covers ::update_network_option565 */566 public function test_update_network_option_should_handle_a_null_new_value_from_cache() {567 add_network_option( null, 'foo', '' );568 569 $num_queries = get_num_queries();570 571 // Comparison will happen against value cached during add_option() above.572 $updated = update_network_option( null, 'foo', null );573 574 $expected_queries = is_multisite() ? 1 : 0;575 $this->assertSame( $expected_queries, get_num_queries() - $num_queries, "The number of queries should have increased by $expected_queries." );576 577 if ( is_multisite() ) {578 $this->assertTrue( $updated, 'update_network_option() should have returned true.' );579 } else {580 $this->assertFalse( $updated, 'update_network_option() should have returned false.' );581 }582 }583 584 /**585 * Tests that update_network_option() handles a null new value when the new value586 * is retrieved from the database.587 *588 * On Single Site, this will result in only 1 additional query as589 * the option_value database field is not nullable.590 *591 * On Multisite, this will result in two additional queries as592 * the meta_value database field is nullable.593 *594 * @ticket 59360595 *596 * @covers ::update_network_option597 */598 public function test_update_network_option_should_handle_a_null_new_value_from_db() {599 add_network_option( null, 'foo', '' );600 601 $num_queries = get_num_queries();602 603 // Delete cache.604 $network_cache_key = get_current_network_id() . ':foo';605 wp_cache_delete( $network_cache_key, 'site-options' );606 wp_cache_delete( 'alloptions', 'options' );607 608 $updated = update_network_option( null, 'foo', null );609 610 $expected_queries = is_multisite() ? 2 : 1;611 $this->assertSame( $expected_queries, get_num_queries() - $num_queries, "The number of queries should have increased by $expected_queries." );612 613 if ( is_multisite() ) {614 $this->assertTrue( $updated, 'update_network_option() should have returned true.' );615 } else {616 $this->assertFalse( $updated, 'update_network_option() should have returned false.' );617 }618 }619 620 /**621 * Tests that update_network_option() handles a null new value when the new value622 * is retrieved from a refreshed cache.623 *624 * On Single Site, this will result in no additional queries as625 * the option_value database field is not nullable.626 *627 * On Multisite, this will result in one additional query as628 * the meta_value database field is nullable.629 *630 * @ticket 59360631 *632 * @covers ::update_network_option633 */634 public function test_update_network_option_should_handle_a_null_new_value_from_refreshed_cache() {635 add_network_option( null, 'foo', '' );636 637 // Delete and refresh cache from DB.638 wp_cache_delete( 'alloptions', 'options' );639 wp_load_alloptions();640 641 $num_queries = get_num_queries();642 $updated = update_network_option( null, 'foo', null );643 644 $expected_queries = is_multisite() ? 1 : 0;645 $this->assertSame( $expected_queries, get_num_queries() - $num_queries, "The number of queries should have increased by $expected_queries." );646 647 if ( is_multisite() ) {648 $this->assertTrue( $updated, 'update_network_option() should have returned true.' );649 } else {650 $this->assertFalse( $updated, 'update_network_option() should have returned false.' );651 }652 }653 654 /**655 * Tests that update_network_option() adds a non-existent option when the new value656 * is stored as an empty string and false is the default value for the option.657 *658 * @ticket 59360659 *660 * @dataProvider data_stored_as_empty_string661 *662 * @param mixed $new_value A value that casts to an empty string.663 */664 public function test_update_network_option_should_add_network_option_when_the_new_value_is_stored_as_an_empty_string_and_matches_default_value_false( $new_value ) {665 global $wpdb;666 667 if ( is_multisite() ) {668 $this->markTestSkipped( 'This test should only run on Single Site.' );669 }670 671 $this->assertTrue( update_network_option( null, 'foo', $new_value ), 'update_network_option() should have returned true.' );672 673 $actual = $wpdb->get_row( "SELECT option_value FROM $wpdb->options WHERE option_name = 'foo' LIMIT 1" );674 675 $this->assertIsObject( $actual, 'The option was not added to the database.' );676 $this->assertObjectHasProperty( 'option_value', $actual, 'The "option_value" property was not included.' );677 $this->assertSame( '', $actual->option_value, 'The value was not stored as an empty string.' );678 }679 680 /**681 * Data provider.682 *683 * @return array[]684 */685 public function data_stored_as_empty_string() {686 return array(687 'empty string' => array( '' ),688 'null' => array( null ),689 );690 }691 692 /**693 * Tests that a non-existent option is added even when its pre filter returns a value.694 *695 * @ticket 59360696 *697 * @covers ::update_network_option698 */699 public function test_update_network_option_with_pre_filter_adds_missing_option() {700 $hook_name = is_multisite() ? 'pre_site_option_foo' : 'pre_option_foo';701 702 // Force a return value of integer 0.703 add_filter( $hook_name, '__return_zero' );704 705 /*706 * This should succeed, since the 'foo' option does not exist in the database.707 * The default value is false, so it differs from 0.708 */709 $this->assertTrue( update_network_option( null, 'foo', 0 ) );710 }711 712 /**713 * Tests that an existing option is updated even when its pre filter returns the same value.714 *715 * @ticket 59360716 *717 * @covers ::update_network_option718 */719 public function test_update_network_option_with_pre_filter_updates_option_with_different_value() {720 $hook_name = is_multisite() ? 'pre_site_option_foo' : 'pre_option_foo';721 722 // Add the option with a value of 1 to the database.723 update_network_option( null, 'foo', 1 );724 725 // Force a return value of integer 0.726 add_filter( $hook_name, '__return_zero' );727 728 /*729 * This should succeed, since the 'foo' option has a value of 1 in the database.730 * Therefore it differs from 0 and should be updated.731 */732 $this->assertTrue( update_network_option( null, 'foo', 0 ) );733 }734 735 /**736 * Tests that calling update_network_option() does not permanently remove pre filters.737 *738 * @ticket 59360739 *740 * @covers ::update_network_option741 */742 public function test_update_network_option_maintains_pre_filters() {743 $hook_name = is_multisite() ? 'pre_site_option_foo' : 'pre_option_foo';744 745 add_filter( $hook_name, '__return_zero' );746 update_network_option( null, 'foo', 0 );747 748 // Assert that the filter is still present.749 $this->assertSame( 10, has_filter( $hook_name, '__return_zero' ) );750 }751 752 /**753 * Tests that update_network_option() conditionally applies754 * 'pre_site_option_{$option}' and 'pre_option_{$option}' filters.755 *756 * @ticket 59360757 *758 * @covers ::update_network_option759 */760 public function test_update_network_option_should_conditionally_apply_pre_site_option_and_pre_option_filters() {761 $option = 'foo';762 $site_hook = new MockAction();763 $option_hook = new MockAction();764 765 add_filter( "pre_site_option_{$option}", array( $site_hook, 'filter' ) );766 add_filter( "pre_option_{$option}", array( $option_hook, 'filter' ) );767 768 update_network_option( null, $option, 'false' );769 770 $this->assertSame( 1, $site_hook->get_call_count(), "'pre_site_option_{$option}' filters occurred an unexpected number of times." );771 $this->assertSame( is_multisite() ? 0 : 1, $option_hook->get_call_count(), "'pre_option_{$option}' filters occurred an unexpected number of times." );772 }773 774 /**775 * Tests that update_network_option() conditionally applies776 * 'default_site_{$option}' and 'default_option_{$option}' filters.777 *778 * @ticket 59360779 *780 * @covers ::update_network_option781 */782 public function test_update_network_option_should_conditionally_apply_site_and_option_default_value_filters() {783 $option = 'foo';784 $site_hook = new MockAction();785 $option_hook = new MockAction();786 787 add_filter( "default_site_option_{$option}", array( $site_hook, 'filter' ) );788 add_filter( "default_option_{$option}", array( $option_hook, 'filter' ) );789 790 update_network_option( null, $option, 'false' );791 792 $this->assertSame( 2, $site_hook->get_call_count(), "'default_site_option_{$option}' filters occurred an unexpected number of times." );793 $this->assertSame( is_multisite() ? 0 : 2, $option_hook->get_call_count(), "'default_option_{$option}' filters occurred an unexpected number of times." );794 }795 796 /**797 * Tests that update_network_option() adds a non-existent option that uses a filtered default value.798 *799 * @ticket 59360800 *801 * @covers ::update_network_option802 */803 public function test_update_network_option_should_add_option_with_filtered_default_value() {804 global $wpdb;805 806 $option = 'foo';807 $default_site_value = 'default-site-value';808 $default_option_value = 'default-option-value';809 810 add_filter(811 "default_site_option_{$option}",812 static function () use ( $default_site_value ) {813 return $default_site_value;814 }815 );816 817 add_filter(818 "default_option_{$option}",819 static function () use ( $default_option_value ) {820 return $default_option_value;821 }822 );823 824 /*825 * For a non existing option with the unfiltered default of false, passing false here wouldn't work.826 * Because the default is different than false here though, passing false is expected to result in827 * a database update.828 */829 $this->assertTrue( update_network_option( null, $option, false ), 'update_network_option() should have returned true.' );830 831 if ( is_multisite() ) {832 $actual = $wpdb->get_row(833 $wpdb->prepare(834 "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s LIMIT 1",835 $option836 )837 );838 } else {839 $actual = $wpdb->get_row(840 $wpdb->prepare(841 "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1",842 $option843 )844 );845 }846 847 $value_field = is_multisite() ? 'meta_value' : 'option_value';848 849 $this->assertIsObject( $actual, 'The option was not added to the database.' );850 $this->assertObjectHasProperty( $value_field, $actual, "The '$value_field' property was not included." );851 $this->assertSame( '', $actual->$value_field, 'The new value was not stored in the database.' );852 }853 231 }
Note: See TracChangeset
for help on using the changeset viewer.