<!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>[BuddyPress][10198] trunk: Introduce cache support for xprofile fields.</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 { 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="http://buddypress.trac.wordpress.org/changeset/10198">10198</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://buddypress.trac.wordpress.org/changeset/10198","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>boonebgorges</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-10-07 14:30:31 +0000 (Wed, 07 Oct 2015)</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'>Introduce cache support for xprofile fields.

`xprofile_get_field()` will now look in the cache before returning a
`BP_XProfile_Field` object (via the new `BP_XProfile_Field::get_instance()`),
and will populate the cache if necessary. It's strongly recommended to use
`xprofile_get_field()` when fetching individual existing fields, instead of
creating new `BP_XProfile_Field` objects directly.

`BP_XProfile_Group::get()` now leverages the field cache too. Instead of a
`SELECT *` query, it fetches a list of matching field IDs `SELECT id`, and
populates the full field objects from the cache, if available. The `fields`
property of `BP_XProfile_Group` objects is now an array of `BP_XProfile_Field`
objects, not `stdClass`. See <a href="http://buddypress.trac.wordpress.org/ticket/6358">#6358</a>.

Props boonebgorges, r-a-y.
See <a href="http://buddypress.trac.wordpress.org/ticket/6638">#6638</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpxprofilebpxprofilecachephp">trunk/src/bp-xprofile/bp-xprofile-cache.php</a></li>
<li><a href="#trunksrcbpxprofilebpxprofilefunctionsphp">trunk/src/bp-xprofile/bp-xprofile-functions.php</a></li>
<li><a href="#trunksrcbpxprofilebpxprofileloaderphp">trunk/src/bp-xprofile/bp-xprofile-loader.php</a></li>
<li><a href="#trunksrcbpxprofileclassesclassbpxprofilefieldphp">trunk/src/bp-xprofile/classes/class-bp-xprofile-field.php</a></li>
<li><a href="#trunksrcbpxprofileclassesclassbpxprofilegroupphp">trunk/src/bp-xprofile/classes/class-bp-xprofile-group.php</a></li>
<li><a href="#trunktestsphpunittestcasesxprofilecachephp">trunk/tests/phpunit/testcases/xprofile/cache.php</a></li>
<li><a href="#trunktestsphpunittestcasesxprofilefunctionsphp">trunk/tests/phpunit/testcases/xprofile/functions.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpxprofilebpxprofilecachephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-xprofile/bp-xprofile-cache.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-xprofile/bp-xprofile-cache.php       2015-10-07 14:12:26 UTC (rev 10197)
+++ trunk/src/bp-xprofile/bp-xprofile-cache.php 2015-10-07 14:30:31 UTC (rev 10198)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -283,5 +283,29 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'update_option_bp-xprofile-fullname-field-name', 'xprofile_clear_fullname_field_id_cache' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/**
+ * Clear a field's caches.
+ *
+ * @since 2.4.0
+ *
+ * @param int|BP_XProfile_Field A field ID or a field object.
+ * @param bool False on failure.
+ */
+function bp_xprofile_clear_field_cache( $field ) {
+       if ( is_numeric( $field ) ) {
+               $field_id = (int) $field;
+       } elseif ( $field instanceof BP_XProfile_Field ) {
+               $field_id = (int) $field->id;
+       }
+
+       if ( ! isset( $field_id ) ) {
+               return false;
+       }
+
+       wp_cache_delete( $field_id, 'bp_xprofile_fields' );
+       wp_cache_delete( $field_id, 'xprofile_meta' );
+}
+add_action( 'xprofile_field_after_save', 'bp_xprofile_clear_field_cache' );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // List actions to clear super cached pages on, if super cache is installed.
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'xprofile_updated_profile', 'bp_core_clear_cache' );
</span></span></pre></div>
<a id="trunksrcbpxprofilebpxprofilefunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-xprofile/bp-xprofile-functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-xprofile/bp-xprofile-functions.php   2015-10-07 14:12:26 UTC (rev 10197)
+++ trunk/src/bp-xprofile/bp-xprofile-functions.php     2015-10-07 14:30:31 UTC (rev 10198)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -275,8 +275,27 @@
</span><span class="cx" style="display: block; padding: 0 10px">        return $field->save();
</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">-function xprofile_get_field( $field_id ) {
-       return new BP_XProfile_Field( $field_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/**
+ * Get a profile field object.
+ *
+ * @param int|object $field ID of the field or object representing field data.
+ * @return BP_XProfile_Field|null Field object if found, otherwise null.
+ */
+function xprofile_get_field( $field ) {
+       if ( $field instanceof BP_XProfile_Field ) {
+               $_field = $field;
+       } elseif ( is_object( $field ) ) {
+               $_field = new BP_XProfile_Field();
+               $_field->fill_data( $field );
+       } else {
+               $_field = BP_XProfile_Field::get_instance( $field );
+       }
+
+       if ( ! $_field ) {
+               return null;
+       }
+
+       return $_field;
</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"> function xprofile_delete_field( $field_id ) {
</span></span></pre></div>
<a id="trunksrcbpxprofilebpxprofileloaderphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-xprofile/bp-xprofile-loader.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-xprofile/bp-xprofile-loader.php      2015-10-07 14:12:26 UTC (rev 10197)
+++ trunk/src/bp-xprofile/bp-xprofile-loader.php        2015-10-07 14:30:31 UTC (rev 10198)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -393,6 +393,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                wp_cache_add_global_groups( array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'bp_xprofile',
</span><span class="cx" style="display: block; padding: 0 10px">                        'bp_xprofile_data',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        'bp_xprofile_fields',
</ins><span class="cx" style="display: block; padding: 0 10px">                         'bp_xprofile_groups',
</span><span class="cx" style="display: block; padding: 0 10px">                        'xprofile_meta'
</span><span class="cx" style="display: block; padding: 0 10px">                ) );
</span></span></pre></div>
<a id="trunksrcbpxprofileclassesclassbpxprofilefieldphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-xprofile/classes/class-bp-xprofile-field.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-xprofile/classes/class-bp-xprofile-field.php 2015-10-07 14:12:26 UTC (rev 10197)
+++ trunk/src/bp-xprofile/classes/class-bp-xprofile-field.php   2015-10-07 14:30:31 UTC (rev 10198)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -211,31 +211,74 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $bp    = buddypress();
</span><span class="cx" style="display: block; padding: 0 10px">                $field = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_fields} WHERE id = %d", $id ) );
</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 ( ! empty( $field ) ) {
-                       $this->id                = $field->id;
-                       $this->group_id          = $field->group_id;
-                       $this->parent_id         = $field->parent_id;
-                       $this->type              = $field->type;
-                       $this->name              = stripslashes( $field->name );
-                       $this->description       = stripslashes( $field->description );
-                       $this->is_required       = $field->is_required;
-                       $this->can_delete        = $field->can_delete;
-                       $this->field_order       = $field->field_order;
-                       $this->option_order      = $field->option_order;
-                       $this->order_by          = $field->order_by;
-                       $this->is_default_option = $field->is_default_option;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->fill_data( $field );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Create the field type and store a reference back to this object.
-                       $this->type_obj            = bp_xprofile_create_field_type( $field->type );
-                       $this->type_obj->field_obj = $this;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( ! empty( $get_data ) && ! empty( $user_id ) ) {
+                       $this->data = $this->get_field_data( $user_id );
+               }
+       }
</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 ( ! empty( $get_data ) && ! empty( $user_id ) ) {
-                               $this->data = $this->get_field_data( $user_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+        * Retrieve a `BP_XProfile_Field` instance.
+        *
+        * @static
+        *
+        * @param int $field_id ID of the field.
+        * @return BP_XProfile_Field|false Field object if found, otherwise false.
+        */
+       public static function get_instance( $field_id ) {
+               global $wpdb;
+
+               $field_id = (int) $field_id;
+               if ( ! $field_id ) {
+                       return false;
+               }
+
+               $field = wp_cache_get( $field_id, 'bp_xprofile_fields' );
+               if ( false === $field ) {
+                       $bp = buddypress();
+
+                       $field = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_fields} WHERE id = %d", $field_id ) );
+
+                       wp_cache_add( $field->id, $field, 'bp_xprofile_fields' );
+
+                       if ( ! $field ) {
+                               return false;
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               $_field = new BP_XProfile_Field();
+               $_field->fill_data( $field );
+
+               return $_field;
</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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Fill object vars based on data passed to the method.
+        *
+        * @since 2.4.0
+        *
+        * @param array|object $args Array or object representing the `BP_XProfile_Field` properties.
+        *                           Generally, this is a row from the fields database table.
+        */
+       public function fill_data( $args ) {
+               if ( is_object( $args ) ) {
+                       $args = (array) $args;
+               }
+
+               foreach ( $args as $k => $v ) {
+                       if ( 'name' === $k || 'description' === $k ) {
+                               $v = stripslashes( $v );
+                       }
+                       $this->{$k} = $v;
+               }
+
+               // Create the field type and store a reference back to this object.
+               $this->type_obj            = bp_xprofile_create_field_type( $this->type );
+               $this->type_obj->field_obj = $this;;
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Magic getter.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 2.4.0
</span></span></pre></div>
<a id="trunksrcbpxprofileclassesclassbpxprofilegroupphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-xprofile/classes/class-bp-xprofile-group.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-xprofile/classes/class-bp-xprofile-group.php 2015-10-07 14:12:26 UTC (rev 10197)
+++ trunk/src/bp-xprofile/classes/class-bp-xprofile-group.php   2015-10-07 14:30:31 UTC (rev 10198)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -369,18 +369,35 @@
</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">                // Fetch the fields.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $fields = $wpdb->get_results( "SELECT id, name, description, type, group_id, is_required FROM {$bp->profile->table_name_fields} WHERE group_id IN ( {$group_ids_in} ) AND parent_id = 0 {$exclude_fields_sql} {$in_sql} ORDER BY field_order" );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $field_ids = $wpdb->get_col( "SELECT id FROM {$bp->profile->table_name_fields} WHERE group_id IN ( {$group_ids_in} ) AND parent_id = 0 {$exclude_fields_sql} {$in_sql} ORDER BY field_order" );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $field_ids = wp_list_pluck( $fields, 'id' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Bail if no fields.
+               if ( empty( $field_ids ) ) {
+                       return $groups;
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $field_ids = array_map( 'intval', $field_ids );
+
+               // Prime the field cache.
+               $uncached_field_ids = bp_get_non_cached_ids( $field_ids, 'bp_xprofile_fields' );
+               if ( ! empty( $uncached_field_ids ) ) {
+                       $_uncached_field_ids = implode( ',', array_map( 'intval', $uncached_field_ids ) );
+                       $uncached_fields = $wpdb->get_results( "SELECT * FROM {$bp->profile->table_name_fields} WHERE id IN ({$_uncached_field_ids})" );
+                       foreach ( $uncached_fields as $uncached_field ) {
+                               $fid = intval( $uncached_field->id );
+                               wp_cache_set( $fid, $uncached_field, 'bp_xprofile_fields' );
+                       }
+               }
+
+               // Pull field objects from the cache.
+               $fields = array();
+               foreach ( $field_ids as $field_id ) {
+                       $fields[] = xprofile_get_field( $field_id );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Store field IDs for meta cache priming.
</span><span class="cx" style="display: block; padding: 0 10px">                $object_ids['field'] = $field_ids;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Bail if no fields.
-               if ( empty( $fields ) ) {
-                       return $groups;
-               }
-
</del><span class="cx" style="display: block; padding: 0 10px">                 // Maybe fetch field data.
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! empty( $r['fetch_field_data'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunktestsphpunittestcasesxprofilecachephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/testcases/xprofile/cache.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/testcases/xprofile/cache.php  2015-10-07 14:12:26 UTC (rev 10197)
+++ trunk/tests/phpunit/testcases/xprofile/cache.php    2015-10-07 14:30:31 UTC (rev 10198)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -144,4 +144,24 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertFalse( wp_cache_get( $f, 'xprofile_field_meta' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertFalse( wp_cache_get( $d->id, 'xprofile_data_meta' ) );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @ticket BP6638
+        */
+       public function test_field_cache_should_be_invalidated_on_save() {
+               $g = $this->factory->xprofile_group->create();
+               $f = $this->factory->xprofile_field->create( array(
+                       'field_group_id' => $g,
+                       'name' => 'Foo',
+               ) );
+
+               $field = xprofile_get_field( $f );
+               $this->assertSame( 'Foo', $field->name );
+
+               $field->name = 'Bar';
+               $this->assertNotEmpty( $field->save() );
+
+               $field_2 = xprofile_get_field( $f );
+               $this->assertSame( 'Bar', $field_2->name );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestcasesxprofilefunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/testcases/xprofile/functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/testcases/xprofile/functions.php      2015-10-07 14:12:26 UTC (rev 10197)
+++ trunk/tests/phpunit/testcases/xprofile/functions.php        2015-10-07 14:30:31 UTC (rev 10198)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -868,4 +868,49 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // assert!
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( array( 1, $g1, $g3, $g2 ), wp_list_pluck( $field_groups, 'id' ) );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @ticket BP6638
+        */
+       public function test_xprofile_get_field_should_return_bp_xprofile_field_object() {
+               global $wpdb;
+
+               $g = $this->factory->xprofile_group->create();
+               $f = $this->factory->xprofile_field->create( array(
+                       'field_group_id' => $g,
+                       'type' => 'selectbox',
+                       'name' => 'Foo',
+               ) );
+
+               $field = xprofile_get_field( $f );
+
+               $this->assertTrue( $field instanceof BP_XProfile_Field );
+       }
+
+       /**
+        * @ticket BP6638
+        * @group cache
+        */
+       public function test_xprofile_get_field_should_prime_field_cache() {
+               global $wpdb;
+
+               $g = $this->factory->xprofile_group->create();
+               $f = $this->factory->xprofile_field->create( array(
+                       'field_group_id' => $g,
+                       'type' => 'selectbox',
+                       'name' => 'Foo',
+               ) );
+
+               $num_queries = $wpdb->num_queries;
+
+               // Prime the cache.
+               $field_1 = xprofile_get_field( $f );
+               $num_queries++;
+               $this->assertSame( $num_queries, $wpdb->num_queries );
+
+               // No more queries.
+               $field_2 = xprofile_get_field( $f );
+               $this->assertEquals( $field_1, $field_2 );
+               $this->assertSame( $num_queries, $wpdb->num_queries );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>