<!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>[56762] trunk/tests/phpunit/tests/option/option.php: Options, Meta APIs: Implement additional tests covering `update_option()`.</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/56762">56762</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/56762","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-03 14:09:07 +0000 (Tue, 03 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: Implement additional tests covering `update_option()`.
As a follow up to <a href="https://core.trac.wordpress.org/changeset/56681">[56681]</a>, this changeset adds further tests, primarily focused on ensuring no unnecessary database queries are run.
Props mukesh27, costdev, joemcgill, spacedmonkey.
See <a href="https://core.trac.wordpress.org/ticket/22192">#22192</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsoptionoptionphp">trunk/tests/phpunit/tests/option/option.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunktestsphpunittestsoptionoptionphp"></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/option.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/option/option.php 2023-10-03 08:52:54 UTC (rev 56761)
+++ trunk/tests/phpunit/tests/option/option.php 2023-10-03 14:09:07 UTC (rev 56762)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -370,60 +370,78 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Ensure the database is getting updated when type changes, but not otherwise.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Tests that update_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.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * The additional query is triggered to update the value in the database.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @ticket 22192
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers ::update_option
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @dataProvider data_update_option_type_juggling
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @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.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_update_loosey_options( $old_value, $new_value, $update = false ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_update_option_should_update_some_loosely_equal_values_from_cache( $old_value, $new_value ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> add_option( 'foo', $old_value );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $num_queries = get_num_queries();
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Comparison will happen against value cached during add_option() above.
</span><span class="cx" style="display: block; padding: 0 10px"> $updated = update_option( 'foo', $new_value );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $update ) {
- $this->assertTrue( $updated, 'This loosely equal option should trigger an update.' );
- } else {
- $this->assertFalse( $updated, 'Loosely equal option should not trigger an update.' );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSame( 1, get_num_queries() - $num_queries, 'One additional query should have run to update the value.' );
+ $this->assertTrue( $updated, 'update_option() should have returned true.' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Ensure the database is getting updated when type changes, but not otherwise.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Tests that update_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.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * 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.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @ticket 22192
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers ::update_option
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @dataProvider data_update_option_type_juggling
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @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.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_update_loosey_options_from_db( $old_value, $new_value, $update = false ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_update_option_should_update_some_loosely_equal_values_from_db( $old_value, $new_value ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> add_option( 'foo', $old_value );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $num_queries = get_num_queries();
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Delete cache.
</span><span class="cx" style="display: block; padding: 0 10px"> wp_cache_delete( 'alloptions', 'options' );
</span><span class="cx" style="display: block; padding: 0 10px"> $updated = update_option( 'foo', $new_value );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $update ) {
- $this->assertTrue( $updated, 'This loosely equal option should trigger an update.' );
- } else {
- $this->assertFalse( $updated, 'Loosely equal option should not trigger an update.' );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSame( 2, get_num_queries() - $num_queries, 'Two additional queries should have run.' );
+ $this->assertTrue( $updated, 'update_option() should have returned true.' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Ensure the database is getting updated when type changes, but not otherwise.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Tests that update_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.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * The additional query is triggered to update the value in the database.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @ticket 22192
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers ::update_option
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @dataProvider data_update_option_type_juggling
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @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.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_update_loosey_options_from_refreshed_cache( $old_value, $new_value, $update = false ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_update_option_should_update_some_loosely_equal_values_from_refreshed_cache( $old_value, $new_value ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> add_option( 'foo', $old_value );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Delete and refresh cache from DB.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -430,13 +448,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> wp_cache_delete( 'alloptions', 'options' );
</span><span class="cx" style="display: block; padding: 0 10px"> wp_load_alloptions();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $updated = update_option( 'foo', $new_value );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $num_queries = get_num_queries();
+ $updated = update_option( 'foo', $new_value );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $update ) {
- $this->assertTrue( $updated, 'This loosely equal option should trigger an update.' );
- } else {
- $this->assertFalse( $updated, 'Loosely equal option should not trigger an update.' );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSame( 1, get_num_queries() - $num_queries, 'One additional query should have run to update the value.' );
+ $this->assertTrue( $updated, 'update_option() should have returned true.' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -444,119 +460,239 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @return array
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function data_update_option_type_juggling() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function data_loosely_equal_values_that_should_update() {
</ins><span class="cx" style="display: block; padding: 0 10px"> return array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- /*
- * Truthy values.
- * Loosely equal truthy scalar values should never result in a DB update.
- */
- array( '1', '1' ),
- array( '1', 1 ),
- array( '1', 1.0 ),
- array( '1', true ),
- array( 1, '1' ),
- array( 1, 1 ),
- array( 1, 1.0 ),
- array( 1, true ),
- array( 1.0, '1' ),
- array( 1.0, 1 ),
- array( 1.0, 1.0 ),
- array( 1.0, true ),
- array( true, '1' ),
- array( true, 1 ),
- array( true, 1.0 ),
- array( true, true ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // 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 ),
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // 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_option() triggers no additional queries and returns false
+ * for some values when the old value is retrieved from the cache.
+ *
+ * @ticket 22192
+ *
+ * @covers ::update_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_option( 'foo', $old_value );
+
+ $num_queries = get_num_queries();
+
+ // Comparison will happen against value cached during add_option() above.
+ $updated = update_option( 'foo', $new_value );
+
+ $this->assertSame( $num_queries, get_num_queries(), 'No additional queries should have run.' );
+ $this->assertFalse( $updated, 'update_option() should have returned false.' );
+ }
+
+ /**
+ * Tests that update_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 22192
+ *
+ * @covers ::update_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_db( $old_value, $new_value ) {
+ add_option( 'foo', $old_value );
+
+ $num_queries = get_num_queries();
+
+ // Delete cache.
+ wp_cache_delete( 'alloptions', 'options' );
+ $updated = update_option( 'foo', $new_value );
+
+ $this->assertSame( 1, get_num_queries() - $num_queries, 'One additional query should have run.' );
+ $this->assertFalse( $updated, 'update_option() should have returned false.' );
+ }
+
+ /**
+ * Tests that update_option() triggers no additional queries and returns false
+ * for some values when the old value is retrieved from a refreshed cache.
+ *
+ * @ticket 22192
+ *
+ * @covers ::update_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_refreshed_cache( $old_value, $new_value ) {
+ add_option( 'foo', $old_value );
+
+ // Delete and refresh cache from DB.
+ wp_cache_delete( 'alloptions', 'options' );
+ wp_load_alloptions();
+
+ $num_queries = get_num_queries();
+ $updated = update_option( 'foo', $new_value );
+
+ $this->assertSame( $num_queries, get_num_queries(), 'No additional queries should have run.' );
+ $this->assertFalse( $updated, 'update_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 ),
+ 'empty string to null' => array( '', null ),
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /*
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Falsey values.
- * Loosely equal falsey scalar values only sometimes result in a DB update.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * 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.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- array( '0', '0' ),
- array( '0', 0 ),
- array( '0', 0.0 ),
- array( '0', false, true ), // Should update.
- array( '', '' ),
- array( '', 0, true ), // Should update.
- array( '', 0.0, true ), // Should update.
- array( '', false ),
- array( 0, '0' ),
- array( 0, '', true ), // Should update.
- array( 0, 0 ),
- array( 0, 0.0 ),
- array( 0, false, true ), // Should update.
- array( 0.0, '0' ),
- array( 0.0, '', true ), // Should update.
- array( 0.0, 0 ),
- array( 0.0, 0.0 ),
- array( 0.0, false, true ), // Should update.
- array( false, '0', true ), // Should update.
- array( false, '' ),
- array( false, 0, true ), // Should update.
- array( false, 0.0, true ), // Should update.
- array( false, false ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * 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() ),
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /*
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Non scalar values.
- * Loosely equal non-scalar values should almost always result in an update.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * false and null are not included in these datasets
+ * because false is the default value, which triggers
+ * a call to add_option().
+ *
+ * See data_stored_as_empty_string() and its related test.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- array( false, array(), true ),
- array( 'false', array(), true ),
- array( '', array(), true ),
- array( 0, array(), true ),
- array( '0', array(), true ),
- array( false, null ), // Does not update.
- array( 'false', null, true ),
- array( '', null ), // Does not update.
- array( 0, null, true ),
- array( '0', null, true ),
- array( array(), false, true ),
- array( array(), 'false', true ),
- array( array(), '', true ),
- array( array(), 0, true ),
- array( array(), '0', true ),
- array( array(), null, true ),
- array( null, false ), // Does not update.
- array( null, 'false', true ),
- array( null, '' ), // Does not update.
- array( null, 0, true ),
- array( null, '0', true ),
- array( null, array(), true ),
</del><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Tests that update_option() stores an option that uses
- * an unfiltered default value of (bool) false.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Tests that update_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.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 22192
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @covers ::update_option
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @dataProvider data_stored_as_empty_string
+ *
+ * @param mixed $new_value A value that casts to an empty string.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_update_option_should_store_option_with_default_value_false() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_update_option_should_add_option_when_the_new_value_is_stored_as_an_empty_string_and_matches_default_value_false( $new_value ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> global $wpdb;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $option = 'update_option_default_false';
- update_option( $option, false );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertTrue( update_option( 'foo', $new_value ), 'update_option() should have returned true.' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $actual = $wpdb->query(
- $wpdb->prepare(
- "SELECT option_name FROM $wpdb->options WHERE option_name = %s LIMIT 1",
- $option
- )
- );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $actual = $wpdb->get_row( "SELECT option_value FROM $wpdb->options WHERE option_name = 'foo' LIMIT 1" );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertSame( 1, $actual );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $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.' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Tests that update_option() stores an option that uses
- * a filtered default value.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Data provider.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return array[]
+ */
+ public function data_stored_as_empty_string() {
+ return array(
+ 'false' => array( false ),
+ 'empty string' => array( '' ),
+ 'null' => array( null ),
+ );
+ }
+
+ /**
+ * Tests that update_option() adds a non-existent option that uses a filtered default value.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @ticket 22192
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers ::update_option
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_update_option_should_store_option_with_filtered_default_value() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_update_option_should_add_option_with_filtered_default_value() {
</ins><span class="cx" style="display: block; padding: 0 10px"> global $wpdb;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $option = 'update_option_custom_default';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -569,20 +705,22 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- update_option( $option, $default_value );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertTrue( update_option( $option, $default_value ), 'update_option() should have returned true.' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $actual = $wpdb->query(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $actual = $wpdb->get_row(
</ins><span class="cx" style="display: block; padding: 0 10px"> $wpdb->prepare(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- "SELECT option_name FROM $wpdb->options WHERE option_name = %s LIMIT 1",
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1",
</ins><span class="cx" style="display: block; padding: 0 10px"> $option
</span><span class="cx" style="display: block; padding: 0 10px"> )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertSame( 1, $actual );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $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( $default_value, $actual->option_value, 'The value was not stored as an empty string.' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Tests that a non-existing option is added even when its pre filter returns a value.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Tests that a non-existent option is added even when its pre filter returns a value.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 22192
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span></span></pre>
</div>
</div>
</body>
</html>