Ticket #6784: 6784.2.diff
File 6784.2.diff, 42.0 KB (added by , 9 years ago) |
---|
-
src/bp-groups/bp-groups-cache.php
diff --git src/bp-groups/bp-groups-cache.php src/bp-groups/bp-groups-cache.php index 3d09916..c2116c1 100644
function groups_clear_group_administrator_cache_on_member_save( BP_Groups_Member 212 212 } 213 213 add_action( 'groups_member_after_save', 'groups_clear_group_administrator_cache_on_member_save' ); 214 214 215 /** 216 * Clear the group type cache for group. 217 * 218 * Called when group is deleted. 219 * 220 * @since 2.6.0 221 * 222 * @param int $group_id The group ID. 223 */ 224 function groups_clear_group_type_cache( $group_id = 0 ) { 225 wp_cache_delete( $group_id, 'bp_groups_group_type' ); 226 } 227 add_action( 'groups_delete_group', 'groups_clear_group_type_cache' ); 228 215 229 /* List actions to clear super cached pages on, if super cache is installed */ 216 230 add_action( 'groups_join_group', 'bp_core_clear_cache' ); 217 231 add_action( 'groups_leave_group', 'bp_core_clear_cache' ); -
src/bp-groups/bp-groups-functions.php
diff --git src/bp-groups/bp-groups-functions.php src/bp-groups/bp-groups-functions.php index c5cdc1f..d569357 100644
function groups_remove_data_for_user( $user_id ) { 1850 1850 add_action( 'wpmu_delete_user', 'groups_remove_data_for_user' ); 1851 1851 add_action( 'delete_user', 'groups_remove_data_for_user' ); 1852 1852 add_action( 'bp_make_spam_user', 'groups_remove_data_for_user' ); 1853 1854 /** Group Types ***************************************************************/ 1855 1856 /** 1857 * Register a group type. 1858 * 1859 * @since 2.6.0 1860 * 1861 * @param string $group_type Unique string identifier for the group type. 1862 * @param array $args { 1863 * Array of arguments describing the group type. 1864 * 1865 * @type array $labels { 1866 * Array of labels to use in various parts of the interface. 1867 * 1868 * @type string $name Default name. Should typically be plural. 1869 * @type string $singular_name Singular name. 1870 * } 1871 * @type bool|string $has_directory Whether the group type should have its own type-specific directory. 1872 * Pass `true` to use the `$group_type` string as the type's slug. 1873 * Pass a string to customize the slug. Pass `false` to disable. 1874 * Default: true. 1875 * } 1876 * @return object|WP_Error Group type object on success, WP_Error object on failure. 1877 */ 1878 function bp_groups_register_group_type( $group_type, $args = array() ) { 1879 $bp = buddypress(); 1880 1881 if ( isset( $bp->groups->types[ $group_type ] ) ) { 1882 return new WP_Error( 'bp_group_type_exists', __( 'Group type already exists.', 'buddypress' ), $group_type ); 1883 } 1884 1885 $r = bp_parse_args( $args, array( 1886 'labels' => array(), 1887 'has_directory' => true, 1888 ), 'register_group_type' ); 1889 1890 $group_type = sanitize_key( $group_type ); 1891 1892 /** 1893 * Filters the list of illegal group type names. 1894 * 1895 * - 'any' is a special pseudo-type, representing items unassociated with any group type. 1896 * - 'null' is a special pseudo-type, representing users without any type. 1897 * - '_none' is used internally to denote an item that should not apply to any group types. 1898 * 1899 * @since 2.6.0 1900 * 1901 * @param array $illegal_names Array of illegal names. 1902 */ 1903 $illegal_names = apply_filters( 'bp_group_type_illegal_names', array( 'any', 'null', '_none' ) ); 1904 if ( in_array( $group_type, $illegal_names, true ) ) { 1905 return new WP_Error( 'bp_group_type_illegal_name', __( 'You may not register a group type with this name.', 'buddypress' ), $group_type ); 1906 } 1907 1908 // Store the group type name as data in the object (not just as the array key). 1909 $r['name'] = $group_type; 1910 1911 // Make sure the relevant labels have been filled in. 1912 $default_name = isset( $r['labels']['name'] ) ? $r['labels']['name'] : ucfirst( $r['name'] ); 1913 $r['labels'] = array_merge( array( 1914 'name' => $default_name, 1915 'singular_name' => $default_name, 1916 ), $r['labels'] ); 1917 1918 if ( $r['has_directory'] ) { 1919 if ( is_string( $r['has_directory'] ) ) { 1920 $directory_slug = $r['has_directory']; 1921 } else { 1922 $directory_slug = $group_type; 1923 } 1924 1925 // Sanitize for use in URIs. 1926 $r['directory_slug'] = sanitize_title( $directory_slug ); 1927 $r['has_directory'] = true; 1928 } else { 1929 $r['directory_slug'] = ''; 1930 $r['has_directory'] = false; 1931 } 1932 1933 $bp->groups->types[ $group_type ] = $type = (object) $r; 1934 1935 /** 1936 * Fires after a group type is registered. 1937 * 1938 * @since 2.6.0 1939 * 1940 * @param string $group_type Group type identifier. 1941 * @param object $$type Group type object. 1942 */ 1943 do_action( 'bp_groups_register_group_type', $group_type, $type ); 1944 1945 return $type; 1946 } 1947 1948 /** 1949 * Get a list of all registered group type objects. 1950 * 1951 * @since 2.6.0 1952 * 1953 * @see bp_groups_register_group_type() for accepted arguments. 1954 * 1955 * @param array|string $args Optional. An array of key => value arguments to match against 1956 * the group type objects. Default empty array. 1957 * @param string $output Optional. The type of output to return. Accepts 'names' 1958 * or 'objects'. Default 'names'. 1959 * @param string $operator Optional. The logical operation to perform. 'or' means only one 1960 * element from the array needs to match; 'and' means all elements 1961 * must match. Accepts 'or' or 'and'. Default 'and'. 1962 * @return array $types A list of groups type names or objects. 1963 */ 1964 function bp_groups_get_group_types( $args = array(), $output = 'names', $operator = 'and' ) { 1965 $types = buddypress()->groups->types; 1966 1967 $type = wp_filter_object_list( $types, $args, $operator ); 1968 1969 /** 1970 * Filters the array of group type objects. 1971 * 1972 * This filter is run before the $output filter has been applied, so that 1973 * filtering functions have access to the entire group type objects. 1974 * 1975 * @since 2.6.0 1976 * 1977 * @param array $types group type objects, keyed by name. 1978 * @param array $args Array of key=>value arguments for filtering. 1979 * @param string $operator 'or' to match any of $args, 'and' to require all. 1980 */ 1981 $types = apply_filters( 'bp_groups_get_group_types', $types, $args, $operator ); 1982 1983 if ( 'names' === $output ) { 1984 $types = wp_list_pluck( $types, 'name' ); 1985 } 1986 1987 return $types; 1988 } 1989 1990 /** 1991 * Retrieve a group type object by name. 1992 * 1993 * @since 2.6.0 1994 * 1995 * @param string $group_type The name of the group type. 1996 * @return object A group type object. 1997 */ 1998 function bp_groups_get_group_type_object( $group_type ) { 1999 $types = bp_groups_get_group_types( array(), 'objects' ); 2000 2001 if ( empty( $types[ $group_type ] ) ) { 2002 return null; 2003 } 2004 2005 return $types[ $group_type ]; 2006 } 2007 2008 /** 2009 * Set type for a group. 2010 * 2011 * @since 2.6.0 2012 * 2013 * @param int $group ID of the group. 2014 * @param string $group_type Group type. 2015 * @param bool $append Optional. True to append this to existing types for group, 2016 * false to replace. Default: false. 2017 * @return array $retval See {@see bp_set_object_terms()}. 2018 */ 2019 function bp_groups_set_group_type( $group_id, $group_type, $append = false ) { 2020 // Pass an empty group type to remove group's type. 2021 if ( ! empty( $group_type ) && ! bp_groups_get_group_type_object( $group_type ) ) { 2022 return false; 2023 } 2024 2025 $retval = bp_set_object_terms( $group_id, $group_type, 'bp_group_type', $append ); 2026 2027 // Bust the cache if the type has been updated. 2028 if ( ! is_wp_error( $retval ) ) { 2029 wp_cache_delete( $group_id, 'bp_groups_group_type' ); 2030 2031 /** 2032 * Fires just after a group type has been changed. 2033 * 2034 * @since 2.6.0 2035 * 2036 * @param int $group_id ID of the group whose group type has been updated. 2037 * @param string $group_type Group type. 2038 * @param bool $append Whether the type is being appended to existing types. 2039 */ 2040 do_action( 'bp_groups_set_group_type', $group_id, $group_type, $append ); 2041 } 2042 2043 return $retval; 2044 } 2045 2046 /** 2047 * Get type for a group. 2048 * 2049 * @since 2.6.0 2050 * 2051 * @param int $group_id ID of the group. 2052 * @param bool $single Optional. Whether to return a single type string. If multiple types are found 2053 * for the group, the oldest one will be returned. Default: true. 2054 * @return string|array|bool On success, returns a single group type (if `$single` is true) or an array of group 2055 * types (if `$single` is false). Returns false on failure. 2056 */ 2057 function bp_groups_get_group_type( $group_id, $single = true ) { 2058 $types = wp_cache_get( $group_id, 'bp_groups_group_type' ); 2059 2060 if ( false === $types ) { 2061 $types = bp_get_object_terms( $group_id, 'bp_group_type' ); 2062 2063 if ( ! is_wp_error( $types ) ) { 2064 $types = wp_list_pluck( $types, 'name' ); 2065 wp_cache_set( $group_id, $types, 'bp_groups_group_type' ); 2066 } 2067 } 2068 2069 $type = false; 2070 if ( ! empty( $types ) ) { 2071 if ( $single ) { 2072 $type = end( $types ); 2073 } else { 2074 $type = $types; 2075 } 2076 } 2077 2078 /** 2079 * Filters a groups's group type(s). 2080 * 2081 * @since 2.6.0 2082 * 2083 * @param string|array $type Group type. 2084 * @param int $group_id ID of the group. 2085 * @param bool $single Whether to return a single type srting, or an array. 2086 */ 2087 return apply_filters( 'bp_groups_get_group_type', $type, $group_id, $single ); 2088 } 2089 2090 /** 2091 * Remove type for a group. 2092 * 2093 * @since 2.6.0 2094 * 2095 * @param int $group ID of the user. 2096 * @param string $group_type Group type. 2097 * @return bool|WP_Error $deleted True on success. False or WP_Error on failure. 2098 */ 2099 function bp_groups_remove_group_type( $group_id, $group_type ) { 2100 if ( empty( $group_type ) || ! bp_groups_get_group_type_object( $group_type ) ) { 2101 return false; 2102 } 2103 2104 $deleted = bp_remove_object_terms( $group_id, $group_type, 'bp_group_type' ); 2105 2106 // Bust the case, if the type has been removed. 2107 if ( ! is_wp_error( $deleted ) ) { 2108 wp_cache_delete( $group_id, 'bp_groups_group_type' ); 2109 2110 /** 2111 * Fires just after a group's group type has been removed. 2112 * 2113 * @since 2.6.0 2114 * 2115 * @param int $group ID of the group whose group type has been removed. 2116 * @param string $group_type Group type. 2117 */ 2118 do_action( 'bp_groups_remove_group_type', $group_id, $group_type ); 2119 } 2120 2121 return $deleted; 2122 } 2123 2124 /** 2125 * Check whether the given group has a certain group type. 2126 * 2127 * @since 2.6.0 2128 * 2129 * @param int $group_id ID of the group. 2130 * @param srting $group_type Group type. 2131 * @return bool Whether the group has the give group type. 2132 */ 2133 function bp_groups_has_group_type( $group_id, $group_type ) { 2134 if ( empty( $group_type ) || ! bp_groups_get_group_type_object( $group_type ) ) { 2135 return false; 2136 } 2137 2138 // Get all group's group types. 2139 $types = bp_groups_get_group_type( $group_id, false ); 2140 2141 if ( ! is_array( $types ) ) { 2142 return false; 2143 } 2144 2145 return in_array( $group_type, $types ); 2146 } 2147 2148 /** 2149 * Delete a group's type when the group is deleted. 2150 * 2151 * @since 2.6.0 2152 * 2153 * @param int $group_id ID of the group. 2154 * @return array $value See {@see bp_groups_set_group_type()}. 2155 */ 2156 function bp_remove_group_type_on_group_delete( $group_id = 0 ) { 2157 bp_groups_set_group_type( $group_id, '' ); 2158 } 2159 add_action( 'groups_delete_group', 'bp_remove_group_type_on_group_delete' ); -
src/bp-groups/classes/class-bp-groups-component.php
diff --git src/bp-groups/classes/class-bp-groups-component.php src/bp-groups/classes/class-bp-groups-component.php index 355ab8c..7de73b2 100644
class BP_Groups_Component extends BP_Component { 832 832 833 833 parent::setup_cache_groups(); 834 834 } 835 836 /** 837 * Set up taxonomies. 838 * 839 * @since 2.6.0 840 */ 841 public function register_taxonomies() { 842 // Group Type. 843 register_taxonomy( 'bp_group_type', 'bp_group', array( 844 'public' => false, 845 ) ); 846 } 835 847 } -
src/bp-groups/classes/class-bp-groups-group.php
diff --git src/bp-groups/classes/class-bp-groups-group.php src/bp-groups/classes/class-bp-groups-group.php index 8d29b0e..abae433 100644
class BP_Groups_Group { 683 683 * parameter format. 684 684 * 685 685 * @since 1.6.0 686 * @since 2.6.0 Added `$group_type`, `$group_type__in`, and `$group_type__not_in` parameters. 686 687 * 687 688 * @param array $args { 688 689 * Array of parameters. All items are optional. 689 * @type string $type Optional. Shorthand for certain orderby/ 690 * order combinations. 'newest', 'active', 'popular', 691 * 'alphabetical', 'random'. When present, will override 692 * orderby and order params. Default: null. 693 * @type string $orderby Optional. Property to sort by. 694 * 'date_created', 'last_activity', 'total_member_count', 695 * 'name', 'random'. Default: 'date_created'. 696 * @type string $order Optional. Sort order. 'ASC' or 'DESC'. 697 * Default: 'DESC'. 698 * @type int $per_page Optional. Number of items to return per page 699 * of results. Default: null (no limit). 700 * @type int $page Optional. Page offset of results to return. 701 * Default: null (no limit). 702 * @type int $user_id Optional. If provided, results will be limited to groups 703 * of which the specified user is a member. Default: null. 704 * @type string $search_terms Optional. If provided, only groups whose names 705 * or descriptions match the search terms will be 706 * returned. Default: false. 707 * @type array $meta_query Optional. An array of meta_query conditions. 708 * See {@link WP_Meta_Query::queries} for description. 709 * @type array|string $value Optional. Array or comma-separated list of group IDs. 710 * Results will be limited to groups within the 711 * list. Default: false. 712 * @type bool $populate_extras Whether to fetch additional information 713 * (such as member count) about groups. Default: true. 714 * @type array|string $exclude Optional. Array or comma-separated list of group IDs. 715 * Results will exclude the listed groups. Default: false. 716 * @type bool $update_meta_cache Whether to pre-fetch groupmeta for 717 * the returned groups. Default: true. 718 * @type bool $show_hidden Whether to include hidden groups in results. Default: false. 690 * @type string $type Optional. Shorthand for certain orderby/ 691 * order combinations. 'newest', 'active', 'popular', 692 * 'alphabetical', 'random'. When present, will override 693 * orderby and order params. Default: null. 694 * @type string $orderby Optional. Property to sort by. 695 * 'date_created', 'last_activity', 'total_member_count', 696 * 'name', 'random'. Default: 'date_created'. 697 * @type string $order Optional. Sort order. 'ASC' or 'DESC'. 698 * Default: 'DESC'. 699 * @type int $per_page Optional. Number of items to return per page 700 * of results. Default: null (no limit). 701 * @type int $page Optional. Page offset of results to return. 702 * Default: null (no limit). 703 * @type int $user_id Optional. If provided, results will be limited to groups 704 * of which the specified user is a member. Default: null. 705 * @type string $search_terms Optional. If provided, only groups whose names 706 * or descriptions match the search terms will be 707 * returned. Default: false. 708 * @type array|string $group_type Array or comma-separated list of group types to limit results to. 709 * @type array|string $group_type__in Array or comma-separated list of group types to limit results to. 710 * @type array|string $group_type__not_in Array or comma-separated list of group types that will be 711 * excluded from results. 712 * @type array $meta_query Optional. An array of meta_query conditions. 713 * See {@link WP_Meta_Query::queries} for description. 714 * @type array|string $value Optional. Array or comma-separated list of group IDs. 715 * Results will be limited to groups within the 716 * list. Default: false. 717 * @type bool $populate_extras Whether to fetch additional information 718 * (such as member count) about groups. Default: true. 719 * @type array|string $exclude Optional. Array or comma-separated list of group IDs. 720 * Results will exclude the listed groups. Default: false. 721 * @type bool $update_meta_cache Whether to pre-fetch groupmeta for 722 * the returned groups. Default: true. 723 * @type bool $show_hidden Whether to include hidden groups in results. Default: false. 719 724 * } 720 725 * @return array { 721 726 * @type array $groups Array of group objects returned by the … … class BP_Groups_Group { 748 753 } 749 754 750 755 $defaults = array( 751 'type' => null, 752 'orderby' => 'date_created', 753 'order' => 'DESC', 754 'per_page' => null, 755 'page' => null, 756 'user_id' => 0, 757 'search_terms' => false, 758 'meta_query' => false, 759 'include' => false, 760 'populate_extras' => true, 761 'update_meta_cache' => true, 762 'exclude' => false, 763 'show_hidden' => false, 756 'type' => null, 757 'orderby' => 'date_created', 758 'order' => 'DESC', 759 'per_page' => null, 760 'page' => null, 761 'user_id' => 0, 762 'search_terms' => false, 763 'group_type' => '', 764 'group_type__in' => '', 765 'group_type__not_in' => '', 766 'meta_query' => false, 767 'include' => false, 768 'populate_extras' => true, 769 'update_meta_cache' => true, 770 'exclude' => false, 771 'show_hidden' => false, 764 772 ); 765 773 766 774 $r = wp_parse_args( $args, $defaults ); … … class BP_Groups_Group { 804 812 $sql['meta'] = $meta_query_sql['where']; 805 813 } 806 814 815 // Only use 'group_type__in', if 'group_type' is not set. 816 if ( empty( $r['group_type'] ) && ! empty( $r['group_type__in']) ) { 817 $r['group_type'] = $r['group_type__in']; 818 } 819 820 // Group types to exclude. This has priority over inclusions. 821 if ( ! empty( $r['group_type__not_in'] ) ) { 822 $group_type_clause = self::get_sql_clause_for_group_types($r['group_type__not_in'], 'NOT IN' ); 823 824 // Group types to include. 825 } elseif ( ! empty( $r['group_type'] ) ) { 826 $group_type_clause = self::get_sql_clause_for_group_types($r['group_type'], 'IN' ); 827 } 828 829 if ( ! empty( $group_type_clause ) ) { 830 $sql['group_type'] = $group_type_clause; 831 } 832 807 833 if ( ! empty( $r['user_id'] ) ) { 808 834 $sql['user'] = $wpdb->prepare( " AND m.user_id = %d AND m.is_confirmed = 1 AND m.is_banned = 0", $r['user_id'] ); 809 835 } … … class BP_Groups_Group { 915 941 $total_sql['where'][] = $meta_query_clause; 916 942 } 917 943 944 // Trim leading 'AND' to match $total_sql query style. 945 if ( ! empty( $group_type_clause ) ) { 946 $total_sql['where'][] = preg_replace( '/^\s*AND\s*/', '', $group_type_clause ); 947 } 948 918 949 // Already escaped in the paginated results block. 919 950 if ( ! empty( $include ) ) { 920 951 $total_sql['where'][] = "g.id IN ({$include})"; … … class BP_Groups_Group { 1624 1655 1625 1656 return $ids; 1626 1657 } 1658 1659 /** 1660 * Get SQL clause for group type(s). 1661 * 1662 * @since 2.6.0 1663 * 1664 * @param string|array $group_types Group type(s). 1665 * @param string $operator 'IN' or 'NOT IN'. 1666 * @return string $clause SQL clause. 1667 */ 1668 protected static function get_sql_clause_for_group_types( $group_types, $operator ) { 1669 global $wpdb; 1670 1671 // Sanitize operator. 1672 if ( 'NOT IN' !== $operator ) { 1673 $operator = 'IN'; 1674 } 1675 1676 // Parse and sanitize types. 1677 if ( ! is_array( $group_types ) ) { 1678 $group_types = preg_split( '/[,\s+]/', $group_types ); 1679 } 1680 1681 $types = array(); 1682 foreach ( $group_types as $gt ) { 1683 if ( bp_groups_get_group_type_object( $gt ) ) { 1684 $types[] = $gt; 1685 } 1686 } 1687 1688 $tax_query = new WP_Tax_Query( array( 1689 array( 1690 'taxonomy' => 'bp_group_type', 1691 'field' => 'name', 1692 'operator' => $operator, 1693 'terms' => $types, 1694 ), 1695 ) ); 1696 1697 $switched = false; 1698 if ( ! bp_is_root_blog() ) { 1699 switch_to_blog( bp_get_root_blog_id() ); 1700 $switched = true; 1701 } 1702 1703 $sql_clauses = $tax_query->get_sql( 'g', 'id' ); 1704 1705 if ( $switched ) { 1706 restore_current_blog(); 1707 } 1708 1709 $clause = ''; 1710 1711 // The no_results clauses are the same between IN and NOT IN. 1712 if ( false !== strpos( $sql_clauses['where'], '0 = 1' ) ) { 1713 $clause = $sql_clauses['where']; 1714 1715 // The tax_query clause generated for NOT IN can be used almost as-is. 1716 } elseif ( 'NOT IN' === $operator ) { 1717 $clause = $sql_clauses['where']; 1718 1719 // IN clauses must be converted to a subquery. 1720 } elseif ( preg_match( '/' . $wpdb->term_relationships . '\.term_taxonomy_id IN \([0-9, ]+\)/', $sql_clauses['where'], $matches ) ) { 1721 $clause = " AND g.id IN ( SELECT object_id FROM $wpdb->term_relationships WHERE {$matches[0]} )"; 1722 } 1723 1724 return $clause; 1725 } 1627 1726 } -
tests/phpunit/testcases/groups/class-bp-groups-group.php
diff --git tests/phpunit/testcases/groups/class-bp-groups-group.php tests/phpunit/testcases/groups/class-bp-groups-group.php index d2bd1a6..0a160bf 100644
class BP_Tests_BP_Groups_Group_TestCases extends BP_UnitTestCase { 1029 1029 1030 1030 $this->set_current_user( $old_user ); 1031 1031 } 1032 1033 /** 1034 * @group group_types 1035 */ 1036 public function test_group_type_single_value() { 1037 $g1 = $this->factory->group->create(); 1038 $g2 = $this->factory->group->create(); 1039 bp_groups_register_group_type( 'foo' ); 1040 bp_groups_register_group_type( 'bar' ); 1041 bp_groups_set_group_type( $g1, 'foo' ); 1042 bp_groups_set_group_type( $g2, 'bar' ); 1043 1044 $groups = BP_Groups_Group::get( array( 1045 'group_type' => 'foo', 1046 ) ); 1047 1048 $found = wp_list_pluck( $groups['groups'], 'id' ); 1049 $this->assertEquals( array( $g1 ), $found ); 1050 } 1051 1052 /** 1053 * @group group_types 1054 */ 1055 public function test_group_type_array_with_single_value() { 1056 $g1 = $this->factory->group->create(); 1057 $g2 = $this->factory->group->create(); 1058 bp_groups_register_group_type( 'foo' ); 1059 bp_groups_register_group_type( 'bar' ); 1060 bp_groups_set_group_type( $g1, 'foo' ); 1061 bp_groups_set_group_type( $g2, 'bar' ); 1062 1063 $groups = BP_Groups_Group::get( array( 1064 'group_type' => array( 'foo' ), 1065 ) ); 1066 1067 $found = wp_list_pluck( $groups['groups'], 'id' ); 1068 $this->assertEquals( array( $g1 ), $found ); 1069 } 1070 1071 /** 1072 * @group group_types 1073 */ 1074 public function test_group_type_with_comma_separated_list() { 1075 $g1 = $this->factory->group->create(); 1076 $g2 = $this->factory->group->create(); 1077 bp_groups_register_group_type( 'foo' ); 1078 bp_groups_register_group_type( 'bar' ); 1079 bp_groups_set_group_type( $g1, 'foo' ); 1080 bp_groups_set_group_type( $g2, 'bar' ); 1081 1082 $groups = BP_Groups_Group::get( array( 1083 'group_type' => 'foo, bar', 1084 ) ); 1085 1086 $found = wp_list_pluck( $groups['groups'], 'id' ); 1087 $this->assertEqualSets( array( $g1, $g2 ), $found ); 1088 } 1089 1090 /** 1091 * @group group_types 1092 */ 1093 public function test_group_type_array_with_multiple_values() { 1094 $g1 = $this->factory->group->create(); 1095 $g2 = $this->factory->group->create(); 1096 bp_groups_register_group_type( 'foo' ); 1097 bp_groups_register_group_type( 'bar' ); 1098 bp_groups_set_group_type( $g1, 'foo' ); 1099 bp_groups_set_group_type( $g2, 'bar' ); 1100 1101 $groups = BP_Groups_Group::get( array( 1102 'group_type' => array( 'foo', 'bar' ), 1103 ) ); 1104 1105 $found = wp_list_pluck( $groups['groups'], 'id' ); 1106 $this->assertEqualSets( array( $g1, $g2 ), $found ); 1107 } 1108 1109 /** 1110 * @group group_types 1111 */ 1112 public function test_group_type_should_discart_non_existing_types_in_comma_separated_value() { 1113 $g1 = $this->factory->group->create(); 1114 $g2 = $this->factory->group->create(); 1115 bp_groups_register_group_type( 'foo' ); 1116 bp_groups_register_group_type( 'bar' ); 1117 bp_groups_set_group_type( $g1, 'foo' ); 1118 bp_groups_set_group_type( $g2, 'bar' ); 1119 1120 $groups = BP_Groups_Group::get( array( 1121 'group_type' => 'foo, baz', 1122 ) ); 1123 1124 $found = wp_list_pluck( $groups['groups'], 'id' ); 1125 $this->assertEquals( array( $g1 ), $found ); 1126 } 1127 1128 /** 1129 * @group group_types 1130 */ 1131 public function test_group_type_should_return_empty_when_no_groups_match_specified_types() { 1132 $g1 = $this->factory->group->create(); 1133 $g2 = $this->factory->group->create(); 1134 1135 $groups = BP_Groups_Group::get( array( 1136 'group_type' => 'foo, baz', 1137 ) ); 1138 1139 $this->assertEmpty( $groups['groups'] ); 1140 } 1141 1142 /** 1143 * @group group_types 1144 */ 1145 public function test_group_type__in_single_value() { 1146 $g1 = $this->factory->group->create(); 1147 $g2 = $this->factory->group->create(); 1148 bp_groups_register_group_type( 'foo' ); 1149 bp_groups_register_group_type( 'bar' ); 1150 bp_groups_set_group_type( $g1, 'foo' ); 1151 bp_groups_set_group_type( $g2, 'bar' ); 1152 1153 $groups = BP_Groups_Group::get( array( 1154 'group_type__in' => 'bar', 1155 ) ); 1156 1157 $found = wp_list_pluck( $groups['groups'], 'id' ); 1158 $this->assertEquals( array( $g2 ), $found ); 1159 } 1160 1161 /** 1162 * @group group_types 1163 */ 1164 public function test_group_type__in_comma_separated_values() { 1165 $g1 = $this->factory->group->create(); 1166 $g2 = $this->factory->group->create(); 1167 bp_groups_register_group_type( 'foo' ); 1168 bp_groups_register_group_type( 'bar' ); 1169 bp_groups_set_group_type( $g1, 'foo' ); 1170 bp_groups_set_group_type( $g2, 'bar' ); 1171 1172 $groups = BP_Groups_Group::get( array( 1173 'group_type__in' => 'foo, bar', 1174 ) ); 1175 1176 $found = wp_list_pluck( $groups['groups'], 'id' ); 1177 $this->assertEqualSets( array( $g1, $g2 ), $found ); 1178 } 1179 1180 /** 1181 * @group group_types 1182 */ 1183 public function test_group_type__in_array_multiple_values() { 1184 $g1 = $this->factory->group->create(); 1185 $g2 = $this->factory->group->create(); 1186 bp_groups_register_group_type( 'foo' ); 1187 bp_groups_register_group_type( 'bar' ); 1188 bp_groups_set_group_type( $g1, 'foo' ); 1189 bp_groups_set_group_type( $g2, 'bar' ); 1190 1191 $groups = BP_Groups_Group::get( array( 1192 'group_type__in' => array( 'foo', 'bar' ), 1193 ) ); 1194 1195 $found = wp_list_pluck( $groups['groups'], 'id' ); 1196 $this->assertEqualSets( array( $g1, $g2 ), $found ); 1197 } 1198 1199 /** 1200 * @group group_types 1201 */ 1202 public function test_group_type__in_array_with_single_value() { 1203 $g1 = $this->factory->group->create(); 1204 $g2 = $this->factory->group->create(); 1205 bp_groups_register_group_type( 'foo' ); 1206 bp_groups_register_group_type( 'bar' ); 1207 bp_groups_set_group_type( $g1, 'foo' ); 1208 bp_groups_set_group_type( $g2, 'bar' ); 1209 1210 $groups = BP_Groups_Group::get( array( 1211 'group_type__in' => array( 'foo' ), 1212 ) ); 1213 1214 $found = wp_list_pluck( $groups['groups'], 'id' ); 1215 $this->assertEquals( array( $g1 ), $found ); 1216 } 1217 1218 /** 1219 * @group group_types 1220 */ 1221 public function test_group_type__in_should_discart_non_existing_types_in_comma_separated_value() { 1222 $g1 = $this->factory->group->create(); 1223 $g2 = $this->factory->group->create(); 1224 bp_groups_register_group_type( 'foo' ); 1225 bp_groups_register_group_type( 'bar' ); 1226 bp_groups_set_group_type( $g1, 'foo' ); 1227 bp_groups_set_group_type( $g2, 'bar' ); 1228 1229 $groups = BP_Groups_Group::get( array( 1230 'group_type__in' => 'foo, baz', 1231 ) ); 1232 1233 $found = wp_list_pluck( $groups['groups'], 'id' ); 1234 $this->assertEquals( array( $g1 ), $found ); 1235 } 1236 1237 /** 1238 * @group group_types 1239 */ 1240 public function test_group_type__in_should_return_empty_when_no_groups_match_specified_types() { 1241 $g1 = $this->factory->group->create(); 1242 $g2 = $this->factory->group->create(); 1243 1244 $groups = BP_Groups_Group::get( array( 1245 'group_type__in' => 'foo, baz', 1246 ) ); 1247 1248 $this->assertEmpty( $groups['groups'] ); 1249 } 1250 1251 /** 1252 * @group group_types 1253 */ 1254 public function test_group_type_should_take_precedence_over_group_type__in() { 1255 $g1 = $this->factory->group->create(); 1256 $g2 = $this->factory->group->create(); 1257 bp_groups_register_group_type( 'foo' ); 1258 bp_groups_register_group_type( 'bar' ); 1259 bp_groups_set_group_type( $g1, 'foo' ); 1260 bp_groups_set_group_type( $g2, 'bar' ); 1261 1262 $groups = BP_Groups_Group::get( array( 1263 'group_type__in' => 'foo', 1264 'group_type' => 'bar', 1265 ) ); 1266 1267 $found = wp_list_pluck( $groups['groups'], 'id' ); 1268 $this->assertEquals( array( $g2 ), $found ); 1269 } 1270 1271 /** 1272 * @group group_types 1273 */ 1274 public function test_group_type__not_in_should_return_groups_with_types_and_without_types() { 1275 $g1 = $this->factory->group->create(); 1276 $g2 = $this->factory->group->create(); 1277 $g3 = $this->factory->group->create(); 1278 bp_groups_register_group_type( 'foo' ); 1279 bp_groups_register_group_type( 'bar' ); 1280 bp_groups_set_group_type( $g1, 'foo' ); 1281 bp_groups_set_group_type( $g2, 'bar' ); 1282 1283 $groups = BP_Groups_Group::get( array( 1284 'group_type__not_in' => 'foo', 1285 ) ); 1286 1287 $found = wp_list_pluck( $groups['groups'], 'id' ); 1288 $this->assertEquals( array( $g2, $g3 ), $found ); 1289 } 1290 1291 /** 1292 * @group group_types 1293 */ 1294 public function test_group_type__not_in_comma_separated_values() { 1295 $g1 = $this->factory->group->create(); 1296 $g2 = $this->factory->group->create(); 1297 $g3 = $this->factory->group->create(); 1298 bp_groups_register_group_type( 'foo' ); 1299 bp_groups_register_group_type( 'bar' ); 1300 bp_groups_set_group_type( $g1, 'foo' ); 1301 bp_groups_set_group_type( $g2, 'bar' ); 1302 bp_groups_set_group_type( $g3, 'baz' ); 1303 1304 $groups = BP_Groups_Group::get( array( 1305 'group_type__not_in' => 'foo, bar', 1306 ) ); 1307 1308 $found = wp_list_pluck( $groups['groups'], 'id' ); 1309 $this->assertEquals( array( $g3 ), $found ); 1310 } 1311 1312 /** 1313 * @group group_types 1314 */ 1315 public function test_group_type__not_array_with_multiple_values() { 1316 $g1 = $this->factory->group->create(); 1317 $g2 = $this->factory->group->create(); 1318 $g3 = $this->factory->group->create(); 1319 bp_groups_register_group_type( 'foo' ); 1320 bp_groups_register_group_type( 'bar' ); 1321 bp_groups_set_group_type( $g1, 'foo' ); 1322 bp_groups_set_group_type( $g2, 'bar' ); 1323 bp_groups_set_group_type( $g3, 'baz' ); 1324 1325 $groups = BP_Groups_Group::get( array( 1326 'group_type__not_in' => array( 'foo', 'bar' ), 1327 ) ); 1328 1329 $found = wp_list_pluck( $groups['groups'], 'id' ); 1330 $this->assertEquals( array( $g3 ), $found ); 1331 } 1332 1333 /** 1334 * @group group_types 1335 */ 1336 public function test_group_type__not_in_should_return_no_results_when_all_groups_mathc_sepecified_type() { 1337 $g1 = $this->factory->group->create(); 1338 $g2 = $this->factory->group->create(); 1339 $g3 = $this->factory->group->create(); 1340 bp_groups_register_group_type( 'foo' ); 1341 bp_groups_set_group_type( $g1, 'foo' ); 1342 bp_groups_set_group_type( $g2, 'foo' ); 1343 bp_groups_set_group_type( $g3, 'foo' ); 1344 1345 $groups = BP_Groups_Group::get( array( 1346 'group_type__not_in' => 'foo', 1347 ) ); 1348 1349 $this->assertEmpty( $groups['groups'] ); 1350 } 1351 1352 /** 1353 * @group group_types 1354 */ 1355 public function test_group_type__not_in_takes_precedence_over_group_type() { 1356 $g1 = $this->factory->group->create(); 1357 $g2 = $this->factory->group->create(); 1358 $g3 = $this->factory->group->create(); 1359 bp_groups_register_group_type( 'foo' ); 1360 bp_groups_set_group_type( $g1, 'foo' ); 1361 bp_groups_set_group_type( $g2, 'foo' ); 1362 bp_groups_set_group_type( $g3, 'foo' ); 1363 1364 $groups = BP_Groups_Group::get( array( 1365 'group_type' => 'foo', 1366 'group_type__not_in' => 'foo', 1367 ) ); 1368 1369 $this->assertEmpty( $groups['groups'] ); 1370 } 1032 1371 } 1033 1372 1034 1373 /** -
new file tests/phpunit/testcases/groups/types.php
diff --git tests/phpunit/testcases/groups/types.php tests/phpunit/testcases/groups/types.php new file mode 100644 index 0000000..12dcfb6
- + 1 <?php 2 3 /** 4 * @group groups 5 * @group group_types 6 */ 7 class BP_Tests_Groups_Types extends BP_UnitTestCase { 8 protected static $u1 = null; 9 10 public function setUp() { 11 parent::setUp(); 12 13 buddypress()->groups->types = array(); 14 15 self::$u1 = $this->factory->user->create(); 16 } 17 18 public static function setUpBeforeClass() { 19 $f = new BP_UnitTest_Factory(); 20 21 self::$u1 = $f->user->create(); 22 } 23 24 public static function tearDownAfterClass() { 25 if ( is_multisite() ) { 26 wpmu_delete_user( self::$u1 ); 27 } else { 28 wp_delete_user( self::$u1 ); 29 } 30 } 31 32 public function test_groups_register_type_should_fail_for_existing_group_type() { 33 bp_groups_register_group_type( 'foo' ); 34 $this->assertWPError( bp_groups_register_group_type( 'foo' ) ); 35 } 36 37 /** 38 * @dataProvider illegal_names 39 */ 40 public function test_illegal_names( $name ) { 41 $this->assertWPError( bp_groups_register_group_type( $name ) ); 42 } 43 44 public function illegal_names() { 45 return array( 46 array( 'any' ), 47 array( 'null' ), 48 array( '_none' ), 49 ); 50 } 51 52 public function test_groups_register_type_should_sanitize_group_type_key() { 53 $key = 'F//oo% -Bar'; 54 $sanitized_key = 'foo-bar'; 55 56 $object = bp_groups_register_group_type( $key ); 57 $this->assertSame( $sanitized_key, $object->name ); 58 } 59 60 public function test_groups_register_type_should_store_group_type_string_as_name_property() { 61 $object = bp_groups_register_group_type( 'foo' ); 62 $this->assertSame( 'foo', $object->name ); 63 } 64 65 public function test_groups_register_type_should_fill_missing_labels_with_ucfirst_group_type() { 66 $object = bp_groups_register_group_type( 'foo' ); 67 foreach ( $object->labels as $label ) { 68 $this->assertSame( 'Foo', $label ); 69 } 70 } 71 72 public function test_groups_register_type_should_respect_custom_name_labels() { 73 $object = bp_groups_register_group_type( 'foo', array( 74 'labels' => array( 75 'name' => 'Bar', 76 ), 77 ) ); 78 79 $this->assertSame( 'Bar', $object->labels['name'] ); 80 $this->assertSame( 'Bar', $object->labels['singular_name'] ); 81 } 82 83 public function test_groups_register_type_should_respect_custom_singular_name_labels() { 84 $object = bp_groups_register_group_type( 'foo', array( 85 'labels' => array( 86 'singular_name' => 'Bar', 87 ), 88 ) ); 89 90 $this->assertSame( 'Foo', $object->labels['name'] ); 91 $this->assertSame( 'Bar', $object->labels['singular_name'] ); 92 } 93 94 public function test_groups_register_type_has_directory_should_default_to_true() { 95 $object = bp_groups_register_group_type( 'foo' ); 96 97 $this->assertTrue( $object->has_directory ); 98 $this->assertSame( 'foo', $object->directory_slug ); 99 } 100 101 public function test_groups_register_type_has_directory_true() { 102 $object = bp_groups_register_group_type( 'foo', array( 103 'has_directory' => true, 104 ) ); 105 106 $this->assertTrue( $object->has_directory ); 107 $this->assertSame( 'foo', $object->directory_slug ); 108 } 109 110 public function test_groups_register_type_has_directory_false() { 111 $object = bp_groups_register_group_type( 'foo', array( 112 'has_directory' => false, 113 ) ); 114 115 $this->assertFalse( $object->has_directory ); 116 $this->assertSame( '', $object->directory_slug ); 117 } 118 119 public function test_groups_register_type_should_store_has_directory_string() { 120 $object = bp_groups_register_group_type( 'foo', array( 121 'has_directory' => 'foos', 122 ) ); 123 124 $this->assertTrue( $object->has_directory ); 125 $this->assertSame( 'foos', $object->directory_slug ); 126 } 127 128 public function test_groups_get_type_object_should_return_null_for_non_existing_group_type() { 129 $this->assertSame( null, bp_groups_get_group_type_object( 'foo' ) ); 130 } 131 132 public function test_groups_get_type_object_should_return_type_object() { 133 bp_groups_register_group_type( 'foo' ); 134 $this->assertInternalType( 'object', bp_groups_register_group_type( 'foo' ) ); 135 } 136 137 public function test_groups_set_type_should_return_false_for_invalid_group_type() { 138 $this->assertFalse( bp_groups_set_group_type( 1, 'foo' ) ); 139 } 140 141 public function test_groups_set_type_success() { 142 $g = $this->factory->group->create( array( 'creator_id' => self::$u1 ) ); 143 bp_groups_register_group_type( 'foo' ); 144 145 $this->assertNotEmpty( bp_groups_set_group_type( $g, 'foo' ) ); 146 } 147 148 public function test_groups_set_type_should_remove_type_when_passing_an_empty_value() { 149 $g = $this->factory->group->create( array( 'creator_id' => self::$u1 ) ); 150 bp_groups_register_group_type( 'foo' ); 151 bp_groups_set_group_type( $g, 'foo' ); 152 153 // Make sure group type is set. 154 $this->assertSame( 'foo', bp_groups_get_group_type( $g ) ); 155 156 $this->assertSame( array(), bp_groups_set_group_type( $g, '' ) ); 157 $this->assertFalse( bp_groups_get_group_type( $g ) ); 158 } 159 160 public function test_groups_get_type_with_default_value_for_single() { 161 $g = $this->factory->group->create( array( 'creator_id' => self::$u1 ) ); 162 bp_groups_register_group_type( 'foo' ); 163 bp_groups_register_group_type( 'bar' ); 164 bp_groups_set_group_type( $g, 'foo' ); 165 bp_groups_set_group_type( $g, 'bar', true ); 166 167 $this->assertSame( 'foo', bp_groups_get_group_type( $g ) ); 168 } 169 170 public function test_groups_get_type_with_single_true() { 171 $g = $this->factory->group->create( array( 'creator_id' => self::$u1 ) ); 172 bp_groups_register_group_type( 'foo' ); 173 bp_groups_register_group_type( 'bar' ); 174 bp_groups_set_group_type( $g, 'foo' ); 175 bp_groups_set_group_type( $g, 'bar', true ); 176 177 $this->assertSame( 'foo', bp_groups_get_group_type( $g, true ) ); 178 } 179 180 public function test_groups_get_type_with_single_false() { 181 $g = $this->factory->group->create( array( 'creator_id' => self::$u1 ) ); 182 bp_groups_register_group_type( 'foo' ); 183 bp_groups_register_group_type( 'bar' ); 184 bp_groups_set_group_type( $g, 'foo' ); 185 bp_groups_set_group_type( $g, 'bar', true ); 186 187 $this->assertEqualSets( array( 'foo', 'bar' ), bp_groups_get_group_type( $g, false ) ); 188 } 189 190 public function test_groups_get_type_should_return_false_when_no_value_is_found() { 191 $g = $this->factory->group->create( array( 'creator_id' => self::$u1 ) ); 192 bp_groups_register_group_type( 'foo' ); 193 194 $this->assertFalse( bp_groups_get_group_type( $g ) ); 195 } 196 197 public function test_groups_remove_type_should_return_false_when_group_type_is_empty() { 198 $this->assertFalse( bp_groups_remove_group_type( 9, '' ) ); 199 } 200 201 public function test_groups_remove_type_should_return_false_when_group_type_is_invalid() { 202 $this->assertFalse( bp_groups_remove_group_type( 9, 'foo' ) ); 203 } 204 205 public function test_groups_remove_type_should_return_false_when_group_is_not_of_provided_type() { 206 $g = $this->factory->group->create( array( 'creator_id' => self::$u1 ) ); 207 bp_groups_register_group_type( 'foo' ); 208 bp_groups_register_group_type( 'bar' ); 209 bp_groups_set_group_type( $g, 'foo' ); 210 211 $this->assertFalse( bp_groups_remove_group_type( $g, 'bar' ) ); 212 $this->assertEquals( array( 'foo' ), bp_groups_get_group_type( $g, false ) ); 213 } 214 215 public function tests_groups_remove_type_should_return_true_on_successful_deletion() { 216 $g = $this->factory->group->create( array( 'creator_id' => self::$u1 ) ); 217 bp_groups_register_group_type( 'foo' ); 218 bp_groups_register_group_type( 'bar' ); 219 bp_groups_set_group_type( $g, 'foo' ); 220 bp_groups_set_group_type( $g, 'bar', true ); 221 222 $this->assertTrue( bp_groups_remove_group_type( $g, 'foo' ) ); 223 $this->assertEquals( array( 'bar' ), bp_groups_get_group_type( $g, false ) ); 224 } 225 226 public function test_groups_has_type_should_return_false_when_group_type_is_empty() { 227 $this->assertFalse( bp_groups_has_group_type( 9, '' ) ); 228 } 229 230 public function test_groups_has_type_should_return_false_when_group_type_is_invalid() { 231 $this->assertFalse( bp_groups_has_group_type( 9, 'foo' ) ); 232 } 233 234 public function test_groups_has_type_should_return_false_when_group_id_is_empty() { 235 bp_groups_register_group_type( 'foo' ); 236 237 $this->assertFalse( bp_groups_has_group_type( '', 'foo' ) ); 238 } 239 240 public function test_groups_has_type_should_return_false_when_group_is_not_of_provided_type() { 241 $g = $this->factory->group->create( array( 'creator_id' => self::$u1 ) ); 242 bp_groups_register_group_type( 'foo' ); 243 bp_groups_register_group_type( 'bar' ); 244 bp_groups_set_group_type( $g, 'foo' ); 245 246 $this->assertFalse( bp_groups_has_group_type( $g, 'bar' ) ); 247 } 248 249 public function test_groups_has_type_should_return_true_on_success() { 250 $g = $this->factory->group->create( array( 'creator_id' => self::$u1 ) ); 251 bp_groups_register_group_type( 'foo' ); 252 bp_groups_register_group_type( 'bar' ); 253 bp_groups_set_group_type( $g, 'foo' ); 254 bp_groups_set_group_type( $g, 'bar', true ); 255 256 $this->assertTrue( bp_groups_has_group_type( $g, 'foo' ) ); 257 $this->assertEqualSets( array( 'bar', 'foo' ), bp_groups_get_group_type( $g, false ) ); 258 } 259 260 /** 261 * @group cache 262 */ 263 public function test_groups_get_type_should_hit_cache() { 264 $g = $this->factory->group->create( array( 'creator_id' => self::$u1 ) ); 265 bp_groups_register_group_type( 'foo' ); 266 bp_groups_set_group_type( $g, 'foo' ); 267 268 global $wpdb; 269 270 // Initial query. Should hit DB. 271 bp_groups_get_group_type( $g ); 272 $num_queries = $wpdb->num_queries; 273 274 // Next query should hit cache 275 bp_groups_get_group_type( $g ); 276 $this->assertSame( $num_queries, $wpdb->num_queries ); 277 } 278 }