<!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>[53483] trunk: Query: Some documentation and test improvements for `update_post_author_caches()`:</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/53483">53483</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/53483","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>SergeyBiryukov</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2022-06-10 15:15:07 +0000 (Fri, 10 Jun 2022)</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'>Query: Some documentation and test improvements for `update_post_author_caches()`:

* Make the descriptions for `update_post_author_caches()` and `update_post_caches()` more specific.
* Move the unit test into its own file, for consistency with `update_post_cache()` tests. This also allows for using shared fixtures in case more tests are added in the future.

Follow-up to <a href="https://core.trac.wordpress.org/changeset/53482">[53482]</a>.

See <a href="https://core.trac.wordpress.org/ticket/55716">#55716</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpqueryphp">trunk/src/wp-includes/class-wp-query.php</a></li>
<li><a href="#trunksrcwpincludespluggablephp">trunk/src/wp-includes/pluggable.php</a></li>
<li><a href="#trunksrcwpincludespostphp">trunk/src/wp-includes/post.php</a></li>
<li><a href="#trunktestsphpunittestspostupdatePostCachephp">trunk/tests/phpunit/tests/post/updatePostCache.php</a></li>
<li><a href="#trunktestsphpunittestsqueryphp">trunk/tests/phpunit/tests/query.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestspostupdatePostAuthorCachesphp">trunk/tests/phpunit/tests/post/updatePostAuthorCaches.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpqueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-query.php  2022-06-10 13:37:52 UTC (rev 53482)
+++ trunk/src/wp-includes/class-wp-query.php    2022-06-10 15:15:07 UTC (rev 53483)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3434,9 +3434,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function the_post() {
</span><span class="cx" style="display: block; padding: 0 10px">                global $post;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( ! $this->in_the_loop ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        update_post_author_caches( $this->posts );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->in_the_loop = true;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( -1 == $this->current_post ) { // Loop has just started.
</span></span></pre></div>
<a id="trunksrcwpincludespluggablephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/pluggable.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/pluggable.php       2022-06-10 13:37:52 UTC (rev 53482)
+++ trunk/src/wp-includes/pluggable.php 2022-06-10 15:15:07 UTC (rev 53483)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -143,6 +143,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $list = implode( ',', $clean );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $users = $wpdb->get_results( "SELECT * FROM $wpdb->users WHERE ID IN ($list)" );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">                 foreach ( $users as $user ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        update_user_caches( $user );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span></span></pre></div>
<a id="trunksrcwpincludespostphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/post.php    2022-06-10 13:37:52 UTC (rev 53482)
+++ trunk/src/wp-includes/post.php      2022-06-10 15:15:07 UTC (rev 53483)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7424,11 +7424,11 @@
</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">- * Calls major cache updating functions for list of Post objects.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Updates post, term, and metadata caches for a list of post objects.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 1.5.0
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_Post[] $posts             Array of Post objects
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param WP_Post[] $posts             Array of post objects (passed by reference).
</ins><span class="cx" style="display: block; padding: 0 10px">  * @param string    $post_type         Optional. Post type. Default 'post'.
</span><span class="cx" style="display: block; padding: 0 10px">  * @param bool      $update_term_cache Optional. Whether to update the term cache. Default true.
</span><span class="cx" style="display: block; padding: 0 10px">  * @param bool      $update_meta_cache Optional. Whether to update the meta cache. Default true.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7475,21 +7475,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">- * Prime post author user caches.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Updates post author user caches for a list of post objects.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 6.1.0
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_Post[] $posts Array of Post objects
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param WP_Post[] $posts Array of post objects.
</ins><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function update_post_author_caches( $posts ) {
</span><span class="cx" style="display: block; padding: 0 10px">        $author_ids = wp_list_pluck( $posts, 'post_author' );
</span><span class="cx" style="display: block; padding: 0 10px">        $author_ids = array_map( 'absint', $author_ids );
</span><span class="cx" style="display: block; padding: 0 10px">        $author_ids = array_unique( array_filter( $author_ids ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">         cache_users( $author_ids );
</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">- * Updates metadata cache for list of post IDs.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Updates metadata cache for a list of post IDs.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * Performs SQL query to retrieve the metadata for the post IDs and updates the
</span><span class="cx" style="display: block; padding: 0 10px">  * metadata cache for the posts. Therefore, the functions, which call this
</span></span></pre></div>
<a id="trunktestsphpunittestspostupdatePostAuthorCachesphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/post/updatePostAuthorCaches.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/post/updatePostAuthorCaches.php                         (rev 0)
+++ trunk/tests/phpunit/tests/post/updatePostAuthorCaches.php   2022-06-10 15:15:07 UTC (rev 53483)
</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 `update_post_author_caches()`.
+ *
+ * @package WordPress
+ */
+
+/**
+ * Test class for `update_post_author_caches()`.
+ *
+ * @group post
+ * @group query
+ * @group user
+ *
+ * @covers ::update_post_author_caches
+ */
+class Tests_Post_UpdatePostAuthorCaches extends WP_UnitTestCase {
+
+       /**
+        * User IDs from the shared fixture.
+        *
+        * @var int[]
+        */
+       public static $user_ids;
+
+       /**
+        * Post author count.
+        *
+        * @var int
+        */
+       public static $post_author_count = 5;
+
+       /**
+        * Set up test resources before the class.
+        *
+        * @param WP_UnitTest_Factory $factory The unit test factory.
+        */
+       public static function wpSetupBeforeClass( WP_UnitTest_Factory $factory ) {
+               self::$user_ids = array();
+
+               for ( $i = 0; $i < self::$post_author_count; $i++ ) {
+                       self::$user_ids[ $i ] = $factory->user->create();
+                       $factory->post->create(
+                               array(
+                                       'post_type'   => 'post',
+                                       'post_author' => self::$user_ids[ $i ],
+                               )
+                       );
+               }
+       }
+
+       /**
+        * @ticket 55716
+        */
+       public function test_update_post_author_caches() {
+               $action = new MockAction();
+               add_filter( 'update_user_metadata_cache', array( $action, 'filter' ), 10, 2 );
+
+               $q = new WP_Query(
+                       array(
+                               'post_type'      => 'post',
+                               'posts_per_page' => self::$post_author_count,
+                       )
+               );
+
+               while ( $q->have_posts() ) {
+                       $q->the_post();
+               }
+
+               $args      = $action->get_args();
+               $last_args = end( $args );
+
+               $this->assertSameSets( self::$user_ids, $last_args[1], 'Ensure that user IDs are primed' );
+       }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/post/updatePostAuthorCaches.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestspostupdatePostCachephp"></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/post/updatePostCache.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/post/updatePostCache.php        2022-06-10 13:37:52 UTC (rev 53482)
+++ trunk/tests/phpunit/tests/post/updatePostCache.php  2022-06-10 15:15:07 UTC (rev 53483)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,12 +1,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Test `wp_update_cache()`.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Test `update_post_cache()`.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @package WordPress
</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">- * Test class for `wp_update_cache()`.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Test class for `update_post_cache()`.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @group post
</span><span class="cx" style="display: block; padding: 0 10px">  * @group query
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -18,7 +18,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Post IDs from the shared fixture.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @var array
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @var int[]
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public static $post_ids;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunktestsphpunittestsqueryphp"></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/query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/query.php       2022-06-10 13:37:52 UTC (rev 53482)
+++ trunk/tests/phpunit/tests/query.php 2022-06-10 15:15:07 UTC (rev 53483)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -621,39 +621,6 @@
</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">-         * @ticket 55716
-        */
-       public function test_prime_user_cache() {
-               $action = new MockAction();
-               add_filter( 'update_user_metadata_cache', array( $action, 'filter' ), 10, 2 );
-               $user_ids = array();
-               $count    = 5;
-               for ( $i = 0; $i < $count; $i ++ ) {
-                       $user_ids[ $i ] = self::factory()->user->create();
-                       self::factory()->post->create(
-                               array(
-                                       'post_type'   => 'post',
-                                       'post_author' => $user_ids[ $i ],
-                               )
-                       );
-               }
-
-               $q = new WP_Query(
-                       array(
-                               'post_type'      => 'post',
-                               'posts_per_page' => $count,
-                       )
-               );
-               while ( $q->have_posts() ) {
-                       $q->the_post();
-               }
-
-               $args      = $action->get_args();
-               $last_args = end( $args );
-               $this->assertSameSets( $user_ids, $last_args[1], 'Ensure that user ids are primed' );
-       }
-
-       /**
</del><span class="cx" style="display: block; padding: 0 10px">          * @ticket 35601
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_ping_status() {
</span></span></pre>
</div>
</div>

</body>
</html>