<!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][7087] trunk: Introduces 'order' and 'orderby' parameters for the bp_has_groups() stack</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">
<dt>Revision</dt> <dd><a href="http://buddypress.trac.wordpress.org/changeset/7087">7087</a></dd>
<dt>Author</dt> <dd>boonebgorges</dd>
<dt>Date</dt> <dd>2013-05-19 00:09:30 +0000 (Sun, 19 May 2013)</dd>
</dl>
<h3>Log Message</h3>
<pre>Introduces 'order' and 'orderby' parameters for the bp_has_groups() stack
'order' and 'orderby' are more specific and flexible versions of the existing
'type' parameter ('popular', 'newest', etc). Backward compatibility is
maintained by always obeying 'type' when it is passed, and by internally
converting the legacy 'type' values into the corresponding 'order' and
'orderby' values.
Adds unit tests to ensure that the adjusted default values for functions in
the stack are backward compatibile (in particular, the fact that the 'type'
parameter is now empty by default, replaced with the corresponding default
values for 'order' and 'orderby'). Unit tests are also added for the new
params, as well as some internal utility methods.
See <a href="http://buddypress.trac.wordpress.org/ticket/4483">#4483</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpgroupsbpgroupsclassesphp">trunk/bp-groups/bp-groups-classes.php</a></li>
<li><a href="#trunkbpgroupsbpgroupsfunctionsphp">trunk/bp-groups/bp-groups-functions.php</a></li>
<li><a href="#trunkbpgroupsbpgroupstemplatephp">trunk/bp-groups/bp-groups-template.php</a></li>
<li><a href="#trunkteststestcasesgroupsclassbpgroupsgroupphp">trunk/tests/testcases/groups/class-bp-groups-group.php</a></li>
<li><a href="#trunkteststestcasesgroupstemplatephp">trunk/tests/testcases/groups/template.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpgroupsbpgroupsclassesphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-groups/bp-groups-classes.php (7086 => 7087)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups/bp-groups-classes.php 2013-05-19 00:09:25 UTC (rev 7086)
+++ trunk/bp-groups/bp-groups-classes.php 2013-05-19 00:09:30 UTC (rev 7087)
</span><span class="lines">@@ -332,7 +332,9 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> $defaults = array(
</span><del>- 'type' => 'newest',
</del><ins>+ 'type' => null,
+ 'orderby' => 'date_created',
+ 'order' => 'DESC',
</ins><span class="cx"> 'per_page' => null,
</span><span class="cx"> 'page' => null,
</span><span class="cx"> 'user_id' => 0,
</span><span class="lines">@@ -341,7 +343,7 @@
</span><span class="cx"> 'include' => false,
</span><span class="cx"> 'populate_extras' => true,
</span><span class="cx"> 'exclude' => false,
</span><del>- 'show_hidden' => false
</del><ins>+ 'show_hidden' => false,
</ins><span class="cx"> );
</span><span class="cx">
</span><span class="cx"> $r = wp_parse_args( $args, $defaults );
</span><span class="lines">@@ -400,25 +402,41 @@
</span><span class="cx"> $sql['exclude'] = " AND g.id NOT IN ({$exclude})";
</span><span class="cx"> }
</span><span class="cx">
</span><del>- switch ( $r['type'] ) {
- case 'newest':
- default:
- $sql['order'] = " ORDER BY g.date_created DESC";
- break;
- case 'active':
- $sql[] = "ORDER BY last_activity DESC";
- break;
- case 'popular':
- $sql[] = "ORDER BY CONVERT(gm1.meta_value, SIGNED) DESC";
- break;
- case 'alphabetical':
- $sql[] = "ORDER BY g.name ASC";
- break;
- case 'random':
- $sql[] = "ORDER BY rand()";
- break;
</del><ins>+ /** Order/orderby ********************************************/
+
+ $order = $r['order'];
+ $orderby = $r['orderby'];
+
+ // If a 'type' parameter was passed, parse it and overwrite
+ // 'order' and 'orderby' params passed to the function
+ if ( ! empty( $r['type'] ) ) {
+ $order_orderby = self::convert_type_to_order_orderby( $r['type'] );
+
+ // If an invalid type is passed, $order_orderby will be
+ // an array with empty values. In this case, we stick
+ // with the default values of $order and $orderby
+ if ( ! empty( $order_orderby['order'] ) ) {
+ $order = $order_orderby['order'];
+ }
+
+ if ( ! empty( $order_orderby['orderby'] ) ) {
+ $orderby = $order_orderby['orderby'];
+ }
</ins><span class="cx"> }
</span><span class="cx">
</span><ins>+ // Sanitize 'order'
+ $order = bp_esc_sql_order( $order );
+
+ // Convert 'orderby' into the proper ORDER BY term
+ $orderby = self::convert_orderby_to_order_by_term( $orderby );
+
+ // Random order is a special case
+ if ( 'rand()' === $orderby ) {
+ $sql[] = "ORDER BY rand()";
+ } else {
+ $sql[] = "ORDER BY {$orderby} {$order}";
+ }
+
</ins><span class="cx"> if ( ! empty( $r['per_page'] ) && ! empty( $r['page'] ) ) {
</span><span class="cx"> $sql['pagination'] = $wpdb->prepare( "LIMIT %d, %d", intval( ( $r['page'] - 1 ) * $r['per_page']), intval( $r['per_page'] ) );
</span><span class="cx"> }
</span><span class="lines">@@ -540,7 +558,84 @@
</span><span class="cx"> return $sql_array;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+ /**
+ * Convert the 'type' parameter to 'order' and 'orderby'
+ *
+ * @since BuddyPress (1.8)
+ * @access protected
+ * @param string $type The 'type' shorthand param
+ * @return array 'order' and 'orderby'
+ */
+ protected function convert_type_to_order_orderby( $type = '' ) {
+ $order = $orderby = '';
</ins><span class="cx">
</span><ins>+ switch ( $type ) {
+ case 'newest' :
+ $order = 'DESC';
+ $orderby = 'date_created';
+ break;
+
+ case 'active' :
+ $order = 'DESC';
+ $orderby = 'last_activity';
+ break;
+
+ case 'popular' :
+ $order = 'DESC';
+ $orderby = 'total_member_count';
+ break;
+
+ case 'alphabetical' :
+ $order = 'ASC';
+ $orderby = 'name';
+ break;
+
+ case 'random' :
+ $order = '';
+ $orderby = 'random';
+ break;
+ }
+
+ return array( 'order' => $order, 'orderby' => $orderby );
+ }
+
+ /**
+ * Convert the 'orderby' param into a proper SQL term/column
+ *
+ * @since BuddyPress (1.8)
+ * @access protected
+ * @param string $orderby
+ * @return string $order_by_term
+ */
+ protected function convert_orderby_to_order_by_term( $orderby ) {
+ $order_by_term = '';
+
+ switch ( $orderby ) {
+ case 'date_created' :
+ default :
+ $order_by_term = 'g.date_created';
+ break;
+
+ case 'last_activity' :
+ $order_by_term = 'last_activity';
+ break;
+
+ case 'total_group_members' :
+ $order_by_term = 'CONVERT(gm1.meta_value, SIGNED)';
+ break;
+
+ case 'name' :
+ $order_by_term = 'g.name';
+ break;
+
+ case 'random' :
+ $order_by_term = 'rand()';
+ break;
+ }
+
+ return $order_by_term;
+ }
+
</ins><span class="cx"> function get_by_most_forum_topics( $limit = null, $page = null, $user_id = 0, $search_terms = false, $populate_extras = true, $exclude = false ) {
</span><span class="cx"> global $wpdb, $bp, $bbdb;
</span><span class="cx">
</span></span></pre></div>
<a id="trunkbpgroupsbpgroupsfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-groups/bp-groups-functions.php (7086 => 7087)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups/bp-groups-functions.php 2013-05-19 00:09:25 UTC (rev 7086)
+++ trunk/bp-groups/bp-groups-functions.php 2013-05-19 00:09:30 UTC (rev 7087)
</span><span class="lines">@@ -413,7 +413,9 @@
</span><span class="cx"> function groups_get_groups( $args = '' ) {
</span><span class="cx">
</span><span class="cx"> $defaults = array(
</span><del>- 'type' => 'active', // active, newest, alphabetical, random, popular, most-forum-topics or most-forum-posts
</del><ins>+ 'type' => false, // active, newest, alphabetical, random, popular, most-forum-topics or most-forum-posts
+ 'order' => 'DESC', // 'ASC' or 'DESC'
+ 'orderby' => 'date_created', // date_created, last_activity, total_member_count, name, random
</ins><span class="cx"> 'user_id' => false, // Pass a user_id to limit to only groups that this user is a member of
</span><span class="cx"> 'include' => false, // Only include these specific groups (group_ids)
</span><span class="cx"> 'exclude' => false, // Do not include these specific groups (group_ids)
</span><span class="lines">@@ -437,7 +439,9 @@
</span><span class="cx"> 'show_hidden' => $r['show_hidden'],
</span><span class="cx"> 'per_page' => $r['per_page'],
</span><span class="cx"> 'page' => $r['page'],
</span><del>- 'populate_extras' => $r['populate_extras']
</del><ins>+ 'populate_extras' => $r['populate_extras'],
+ 'order' => $r['order'],
+ 'orderby' => $r['orderby'],
</ins><span class="cx"> ) );
</span><span class="cx">
</span><span class="cx"> return apply_filters_ref_array( 'groups_get_groups', array( &$groups, &$r ) );
</span></span></pre></div>
<a id="trunkbpgroupsbpgroupstemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/bp-groups/bp-groups-template.php (7086 => 7087)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups/bp-groups-template.php 2013-05-19 00:09:25 UTC (rev 7086)
+++ trunk/bp-groups/bp-groups-template.php 2013-05-19 00:09:30 UTC (rev 7087)
</span><span class="lines">@@ -166,6 +166,8 @@
</span><span class="cx"> } else {
</span><span class="cx"> $this->groups = groups_get_groups( array(
</span><span class="cx"> 'type' => $type,
</span><ins>+ 'order' => $order,
+ 'orderby' => $orderby,
</ins><span class="cx"> 'per_page' => $this->pag_num,
</span><span class="cx"> 'page' => $this->pag_page,
</span><span class="cx"> 'user_id' => $user_id,
</span><span class="lines">@@ -266,6 +268,17 @@
</span><span class="cx"> }
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+/**
+ * Start the Groups Template Loop
+ *
+ * See the $defaults definition below for a description of parameters.
+ *
+ * Note that the 'type' parameter overrides 'order' and 'orderby'. See
+ * BP_Groups_Group::get() for more details.
+ *
+ * @param array $args
+ * @return bool True if there are groups to display that match the params
+ */
</ins><span class="cx"> function bp_has_groups( $args = '' ) {
</span><span class="cx"> global $groups_template, $bp;
</span><span class="cx">
</span><span class="lines">@@ -275,7 +288,7 @@
</span><span class="cx"> * pass their parameters directly to the loop.
</span><span class="cx"> */
</span><span class="cx"> $slug = false;
</span><del>- $type = 'active';
</del><ins>+ $type = '';
</ins><span class="cx"> $user_id = 0;
</span><span class="cx"> $order = '';
</span><span class="cx">
</span><span class="lines">@@ -284,6 +297,7 @@
</span><span class="cx"> $user_id = bp_displayed_user_id();
</span><span class="cx">
</span><span class="cx"> // Type
</span><ins>+ // @todo What is $order? At some point it was removed incompletely?
</ins><span class="cx"> if ( bp_is_current_action( 'my-groups' ) ) {
</span><span class="cx"> if ( 'most-popular' == $order ) {
</span><span class="cx"> $type = 'popular';
</span><span class="lines">@@ -298,7 +312,9 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> $defaults = array(
</span><del>- 'type' => $type,
</del><ins>+ 'type' => $type, // 'type' is an override for 'order' and 'orderby'. See docblock.
+ 'order' => 'DESC',
+ 'orderby' => 'last_activity',
</ins><span class="cx"> 'page' => 1,
</span><span class="cx"> 'per_page' => 20,
</span><span class="cx"> 'max' => false,
</span><span class="lines">@@ -313,7 +329,7 @@
</span><span class="cx"> 'include' => false, // Pass comma separated list or array of group ID's to return only these groups
</span><span class="cx"> 'exclude' => false, // Pass comma separated list or array of group ID's to exclude these groups
</span><span class="cx">
</span><del>- 'populate_extras' => true // Get extra meta - is_member, is_banned
</del><ins>+ 'populate_extras' => true, // Get extra meta - is_member, is_banned
</ins><span class="cx"> );
</span><span class="cx">
</span><span class="cx"> $r = wp_parse_args( $args, $defaults );
</span><span class="lines">@@ -329,6 +345,8 @@
</span><span class="cx">
</span><span class="cx"> $groups_template = new BP_Groups_Template( array(
</span><span class="cx"> 'type' => $r['type'],
</span><ins>+ 'order' => $r['order'],
+ 'orderby' => $r['orderby'],
</ins><span class="cx"> 'page' => (int) $r['page'],
</span><span class="cx"> 'per_page' => (int) $r['per_page'],
</span><span class="cx"> 'max' => (int) $r['max'],
</span></span></pre></div>
<a id="trunkteststestcasesgroupsclassbpgroupsgroupphp"></a>
<div class="modfile"><h4>Modified: trunk/tests/testcases/groups/class-bp-groups-group.php (7086 => 7087)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/testcases/groups/class-bp-groups-group.php 2013-05-19 00:09:25 UTC (rev 7086)
+++ trunk/tests/testcases/groups/class-bp-groups-group.php 2013-05-19 00:09:30 UTC (rev 7087)
</span><span class="lines">@@ -171,6 +171,180 @@
</span><span class="cx"> $this->assertEquals( array( $g1 ), $found );
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+ /**
+ * BP 1.8 will change the default 'type' param in favor of default
+ * 'order' and 'orderby'. This is to make sure that existing plugins
+ * will work appropriately
+ *
+ * @group get
+ */
+ public function test_get_with_default_type_value_should_be_newest() {
+ $g1 = $this->factory->group->create( array(
+ 'name' => 'A Group',
+ 'date_created' => bp_core_current_time(),
+ ) );
+ $g2 = $this->factory->group->create( array(
+ 'name' => 'D Group',
+ 'date_created' => gmdate( 'Y-m-d H:i:s', time() - 100 ),
+ ) );
+ $g3 = $this->factory->group->create( array(
+ 'name' => 'B Group',
+ 'date_created' => gmdate( 'Y-m-d H:i:s', time() - 100000 ),
+ ) );
+ $g4 = $this->factory->group->create( array(
+ 'name' => 'C Group',
+ 'date_created' => gmdate( 'Y-m-d H:i:s', time() - 1000 ),
+ ) );
+
+ $found = BP_Groups_Group::get();
+
+ $this->assertEquals( BP_Groups_Group::get( array( 'type' => 'newest' ) ), $found );
+ }
+
+ /**
+ * @group get
+ */
+ public function test_get_with_type_newest() {
+ $g1 = $this->factory->group->create( array(
+ 'name' => 'A Group',
+ 'date_created' => bp_core_current_time(),
+ ) );
+ $g2 = $this->factory->group->create( array(
+ 'name' => 'D Group',
+ 'date_created' => gmdate( 'Y-m-d H:i:s', $time - 100 ),
+ ) );
+ $g3 = $this->factory->group->create( array(
+ 'name' => 'B Group',
+ 'date_created' => gmdate( 'Y-m-d H:i:s', $time - 100000 ),
+ ) );
+ $g4 = $this->factory->group->create( array(
+ 'name' => 'C Group',
+ 'date_created' => gmdate( 'Y-m-d H:i:s', $time - 1000 ),
+ ) );
+
+ $groups = BP_Groups_Group::get( array( 'type' => 'newest' ) );
+ $found = wp_parse_id_list( wp_list_pluck( $groups['groups'], 'id' ) );
+ $this->assertEquals( array( $g1, $g2, $g4, $g3 ), $found );
+ }
+
+ /** convert_type_to_order_orderby() **********************************/
+
+ /**
+ * @group convert_type_to_order_orderby
+ */
+ public function test_convert_type_to_order_orderby_newest() {
+ $expected = array(
+ 'order' => 'DESC',
+ 'orderby' => 'date_created',
+ );
+ $this->assertEquals( $expected, _BP_Groups_Group::_convert_type_to_order_orderby( 'newest' ) );
+ }
+
+ /**
+ * @group convert_type_to_order_orderby
+ */
+ public function test_convert_type_to_order_orderby_active() {
+ $expected = array(
+ 'order' => 'DESC',
+ 'orderby' => 'last_activity',
+ );
+ $this->assertEquals( $expected, _BP_Groups_Group::_convert_type_to_order_orderby( 'active' ) );
+ }
+
+ /**
+ * @group convert_type_to_order_orderby
+ */
+ public function test_convert_type_to_order_orderby_popular() {
+ $expected = array(
+ 'order' => 'DESC',
+ 'orderby' => 'total_member_count',
+ );
+ $this->assertEquals( $expected, _BP_Groups_Group::_convert_type_to_order_orderby( 'popular' ) );
+ }
+
+ /**
+ * @group convert_type_to_order_orderby
+ */
+ public function test_convert_type_to_order_orderby_alphabetical() {
+ $expected = array(
+ 'order' => 'ASC',
+ 'orderby' => 'name',
+ );
+ $this->assertEquals( $expected, _BP_Groups_Group::_convert_type_to_order_orderby( 'alphabetical' ) );
+ }
+
+ /**
+ * @group convert_type_to_order_orderby
+ */
+ public function test_convert_type_to_order_orderby_random() {
+ $expected = array(
+ // order gets thrown out
+ 'order' => '',
+ 'orderby' => 'random',
+ );
+ $this->assertEquals( $expected, _BP_Groups_Group::_convert_type_to_order_orderby( 'random' ) );
+ }
+
+ /**
+ * @group convert_type_to_order_orderby
+ */
+ public function test_convert_type_to_order_orderby_invalid() {
+ $expected = array(
+ 'order' => '',
+ 'orderby' => '',
+ );
+ $this->assertEquals( $expected, _BP_Groups_Group::_convert_type_to_order_orderby( 'foooooooooooooooobar' ) );
+ }
+
+ /** convert_orderby_to_order_by_term() **********************************/
+
+ /**
+ * @group convert_orderby_to_order_by_term
+ */
+ public function test_convert_orderby_to_order_by_term_date_created() {
+ $this->assertEquals( 'g.date_created', _BP_Groups_Group::_convert_orderby_to_order_by_term( 'date_created' ) );
+ }
+
+ /**
+ * @group convert_orderby_to_order_by_term
+ */
+ public function test_convert_orderby_to_order_by_term_last_activity() {
+ $c = new _BP_Groups_Group();
+ $this->assertEquals( 'last_activity', _BP_Groups_Group::_convert_orderby_to_order_by_term( 'last_activity' ) );
+ }
+
+ /**
+ * @group convert_orderby_to_order_by_term
+ */
+ public function test_convert_orderby_to_order_by_term_total_group_members() {
+ $c = new _BP_Groups_Group();
+ $this->assertEquals( 'CONVERT(gm1.meta_value, SIGNED)', _BP_Groups_Group::_convert_orderby_to_order_by_term( 'total_group_members' ) );
+ }
+
+ /**
+ * @group convert_orderby_to_order_by_term
+ */
+ public function test_convert_orderby_to_order_by_term_name() {
+ $c = new _BP_Groups_Group();
+ $this->assertEquals( 'g.name', _BP_Groups_Group::_convert_orderby_to_order_by_term( 'name' ) );
+ }
+
+ /**
+ * @group convert_orderby_to_order_by_term
+ */
+ public function test_convert_orderby_to_order_by_term_random() {
+ $c = new _BP_Groups_Group();
+ $this->assertEquals( 'rand()', _BP_Groups_Group::_convert_orderby_to_order_by_term( 'random' ) );
+ }
+
+ /**
+ * @group convert_orderby_to_order_by_term
+ */
+ public function test_convert_orderby_to_order_by_term_invalid_fallback_to_date_created() {
+ $c = new _BP_Groups_Group();
+ $this->assertEquals( _BP_Groups_Group::_convert_orderby_to_order_by_term( 'date_created' ), _BP_Groups_Group::_convert_orderby_to_order_by_term( 'I am a bad boy' ) );
+ }
+
</ins><span class="cx"> public function test_filter_user_groups_normal_search() {
</span><span class="cx"> $g1 = $this->factory->group->create( array(
</span><span class="cx"> 'name' => 'Cool Group',
</span><span class="lines">@@ -357,3 +531,16 @@
</span><span class="cx"> $this->assertEquals( array( $g1 ), $found );
</span><span class="cx"> }
</span><span class="cx"> }
</span><ins>+
+/**
+ * Stub class for accessing protected methods
+ */
+class _BP_Groups_Group extends BP_Groups_Group {
+ public function _convert_type_to_order_orderby( $type ) {
+ return self::convert_type_to_order_orderby( $type );
+ }
+
+ public function _convert_orderby_to_order_by_term( $term ) {
+ return self::convert_orderby_to_order_by_term( $term );
+ }
+}
</ins></span></pre></div>
<a id="trunkteststestcasesgroupstemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/tests/testcases/groups/template.php (7086 => 7087)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/testcases/groups/template.php 2013-05-19 00:09:25 UTC (rev 7086)
+++ trunk/tests/testcases/groups/template.php 2013-05-19 00:09:30 UTC (rev 7087)
</span><span class="lines">@@ -34,4 +34,67 @@
</span><span class="cx"> $ids = wp_list_pluck( $groups_template->groups, 'id' );
</span><span class="cx"> $this->assertEquals( $ids, array( $g1, ) );
</span><span class="cx"> }
</span><ins>+
+ /**
+ * Integration test to make sure order and orderby are interpreted when
+ * no 'type' value has been passed
+ *
+ * @group bp_has_groups
+ */
+ public function test_bp_has_groups_with_order_orderby_with_null_type() {
+ $g1 = $this->factory->group->create( array(
+ 'name' => 'AAAAA',
+ 'date_created' => gmdate( 'Y-m-d H:i:s', time() - 100 ),
+ 'last_activity' => gmdate( 'Y-m-d H:i:s', time() - 1000000 ),
+ ) );
+ $g2 = $this->factory->group->create( array(
+ 'name' => 'BBBBB',
+ 'date_created' => gmdate( 'Y-m-d H:i:s', time() - 1000000 ),
+ 'last_activity' => gmdate( 'Y-m-d H:i:s', time() - 10000 ),
+ ) );
+ $g3 = $this->factory->group->create( array(
+ 'name' => 'CCCCC',
+ 'date_created' => gmdate( 'Y-m-d H:i:s', time() - 10000 ),
+ 'last_activity' => gmdate( 'Y-m-d H:i:s', time() - 10 ),
+ ) );
+
+ global $groups_template;
+ bp_has_groups( array(
+ 'order' => 'ASC',
+ 'orderby' => 'name',
+ ) );
+
+ $ids = wp_parse_id_list( wp_list_pluck( $groups_template->groups, 'id' ) );
+ $this->assertEquals( array( $g1, $g2, $g3, ), $ids );
+ }
+
+ /**
+ * Integration test to make sure 'order' is set to 'DESC' and 'orderby'
+ * to 'last_activity' when no type or order/orderby params are passed.
+ * This ensures backpat with the old system, where 'active' was the
+ * default type param, and there were no order/orderby params.
+ *
+ * @group bp_has_groups
+ */
+ public function test_bp_has_groups_defaults_to_DESC_last_activity_for_default_type_active_backpat() {
+ $g1 = $this->factory->group->create( array(
+ 'name' => 'AAAAA',
+ 'last_activity' => gmdate( 'Y-m-d H:i:s', time() - 100 ),
+ ) );
+ $g2 = $this->factory->group->create( array(
+ 'name' => 'BBBBB',
+ 'last_activity' => gmdate( 'Y-m-d H:i:s', time() - 1000000 ),
+ ) );
+ $g3 = $this->factory->group->create( array(
+ 'name' => 'CCCCC',
+ 'last_activity' => gmdate( 'Y-m-d H:i:s', time() - 10000 ),
+ ) );
+
+ global $groups_template;
+ bp_has_groups();
+
+ $ids = wp_parse_id_list( wp_list_pluck( $groups_template->groups, 'id' ) );
+ $this->assertEquals( array( $g1, $g3, $g2, ), $ids );
+ }
+
</ins><span class="cx"> }
</span></span></pre>
</div>
</div>
</body>
</html>