[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Suggestions API tests for authenticated (logged in) users. 4 * 5 * @group api 6 * @group suggestions 7 */ 8 class BP_Tests_Suggestions_Authenticated extends BP_UnitTestCase { 9 protected static $current_user = null; 10 protected static $group_ids = array(); 11 protected static $group_slugs = array(); 12 protected static $old_user_id = 0; 13 protected static $user_ids = array(); 14 15 public static function wpSetUpBeforeClass( $factory ) { 16 self::$old_user_id = get_current_user_id(); 17 self::$current_user = $factory->user->create( array( 18 'display_name' => 'Katie Parker', 19 'user_login' => 'katie', 20 'user_email' => 'test-katie@example.com', 21 ) ); 22 23 $users = array( 24 // user_login, display_name 25 array( 'aardvark', 'Bob Smith' ), 26 array( 'alpaca red', 'William Quinn' ), 27 array( 'cat', 'Lauren Curtis' ), 28 array( 'caterpillar', 'Eldon Burrows' ), 29 array( 'dog green', 'Reece Thornton' ), 30 array( 'pig', 'Joshua Barton' ), 31 array( 'rabbit blue', 'Amber Hooper' ), 32 array( 'smith', 'Robert Bar' ), 33 array( 'snake', 'Eleanor Moore' ), 34 array( 'xylo', 'Silver McFadden' ), 35 array( 'zoom', 'Lisa Smithy' ), 36 ); 37 38 // Create some dummy users. 39 foreach ( $users as $user_index => $user ) { 40 $new_user = $factory->user->create( array( 41 'display_name' => $user[1], 42 'user_login' => $user[0], 43 'user_email' => "test-$user_index@example.com", 44 ) ); 45 46 self::$user_ids[ $user[0] ] = $new_user; 47 } 48 49 // Create some dummy friendships (but not the corresponding activity items). 50 remove_action( 'friends_friendship_accepted', 'bp_friends_friendship_accepted_activity', 10 ); 51 friends_add_friend( self::$current_user, self::$user_ids['aardvark'], true ); 52 friends_add_friend( self::$current_user, self::$user_ids['cat'], true ); 53 friends_add_friend( self::$current_user, self::$user_ids['caterpillar'], true ); 54 friends_add_friend( self::$current_user, self::$user_ids['pig'], true ); 55 add_action( 'friends_friendship_accepted', 'bp_friends_friendship_accepted_activity', 10, 4 ); 56 57 self::$group_slugs['hidden'] = 'the-maw'; 58 self::$group_slugs['public'] = 'the-great-journey'; 59 self::$group_slugs['private'] = 'tsavo-highway'; 60 61 // Create dummy groups. 62 self::$group_ids['hidden'] = $factory->group->create( array( 63 'creator_id' => self::$user_ids['xylo'], 64 'slug' => self::$group_slugs['hidden'], 65 'status' => 'hidden', 66 ) ); 67 self::$group_ids['public'] = $factory->group->create( array( 68 'creator_id' => self::$user_ids['xylo'], 69 'slug' => self::$group_slugs['public'], 70 'status' => 'public', 71 ) ); 72 self::$group_ids['private'] = $factory->group->create( array( 73 'creator_id' => self::$user_ids['xylo'], 74 'slug' => self::$group_slugs['private'], 75 'status' => 'private', 76 ) ); 77 78 // Add dummy users to dummy hidden groups. 79 groups_join_group( self::$group_ids['hidden'], self::$user_ids['pig'] ); 80 groups_join_group( self::$group_ids['hidden'], self::$user_ids['alpaca red'] ); 81 82 // Add dummy users to dummy public groups. 83 groups_join_group( self::$group_ids['public'], self::$current_user ); 84 groups_join_group( self::$group_ids['public'], self::$user_ids['aardvark'] ); 85 groups_join_group( self::$group_ids['public'], self::$user_ids['alpaca red'] ); 86 groups_join_group( self::$group_ids['public'], self::$user_ids['cat'] ); 87 groups_join_group( self::$group_ids['public'], self::$user_ids['smith'] ); 88 89 // Add dummy users to dummy private groups. 90 groups_join_group( self::$group_ids['private'], self::$user_ids['cat'] ); 91 groups_join_group( self::$group_ids['private'], self::$user_ids['caterpillar'] ); 92 93 self::commit_transaction(); 94 } 95 96 public static function tearDownAfterClass() { 97 foreach ( self::$group_ids as $group_id ) { 98 groups_delete_group( $group_id ); 99 } 100 101 foreach ( array_merge( self::$user_ids, array( self::$current_user ) ) as $user_id ) { 102 if ( is_multisite() ) { 103 wpmu_delete_user( $user_id ); 104 } else { 105 wp_delete_user( $user_id ); 106 } 107 } 108 109 self::commit_transaction(); 110 } 111 112 public function setUp() { 113 parent::setUp(); 114 $this->set_current_user( self::$current_user ); 115 } 116 117 public function tearDown() { 118 parent::tearDown(); 119 $this->set_current_user( self::$old_user_id ); 120 } 121 122 public function test_suggestions_with_type_members() { 123 $suggestions = bp_core_get_suggestions( array( 124 'type' => 'members', 125 'term' => 'smith', 126 ) ); 127 128 $this->assertFalse( is_wp_error( $suggestions ) ); 129 $this->assertEquals( 3, count( $suggestions ) ); // aardvark, smith, zoom. 130 } 131 132 public function test_suggestions_with_type_members_and_limit() { 133 $suggestions = bp_core_get_suggestions( array( 134 'limit' => 2, 135 'type' => 'members', 136 'term' => 'smith', 137 ) ); 138 139 $this->assertFalse( is_wp_error( $suggestions ) ); 140 $this->assertEquals( 2, count( $suggestions ) ); // two of: aardvark, smith, zoom. 141 } 142 143 public function test_suggestions_with_type_members_and_only_friends() { 144 $suggestions = bp_core_get_suggestions( array( 145 'only_friends' => true, 146 'type' => 'members', 147 'term' => 'smith', 148 ) ); 149 $this->assertFalse( is_wp_error( $suggestions ) ); 150 $this->assertEquals( 1, count( $suggestions ) ); // aardvark. 151 152 $suggestions = bp_core_get_suggestions( array( 153 'only_friends' => true, 154 'type' => 'members', 155 'term' => 'cat', 156 ) ); 157 $this->assertFalse( is_wp_error( $suggestions ) ); 158 $this->assertEquals( 2, count( $suggestions ) ); // cat, caterpillar. 159 } 160 161 public function test_suggestions_with_type_members_and_term_as_displayname() { 162 $suggestions = bp_core_get_suggestions( array( 163 'type' => 'members', 164 'term' => 'aardvark', 165 ) ); 166 167 $this->assertFalse( is_wp_error( $suggestions ) ); 168 $this->assertEquals( 1, count( $suggestions ) ); // aardvark. 169 } 170 171 public function test_suggestions_with_type_members_and_term_as_usernicename() { 172 $suggestions = bp_core_get_suggestions( array( 173 'type' => 'members', 174 'term' => 'eleanor', 175 ) ); 176 177 $this->assertFalse( is_wp_error( $suggestions ) ); 178 $this->assertEquals( 1, count( $suggestions ) ); // snake. 179 } 180 181 public function test_suggestions_with_term_as_current_user() { 182 $suggestions = bp_core_get_suggestions( array( 183 'type' => 'members', 184 'term' => 'katie', 185 ) ); 186 187 $this->assertFalse( is_wp_error( $suggestions ) ); 188 $this->assertEquals( 1, count( $suggestions ) ); 189 $this->assertSame( 'katie', $suggestions[0]->ID ); 190 } 191 192 193 public function test_suggestions_with_type_groupmembers_public() { 194 $suggestions = bp_core_get_suggestions( array( 195 'group_id' => self::$group_ids['public'], 196 'type' => 'members', 197 'term' => 'smith', 198 ) ); 199 200 $this->assertFalse( is_wp_error( $suggestions ) ); 201 $this->assertEquals( 2, count( $suggestions ) ); // aardvark, smith. 202 } 203 204 public function test_suggestions_with_type_groupmembers_public_and_limit() { 205 $suggestions = bp_core_get_suggestions( array( 206 'limit' => 1, 207 'group_id' => self::$group_ids['public'], 208 'type' => 'members', 209 'term' => 'smith', 210 ) ); 211 212 $this->assertFalse( is_wp_error( $suggestions ) ); 213 $this->assertEquals( 1, count( $suggestions ) ); // one of: aardvark, smith. 214 } 215 216 public function test_suggestions_with_type_groupmembers_public_and_only_friends() { 217 $suggestions = bp_core_get_suggestions( array( 218 'group_id' => self::$group_ids['public'], 219 'only_friends' => true, 220 'type' => 'members', 221 'term' => 'smith', 222 ) ); 223 224 $this->assertFalse( is_wp_error( $suggestions ) ); 225 $this->assertEquals( 1, count( $suggestions ) ); // aardvark. 226 } 227 228 public function test_suggestions_with_type_groupmembers_public_and_term_as_displayname() { 229 $suggestions = bp_core_get_suggestions( array( 230 'group_id' => self::$group_ids['public'], 231 'type' => 'members', 232 'term' => 'aardvark', 233 ) ); 234 235 $this->assertFalse( is_wp_error( $suggestions ) ); 236 $this->assertEquals( 1, count( $suggestions ) ); // aardvark. 237 } 238 239 public function test_suggestions_with_type_groupmembers_public_and_term_as_usernicename() { 240 $suggestions = bp_core_get_suggestions( array( 241 'group_id' => self::$group_ids['public'], 242 'type' => 'members', 243 'term' => 'robert', 244 ) ); 245 246 $this->assertFalse( is_wp_error( $suggestions ) ); 247 $this->assertEquals( 1, count( $suggestions ) ); // smith. 248 } 249 250 public function test_suggestions_with_type_groupmembers_public_as_id() { 251 $suggestions = bp_core_get_suggestions( array( 252 'group_id' => self::$group_ids['public'], 253 'type' => 'members', 254 'term' => 'smith', 255 ) ); 256 257 $this->assertFalse( is_wp_error( $suggestions ) ); 258 $this->assertEquals( 2, count( $suggestions ) ); // aardvark, smith. 259 } 260 261 public function test_suggestions_with_type_groupmembers_hidden() { 262 // current_user isn't a member of the hidden group 263 $suggestions = bp_core_get_suggestions( array( 264 'group_id' => self::$group_ids['hidden'], 265 'type' => 'members', 266 'term' => 'pig', 267 ) ); 268 $this->assertTrue( is_wp_error( $suggestions ) ); 269 270 // "alpaca red" is in the hidden group 271 $this->set_current_user( self::$user_ids['alpaca red'] ); 272 $suggestions = bp_core_get_suggestions( array( 273 'group_id' => self::$group_ids['hidden'], 274 'type' => 'members', 275 'term' => 'pig', 276 ) ); 277 $this->assertFalse( is_wp_error( $suggestions ) ); 278 $this->assertEquals( 1, count( $suggestions ) ); // pig 279 } 280 281 public function test_suggestions_with_type_groupmembers_private() { 282 // current_user isn't a member of the private group. 283 $suggestions = bp_core_get_suggestions( array( 284 'group_id' => self::$group_ids['private'], 285 'type' => 'members', 286 'term' => 'cat', 287 ) ); 288 $this->assertTrue( is_wp_error( $suggestions ) ); 289 290 // "caterpillar" is in the private group 291 $this->set_current_user( self::$user_ids['caterpillar'] ); 292 $suggestions = bp_core_get_suggestions( array( 293 'group_id' => self::$group_ids['private'], 294 'type' => 'members', 295 'term' => 'cat', 296 ) ); 297 $this->assertFalse( is_wp_error( $suggestions ) ); 298 $this->assertEquals( 2, count( $suggestions ) ); // cat, caterpillar 299 } 300 301 302 public function test_suggestions_with_type_groupmembers_public_and_exclude_group_from_results() { 303 $suggestions = bp_core_get_suggestions( array( 304 'group_id' => self::$group_ids['public'], 305 'type' => 'members', 306 'term' => 'smith', 307 ) ); 308 $this->assertFalse( is_wp_error( $suggestions ) ); 309 $this->assertEquals( 2, count( $suggestions ) ); // aardvark, smith. 310 311 $suggestions = bp_core_get_suggestions( array( 312 'group_id' => -self::$group_ids['public'], 313 'type' => 'members', 314 'term' => 'smith', 315 ) ); 316 $this->assertFalse( is_wp_error( $suggestions ) ); 317 $this->assertEquals( 1, count( $suggestions ) ); // zoom 318 } 319 320 public function test_suggestions_with_type_groupmembers_private_and_exclude_group_from_results() { 321 // current_user isn't a member of the private group. 322 $suggestions = bp_core_get_suggestions( array( 323 'group_id' => -self::$group_ids['private'], 324 'type' => 'members', 325 'term' => 'cat', 326 ) ); 327 $this->assertTrue( is_wp_error( $suggestions ) ); 328 329 330 $this->set_current_user( self::$user_ids['caterpillar'] ); 331 332 // "cat" is in the private group, so won't show up here. 333 $suggestions = bp_core_get_suggestions( array( 334 'group_id' => -self::$group_ids['private'], 335 'type' => 'members', 336 'term' => 'cat', 337 ) ); 338 $this->assertFalse( is_wp_error( $suggestions ) ); 339 $this->assertEmpty( $suggestions ); 340 341 // "zoo" is not the private group, so will show up here. 342 $suggestions = bp_core_get_suggestions( array( 343 'group_id' => -self::$group_ids['private'], 344 'type' => 'members', 345 'term' => 'zoo', 346 ) ); 347 $this->assertFalse( is_wp_error( $suggestions ) ); 348 $this->assertEquals( 1, count( $suggestions ) ); // zoo 349 } 350 351 public function test_suggestions_with_type_groupmembers_hidden_and_exclude_group_from_results() { 352 // current_user isn't a member of the hidden group. 353 $suggestions = bp_core_get_suggestions( array( 354 'group_id' => self::$group_ids['hidden'], 355 'type' => 'members', 356 'term' => 'pig', 357 ) ); 358 $this->assertTrue( is_wp_error( $suggestions ) ); 359 360 361 $this->set_current_user( self::$user_ids['alpaca red'] ); 362 363 // "alpaca red" is in the hidden group, so won't show up here. 364 $suggestions = bp_core_get_suggestions( array( 365 'group_id' => -self::$group_ids['hidden'], 366 'type' => 'members', 367 'term' => 'alpaca red', 368 ) ); 369 $this->assertFalse( is_wp_error( $suggestions ) ); 370 $this->assertEmpty( $suggestions ); 371 372 // "zoo" is not the hidden group, so will show up here. 373 $suggestions = bp_core_get_suggestions( array( 374 'group_id' => -self::$group_ids['hidden'], 375 'type' => 'members', 376 'term' => 'zoo', 377 ) ); 378 $this->assertFalse( is_wp_error( $suggestions ) ); 379 $this->assertEquals( 1, count( $suggestions ) ); // zoo 380 } 381 382 383 /** 384 * These next tests check the format of the response from the Suggestions API. 385 */ 386 387 public function test_suggestions_response_no_matches() { 388 $suggestions = bp_core_get_suggestions( array( 389 'term' => 'abcdefghijklmnopqrstuvwxyz', 390 'type' => 'members', 391 ) ); 392 393 $this->assertFalse( is_wp_error( $suggestions ) ); 394 $this->assertInternalType( 'array', $suggestions ); 395 $this->assertEmpty( $suggestions ); 396 } 397 398 public function test_suggestions_response_single_match() { 399 $suggestion = bp_core_get_suggestions( array( 400 'term' => 'zoom', 401 'type' => 'members', 402 ) ); 403 404 $this->assertFalse( is_wp_error( $suggestion ) ); 405 $this->assertInternalType( 'array', $suggestion ); 406 $this->assertNotEmpty( $suggestion ); 407 408 $suggestion = array_shift( $suggestion ); 409 410 $this->assertInternalType( 'object', $suggestion ); 411 $this->assertAttributeNotEmpty( 'image', $suggestion ); 412 $this->assertAttributeNotEmpty( 'ID', $suggestion ); 413 $this->assertAttributeNotEmpty( 'name', $suggestion ); 414 } 415 416 public function test_suggestions_response_multiple_matches() { 417 $suggestions = bp_core_get_suggestions( array( 418 'term' => 'cat', 419 'type' => 'members', 420 ) ); 421 422 $this->assertFalse( is_wp_error( $suggestions ) ); 423 $this->assertInternalType( 'array', $suggestions ); 424 $this->assertNotEmpty( $suggestions ); 425 426 foreach ( $suggestions as $suggestion ) { 427 $this->assertInternalType( 'object', $suggestion ); 428 $this->assertAttributeNotEmpty( 'image', $suggestion ); 429 $this->assertAttributeNotEmpty( 'ID', $suggestion ); 430 $this->assertAttributeNotEmpty( 'name', $suggestion ); 431 } 432 } 433 434 public function test_suggestions_term_is_case_insensitive() { 435 $lowercase = bp_core_get_suggestions( array( 436 'term' => 'lisa', 437 'type' => 'members', 438 ) ); 439 $this->assertFalse( is_wp_error( $lowercase ) ); 440 $this->assertEquals( 1, count( $lowercase ) ); 441 442 $uppercase = bp_core_get_suggestions( array( 443 'term' => 'LISA', 444 'type' => 'members', 445 ) ); 446 $this->assertFalse( is_wp_error( $uppercase ) ); 447 $this->assertEquals( 1, count( $uppercase ) ); 448 449 $this->assertSame( $lowercase[0]->ID, $uppercase[0]->ID ); 450 $this->assertSame( 'zoom', $lowercase[0]->ID ); 451 } 452 453 public function test_suggestions_response_property_types() { 454 $suggestion = bp_core_get_suggestions( array( 455 'term' => 'zoom', 456 'type' => 'members', 457 ) ); 458 459 $this->assertFalse( is_wp_error( $suggestion ) ); 460 $this->assertInternalType( 'array', $suggestion ); 461 $this->assertNotEmpty( $suggestion ); 462 463 $suggestion = array_shift( $suggestion ); 464 465 $this->assertInternalType( 'object', $suggestion ); 466 $this->assertAttributeInternalType( 'string', 'image', $suggestion ); 467 $this->assertAttributeInternalType( 'string', 'ID', $suggestion ); 468 $this->assertAttributeInternalType( 'string', 'name', $suggestion ); 469 } 470 471 472 /** 473 * Tests below this point are expected to fail. 474 */ 475 476 public function test_suggestions_with_bad_type() { 477 $suggestions = bp_core_get_suggestions( array( 478 'type' => 'fake_type', 479 ) ); 480 481 $this->assertTrue( is_wp_error( $suggestions ) ); 482 } 483 484 public function test_suggestions_with_type_groupmembers_and_bad_group_ids() { 485 // group_ids can't be a group slug. 486 $suggestions = bp_core_get_suggestions( array( 487 'group_id' => 'fake-group-slug', 488 'type' => 'members', 489 ) ); 490 491 $this->assertTrue( is_wp_error( $suggestions ) ); 492 } 493 494 public function test_suggestions_with_bad_term() { 495 // a non-empty term is mandatory 496 $suggestions = bp_core_get_suggestions( array( 497 'term' => '', 498 'type' => 'members', 499 ) ); 500 501 $this->assertTrue( is_wp_error( $suggestions ) ); 502 } 503 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Nov 21 01:00:57 2024 | Cross-referenced by PHPXref 0.7.1 |