Skip to:
Content

BuddyPress.org

Changeset 14078


Ignore:
Timestamp:
11/03/2024 10:39:48 PM (16 months ago)
Author:
espellcaste
Message:

Groups: add support to query for groups matching a taxonomy term.

Support was added (with unit tests) to pass a tax_query to bp_has_groups() and friends, to query for groups matching a taxonomy term.

Props boonebgorges, DJPaul, and imath.

Closes https://github.com/buddypress/buddypress/pull/402/
See #4017
Fixes #7877

Location:
trunk
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/classes/class-bp-groups-group.php

    r14077 r14078  
    10561056     *     @type array|string $group_type__not_in Array or comma-separated list of group types that will be
    10571057     *                                            excluded from results.
     1058     *     @type array        $tax_query          Optional. An array of tax_query conditions. See
     1059     *                                            {@link WP_Tax_Query::__construct()} for syntax.
    10581060     *     @type array        $meta_query         Optional. An array of meta_query conditions.
    10591061     *                                            See {@link WP_Meta_Query::queries} for description.
     
    11331135            'group_type__in'     => '',
    11341136            'group_type__not_in' => '',
     1137            'tax_query'          => false,
    11351138            'meta_query'         => false,
    11361139            'date_query'         => false,
     
    12441247            $group_type_clause = self::get_sql_clause_for_group_types( $r['group_type__not_in'], 'NOT IN' );
    12451248
    1246         // Group types to include.
     1249            // Group types to include.
    12471250        } elseif ( ! empty( $r['group_type'] ) ) {
    12481251            $group_type_clause = self::get_sql_clause_for_group_types( $r['group_type'], 'IN' );
     
    12511254        if ( ! empty( $group_type_clause ) ) {
    12521255            $where_conditions['group_type'] = $group_type_clause;
     1256        }
     1257
     1258        $tax_query_sql = self::get_tax_query_sql( $r['tax_query'] );
     1259
     1260        if ( ! empty( $tax_query_sql['join'] ) ) {
     1261            $sql['from'] .= $tax_query_sql['join'];
     1262        }
     1263
     1264        if ( ! empty( $tax_query_sql['where'] ) ) {
     1265            $where_conditions['tax_query'] = $tax_query_sql['where'];
    12531266        }
    12541267
     
    12921305             * @since 2.1.0
    12931306             *
    1294              * @param array  $value Converted 'type' value for order and orderby.
    1295              * @param string $value Parsed 'type' value for the get method.
     1307             * @param array  $converted_type Converted 'type' value for order and orderby.
     1308             * @param string $type          Parsed 'type' value for the get method.
    12961309             */
    12971310            $order_orderby = apply_filters( 'bp_groups_get_orderby', self::convert_type_to_order_orderby( $r['type'] ), $r['type'] );
     
    13411354         * @since 2.1.0
    13421355         *
    1343          * @param string $value  Converted 'orderby' term.
    1344          * @param string $orderby Original orderby value.
    1345          * @param string $value   Parsed 'type' value for the get method.
     1356         * @param string $converted_orderby Converted 'orderby' term.
     1357         * @param string $orderby           Original orderby value.
     1358         * @param string $type              Parsed 'type' value for the get method.
    13461359         */
    13471360        $orderby = apply_filters( 'bp_groups_get_orderby_converted_by_term', self::convert_orderby_to_order_by_term( $orderby ), $orderby, $r['type'] );
     
    13711384         * @since 1.5.0
    13721385         *
    1373          * @param string $value Concatenated SQL statement.
    1374          * @param array  $sql   Array of SQL parts before concatenation.
    1375          * @param array  $r     Array of parsed arguments for the get method.
     1386         * @param string $paged_groups_sql Concatenated SQL statement.
     1387         * @param array  $sql              Array of SQL parts before concatenation.
     1388         * @param array  $r                Array of parsed arguments for the get method.
    13761389         */
    13771390        $paged_groups_sql = apply_filters( 'bp_groups_get_paged_groups_sql', $paged_groups_sql, $sql, $r );
     
    14501463         * @since 1.5.0
    14511464         *
    1452          * @param string $t_sql     Concatenated SQL statement used for retrieving total group results.
    1453          * @param array  $total_sql Array of SQL parts for the query.
    1454          * @param array  $r         Array of parsed arguments for the get method.
     1465         * @param string $total_groups_sql  Concatenated SQL statement used for retrieving total group results.
     1466         * @param array  $sql              Array of SQL parts for the query.
     1467         * @param array  $r                 Array of parsed arguments for the get method.
    14551468         */
    14561469        $total_groups_sql = apply_filters( 'bp_groups_get_total_groups_sql', $total_groups_sql, $sql, $r );
     
    14831496     *
    14841497     * @param array $meta_query An array of meta_query filters. See the
    1485      *                          documentation for {@link WP_Meta_Query} for details.
    1486      * @return array $sql_array 'join' and 'where' clauses.
     1498     *                          documentation for {@link WP_Meta_Query} for more details.
     1499     * @return array
    14871500     */
    14881501    protected static function get_meta_query_sql( $meta_query = array() ) {
     
    14941507        );
    14951508
    1496         if ( ! empty( $meta_query ) ) {
    1497             $groups_meta_query = new WP_Meta_Query( $meta_query );
    1498 
    1499             // WP_Meta_Query expects the table name at
    1500             // $wpdb->group.
    1501             $wpdb->groupmeta = buddypress()->groups->table_name_groupmeta;
    1502 
    1503             $meta_sql = $groups_meta_query->get_sql( 'group', 'g', 'id' );
    1504             $sql_array['join']  = $meta_sql['join'];
    1505             $sql_array['where'] = self::strip_leading_and( $meta_sql['where'] );
    1506         }
     1509        if ( empty( $meta_query ) ) {
     1510            return $sql_array;
     1511        }
     1512
     1513        $groups_meta_query = new WP_Meta_Query( $meta_query );
     1514
     1515        // WP_Meta_Query expects the table name at $wpdb->group.
     1516        $wpdb->groupmeta = buddypress()->groups->table_name_groupmeta;
     1517
     1518        $meta_sql           = $groups_meta_query->get_sql( 'group', 'g', 'id' );
     1519        $sql_array['join']  = $meta_sql['join'];
     1520        $sql_array['where'] = self::strip_leading_and( $meta_sql['where'] );
    15071521
    15081522        return $sql_array;
     1523    }
     1524
     1525    /**
     1526     * Get the SQL for the 'tax_query' param in BP_Groups_Group::get().
     1527     *
     1528     * @param array $tax_query An array of tax query arguments. See the
     1529     *                         documentation for {@link WP_Tax_Query} for more details.
     1530     * @return array
     1531     */
     1532    protected static function get_tax_query_sql( $tax_query = array() ) {
     1533        $sql_array = array(
     1534            'join'  => '',
     1535            'where' => '',
     1536        );
     1537
     1538        if ( empty( $tax_query ) || ! is_array( $tax_query ) ) {
     1539            return $sql_array;
     1540        }
     1541
     1542        $tax_query          = new WP_Tax_Query( $tax_query );
     1543        $tax_query_sql      = $tax_query->get_sql( 'g', 'id' );
     1544        $sql_array['join']  = $tax_query_sql['join'];
     1545        $sql_array['where'] = self::strip_leading_and( $tax_query_sql['where'] );
     1546
     1547        return $sql_array;
     1548    }
     1549
     1550    /**
     1551     * Get SQL clause for group type(s).
     1552     *
     1553     * @since 2.6.0
     1554     *
     1555     * @global wpdb $wpdb WordPress database object.
     1556     *
     1557     * @param  string|array $group_types Group type(s).
     1558     * @param  string       $operator    'IN' or 'NOT IN'.
     1559     *
     1560     * @return string SQL clause.
     1561     */
     1562    protected static function get_sql_clause_for_group_types( $group_types, $operator ) {
     1563        global $wpdb;
     1564
     1565        // Sanitize operator.
     1566        if ( 'NOT IN' !== $operator ) {
     1567            $operator = 'IN';
     1568        }
     1569
     1570        // Parse and sanitize types.
     1571        if ( ! is_array( $group_types ) ) {
     1572            $group_types = preg_split( '/[,\s+]/', $group_types );
     1573        }
     1574
     1575        $types = array();
     1576        foreach ( $group_types as $gt ) {
     1577            if ( bp_groups_get_group_type_object( $gt ) ) {
     1578                $types[] = $gt;
     1579            }
     1580        }
     1581
     1582        $tax_query = new WP_Tax_Query(
     1583            array(
     1584                array(
     1585                    'taxonomy' => bp_get_group_type_tax_name(),
     1586                    'field'    => 'name',
     1587                    'operator' => $operator,
     1588                    'terms'    => $types,
     1589                ),
     1590            )
     1591         );
     1592
     1593        $site_id  = bp_get_taxonomy_term_site_id( bp_get_group_type_tax_name() );
     1594        $switched = false;
     1595        if ( $site_id !== get_current_blog_id() ) {
     1596            switch_to_blog( $site_id );
     1597            $switched = true;
     1598        }
     1599
     1600        $sql_clauses = $tax_query->get_sql( 'g', 'id' );
     1601        $clause      = '';
     1602
     1603        // The no_results clauses are the same between IN and NOT IN.
     1604        if ( str_contains( $sql_clauses['where'], '0 = 1' ) ) {
     1605            $clause = self::strip_leading_and( $sql_clauses['where'] );
     1606
     1607            // The tax_query clause generated for NOT IN can be used almost as-is.
     1608        } elseif ( 'NOT IN' === $operator ) {
     1609            $clause = self::strip_leading_and( $sql_clauses['where'] );
     1610
     1611            // IN clauses must be converted to a subquery.
     1612        } elseif ( preg_match( '/' . $wpdb->term_relationships . '\.term_taxonomy_id IN \([0-9, ]+\)/', $sql_clauses['where'], $matches ) ) {
     1613            $clause = " g.id IN ( SELECT object_id FROM $wpdb->term_relationships WHERE {$matches[0]} )";
     1614        }
     1615
     1616        if ( $switched ) {
     1617            restore_current_blog();
     1618        }
     1619
     1620        return $clause;
    15091621    }
    15101622
     
    15511663        }
    15521664
    1553         return array( 'order' => $order, 'orderby' => $orderby );
     1665        return array(
     1666            'order' => $order,
     1667            'orderby' => $orderby
     1668        );
    15541669    }
    15551670
     
    15601675     *
    15611676     * @param string $orderby Orderby term as passed to get().
    1562      * @return string $order_by_term SQL-friendly orderby term.
     1677     *
     1678     * @return string SQL-friendly orderby term.
    15631679     */
    15641680    protected static function convert_orderby_to_order_by_term( $orderby ) {
    1565         $order_by_term = '';
    1566 
    15671681        switch ( $orderby ) {
    15681682            case 'date_created' :
     
    18461960     * @since 1.7.0
    18471961     *
     1962     * @global wpdb $wpdb WordPress database object.
     1963     *
    18481964     * @return array
    18491965     */
     
    18631979
    18641980    /**
    1865      * Get SQL clause for group type(s).
    1866      *
    1867      * @since 2.6.0
    1868      *
    1869      * @param  string|array $group_types Group type(s).
    1870      * @param  string       $operator    'IN' or 'NOT IN'.
    1871      * @return string       $clause      SQL clause.
    1872      */
    1873     protected static function get_sql_clause_for_group_types( $group_types, $operator ) {
    1874         global $wpdb;
    1875 
    1876         // Sanitize operator.
    1877         if ( 'NOT IN' !== $operator ) {
    1878             $operator = 'IN';
    1879         }
    1880 
    1881         // Parse and sanitize types.
    1882         if ( ! is_array( $group_types ) ) {
    1883             $group_types = preg_split( '/[,\s+]/', $group_types );
    1884         }
    1885 
    1886         $types = array();
    1887         foreach ( $group_types as $gt ) {
    1888             if ( bp_groups_get_group_type_object( $gt ) ) {
    1889                 $types[] = $gt;
    1890             }
    1891         }
    1892 
    1893         $tax_query = new WP_Tax_Query( array(
    1894             array(
    1895                 'taxonomy' => bp_get_group_type_tax_name(),
    1896                 'field'    => 'name',
    1897                 'operator' => $operator,
    1898                 'terms'    => $types,
    1899             ),
    1900         ) );
    1901 
    1902         $site_id  = bp_get_taxonomy_term_site_id( bp_get_group_type_tax_name() );
    1903         $switched = false;
    1904         if ( $site_id !== get_current_blog_id() ) {
    1905             switch_to_blog( $site_id );
    1906             $switched = true;
    1907         }
    1908 
    1909         $sql_clauses = $tax_query->get_sql( 'g', 'id' );
    1910 
    1911         $clause = '';
    1912 
    1913         // The no_results clauses are the same between IN and NOT IN.
    1914         if ( false !== strpos( $sql_clauses['where'], '0 = 1' ) ) {
    1915             $clause = self::strip_leading_and( $sql_clauses['where'] );
    1916 
    1917         // The tax_query clause generated for NOT IN can be used almost as-is.
    1918         } elseif ( 'NOT IN' === $operator ) {
    1919             $clause = self::strip_leading_and( $sql_clauses['where'] );
    1920 
    1921         // IN clauses must be converted to a subquery.
    1922         } elseif ( preg_match( '/' . $wpdb->term_relationships . '\.term_taxonomy_id IN \([0-9, ]+\)/', $sql_clauses['where'], $matches ) ) {
    1923             $clause = " g.id IN ( SELECT object_id FROM $wpdb->term_relationships WHERE {$matches[0]} )";
    1924         }
    1925 
    1926         if ( $switched ) {
    1927             restore_current_blog();
    1928         }
    1929 
    1930         return $clause;
    1931     }
    1932 
    1933     /**
    19341981     * Strips the leading AND and any surrounding whitespace from a string.
    19351982     *
  • trunk/tests/phpunit/testcases/groups/class-bp-groups-group.php

    r14070 r14078  
    66#[AllowDynamicProperties]
    77class BP_Tests_BP_Groups_Group_TestCases extends BP_UnitTestCase {
    8 
    9     /** __construct()  ***************************************************/
    108
    119    /**
     
    856854    /**
    857855     * @group cache
    858      * @group group_types
    859856     * @ticket BP5451
    860857     * @ticket BP6643
    861858     */
    862     public function test_get_query_caches_should_be_busted_by_group_term_change() {
    863         global $wpdb;
    864 
    865         bp_groups_register_group_type( 'foo' );
    866         bp_groups_register_group_type( 'bar' );
    867 
     859    public function test_get_query_caches_should_be_busted_by_group_save() {
    868860        $groups = self::factory()->group->create_many( 2 );
    869         bp_groups_set_group_type( $groups[0], 'foo' );
    870         bp_groups_set_group_type( $groups[1], 'bar' );
     861        groups_update_groupmeta( $groups[0], 'foo', 'bar' );
     862        groups_update_groupmeta( $groups[1], 'foo', 'bar' );
    871863
    872864        $found1 = BP_Groups_Group::get( array(
    873             'group_type' => 'foo',
    874         ) );
    875 
    876         $this->assertEqualSets( array( $groups[0] ), wp_list_pluck( $found1['groups'], 'id' ) );
    877 
    878         bp_groups_set_group_type( $groups[1], 'foo' );
     865            'search_terms' => 'Foo',
     866        ) );
     867
     868        $this->assertEmpty( $found1['groups'] );
     869
     870        $group0 = groups_get_group( $groups[0] );
     871        $group0->name = 'Foo';
     872        $group0->save();
    879873
    880874        $found2 = BP_Groups_Group::get( array(
    881             'group_type' => 'foo',
    882         ) );
    883 
    884         $this->assertEqualSets( array( $groups[0], $groups[1] ), wp_list_pluck( $found2['groups'], 'id' ) );
    885     }
    886 
    887     /**
    888      * @group cache
    889      * @group group_types
    890      * @ticket BP5451
    891      * @ticket BP6643
    892      */
    893     public function test_get_query_caches_should_be_busted_by_group_term_removal() {
    894         global $wpdb;
    895 
    896         bp_groups_register_group_type( 'foo' );
    897 
    898         $groups = self::factory()->group->create_many( 2 );
    899         bp_groups_set_group_type( $groups[0], 'foo' );
    900         bp_groups_set_group_type( $groups[1], 'foo' );
    901 
    902         $found1 = BP_Groups_Group::get( array(
    903             'group_type' => 'foo',
    904         ) );
    905 
    906         $this->assertEqualSets( array( $groups[0], $groups[1] ), wp_list_pluck( $found1['groups'], 'id' ) );
    907 
    908         bp_groups_remove_group_type( $groups[1], 'foo' );
    909 
    910         $found2 = BP_Groups_Group::get( array(
    911             'group_type' => 'foo',
     875            'search_terms' => 'Foo',
    912876        ) );
    913877
     
    920884     * @ticket BP6643
    921885     */
    922     public function test_get_query_caches_should_be_busted_by_group_save() {
    923         global $wpdb;
    924 
    925         $groups = self::factory()->group->create_many( 2 );
    926         groups_update_groupmeta( $groups[0], 'foo', 'bar' );
    927         groups_update_groupmeta( $groups[1], 'foo', 'bar' );
    928 
    929         $found1 = BP_Groups_Group::get( array(
    930             'search_terms' => 'Foo',
    931         ) );
    932 
    933         $this->assertEmpty( $found1['groups'] );
    934 
    935         $group0 = groups_get_group( $groups[0] );
    936         $group0->name = 'Foo';
    937         $group0->save();
    938 
    939         $found2 = BP_Groups_Group::get( array(
    940             'search_terms' => 'Foo',
    941         ) );
    942 
    943         $this->assertEqualSets( array( $groups[0] ), wp_list_pluck( $found2['groups'], 'id' ) );
    944     }
    945 
    946     /**
    947      * @group cache
    948      * @ticket BP5451
    949      * @ticket BP6643
    950      */
    951886    public function test_get_query_caches_should_be_busted_by_group_delete() {
    952         global $wpdb;
    953 
    954887        $groups = self::factory()->group->create_many( 2 );
    955888
     
    18191752
    18201753    /**
    1821      * @group group_types
    1822      */
    1823     public function test_group_type_single_value() {
    1824         $g1 = self::factory()->group->create();
    1825         $g2 = self::factory()->group->create();
    1826         bp_groups_register_group_type( 'foo' );
    1827         bp_groups_register_group_type( 'bar' );
    1828         bp_groups_set_group_type( $g1, 'foo' );
    1829         bp_groups_set_group_type( $g2, 'bar' );
    1830 
    1831         $groups = BP_Groups_Group::get( array(
    1832             'group_type' => 'foo',
    1833         ) );
    1834 
    1835         $found = wp_list_pluck( $groups['groups'], 'id' );
    1836         $this->assertEquals( array( $g1 ), $found );
    1837     }
    1838 
    1839     /**
    1840      * @group group_types
    1841      */
    1842     public function test_group_type_array_with_single_value() {
    1843         $g1 = self::factory()->group->create();
    1844         $g2 = self::factory()->group->create();
    1845         bp_groups_register_group_type( 'foo' );
    1846         bp_groups_register_group_type( 'bar' );
    1847         bp_groups_set_group_type( $g1, 'foo' );
    1848         bp_groups_set_group_type( $g2, 'bar' );
    1849 
    1850         $groups = BP_Groups_Group::get( array(
    1851             'group_type' => array( 'foo' ),
    1852         ) );
    1853 
    1854         $found = wp_list_pluck( $groups['groups'], 'id' );
    1855         $this->assertEquals( array( $g1 ), $found );
    1856     }
    1857 
    1858     /**
    1859      * @group group_types
    1860      */
    1861     public function test_group_type_with_comma_separated_list() {
    1862         $g1 = self::factory()->group->create();
    1863         $g2 = self::factory()->group->create();
    1864         bp_groups_register_group_type( 'foo' );
    1865         bp_groups_register_group_type( 'bar' );
    1866         bp_groups_set_group_type( $g1, 'foo' );
    1867         bp_groups_set_group_type( $g2, 'bar' );
    1868 
    1869         $groups = BP_Groups_Group::get( array(
    1870             'group_type' => 'foo, bar',
    1871         ) );
    1872 
    1873         $found = wp_list_pluck( $groups['groups'], 'id' );
    1874         $this->assertEqualSets( array( $g1, $g2 ), $found );
    1875     }
    1876 
    1877     /**
    1878      * @group group_types
    1879      */
    1880     public function test_group_type_array_with_multiple_values() {
    1881         $g1 = self::factory()->group->create();
    1882         $g2 = self::factory()->group->create();
    1883         bp_groups_register_group_type( 'foo' );
    1884         bp_groups_register_group_type( 'bar' );
    1885         bp_groups_set_group_type( $g1, 'foo' );
    1886         bp_groups_set_group_type( $g2, 'bar' );
    1887 
    1888         $groups = BP_Groups_Group::get( array(
    1889             'group_type' => array( 'foo', 'bar' ),
    1890         ) );
    1891 
    1892         $found = wp_list_pluck( $groups['groups'], 'id' );
    1893         $this->assertEqualSets( array( $g1, $g2 ), $found );
    1894     }
    1895 
    1896     /**
    1897      * @group group_types
    1898      */
    1899     public function test_group_type_should_discart_non_existing_types_in_comma_separated_value() {
    1900         $g1 = self::factory()->group->create();
    1901         $g2 = self::factory()->group->create();
    1902         bp_groups_register_group_type( 'foo' );
    1903         bp_groups_register_group_type( 'bar' );
    1904         bp_groups_set_group_type( $g1, 'foo' );
    1905         bp_groups_set_group_type( $g2, 'bar' );
    1906 
    1907         $groups = BP_Groups_Group::get( array(
    1908             'group_type' => 'foo, baz',
    1909         ) );
    1910 
    1911         $found = wp_list_pluck( $groups['groups'], 'id' );
    1912         $this->assertEquals( array( $g1 ), $found );
    1913     }
    1914 
    1915     /**
    1916      * @group group_types
    1917      */
    1918     public function test_group_type_should_return_empty_when_no_groups_match_specified_types() {
    1919         $g1 = self::factory()->group->create();
    1920         $g2 = self::factory()->group->create();
    1921 
    1922         $groups = BP_Groups_Group::get( array(
    1923             'group_type' => 'foo, baz',
    1924         ) );
    1925 
    1926         $this->assertEmpty( $groups['groups'] );
    1927     }
    1928 
    1929     /**
    1930      * @group group_types
    1931      */
    1932     public function test_group_type__in_single_value() {
    1933         $g1 = self::factory()->group->create();
    1934         $g2 = self::factory()->group->create();
    1935         bp_groups_register_group_type( 'foo' );
    1936         bp_groups_register_group_type( 'bar' );
    1937         bp_groups_set_group_type( $g1, 'foo' );
    1938         bp_groups_set_group_type( $g2, 'bar' );
    1939 
    1940         $groups = BP_Groups_Group::get( array(
    1941             'group_type__in' => 'bar',
    1942         ) );
    1943 
    1944         $found = wp_list_pluck( $groups['groups'], 'id' );
    1945         $this->assertEquals( array( $g2 ), $found );
    1946     }
    1947 
    1948     /**
    1949      * @group group_types
    1950      */
    1951     public function test_group_type__in_comma_separated_values() {
    1952         $g1 = self::factory()->group->create();
    1953         $g2 = self::factory()->group->create();
    1954         bp_groups_register_group_type( 'foo' );
    1955         bp_groups_register_group_type( 'bar' );
    1956         bp_groups_set_group_type( $g1, 'foo' );
    1957         bp_groups_set_group_type( $g2, 'bar' );
    1958 
    1959         $groups = BP_Groups_Group::get( array(
    1960             'group_type__in' => 'foo, bar',
    1961         ) );
    1962 
    1963         $found = wp_list_pluck( $groups['groups'], 'id' );
    1964         $this->assertEqualSets( array( $g1, $g2 ), $found );
    1965     }
    1966 
    1967     /**
    1968      * @group group_types
    1969      */
    1970     public function test_group_type__in_array_multiple_values() {
    1971         $g1 = self::factory()->group->create();
    1972         $g2 = self::factory()->group->create();
    1973         bp_groups_register_group_type( 'foo' );
    1974         bp_groups_register_group_type( 'bar' );
    1975         bp_groups_set_group_type( $g1, 'foo' );
    1976         bp_groups_set_group_type( $g2, 'bar' );
    1977 
    1978         $groups = BP_Groups_Group::get( array(
    1979             'group_type__in' => array( 'foo', 'bar' ),
    1980         ) );
    1981 
    1982         $found = wp_list_pluck( $groups['groups'], 'id' );
    1983         $this->assertEqualSets( array( $g1, $g2 ), $found );
    1984     }
    1985 
    1986     /**
    1987      * @group group_types
    1988      */
    1989     public function test_group_type__in_array_with_single_value() {
    1990         $g1 = self::factory()->group->create();
    1991         $g2 = self::factory()->group->create();
    1992         bp_groups_register_group_type( 'foo' );
    1993         bp_groups_register_group_type( 'bar' );
    1994         bp_groups_set_group_type( $g1, 'foo' );
    1995         bp_groups_set_group_type( $g2, 'bar' );
    1996 
    1997         $groups = BP_Groups_Group::get( array(
    1998             'group_type__in' => array( 'foo' ),
    1999         ) );
    2000 
    2001         $found = wp_list_pluck( $groups['groups'], 'id' );
    2002         $this->assertEquals( array( $g1 ), $found );
    2003     }
    2004 
    2005     /**
    2006      * @group group_types
    2007      */
    2008     public function test_group_type__in_should_discart_non_existing_types_in_comma_separated_value() {
    2009         $g1 = self::factory()->group->create();
    2010         $g2 = self::factory()->group->create();
    2011         bp_groups_register_group_type( 'foo' );
    2012         bp_groups_register_group_type( 'bar' );
    2013         bp_groups_set_group_type( $g1, 'foo' );
    2014         bp_groups_set_group_type( $g2, 'bar' );
    2015 
    2016         $groups = BP_Groups_Group::get( array(
    2017             'group_type__in' => 'foo, baz',
    2018         ) );
    2019 
    2020         $found = wp_list_pluck( $groups['groups'], 'id' );
    2021         $this->assertEquals( array( $g1 ), $found );
    2022     }
    2023 
    2024     /**
    2025      * @group group_types
    2026      */
    2027     public function test_group_type__in_should_return_empty_when_no_groups_match_specified_types() {
    2028         $g1 = self::factory()->group->create();
    2029         $g2 = self::factory()->group->create();
    2030 
    2031         $groups = BP_Groups_Group::get( array(
    2032             'group_type__in' => 'foo, baz',
    2033         ) );
    2034 
    2035         $this->assertEmpty( $groups['groups'] );
    2036     }
    2037 
    2038     /**
    2039      * @group group_types
    2040      */
    2041     public function test_group_type_should_take_precedence_over_group_type__in() {
    2042         $g1 = self::factory()->group->create();
    2043         $g2 = self::factory()->group->create();
    2044         bp_groups_register_group_type( 'foo' );
    2045         bp_groups_register_group_type( 'bar' );
    2046         bp_groups_set_group_type( $g1, 'foo' );
    2047         bp_groups_set_group_type( $g2, 'bar' );
    2048 
    2049         $groups = BP_Groups_Group::get( array(
    2050             'group_type__in' => 'foo',
    2051             'group_type' => 'bar',
    2052         ) );
    2053 
    2054         $found = wp_list_pluck( $groups['groups'], 'id' );
    2055         $this->assertEquals( array( $g2 ), $found );
    2056     }
    2057 
    2058     /**
    2059      * @group group_types
    2060      */
    2061     public function test_group_type__not_in_should_return_groups_with_types_and_without_types() {
    2062         $g1 = self::factory()->group->create();
    2063         $g2 = self::factory()->group->create();
    2064         $g3 = self::factory()->group->create();
    2065         bp_groups_register_group_type( 'foo' );
    2066         bp_groups_register_group_type( 'bar' );
    2067         bp_groups_set_group_type( $g1, 'foo' );
    2068         bp_groups_set_group_type( $g2, 'bar' );
    2069 
    2070         $groups = BP_Groups_Group::get( array(
    2071             'group_type__not_in' => 'foo',
    2072         ) );
    2073 
    2074         $found = wp_list_pluck( $groups['groups'], 'id' );
    2075         $this->assertEquals( array( $g2, $g3 ), $found );
    2076     }
    2077 
    2078     /**
    2079      * @group group_types
    2080      */
    2081     public function test_group_type__not_in_comma_separated_values() {
    2082         $g1 = self::factory()->group->create();
    2083         $g2 = self::factory()->group->create();
    2084         $g3 = self::factory()->group->create();
    2085         bp_groups_register_group_type( 'foo' );
    2086         bp_groups_register_group_type( 'bar' );
    2087         bp_groups_set_group_type( $g1, 'foo' );
    2088         bp_groups_set_group_type( $g2, 'bar' );
    2089         bp_groups_set_group_type( $g3, 'baz' );
    2090 
    2091         $groups = BP_Groups_Group::get( array(
    2092             'group_type__not_in' => 'foo, bar',
    2093         ) );
    2094 
    2095         $found = wp_list_pluck( $groups['groups'], 'id' );
    2096         $this->assertEquals( array( $g3 ), $found );
    2097     }
    2098 
    2099     /**
    2100      * @group group_types
    2101      */
    2102     public function test_group_type__not_array_with_multiple_values() {
    2103         $g1 = self::factory()->group->create();
    2104         $g2 = self::factory()->group->create();
    2105         $g3 = self::factory()->group->create();
    2106         bp_groups_register_group_type( 'foo' );
    2107         bp_groups_register_group_type( 'bar' );
    2108         bp_groups_set_group_type( $g1, 'foo' );
    2109         bp_groups_set_group_type( $g2, 'bar' );
    2110         bp_groups_set_group_type( $g3, 'baz' );
    2111 
    2112         $groups = BP_Groups_Group::get( array(
    2113             'group_type__not_in' => array( 'foo', 'bar' ),
    2114         ) );
    2115 
    2116         $found = wp_list_pluck( $groups['groups'], 'id' );
    2117         $this->assertEquals( array( $g3 ), $found );
    2118     }
    2119 
    2120     /**
    2121      * @group group_types
    2122      */
    2123     public function test_group_type__not_in_should_return_no_results_when_all_groups_mathc_sepecified_type() {
    2124         $g1 = self::factory()->group->create();
    2125         $g2 = self::factory()->group->create();
    2126         $g3 = self::factory()->group->create();
    2127         bp_groups_register_group_type( 'foo' );
    2128         bp_groups_set_group_type( $g1, 'foo' );
    2129         bp_groups_set_group_type( $g2, 'foo' );
    2130         bp_groups_set_group_type( $g3, 'foo' );
    2131 
    2132         $groups = BP_Groups_Group::get( array(
    2133             'group_type__not_in' => 'foo',
    2134         ) );
    2135 
    2136         $this->assertEmpty( $groups['groups'] );
    2137     }
    2138 
    2139     /**
    2140      * @group group_types
    2141      */
    2142     public function test_group_type__not_in_takes_precedence_over_group_type() {
    2143         $g1 = self::factory()->group->create();
    2144         $g2 = self::factory()->group->create();
    2145         $g3 = self::factory()->group->create();
    2146         bp_groups_register_group_type( 'foo' );
    2147         bp_groups_set_group_type( $g1, 'foo' );
    2148         bp_groups_set_group_type( $g2, 'foo' );
    2149         bp_groups_set_group_type( $g3, 'foo' );
    2150 
    2151         $groups = BP_Groups_Group::get( array(
    2152             'group_type' => 'foo',
    2153             'group_type__not_in' => 'foo',
    2154         ) );
    2155 
    2156         $this->assertEmpty( $groups['groups'] );
    2157     }
    2158 
    2159     /**
    21601754     * @group hierarchical_groups
    21611755     */
Note: See TracChangeset for help on using the changeset viewer.