<!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>[57016] branches/6.4/tests/phpunit/tests/option: Options, Meta APIs: Rename option cache priming functions.</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/57016">57016</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/57016","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>peterwilsoncc</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-10-27 00:00:15 +0000 (Fri, 27 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: Rename option cache priming functions.
Rename the option cache priming functions to more closely follow the naming convention used by other cache priming functions.
* `wp_load_options()` becomes `wp_prime_option_caches()`
* `wp_load_options_by_group()` becomes `wp_prime_option_caches_by_group()`
The unit test files and classes are renamed accordingly.
Unlike the existing cache priming functions, these functions were introduced with the intention of being public so use the `wp_` prefix rather than the `_` prefix used by the functions initially introduced as private functions but since made public.
Follow up to <a href="https://core.trac.wordpress.org/changeset/56445">[56445]</a>,<a href="https://core.trac.wordpress.org/changeset/56990">[56990]</a>.
Props flixos90, peterwilsoncc, joemcgill, SergeyBiryukov, desrosj.
Reviewed by flixos90.
Merges <a href="https://core.trac.wordpress.org/changeset/57013">[57013]</a> to the 6.4 branch.
Fixes <a href="https://core.trac.wordpress.org/ticket/58962">#58962</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#branches64srcwpincludesoptionphp">branches/6.4/src/wp-includes/option.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#branches64testsphpunittestsoptionwpPrimeOptionCachesphp">branches/6.4/tests/phpunit/tests/option/wpPrimeOptionCaches.php</a></li>
<li><a href="#branches64testsphpunittestsoptionwpPrimeOptionCachesByGroupphp">branches/6.4/tests/phpunit/tests/option/wpPrimeOptionCachesByGroup.php</a></li>
</ul>
<h3>Removed Paths</h3>
<ul>
<li><a href="#branches64testsphpunittestsoptionwpLoadOptionsphp">branches/6.4/tests/phpunit/tests/option/wpLoadOptions.php</a></li>
<li><a href="#branches64testsphpunittestsoptionwpLoadOptionsByGroupphp">branches/6.4/tests/phpunit/tests/option/wpLoadOptionsByGroup.php</a></li>
</ul>
<h3>Property Changed</h3>
<ul>
<li><a href="#branches64">branches/6.4/</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<span class="cx" style="display: block; padding: 0 10px">Index: branches/6.4
</span><span class="cx" style="display: block; padding: 0 10px">===================================================================
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">--- branches/6.4 2023-10-26 23:15:31 UTC (rev 57015)
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+++ branches/6.4 2023-10-27 00:00:15 UTC (rev 57016)
</ins><a id="branches64"></a>
<div class="propset"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Property changes: branches/6.4</h4>
<pre class="diff"><span>
</span></pre></div>
<a id="svnmergeinfo"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: svn:mergeinfo</h4></div>
<span class="cx" style="display: block; padding: 0 10px"> /branches/5.0:43681-43682,43684-43688,43719-43720,43723,43726-43727,43729-43731,43734-43744,43747,43751-43754,43758,43760-43765,43767-43770,43772,43774-43781,43783,43785,43790-43806,43808-43821,43825,43828,43830-43834,43836-43843,43846-43863,43867-43889,43891-43894,43897-43905,43908-43909,43911-43929,43931-43942,43946-43947,43949-43956,43959-43964,43967-43969,43988,43994,44014,44017,44047,44183,44185,44187-44206,44208-44213,44231-44232,44235,44248,44284,44287-44288
</span><span class="cx" style="display: block; padding: 0 10px"> /branches/5.5:49373-49379,49381
</span><span class="cx" style="display: block; padding: 0 10px"> /branches/5.8:51889
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-/trunk:56974,56978,56984,56987,56990-56992,56996,56999,57003,57009,57012
</del><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/trunk:56974,56978,56984,56987,56990-56992,56996,56999,57003,57009,57012-57013
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="branches64srcwpincludesoptionphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/6.4/src/wp-includes/option.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/6.4/src/wp-includes/option.php 2023-10-26 23:15:31 UTC (rev 57015)
+++ branches/6.4/src/wp-includes/option.php 2023-10-27 00:00:15 UTC (rev 57016)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -248,7 +248,7 @@
</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">- * Loads specific options into the cache with a single database query.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Primes specific options into the cache with a single database query.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * Only options that do not already exist in cache will be loaded.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -258,7 +258,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $options An array of option names to be loaded.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function wp_load_options( $options ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function wp_prime_option_caches( $options ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $alloptions = wp_load_alloptions();
</span><span class="cx" style="display: block; padding: 0 10px"> $cached_options = wp_cache_get_multiple( $options, 'options' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -321,7 +321,7 @@
</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">- * Loads all options registered with a specific option group.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Primes the cache of all options registered with a specific option group.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 6.4.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -329,11 +329,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $option_group The option group to load options for.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function wp_load_options_by_group( $option_group ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function wp_prime_option_caches_by_group( $option_group ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> global $new_allowed_options;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $new_allowed_options[ $option_group ] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_load_options( $new_allowed_options[ $option_group ] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_prime_option_caches( $new_allowed_options[ $option_group ] );
</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">@@ -348,7 +348,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return array An array of key-value pairs for the requested options.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function get_options( $options ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- wp_load_options( $options );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_prime_option_caches( $options );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $result = array();
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $options as $option ) {
</span></span></pre></div>
<a id="branches64testsphpunittestsoptionwpLoadOptionsphp"></a>
<div class="delfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Deleted: branches/6.4/tests/phpunit/tests/option/wpLoadOptions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/6.4/tests/phpunit/tests/option/wpLoadOptions.php 2023-10-26 23:15:31 UTC (rev 57015)
+++ branches/6.4/tests/phpunit/tests/option/wpLoadOptions.php 2023-10-27 00:00:15 UTC (rev 57016)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,130 +0,0 @@
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-<?php
-/**
- * Test wp_load_options().
- *
- * @group option
- *
- * @covers ::wp_load_options
- */
-class Tests_Option_PrimeOptions extends WP_UnitTestCase {
-
- /**
- * Tests that wp_load_options() loads multiple options.
- *
- * @ticket 58962
- */
- public function test_wp_load_options() {
- // Create some options to load.
- $options_to_load = array(
- 'option1',
- 'option2',
- 'option3',
- );
-
- /*
- * Set values for the options,
- * clear the cache for the options,
- * check options are not in cache initially.
- */
- foreach ( $options_to_load as $option ) {
- update_option( $option, "value_$option", false );
- wp_cache_delete( $option, 'options' );
- $this->assertFalse( wp_cache_get( $option, 'options' ), "$option was not deleted from the cache." );
- }
-
- // Call the wp_load_options function to load the options.
- wp_load_options( $options_to_load );
-
- // Store the initial database query count.
- $initial_query_count = get_num_queries();
-
- // Check that options are only in the 'options' cache group.
- foreach ( $options_to_load as $option ) {
- $this->assertSame(
- wp_cache_get( $option, 'options' ),
- get_option( $option ),
- "$option was not loaded to the 'options' cache group."
- );
-
- $this->assertFalse(
- wp_cache_get( $option, 'notoptions' ),
- get_option( $option ),
- "$option was loaded to the 'notoptions' cache group."
- );
- }
-
- // Ensure no additional database queries were made.
- $this->assertSame(
- $initial_query_count,
- get_num_queries(),
- 'Additional database queries were made.'
- );
- }
-
- /**
- * Tests wp_load_options() with options that do not exist in the database.
- *
- * @ticket 58962
- */
- public function test_wp_load_options_with_nonexistent_options() {
- // Create some options to load.
- $options_to_load = array(
- 'option1',
- 'option2',
- );
-
- /*
- * Set values for the options,
- * clear the cache for the options,
- * check options are not in cache initially.
- */
- foreach ( $options_to_load as $option ) {
- $this->assertFalse( wp_cache_get( $option, 'options' ), "$option was not deleted from the cache." );
- }
-
- // Call the wp_load_options function to load the options.
- wp_load_options( $options_to_load );
-
- // Check that options are not in the cache or database.
- foreach ( $options_to_load as $option ) {
- $this->assertFalse( wp_cache_get( $option, 'options' ), "$option was not deleted from the cache." );
- }
-
- // Check that options are present in the notoptions cache.
- $new_notoptions = wp_cache_get( 'notoptions', 'options' );
- $this->assertIsArray( $new_notoptions, 'The notoptions cache should be an array.' );
- foreach ( $options_to_load as $option ) {
- $this->assertArrayHasKey( $option, $new_notoptions, "$option was not added to the notoptions cache." );
- }
- }
-
- /**
- * Tests wp_load_options() with an empty array.
- *
- * @ticket 58962
- */
- public function test_wp_load_options_with_empty_array() {
- $alloptions = wp_load_alloptions();
- $notoptions = wp_cache_get( 'notoptions', 'options' );
-
- wp_load_options( array() );
-
- $this->assertSame( $alloptions, wp_cache_get( 'alloptions', 'options' ), 'The alloptions cache was modified.' );
- $this->assertSame( $notoptions, wp_cache_get( 'notoptions', 'options' ), 'The notoptions cache was modified.' );
- }
-
- /**
- * Tests that wp_load_options handles an empty "notoptions" cache.
- *
- * @ticket 58962
- */
- public function test_wp_load_options_handles_empty_notoptions_cache() {
- wp_cache_delete( 'notoptions', 'options' );
-
- wp_load_options( array( 'nonexistent_option' ) );
-
- $notoptions = wp_cache_get( 'notoptions', 'options' );
- $this->assertIsArray( $notoptions, 'The notoptions cache should be an array.' );
- $this->assertArrayHasKey( 'nonexistent_option', $notoptions, 'nonexistent_option was not added to notoptions.' );
- }
-}
</del></span></pre></div>
<a id="branches64testsphpunittestsoptionwpLoadOptionsByGroupphp"></a>
<div class="delfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Deleted: branches/6.4/tests/phpunit/tests/option/wpLoadOptionsByGroup.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/6.4/tests/phpunit/tests/option/wpLoadOptionsByGroup.php 2023-10-26 23:15:31 UTC (rev 57015)
+++ branches/6.4/tests/phpunit/tests/option/wpLoadOptionsByGroup.php 2023-10-27 00:00:15 UTC (rev 57016)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,75 +0,0 @@
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-<?php
-/**
- * Test wp_load_options_by_group().
- *
- * @group option
- *
- * @covers ::wp_load_options_by_group
- */
-class Tests_Option_PrimeOptionsByGroup extends WP_UnitTestCase {
-
- /**
- * Tests that wp_load_options_by_group() only loads options in the specified group.
- *
- * @ticket 58962
- */
- public function test_wp_load_options_by_group() {
- global $new_allowed_options;
-
- // Create some options to load.
- $new_allowed_options = array(
- 'group1' => array(
- 'option1',
- 'option2',
- ),
- 'group2' => array(
- 'option3',
- ),
- );
-
- $options_to_load = array(
- 'option1',
- 'option2',
- 'option3',
- );
-
- /*
- * Set values for the options,
- * clear the cache for the options,
- * check options are not in cache initially.
- */
- foreach ( $options_to_load as $option ) {
- update_option( $option, "value_$option", false );
- wp_cache_delete( $option, 'options' );
- $this->assertFalse( wp_cache_get( $option, 'options' ), "$option was not deleted from the cache." );
- }
-
- // Call the wp_load_options_by_group function to load the options.
- wp_load_options_by_group( 'group1' );
-
- // Check that options are now in the cache.
- $this->assertSame( get_option( 'option1' ), wp_cache_get( 'option1', 'options' ), 'option1 was not loaded.' );
- $this->assertSame( get_option( 'option2' ), wp_cache_get( 'option2', 'options' ), 'option2 was not loaded.' );
-
- // Make sure option3 is still not in cache.
- $this->assertFalse( wp_cache_get( 'option3', 'options' ), 'option3 was not deleted from the cache.' );
- }
-
- /**
- * Tests wp_load_options_by_group() with a nonexistent option group.
- *
- * @ticket 58962
- */
- public function test_wp_load_options_by_group_with_nonexistent_group() {
- // Make sure options are not in cache or database initially.
- $this->assertFalse( wp_cache_get( 'option1', 'options' ), 'option1 was not deleted from the cache.' );
- $this->assertFalse( wp_cache_get( 'option2', 'options' ), 'option2 was not deleted from the cache.' );
-
- // Call the wp_load_options_by_group function with a nonexistent group.
- wp_load_options_by_group( 'nonexistent_group' );
-
- // Check that options are still not in the cache or database.
- $this->assertFalse( wp_cache_get( 'option1', 'options' ), 'option1 was not deleted from the cache.' );
- $this->assertFalse( wp_cache_get( 'option2', 'options' ), 'option2 was not deleted from the cache.' );
- }
-}
</del></span></pre></div>
<a id="branches64testsphpunittestsoptionwpPrimeOptionCachesphpfromrev57013trunktestsphpunittestsoptionwpPrimeOptionCachesphp"></a>
<div class="copfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Copied: branches/6.4/tests/phpunit/tests/option/wpPrimeOptionCaches.php (from rev 57013, trunk/tests/phpunit/tests/option/wpPrimeOptionCaches.php)</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/6.4/tests/phpunit/tests/option/wpPrimeOptionCaches.php (rev 0)
+++ branches/6.4/tests/phpunit/tests/option/wpPrimeOptionCaches.php 2023-10-27 00:00:15 UTC (rev 57016)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,130 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Test wp_prime_option_caches().
+ *
+ * @group option
+ *
+ * @covers ::wp_prime_option_caches
+ */
+class Tests_Option_WpPrimeOptionCaches extends WP_UnitTestCase {
+
+ /**
+ * Tests that wp_prime_option_caches() primes multiple options.
+ *
+ * @ticket 58962
+ */
+ public function test_wp_prime_option_caches() {
+ // Create some options to prime.
+ $options_to_prime = array(
+ 'option1',
+ 'option2',
+ 'option3',
+ );
+
+ /*
+ * Set values for the options,
+ * clear the cache for the options,
+ * check options are not in cache initially.
+ */
+ foreach ( $options_to_prime as $option ) {
+ update_option( $option, "value_$option", false );
+ wp_cache_delete( $option, 'options' );
+ $this->assertFalse( wp_cache_get( $option, 'options' ), "$option was not deleted from the cache." );
+ }
+
+ // Call the wp_prime_option_caches function to prime the options.
+ wp_prime_option_caches( $options_to_prime );
+
+ // Store the initial database query count.
+ $initial_query_count = get_num_queries();
+
+ // Check that options are only in the 'options' cache group.
+ foreach ( $options_to_prime as $option ) {
+ $this->assertSame(
+ wp_cache_get( $option, 'options' ),
+ get_option( $option ),
+ "$option was not primed in the 'options' cache group."
+ );
+
+ $this->assertFalse(
+ wp_cache_get( $option, 'notoptions' ),
+ get_option( $option ),
+ "$option was primed in the 'notoptions' cache group."
+ );
+ }
+
+ // Ensure no additional database queries were made.
+ $this->assertSame(
+ $initial_query_count,
+ get_num_queries(),
+ 'Additional database queries were made.'
+ );
+ }
+
+ /**
+ * Tests wp_prime_option_caches() with options that do not exist in the database.
+ *
+ * @ticket 58962
+ */
+ public function test_wp_prime_option_caches_with_nonexistent_options() {
+ // Create some options to prime.
+ $options_to_prime = array(
+ 'option1',
+ 'option2',
+ );
+
+ /*
+ * Set values for the options,
+ * clear the cache for the options,
+ * check options are not in cache initially.
+ */
+ foreach ( $options_to_prime as $option ) {
+ $this->assertFalse( wp_cache_get( $option, 'options' ), "$option was not deleted from the cache." );
+ }
+
+ // Call the wp_prime_option_caches function to prime the options.
+ wp_prime_option_caches( $options_to_prime );
+
+ // Check that options are not in the cache or database.
+ foreach ( $options_to_prime as $option ) {
+ $this->assertFalse( wp_cache_get( $option, 'options' ), "$option was not deleted from the cache." );
+ }
+
+ // Check that options are present in the notoptions cache.
+ $new_notoptions = wp_cache_get( 'notoptions', 'options' );
+ $this->assertIsArray( $new_notoptions, 'The notoptions cache should be an array.' );
+ foreach ( $options_to_prime as $option ) {
+ $this->assertArrayHasKey( $option, $new_notoptions, "$option was not added to the notoptions cache." );
+ }
+ }
+
+ /**
+ * Tests wp_prime_option_caches() with an empty array.
+ *
+ * @ticket 58962
+ */
+ public function test_wp_prime_option_caches_with_empty_array() {
+ $alloptions = wp_load_alloptions();
+ $notoptions = wp_cache_get( 'notoptions', 'options' );
+
+ wp_prime_option_caches( array() );
+
+ $this->assertSame( $alloptions, wp_cache_get( 'alloptions', 'options' ), 'The alloptions cache was modified.' );
+ $this->assertSame( $notoptions, wp_cache_get( 'notoptions', 'options' ), 'The notoptions cache was modified.' );
+ }
+
+ /**
+ * Tests that wp_prime_option_caches() handles an empty "notoptions" cache.
+ *
+ * @ticket 58962
+ */
+ public function test_wp_prime_option_caches_handles_empty_notoptions_cache() {
+ wp_cache_delete( 'notoptions', 'options' );
+
+ wp_prime_option_caches( array( 'nonexistent_option' ) );
+
+ $notoptions = wp_cache_get( 'notoptions', 'options' );
+ $this->assertIsArray( $notoptions, 'The notoptions cache should be an array.' );
+ $this->assertArrayHasKey( 'nonexistent_option', $notoptions, 'nonexistent_option was not added to notoptions.' );
+ }
+}
</ins></span></pre></div>
<a id="branches64testsphpunittestsoptionwpPrimeOptionCachesByGroupphpfromrev57013trunktestsphpunittestsoptionwpPrimeOptionCachesByGroupphp"></a>
<div class="copfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Copied: branches/6.4/tests/phpunit/tests/option/wpPrimeOptionCachesByGroup.php (from rev 57013, trunk/tests/phpunit/tests/option/wpPrimeOptionCachesByGroup.php)</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/6.4/tests/phpunit/tests/option/wpPrimeOptionCachesByGroup.php (rev 0)
+++ branches/6.4/tests/phpunit/tests/option/wpPrimeOptionCachesByGroup.php 2023-10-27 00:00:15 UTC (rev 57016)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,75 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Test wp_prime_option_caches_by_group().
+ *
+ * @group option
+ *
+ * @covers ::wp_prime_option_caches_by_group
+ */
+class Tests_Option_WpPrimeOptionCachesByGroup extends WP_UnitTestCase {
+
+ /**
+ * Tests that wp_prime_option_caches_by_group() only primes options in the specified group.
+ *
+ * @ticket 58962
+ */
+ public function test_wp_prime_option_caches_by_group() {
+ global $new_allowed_options;
+
+ // Create some options to prime.
+ $new_allowed_options = array(
+ 'group1' => array(
+ 'option1',
+ 'option2',
+ ),
+ 'group2' => array(
+ 'option3',
+ ),
+ );
+
+ $options_to_prime = array(
+ 'option1',
+ 'option2',
+ 'option3',
+ );
+
+ /*
+ * Set values for the options,
+ * clear the cache for the options,
+ * check options are not in cache initially.
+ */
+ foreach ( $options_to_prime as $option ) {
+ update_option( $option, "value_$option", false );
+ wp_cache_delete( $option, 'options' );
+ $this->assertFalse( wp_cache_get( $option, 'options' ), "$option was not deleted from the cache." );
+ }
+
+ // Call the wp_prime_option_caches_by_group function to prime the options.
+ wp_prime_option_caches_by_group( 'group1' );
+
+ // Check that options are now in the cache.
+ $this->assertSame( get_option( 'option1' ), wp_cache_get( 'option1', 'options' ), 'option1\'s cache was not primed.' );
+ $this->assertSame( get_option( 'option2' ), wp_cache_get( 'option2', 'options' ), 'option2\'s cache was not primed.' );
+
+ // Make sure option3 is still not in cache.
+ $this->assertFalse( wp_cache_get( 'option3', 'options' ), 'option3 was not deleted from the cache.' );
+ }
+
+ /**
+ * Tests wp_prime_option_caches_by_group() with a nonexistent option group.
+ *
+ * @ticket 58962
+ */
+ public function test_wp_prime_option_caches_by_group_with_nonexistent_group() {
+ // Make sure options are not in cache or database initially.
+ $this->assertFalse( wp_cache_get( 'option1', 'options' ), 'option1 was not deleted from the cache.' );
+ $this->assertFalse( wp_cache_get( 'option2', 'options' ), 'option2 was not deleted from the cache.' );
+
+ // Call the wp_prime_option_caches_by_group function with a nonexistent group.
+ wp_prime_option_caches_by_group( 'nonexistent_group' );
+
+ // Check that options are still not in the cache or database.
+ $this->assertFalse( wp_cache_get( 'option1', 'options' ), 'option1 was not deleted from the cache.' );
+ $this->assertFalse( wp_cache_get( 'option2', 'options' ), 'option2 was not deleted from the cache.' );
+ }
+}
</ins></span></pre>
</div>
</div>
</body>
</html>