Skip to:
Content

BuddyPress.org

Ticket #4933: 4933.patch

File 4933.patch, 3.2 KB (added by boonebgorges, 13 years ago)
  • bp-core/bp-core-classes.php

    diff --git bp-core/bp-core-classes.php bp-core/bp-core-classes.php
    index d2c18ba..b885a6a 100644
    class BP_User_Query { 
    318318                // To avoid global joins, do a separate query
    319319                // @todo remove need for bp_is_active() check
    320320                if ( false !== $search_terms && bp_is_active( 'xprofile' ) ) {
    321                         $found_user_ids = $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM {$bp->profile->table_name_data} WHERE value LIKE %s", '%%' . like_escape( $search_terms ) . '%%' ) );
     321                        $search_terms_clean = mysql_real_escape_string( mysql_real_escape_string( $search_terms ) );
     322                        $search_terms_clean = like_escape( $search_terms_clean );
     323                        $found_user_ids_query = "SELECT user_id FROM {$bp->profile->table_name_data} WHERE value LIKE '%" . $search_terms_clean . "%'";
     324                        $found_user_ids = $wpdb->get_col( $found_user_ids_query );
    322325
    323326                        if ( ! empty( $found_user_ids ) ) {
    324327                                $sql['where'][] = "u.{$this->uid_name} IN (" . implode( ',', wp_parse_id_list( $found_user_ids ) ) . ")";
  • new file tests/testcases/core/classes.php

    diff --git tests/testcases/core/classes.php tests/testcases/core/classes.php
    new file mode 100644
    index 0000000..6c772c9
    - +  
     1<?php
     2/**
     3 * @group core
     4 */
     5class BP_Tests_Core_Classes extends BP_UnitTestCase {
     6        protected $old_current_user = 0;
     7
     8        public function setUp() {
     9                parent::setUp();
     10        }
     11
     12        public function tearDown() {
     13                parent::tearDown();
     14        }
     15
     16        /**
     17         * Mark a user as active
     18         *
     19         * Users only show up in directories if marked as active in the database
     20         */
     21        public function create_active_user() {
     22                $user_id = $this->factory->user->create( array( 'role' => 'subscriber' ) );
     23                bp_update_user_meta( $user_id, 'last_activity', bp_core_current_time() );
     24                return $user_id;
     25        }
     26
     27        public function test_bp_user_query_search_with_apostrophe() {
     28                // Apostrophe. Search_terms must escaped to mimic POST payload
     29                $user_id = $this->create_active_user();
     30                xprofile_set_field_data( 1, $user_id, "Foo'Bar" );
     31                $q = new BP_User_Query( array( 'search_terms' => "oo\'Ba", ) );
     32
     33                $found_user_id = null;
     34                if ( ! empty( $q->results ) ) {
     35                        $found_user = array_pop( $q->results );
     36                        $found_user_id = $found_user->ID;
     37                }
     38
     39                $this->assertEquals( $user_id, $found_user_id );
     40        }
     41
     42        public function test_bp_user_query_search_with_percent_sign() {
     43
     44                // LIKE special character: %
     45                $user_id = $this->create_active_user();
     46                xprofile_set_field_data( 1, $user_id, "Foo%Bar" );
     47                $q = new BP_User_Query( array( 'search_terms' => "oo%Bar", ) );
     48
     49                $found_user_id = null;
     50                if ( ! empty( $q->results ) ) {
     51                        $found_user = array_pop( $q->results );
     52                        $found_user_id = $found_user->ID;
     53                }
     54
     55                $this->assertEquals( $user_id, $found_user_id );
     56
     57        }
     58
     59        public function test_bp_user_query_search_with_underscore() {
     60
     61                // LIKE special character: _
     62                $user_id = $this->create_active_user();
     63                xprofile_set_field_data( 1, $user_id, "Foo_Bar" );
     64                $q = new BP_User_Query( array( 'search_terms' => "oo_Bar", ) );
     65
     66                $found_user_id = null;
     67                if ( ! empty( $q->results ) ) {
     68                        $found_user = array_pop( $q->results );
     69                        $found_user_id = $found_user->ID;
     70                }
     71
     72                $this->assertEquals( $user_id, $found_user_id );
     73
     74        }
     75}
     76