Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
05/19/2013 12:09:30 AM (12 years ago)
Author:
boonebgorges
Message:

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 #4483

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/testcases/groups/class-bp-groups-group.php

    r7086 r7087  
    172172    }
    173173
     174    /**
     175     * BP 1.8 will change the default 'type' param in favor of default
     176     * 'order' and 'orderby'. This is to make sure that existing plugins
     177     * will work appropriately
     178     *
     179     * @group get
     180     */
     181    public function test_get_with_default_type_value_should_be_newest() {
     182        $g1 = $this->factory->group->create( array(
     183            'name' => 'A Group',
     184            'date_created' => bp_core_current_time(),
     185        ) );
     186        $g2 = $this->factory->group->create( array(
     187            'name' => 'D Group',
     188            'date_created' => gmdate( 'Y-m-d H:i:s', time() - 100 ),
     189        ) );
     190        $g3 = $this->factory->group->create( array(
     191            'name' => 'B Group',
     192            'date_created' => gmdate( 'Y-m-d H:i:s', time() - 100000 ),
     193        ) );
     194        $g4 = $this->factory->group->create( array(
     195            'name' => 'C Group',
     196            'date_created' => gmdate( 'Y-m-d H:i:s', time() - 1000 ),
     197        ) );
     198
     199        $found = BP_Groups_Group::get();
     200
     201        $this->assertEquals( BP_Groups_Group::get( array( 'type' => 'newest' ) ), $found );
     202    }
     203
     204    /**
     205     * @group get
     206     */
     207    public function test_get_with_type_newest() {
     208        $g1 = $this->factory->group->create( array(
     209            'name' => 'A Group',
     210            'date_created' => bp_core_current_time(),
     211        ) );
     212        $g2 = $this->factory->group->create( array(
     213            'name' => 'D Group',
     214            'date_created' => gmdate( 'Y-m-d H:i:s', $time - 100 ),
     215        ) );
     216        $g3 = $this->factory->group->create( array(
     217            'name' => 'B Group',
     218            'date_created' => gmdate( 'Y-m-d H:i:s', $time - 100000 ),
     219        ) );
     220        $g4 = $this->factory->group->create( array(
     221            'name' => 'C Group',
     222            'date_created' => gmdate( 'Y-m-d H:i:s', $time - 1000 ),
     223        ) );
     224
     225        $groups = BP_Groups_Group::get( array( 'type' => 'newest' ) );
     226        $found = wp_parse_id_list( wp_list_pluck( $groups['groups'], 'id' ) );
     227        $this->assertEquals( array( $g1, $g2, $g4, $g3 ), $found );
     228    }
     229
     230    /** convert_type_to_order_orderby() **********************************/
     231
     232    /**
     233     * @group convert_type_to_order_orderby
     234     */
     235    public function test_convert_type_to_order_orderby_newest() {
     236        $expected = array(
     237            'order' => 'DESC',
     238            'orderby' => 'date_created',
     239        );
     240        $this->assertEquals( $expected, _BP_Groups_Group::_convert_type_to_order_orderby( 'newest' ) );
     241    }
     242
     243    /**
     244     * @group convert_type_to_order_orderby
     245     */
     246    public function test_convert_type_to_order_orderby_active() {
     247        $expected = array(
     248            'order' => 'DESC',
     249            'orderby' => 'last_activity',
     250        );
     251        $this->assertEquals( $expected, _BP_Groups_Group::_convert_type_to_order_orderby( 'active' ) );
     252    }
     253
     254    /**
     255     * @group convert_type_to_order_orderby
     256     */
     257    public function test_convert_type_to_order_orderby_popular() {
     258        $expected = array(
     259            'order' => 'DESC',
     260            'orderby' => 'total_member_count',
     261        );
     262        $this->assertEquals( $expected, _BP_Groups_Group::_convert_type_to_order_orderby( 'popular' ) );
     263    }
     264
     265    /**
     266     * @group convert_type_to_order_orderby
     267     */
     268    public function test_convert_type_to_order_orderby_alphabetical() {
     269        $expected = array(
     270            'order' => 'ASC',
     271            'orderby' => 'name',
     272        );
     273        $this->assertEquals( $expected, _BP_Groups_Group::_convert_type_to_order_orderby( 'alphabetical' ) );
     274    }
     275
     276    /**
     277     * @group convert_type_to_order_orderby
     278     */
     279    public function test_convert_type_to_order_orderby_random() {
     280        $expected = array(
     281            // order gets thrown out
     282            'order' => '',
     283            'orderby' => 'random',
     284        );
     285        $this->assertEquals( $expected, _BP_Groups_Group::_convert_type_to_order_orderby( 'random' ) );
     286    }
     287
     288    /**
     289     * @group convert_type_to_order_orderby
     290     */
     291    public function test_convert_type_to_order_orderby_invalid() {
     292        $expected = array(
     293            'order' => '',
     294            'orderby' => '',
     295        );
     296        $this->assertEquals( $expected, _BP_Groups_Group::_convert_type_to_order_orderby( 'foooooooooooooooobar' ) );
     297    }
     298
     299    /** convert_orderby_to_order_by_term() **********************************/
     300
     301    /**
     302     * @group convert_orderby_to_order_by_term
     303     */
     304    public function test_convert_orderby_to_order_by_term_date_created() {
     305        $this->assertEquals( 'g.date_created', _BP_Groups_Group::_convert_orderby_to_order_by_term( 'date_created' ) );
     306    }
     307
     308    /**
     309     * @group convert_orderby_to_order_by_term
     310     */
     311    public function test_convert_orderby_to_order_by_term_last_activity() {
     312        $c = new _BP_Groups_Group();
     313        $this->assertEquals( 'last_activity', _BP_Groups_Group::_convert_orderby_to_order_by_term( 'last_activity' ) );
     314    }
     315
     316    /**
     317     * @group convert_orderby_to_order_by_term
     318     */
     319    public function test_convert_orderby_to_order_by_term_total_group_members() {
     320        $c = new _BP_Groups_Group();
     321        $this->assertEquals( 'CONVERT(gm1.meta_value, SIGNED)', _BP_Groups_Group::_convert_orderby_to_order_by_term( 'total_group_members' ) );
     322    }
     323
     324    /**
     325     * @group convert_orderby_to_order_by_term
     326     */
     327    public function test_convert_orderby_to_order_by_term_name() {
     328        $c = new _BP_Groups_Group();
     329        $this->assertEquals( 'g.name', _BP_Groups_Group::_convert_orderby_to_order_by_term( 'name' ) );
     330    }
     331
     332    /**
     333     * @group convert_orderby_to_order_by_term
     334     */
     335    public function test_convert_orderby_to_order_by_term_random() {
     336        $c = new _BP_Groups_Group();
     337        $this->assertEquals( 'rand()', _BP_Groups_Group::_convert_orderby_to_order_by_term( 'random' ) );
     338    }
     339
     340    /**
     341     * @group convert_orderby_to_order_by_term
     342     */
     343    public function test_convert_orderby_to_order_by_term_invalid_fallback_to_date_created() {
     344        $c = new _BP_Groups_Group();
     345        $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' ) );
     346    }
     347
    174348    public function test_filter_user_groups_normal_search() {
    175349        $g1 = $this->factory->group->create( array(
     
    358532    }
    359533}
     534
     535/**
     536 * Stub class for accessing protected methods
     537 */
     538class _BP_Groups_Group extends BP_Groups_Group {
     539    public function _convert_type_to_order_orderby( $type ) {
     540        return self::convert_type_to_order_orderby( $type );
     541    }
     542
     543    public function _convert_orderby_to_order_by_term( $term ) {
     544        return self::convert_orderby_to_order_by_term( $term );
     545    }
     546}
Note: See TracChangeset for help on using the changeset viewer.