<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[56797] trunk/tests/phpunit/tests/option/networkOption.php: Options, Meta APIs: Add test coverage for `update_network_option()` comparison of new and existing value.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/56797">56797</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/56797","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>flixos90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-10-06 17:01:48 +0000 (Fri, 06 Oct 2023)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Options, Meta APIs: Add test coverage for `update_network_option()` comparison of new and existing value.
Having those tests in `trunk` already will help ensure potential future fixes to this logic maintain backward compatibility.
Props mukesh27, spacedmonkey.
See <a href="https://core.trac.wordpress.org/ticket/59360">#59360</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsoptionnetworkOptionphp">trunk/tests/phpunit/tests/option/networkOption.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunktestsphpunittestsoptionnetworkOptionphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/option/networkOption.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/option/networkOption.php 2023-10-06 16:31:22 UTC (rev 56796)
+++ trunk/tests/phpunit/tests/option/networkOption.php 2023-10-06 17:01:48 UTC (rev 56797)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -228,4 +228,475 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Check that no new database queries were performed.
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( $num_queries_pre_update, get_num_queries() );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * Tests that update_network_option() triggers one additional query and returns true
+ * for some loosely equal old and new values when the old value is retrieved from the cache.
+ *
+ * The additional query is triggered to update the value in the database.
+ *
+ * If the old value is false, the additional queries are triggered to:
+ * 1. get the old value from the database via get_network_option() -> get_option().
+ * 2. (Single Site only) get the old value from the database via update_network_option() -> update_option() -> get_option().
+ * 3. update the value in the database via update_network_options() -> update_option().
+ *
+ * @ticket 59360
+ *
+ * @covers ::update_network_option
+ *
+ * @dataProvider data_loosely_equal_values_that_should_update
+ *
+ * @param mixed $old_value The old value.
+ * @param mixed $new_value The new value to try to set.
+ */
+ public function test_update_network_option_should_update_some_loosely_equal_values_from_cache( $old_value, $new_value ) {
+ add_network_option( null, 'foo', $old_value );
+
+ $num_queries = get_num_queries();
+
+ // Comparison will happen against value cached during add_network_option() above.
+ $updated = update_network_option( null, 'foo', $new_value );
+
+ $expected_queries = 1;
+
+ if ( false === $old_value ) {
+ $expected_queries = is_multisite() ? 2 : 3;
+ }
+
+ $this->assertSame( $expected_queries, get_num_queries() - $num_queries, "The number of queries should have increased by $expected_queries." );
+ $this->assertTrue( $updated, 'update_network_option() should have returned true.' );
+ }
+
+ /**
+ * Tests that update_network_option() triggers two additional queries and returns true
+ * for some loosely equal old and new values when the old value is retrieved from the database.
+ *
+ * The two additional queries are triggered to:
+ * 1. retrieve the old value from the database, as the option does not exist in the cache.
+ * 2. update the value in the database.
+ *
+ * On Single Site, if the old value is false, the four additional queries are triggered to:
+ * 1. get the old value from the database via get_network_option() -> get_option().
+ * 2. get the alloptions cache via get_network_option() -> get_option().
+ * 3. get the old value from the database via update_network_option() -> update_option() -> get_option().
+ * 4. update the value in the database via update_network_options() -> update_option().
+ *
+ * @ticket 59360
+ *
+ * @covers ::update_network_option
+ *
+ * @dataProvider data_loosely_equal_values_that_should_update
+ *
+ * @param mixed $old_value The old value.
+ * @param mixed $new_value The new value to try to set.
+ */
+ public function test_update_network_option_should_update_some_loosely_equal_values_from_db( $old_value, $new_value ) {
+ add_network_option( null, 'foo', $old_value );
+
+ $num_queries = get_num_queries();
+
+ // Delete cache.
+ $network_cache_key = get_current_network_id() . ':foo';
+ wp_cache_delete( $network_cache_key, 'site-options' );
+ wp_cache_delete( 'alloptions', 'options' );
+
+ $updated = update_network_option( null, 'foo', $new_value );
+
+ $expected_queries = false === $old_value && ! is_multisite() ? 4 : 2;
+
+ $this->assertSame( $expected_queries, get_num_queries() - $num_queries, "The number of queries should have increased by $expected_queries." );
+ $this->assertTrue( $updated, 'update_network_option() should have returned true.' );
+ }
+
+ /**
+ * Tests that update_network_option() triggers one additional query and returns true
+ * for some loosely equal old and new values when the old value is retrieved from a refreshed cache.
+ *
+ * The additional query is triggered to update the value in the database.
+ *
+ * If the old value is false, the additional queries are triggered to:
+ * 1. get the old value from the database via get_network_option() -> get_option().
+ * 2. get the old value from the database via update_network_option() -> update_option() -> get_option().
+ * 3. update the value in the database via update_network_options() -> update_option().
+ *
+ * @ticket 59360
+ *
+ * @covers ::update_network_option
+ *
+ * @dataProvider data_loosely_equal_values_that_should_update
+ *
+ * @param mixed $old_value The old value.
+ * @param mixed $new_value The new value to try to set.
+ */
+ public function test_update_network_option_should_update_some_loosely_equal_values_from_refreshed_cache( $old_value, $new_value ) {
+ add_network_option( null, 'foo', $old_value );
+
+ // Delete and refresh cache from DB.
+ wp_cache_delete( 'alloptions', 'options' );
+ wp_load_alloptions();
+
+ $num_queries = get_num_queries();
+ $updated = update_network_option( null, 'foo', $new_value );
+
+ $expected_queries = 1;
+
+ if ( false === $old_value ) {
+ $expected_queries = is_multisite() ? 2 : 3;
+ }
+
+ $this->assertSame( $expected_queries, get_num_queries() - $num_queries, "The number of queries should have increased by $expected_queries." );
+ $this->assertTrue( $updated, 'update_network_option() should have returned true.' );
+ }
+
+ /**
+ * Data provider.
+ *
+ * @return array
+ */
+ public function data_loosely_equal_values_that_should_update() {
+ return array(
+ // Falsey values.
+ '(string) "0" to false' => array( '0', false ),
+ 'empty string to (int) 0' => array( '', 0 ),
+ 'empty string to (float) 0.0' => array( '', 0.0 ),
+ '(int) 0 to empty string' => array( 0, '' ),
+ '(int) 0 to false' => array( 0, false ),
+ '(float) 0.0 to empty string' => array( 0.0, '' ),
+ '(float) 0.0 to false' => array( 0.0, false ),
+ 'false to (string) "0"' => array( false, '0' ),
+ 'false to (int) 0' => array( false, 0 ),
+ 'false to (float) 0.0' => array( false, 0.0 ),
+
+ // Non-scalar values.
+ 'false to array()' => array( false, array() ),
+ '(string) "false" to array()' => array( 'false', array() ),
+ 'empty string to array()' => array( '', array() ),
+ '(int 0) to array()' => array( 0, array() ),
+ '(string) "0" to array()' => array( '0', array() ),
+ '(string) "false" to null' => array( 'false', null ),
+ '(int) 0 to null' => array( 0, null ),
+ '(string) "0" to null' => array( '0', null ),
+ 'array() to false' => array( array(), false ),
+ 'array() to (string) "false"' => array( array(), 'false' ),
+ 'array() to empty string' => array( array(), '' ),
+ 'array() to (int) 0' => array( array(), 0 ),
+ 'array() to (string) "0"' => array( array(), '0' ),
+ 'array() to null' => array( array(), null ),
+ );
+ }
+
+ /**
+ * Tests that update_network_option() triggers no additional queries and returns false
+ * for some values when the old value is retrieved from the cache.
+ *
+ * @ticket 59360
+ *
+ * @covers ::update_network_option
+ *
+ * @dataProvider data_loosely_equal_values_that_should_not_update
+ * @dataProvider data_strictly_equal_values
+ *
+ * @param mixed $old_value The old value.
+ * @param mixed $new_value The new value to try to set.
+ */
+ public function test_update_option_should_not_update_some_values_from_cache( $old_value, $new_value ) {
+ add_network_option( null, 'foo', $old_value );
+
+ $num_queries = get_num_queries();
+
+ // Comparison will happen against value cached during add_option() above.
+ $updated = update_network_option( null, 'foo', $new_value );
+
+ $expected_queries = $old_value === $new_value || ! is_multisite() ? 0 : 1;
+ $this->assertSame( $expected_queries, get_num_queries() - $num_queries, "The number of queries should have increased by $expected_queries." );
+
+ $this->assertFalse( $updated, 'update_network_option() should have returned false.' );
+ }
+
+ /**
+ * Tests that update_network_option() triggers one additional query and returns false
+ * for some values when the old value is retrieved from the database.
+ *
+ * The additional query is triggered to retrieve the old value from the database.
+ *
+ * @ticket 59360
+ *
+ * @covers ::update_network_option
+ *
+ * @dataProvider data_loosely_equal_values_that_should_not_update
+ * @dataProvider data_strictly_equal_values
+ *
+ * @param mixed $old_value The old value.
+ * @param mixed $new_value The new value to try to set.
+ */
+ public function test_update_network_option_should_not_update_some_values_from_db( $old_value, $new_value ) {
+ add_network_option( null, 'foo', $old_value );
+
+ $num_queries = get_num_queries();
+
+ // Delete cache.
+ $network_cache_key = get_current_network_id() . ':foo';
+ wp_cache_delete( $network_cache_key, 'site-options' );
+ wp_cache_delete( 'alloptions', 'options' );
+
+ $updated = update_network_option( null, 'foo', $new_value );
+
+ // Mimic the behavior of the database by casting the old value to string.
+ if ( is_scalar( $old_value ) ) {
+ $old_value = (string) $old_value;
+ }
+
+ $expected_queries = $old_value === $new_value || ! is_multisite() ? 1 : 2;
+ $this->assertSame( $expected_queries, get_num_queries() - $num_queries, "The number of queries should have increased by $expected_queries." );
+
+ $this->assertFalse( $updated, 'update_network_option() should have returned false.' );
+ }
+
+ /**
+ * Tests that update_network_option() triggers no additional queries and returns false
+ * for some values when the old value is retrieved from a refreshed cache.
+ *
+ * @ticket 59360
+ *
+ * @covers ::update_network_option
+ *
+ * @dataProvider data_loosely_equal_values_that_should_not_update
+ * @dataProvider data_strictly_equal_values
+ *
+ * @param mixed $old_value The old value.
+ * @param mixed $new_value The new value to try to set.
+ */
+ public function test_update_network_option_should_not_update_some_values_from_refreshed_cache( $old_value, $new_value ) {
+ add_network_option( null, 'foo', $old_value );
+
+ // Delete and refresh cache from DB.
+ wp_cache_delete( 'alloptions', 'options' );
+ wp_load_alloptions();
+
+ $num_queries = get_num_queries();
+ $updated = update_network_option( null, 'foo', $new_value );
+
+ /*
+ * Strictly equal old and new values will cause an early return
+ * with no additional queries.
+ */
+ $expected_queries = $old_value === $new_value || ! is_multisite() ? 0 : 1;
+ $this->assertSame( $expected_queries, get_num_queries() - $num_queries, "The number of queries should have increased by $expected_queries." );
+
+ $this->assertFalse( $updated, 'update_network_option() should have returned false.' );
+ }
+
+ /**
+ * Data provider.
+ *
+ * @return array[]
+ */
+ public function data_loosely_equal_values_that_should_not_update() {
+ return array(
+ // Truthy values.
+ '(string) "1" to (int) 1' => array( '1', 1 ),
+ '(string) "1" to (float) 1.0' => array( '1', 1.0 ),
+ '(string) "1" to true' => array( '1', true ),
+ '(int) 1 to (string) "1"' => array( 1, '1' ),
+ '1 to (float) 1.0' => array( 1, 1.0 ),
+ '(int) 1 to true' => array( 1, true ),
+ '(float) 1.0 to (string) "1"' => array( 1.0, '1' ),
+ '(float) 1.0 to (int) 1' => array( 1.0, 1 ),
+ '1.0 to true' => array( 1.0, true ),
+ 'true to (string) "1"' => array( true, '1' ),
+ 'true to 1' => array( true, 1 ),
+ 'true to (float) 1.0' => array( true, 1.0 ),
+
+ // Falsey values.
+ '(string) "0" to (int) 0' => array( '0', 0 ),
+ '(string) "0" to (float) 0.0' => array( '0', 0.0 ),
+ '(int) 0 to (string) "0"' => array( 0, '0' ),
+ '(int) 0 to (float) 0.0' => array( 0, 0.0 ),
+ '(float) 0.0 to (string) "0"' => array( 0.0, '0' ),
+ '(float) 0.0 to (int) 0' => array( 0.0, 0 ),
+ 'empty string to false' => array( '', false ),
+
+ /*
+ * null as an initial value behaves differently by triggering
+ * a query, so it is not included in these datasets.
+ *
+ * See data_stored_as_empty_string() and its related test.
+ */
+ );
+ }
+
+ /**
+ * Data provider.
+ *
+ * @return array
+ */
+ public function data_strictly_equal_values() {
+ $obj = new stdClass();
+
+ return array(
+ // Truthy values.
+ '(string) "1"' => array( '1', '1' ),
+ '(int) 1' => array( 1, 1 ),
+ '(float) 1.0' => array( 1.0, 1.0 ),
+ 'true' => array( true, true ),
+ 'string with spaces' => array( ' ', ' ' ),
+ 'non-empty array' => array( array( 'false' ), array( 'false' ) ),
+ 'object' => array( $obj, $obj ),
+
+ // Falsey values.
+ '(string) "0"' => array( '0', '0' ),
+ 'empty string' => array( '', '' ),
+ '(int) 0' => array( 0, 0 ),
+ '(float) 0.0' => array( 0.0, 0.0 ),
+ 'empty array' => array( array(), array() ),
+
+ /*
+ * false and null are not included in these datasets
+ * because false is the default value, which triggers
+ * a call to add_network_option().
+ *
+ * See data_stored_as_empty_string() and its related test.
+ */
+ );
+ }
+
+ /**
+ * Tests that update_network_option() handles a null new value when the new value
+ * is retrieved from the cache.
+ *
+ * On Single Site, this will result in no additional queries as
+ * the option_value database field is not nullable.
+ *
+ * On Multisite, this will result in one additional query as
+ * the meta_value database field is nullable.
+ *
+ * @ticket 59360
+ *
+ * @covers ::update_network_option
+ */
+ public function test_update_option_should_handle_a_null_new_value_from_cache() {
+ add_network_option( null, 'foo', '' );
+
+ $num_queries = get_num_queries();
+
+ // Comparison will happen against value cached during add_option() above.
+ $updated = update_network_option( null, 'foo', null );
+
+ $expected_queries = is_multisite() ? 1 : 0;
+ $this->assertSame( $expected_queries, get_num_queries() - $num_queries, "The number of queries should have increased by $expected_queries." );
+
+ if ( is_multisite() ) {
+ $this->assertTrue( $updated, 'update_network_option() should have returned true.' );
+ } else {
+ $this->assertFalse( $updated, 'update_network_option() should have returned false.' );
+ }
+ }
+
+ /**
+ * Tests that update_network_option() handles a null new value when the new value
+ * is retrieved from the database.
+ *
+ * On Single Site, this will result in only 1 additional query as
+ * the option_value database field is not nullable.
+ *
+ * On Multisite, this will result in two additional queries as
+ * the meta_value database field is nullable.
+ *
+ * @ticket 59360
+ *
+ * @covers ::update_network_option
+ */
+ public function test_update_option_should_handle_a_null_new_value_from_db() {
+ add_network_option( null, 'foo', '' );
+
+ $num_queries = get_num_queries();
+
+ // Delete cache.
+ $network_cache_key = get_current_network_id() . ':foo';
+ wp_cache_delete( $network_cache_key, 'site-options' );
+ wp_cache_delete( 'alloptions', 'options' );
+
+ $updated = update_network_option( null, 'foo', null );
+
+ $expected_queries = is_multisite() ? 2 : 1;
+ $this->assertSame( $expected_queries, get_num_queries() - $num_queries, "The number of queries should have increased by $expected_queries." );
+
+ if ( is_multisite() ) {
+ $this->assertTrue( $updated, 'update_network_option() should have returned true.' );
+ } else {
+ $this->assertFalse( $updated, 'update_network_option() should have returned false.' );
+ }
+ }
+
+ /**
+ * Tests that update_network_option() handles a null new value when the new value
+ * is retrieved from a refreshed cache.
+ *
+ * On Single Site, this will result in no additional queries as
+ * the option_value database field is not nullable.
+ *
+ * On Multisite, this will result in one additional query as
+ * the meta_value database field is nullable.
+ *
+ * @ticket 59360
+ *
+ * @covers ::update_network_option
+ */
+ public function test_update_option_should_handle_a_null_new_value_from_refreshed_cache() {
+ add_network_option( null, 'foo', '' );
+
+ // Delete and refresh cache from DB.
+ wp_cache_delete( 'alloptions', 'options' );
+ wp_load_alloptions();
+
+ $num_queries = get_num_queries();
+ $updated = update_network_option( null, 'foo', null );
+
+ $expected_queries = is_multisite() ? 1 : 0;
+ $this->assertSame( $expected_queries, get_num_queries() - $num_queries, "The number of queries should have increased by $expected_queries." );
+
+ if ( is_multisite() ) {
+ $this->assertTrue( $updated, 'update_network_option() should have returned true.' );
+ } else {
+ $this->assertFalse( $updated, 'update_network_option() should have returned false.' );
+ }
+ }
+
+ /**
+ * Tests that update_network_option() adds a non-existent option when the new value
+ * is stored as an empty string and false is the default value for the option.
+ *
+ * @ticket 59360
+ *
+ * @dataProvider data_stored_as_empty_string
+ *
+ * @param mixed $new_value A value that casts to an empty string.
+ */
+ 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 ) {
+ global $wpdb;
+
+ if ( is_multisite() ) {
+ $this->markTestSkipped( 'This test should only run on Single Site.' );
+ }
+
+ $this->assertTrue( update_network_option( null, 'foo', $new_value ), 'update_network_option() should have returned true.' );
+
+ $actual = $wpdb->get_row( "SELECT option_value FROM $wpdb->options WHERE option_name = 'foo' LIMIT 1" );
+
+ $this->assertIsObject( $actual, 'The option was not added to the database.' );
+ $this->assertObjectHasProperty( 'option_value', $actual, 'The "option_value" property was not included.' );
+ $this->assertSame( '', $actual->option_value, 'The value was not stored as an empty string.' );
+ }
+
+ /**
+ * Data provider.
+ *
+ * @return array[]
+ */
+ public function data_stored_as_empty_string() {
+ return array(
+ 'empty string' => array( '' ),
+ 'null' => array( null ),
+ );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>