Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
08/09/2024 01:13:58 PM (4 months ago)
Author:
espellcaste
Message:

XProfile: Add cache_results flag to the BP_XProfile_Group::get getter.

When performing a request with cache_results, it stops the XProfile group information retrieved from being added to the cache.

Props imath.

See #8552
Closes https://github.com/buddypress/buddypress/pull/357

File:
1 edited

Legend:

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

    r13358 r14000  
    1111     */
    1212    public function test_save_should_not_return_false_when_no_fields_have_been_altered() {
    13         $g = self::factory()->xprofile_group->create();
     13        $g     = self::factory()->xprofile_group->create();
    1414        $group = new BP_XProfile_Group( $g );
    1515
     
    2222     * @ticket BP7916
    2323     */
    24     public function test_delete() {
     24    public function test_delete_xprofile_group() {
    2525        $g = self::factory()->xprofile_group->create();
    2626
     
    4343        $u = self::factory()->user->create();
    4444        $g = self::factory()->xprofile_group->create();
    45         $f = self::factory()->xprofile_field->create( array(
    46             'field_group_id' => $g,
    47         ) );
     45        $f = self::factory()->xprofile_field->create(
     46            array(
     47                'field_group_id' => $g,
     48            )
     49        );
    4850
    4951        $f_obj = new BP_XProfile_Field( $f );
    5052
    5153        $fields = array(
    52             0 => new stdClass,
    53         );
    54 
    55         $fields[0]->id = $f;
    56         $fields[0]->name = $f_obj->name;
     54            0 => new stdClass(),
     55        );
     56
     57        $fields[0]->id          = $f;
     58        $fields[0]->name        = $f_obj->name;
    5759        $fields[0]->description = $f_obj->description;
    58         $fields[0]->type = $f_obj->type;
    59         $fields[0]->group_id = $f_obj->group_id;
     60        $fields[0]->type        = $f_obj->type;
     61        $fields[0]->group_id    = $f_obj->group_id;
    6062        $fields[0]->is_required = $f_obj->is_required;
    61         $fields[0]->data = new stdClass;
     63        $fields[0]->data        = new stdClass();
    6264        $fields[0]->data->value = 'foo';
    63         $fields[0]->data->id = 123;
     65        $fields[0]->data->id    = 123;
    6466
    6567        // custom visibility enabled, but no fallback
     
    6971        $found = BP_XProfile_Group::fetch_visibility_level( $u, $fields );
    7072
    71         $expected = $fields;
     73        $expected                      = $fields;
    7274        $expected[0]->visibility_level = 'adminsonly';
    7375
     
    8183        $found = BP_XProfile_Group::fetch_visibility_level( $u, $fields );
    8284
    83         $expected = $fields;
     85        $expected                      = $fields;
    8486        $expected[0]->visibility_level = 'public';
    8587
     
    9395        $found = BP_XProfile_Group::fetch_visibility_level( $u, $fields );
    9496
    95         $expected = $fields;
     97        $expected                      = $fields;
    9698        $expected[0]->visibility_level = 'adminsonly';
    9799
     
    107109        $g3 = self::factory()->xprofile_group->create();
    108110
    109         $all = BP_XProfile_Group::get();
     111        $all         = BP_XProfile_Group::get();
    110112        $all_results = array_map( 'absint', wp_list_pluck( $all, 'id' ) );
    111113
    112         $e1 = array( $g1, $g2 );
    113         $groups1 = BP_XProfile_Group::get( array(
    114             'exclude_groups' => implode( ',', $e1 ),
    115         ) );
     114        $e1      = array( $g1, $g2 );
     115        $groups1 = BP_XProfile_Group::get(
     116            array(
     117                'exclude_groups' => implode( ',', $e1 ),
     118            )
     119        );
    116120
    117121        $r_groups1 = array_map( 'absint', wp_list_pluck( $groups1, 'id' ) );
    118         $found1 = array_diff( $all_results, $r_groups1 );
     122        $found1    = array_diff( $all_results, $r_groups1 );
    119123
    120124        $this->assertSame( $e1, array_merge( $found1, array() ) );
    121125
    122         $e2 = array( $g2, $g3 );
    123         $groups2 = BP_XProfile_Group::get( array(
    124             'exclude_groups' => $e2,
    125         ) );
     126        $e2      = array( $g2, $g3 );
     127        $groups2 = BP_XProfile_Group::get(
     128            array(
     129                'exclude_groups' => $e2,
     130            )
     131        );
    126132
    127133        $r_groups2 = array_map( 'absint', wp_list_pluck( $groups2, 'id' ) );
    128         $found2 = array_diff( $all_results, $r_groups2 );
     134        $found2    = array_diff( $all_results, $r_groups2 );
    129135
    130136        $this->assertSame( $e2, array_merge( $found2, array() ) );
     
    139145        $g2 = self::factory()->xprofile_group->create();
    140146        $g3 = self::factory()->xprofile_group->create();
    141         $e1 = [ $g1, $g2 ];
     147        $e1 = array( $g1, $g2 );
    142148
    143149        // Comma-separated list of profile group ids.
    144         $groups1 = BP_XProfile_Group::get( [ 'profile_group_id' => join( ',', $e1 ) ] );
     150        $groups1 = BP_XProfile_Group::get( array( 'profile_group_id' => join( ',', $e1 ) ) );
    145151
    146152        $this->assertSame( $e1, array_map( 'absint', wp_list_pluck( $groups1, 'id' ) ) );
    147153
    148154        // Array of profile group ids.
    149         $groups2 = BP_XProfile_Group::get( [ 'profile_group_id' => $e1 ] );
     155        $groups2 = BP_XProfile_Group::get( array( 'profile_group_id' => $e1 ) );
    150156
    151157        $this->assertSame( $e1, array_map( 'absint', wp_list_pluck( $groups2, 'id' ) ) );
     
    164170        $field->set_member_types( array( 'foo' ) );
    165171
    166         $found_groups = BP_XProfile_Group::get( array(
    167             'user_id' => false,
    168             'fetch_fields' => true,
    169         ) );
     172        $found_groups = BP_XProfile_Group::get(
     173            array(
     174                'user_id'      => false,
     175                'fetch_fields' => true,
     176            )
     177        );
    170178
    171179        // The groups aren't indexed, so we have to go looking for it.
     
    191199        $field->set_member_types( array( 'foo' ) );
    192200
    193         $found_groups = BP_XProfile_Group::get( array(
    194             'user_id' => 0,
    195             'member_type' => false,
    196             'fetch_fields' => true,
    197         ) );
     201        $found_groups = BP_XProfile_Group::get(
     202            array(
     203                'user_id'      => 0,
     204                'member_type'  => false,
     205                'fetch_fields' => true,
     206            )
     207        );
    198208
    199209        // The groups aren't indexed, so we have to go looking for it.
     
    212222     */
    213223    public function test_member_type_restrictions_should_be_obeyed_for_nonzero_user_id() {
    214         $g = self::factory()->xprofile_group->create();
     224        $g  = self::factory()->xprofile_group->create();
    215225        $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) );
    216226        $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) );
     
    238248        bp_set_member_type( $u, 'foo' );
    239249
    240         $found_groups = BP_XProfile_Group::get( array(
    241             'user_id' => $u,
    242             'fetch_fields' => true,
    243         ) );
     250        $found_groups = BP_XProfile_Group::get(
     251            array(
     252                'user_id'      => $u,
     253                'fetch_fields' => true,
     254            )
     255        );
    244256
    245257        // The groups aren't indexed, so we have to go looking for it.
     
    261273     */
    262274    public function test_member_type_restrictions_should_be_obeyed_for_nonzero_user_id_with_no_member_types() {
    263         $g = self::factory()->xprofile_group->create();
     275        $g  = self::factory()->xprofile_group->create();
    264276        $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) );
    265277        $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) );
     
    281293        $u = self::factory()->user->create();
    282294
    283         $found_groups = BP_XProfile_Group::get( array(
    284             'user_id' => $u,
    285             'fetch_fields' => true,
    286         ) );
     295        $found_groups = BP_XProfile_Group::get(
     296            array(
     297                'user_id'      => $u,
     298                'fetch_fields' => true,
     299            )
     300        );
    287301
    288302        // The groups aren't indexed, so we have to go looking for it.
     
    303317     */
    304318    public function test_member_types_of_provided_user_id_should_take_precedence_over_provided_member_type() {
    305         $g = self::factory()->xprofile_group->create();
     319        $g  = self::factory()->xprofile_group->create();
    306320        $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) );
    307321        $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) );
     
    317331        bp_set_member_type( $u, 'foo' );
    318332
    319         $found_groups = BP_XProfile_Group::get( array(
    320             'user_id' => $u,
    321             'member_type' => 'bar',
    322             'fetch_fields' => true,
    323         ) );
     333        $found_groups = BP_XProfile_Group::get(
     334            array(
     335                'user_id'      => $u,
     336                'member_type'  => 'bar',
     337                'fetch_fields' => true,
     338            )
     339        );
    324340
    325341        // The groups aren't indexed, so we have to go looking for it.
     
    338354     */
    339355    public function test_member_type_single_value_should_be_respected() {
    340         $g = self::factory()->xprofile_group->create();
     356        $g  = self::factory()->xprofile_group->create();
    341357        $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) );
    342358        $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) );
     
    349365        $field2->set_member_types( array( 'bar' ) );
    350366
    351         $found_groups = BP_XProfile_Group::get( array(
    352             'member_type' => 'bar',
    353             'fetch_fields' => true,
    354         ) );
     367        $found_groups = BP_XProfile_Group::get(
     368            array(
     369                'member_type'  => 'bar',
     370                'fetch_fields' => true,
     371            )
     372        );
    355373
    356374        // The groups aren't indexed, so we have to go looking for it.
     
    370388     */
    371389    public function test_member_type_array_value_should_be_respected() {
    372         $g = self::factory()->xprofile_group->create();
     390        $g  = self::factory()->xprofile_group->create();
    373391        $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) );
    374392        $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) );
     
    381399        $field2->set_member_types( array( 'bar' ) );
    382400
    383         $found_groups = BP_XProfile_Group::get( array(
    384             'member_type' => array( 'bar' ),
    385             'fetch_fields' => true,
    386         ) );
     401        $found_groups = BP_XProfile_Group::get(
     402            array(
     403                'member_type'  => array( 'bar' ),
     404                'fetch_fields' => true,
     405            )
     406        );
    387407
    388408        // The groups aren't indexed, so we have to go looking for it.
     
    402422     */
    403423    public function test_member_type_null_should_be_respected() {
    404         $g = self::factory()->xprofile_group->create();
     424        $g  = self::factory()->xprofile_group->create();
    405425        $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) );
    406426        $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) );
     
    411431        $field1->set_member_types( array( 'foo' ) );
    412432
    413         $found_groups = BP_XProfile_Group::get( array(
    414             'member_type' => array( 'null' ),
    415             'fetch_fields' => true,
    416         ) );
     433        $found_groups = BP_XProfile_Group::get(
     434            array(
     435                'member_type'  => array( 'null' ),
     436                'fetch_fields' => true,
     437            )
     438        );
    417439
    418440        // The groups aren't indexed, so we have to go looking for it.
     
    431453     */
    432454    public function test_save_xprofile_group_name() {
    433         $g1 = self::factory()->xprofile_group->create( array(
    434             'name' => "Test ' Name"
    435         ) );
     455        $g1 = self::factory()->xprofile_group->create(
     456            array(
     457                'name' => "Test ' Name",
     458            )
     459        );
    436460
    437461        $e1 = new BP_XProfile_Group( $g1 );
     
    454478
    455479        // Create a group
    456         $g1 = self::factory()->xprofile_group->create( array(
    457             'name' => $pristine_name
    458         ) );
     480        $g1 = self::factory()->xprofile_group->create(
     481            array(
     482                'name' => $pristine_name,
     483            )
     484        );
    459485
    460486        // Get the field
     
    471497        $g2 = self::factory()->xprofile_group->create();
    472498
    473         $group_ids   = [ 1 ]; // Default group.
     499        $group_ids   = array( 1 ); // Default group.
    474500        $group_ids[] = self::factory()->xprofile_group->create();
    475501        $group_ids[] = self::factory()->xprofile_group->create();
     
    481507        $this->assertEqualSets( $group_ids, $found_1 );
    482508
    483         $found_2 = BP_XProfile_Group::get_group_ids( [ 'profile_group_id' => $g1 ] );
     509        $found_2 = BP_XProfile_Group::get_group_ids( array( 'profile_group_id' => $g1 ) );
    484510        $this->assertCount( 1, $found_2 );
    485         $this->assertSame( [ $g1 ], $found_2 );
    486 
    487         $found_3 = BP_XProfile_Group::get_group_ids( [ 'profile_group_id' => [ $g1 ] ] );
     511        $this->assertSame( array( $g1 ), $found_2 );
     512
     513        $found_3 = BP_XProfile_Group::get_group_ids( array( 'profile_group_id' => array( $g1 ) ) );
    488514        $this->assertCount( 1, $found_3 );
    489         $this->assertSame( [ $g1 ], $found_3 );
    490 
    491         $found_4 = BP_XProfile_Group::get_group_ids( [ 'profile_group_id' => [ $g2 ] ] );
     515        $this->assertSame( array( $g1 ), $found_3 );
     516
     517        $found_4 = BP_XProfile_Group::get_group_ids( array( 'profile_group_id' => array( $g2 ) ) );
    492518        $this->assertCount( 1, $found_4 );
    493         $this->assertSame( [ $g2 ], $found_4 );
    494 
    495         $found_5 = BP_XProfile_Group::get_group_ids( [ 'profile_group_id' => [ $g1, $g2 ] ] );
     519        $this->assertSame( array( $g2 ), $found_4 );
     520
     521        $found_5 = BP_XProfile_Group::get_group_ids( array( 'profile_group_id' => array( $g1, $g2 ) ) );
    496522        $this->assertCount( 2, $found_5 );
    497         $this->assertSame( [ $g1, $g2 ], $found_5 );
    498 
    499         $found_6 = BP_XProfile_Group::get_group_ids( [ 'profile_group_id' => join( ',', [ $g1, $g2 ] ) ] );
     523        $this->assertSame( array( $g1, $g2 ), $found_5 );
     524
     525        $found_6 = BP_XProfile_Group::get_group_ids( array( 'profile_group_id' => join( ',', array( $g1, $g2 ) ) ) );
    500526        $this->assertCount( 2, $found_6 );
    501         $this->assertSame( [ $g1, $g2 ], $found_6 );
    502 
    503         $found_7 = BP_XProfile_Group::get_group_ids( [ 'profile_group_id' => true ] );
     527        $this->assertSame( array( $g1, $g2 ), $found_6 );
     528
     529        $found_7 = BP_XProfile_Group::get_group_ids( array( 'profile_group_id' => true ) );
    504530        $this->assertEqualSets( $group_ids, $found_7 );
    505531    }
     
    521547        $group_ids[] = $g2;
    522548
    523         $params_1 = [ 'exclude_groups' => false ];
     549        $params_1 = array( 'exclude_groups' => false );
    524550
    525551        // Prime cache.
     
    534560
    535561        // Different parameters should trigger a cache miss.
    536         $found_3 = BP_XProfile_Group::get_group_ids( [ 'exclude_groups' => [ 0 ] ] );
     562        $found_3 = BP_XProfile_Group::get_group_ids( array( 'exclude_groups' => array( 0 ) ) );
    537563        $this->assertEqualSets( $group_ids, $found_3 );
    538564        $this->assertNotSame( $num_queries, $wpdb->num_queries );
    539565
    540566        // Again, different parameters should trigger a cache miss.
    541         $found_4 = BP_XProfile_Group::get_group_ids( [ 'profile_group_id' => [ $g1, $g2 ] ] );
    542         $this->assertEqualSets( [ $g1, $g2 ], $found_4 );
     567        $found_4 = BP_XProfile_Group::get_group_ids( array( 'profile_group_id' => array( $g1, $g2 ) ) );
     568        $this->assertEqualSets( array( $g1, $g2 ), $found_4 );
    543569        $this->assertNotSame( $num_queries, $wpdb->num_queries );
    544570    }
     
    551577        $group_ids    = array( 1 ); // Default group.
    552578        $group_ids[1] = self::factory()->xprofile_group->create();
    553         $group_ids[2] = self::factory()->xprofile_group->create();
    554         $group_ids[3] = self::factory()->xprofile_group->create();
     579
     580        self::factory()->xprofile_group->create();
     581        self::factory()->xprofile_group->create();
    555582
    556583        // Prime cache.
     
    574601        $group_ids    = array( 1 ); // Default group.
    575602        $group_ids[1] = self::factory()->xprofile_group->create();
    576         $group_ids[2] = self::factory()->xprofile_group->create();
    577         $group_ids[3] = self::factory()->xprofile_group->create();
     603
     604        self::factory()->xprofile_group->create();
     605        self::factory()->xprofile_group->create();
    578606
    579607        // Prime cache.
     
    586614        $num_queries = $wpdb->num_queries;
    587615
    588         $found = BP_XProfile_Group::get_group_ids();
     616        BP_XProfile_Group::get_group_ids();
     617
    589618        $this->assertNotSame( $num_queries, $wpdb->num_queries );
    590619    }
     
    599628        $group_ids    = array( 1 ); // Default group.
    600629        $group_ids[1] = self::factory()->xprofile_group->create();
    601         $group_ids[2] = self::factory()->xprofile_group->create();
    602         $group_ids[3] = self::factory()->xprofile_group->create();
    603 
    604         $f = self::factory()->xprofile_field->create( array(
    605             'field_group_id' => $group_ids[1],
    606         ) );
     630
     631        self::factory()->xprofile_group->create();
     632        self::factory()->xprofile_group->create();
     633
     634        $f     = self::factory()->xprofile_field->create(
     635            array(
     636                'field_group_id' => $group_ids[1],
     637            )
     638        );
    607639        $field = new BP_XProfile_Field( $f );
    608640
     
    615647        $num_queries = $wpdb->num_queries;
    616648
    617         $found = BP_XProfile_Group::get_group_ids();
     649        BP_XProfile_Group::get_group_ids();
     650
    618651        $this->assertNotSame( $num_queries, $wpdb->num_queries );
    619652    }
     
    626659        $group_ids    = array( 1 ); // Default group.
    627660        $group_ids[1] = self::factory()->xprofile_group->create();
    628         $group_ids[2] = self::factory()->xprofile_group->create();
    629         $group_ids[3] = self::factory()->xprofile_group->create();
    630 
    631         $f = self::factory()->xprofile_field->create( array(
    632             'field_group_id' => $group_ids[1],
    633         ) );
     661
     662        self::factory()->xprofile_group->create();
     663        self::factory()->xprofile_group->create();
     664
     665        $f     = self::factory()->xprofile_field->create(
     666            array(
     667                'field_group_id' => $group_ids[1],
     668            )
     669        );
    634670        $field = new BP_XProfile_Field( $f );
    635671
     
    657693        $group_id = self::factory()->xprofile_group->create();
    658694
    659         $f = self::factory()->xprofile_field->create( array(
    660             'field_group_id' => $group_id,
    661         ) );
    662         $field = new BP_XProfile_Field( $f );
     695        $f = self::factory()->xprofile_field->create(
     696            array(
     697                'field_group_id' => $group_id,
     698            )
     699        );
     700
     701        new BP_XProfile_Field( $f );
    663702
    664703        // Prime cache.
     
    682721        $group_id = self::factory()->xprofile_group->create();
    683722
    684         $f = self::factory()->xprofile_field->create( array(
    685             'field_group_id' => $group_id,
    686         ) );
     723        $f     = self::factory()->xprofile_field->create(
     724            array(
     725                'field_group_id' => $group_id,
     726            )
     727        );
    687728        $field = new BP_XProfile_Field( $f );
    688729
     
    706747        $group_id = self::factory()->xprofile_group->create();
    707748
    708         $f = self::factory()->xprofile_field->create( array(
    709             'field_group_id' => $group_id,
    710         ) );
     749        $f     = self::factory()->xprofile_field->create(
     750            array(
     751                'field_group_id' => $group_id,
     752            )
     753        );
    711754        $field = new BP_XProfile_Field( $f );
    712755
     
    720763        $this->assertNotContains( $f, $found );
    721764    }
     765
     766    /**
     767     * @ticket BP8552
     768     * @group cache
     769     */
     770    public function test_group_query_getter_with_cache_results() {
     771        global $wpdb;
     772
     773        self::factory()->xprofile_group->create_many( 2 );
     774
     775        $wpdb->num_queries = 0;
     776
     777        $first_query = BP_XProfile_Group::get( array( 'cache_results' => true ) );
     778
     779        $queries_before = get_num_queries();
     780
     781        $second_query = BP_XProfile_Group::get( array( 'cache_results' => false ) );
     782
     783        $queries_after = get_num_queries();
     784
     785        $this->assertNotSame( $queries_before, $queries_after, 'Assert that queries are run' );
     786        $this->assertSame( 3, $queries_after, 'Assert that the uncached query was run' );
     787        $this->assertEquals( $first_query, $second_query, 'Results of the query are expected to match.' );
     788    }
    722789}
Note: See TracChangeset for help on using the changeset viewer.