[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @group members 5 * @group member_types 6 */ 7 class BP_Tests_Members_Types extends BP_UnitTestCase { 8 public function setUp() { 9 parent::setUp(); 10 11 buddypress()->members->types = array(); 12 } 13 14 public function test_bp_register_member_type_should_fail_for_existing_member_type() { 15 bp_register_member_type( 'foo' ); 16 $this->assertWPError( bp_register_member_type( 'foo' ) ); 17 } 18 19 public function test_bp_register_member_type_should_return_type_object() { 20 $this->assertInternalType( 'object', bp_register_member_type( 'foo' ) ); 21 } 22 23 /** 24 * @dataProvider illegal_names 25 * @ticket BP5192 26 */ 27 public function test_illegal_names( $name ) { 28 $this->assertWPError( bp_register_member_type( $name ) ); 29 } 30 31 public function illegal_names() { 32 return array( 33 array( 'any' ), 34 array( 'null' ), 35 array( '_none' ), 36 ); 37 } 38 39 /** 40 * @ticket BP6139 41 */ 42 public function test_bp_register_member_type_should_sanitize_member_type_key() { 43 $key = 'F//oo% -Bar'; 44 $sanitized_key = 'foo-bar'; 45 46 $object = bp_register_member_type( $key ); 47 $this->assertSame( $sanitized_key, $object->name ); 48 } 49 50 public function test_bp_register_member_type_should_store_member_type_string_as_name_property() { 51 $object = bp_register_member_type( 'foo' ); 52 $this->assertSame( 'foo', $object->name ); 53 } 54 55 public function test_bp_register_member_type_should_fill_in_missing_labels_with_ucfirst_member_type() { 56 $object = bp_register_member_type( 'foo' ); 57 foreach ( $object->labels as $label ) { 58 $this->assertSame( 'Foo', $label ); 59 } 60 } 61 62 /** 63 * @ticket BP6125 64 */ 65 public function test_bp_register_member_type_should_respect_custom_name_label() { 66 $object = bp_register_member_type( 'foo', array( 67 'labels' => array( 68 'name' => 'Bar', 69 ), 70 ) ); 71 72 // 'singular_name' falls back on 'name'. 73 $this->assertSame( 'Bar', $object->labels['name'] ); 74 $this->assertSame( 'Bar', $object->labels['singular_name'] ); 75 } 76 77 /** 78 * @ticket BP6125 79 */ 80 public function test_bp_register_member_type_should_respect_custom_singular_name_label() { 81 $object = bp_register_member_type( 'foo', array( 82 'labels' => array( 83 'singular_name' => 'Bar', 84 ), 85 ) ); 86 87 // 'name' is set to upper-case version of member type name. 88 $this->assertSame( 'Foo', $object->labels['name'] ); 89 $this->assertSame( 'Bar', $object->labels['singular_name'] ); 90 } 91 92 /** 93 * @ticket BP6286 94 */ 95 public function test_bp_register_member_type_has_directory_should_default_to_true() { 96 $object = bp_register_member_type( 'foo', array( 97 'has_directory' => true, 98 ) ); 99 100 $this->assertTrue( $object->has_directory ); 101 $this->assertSame( 'foo', $object->directory_slug ); 102 } 103 104 /** 105 * @ticket BP6286 106 */ 107 public function test_bp_register_member_type_has_directory_true() { 108 $object = bp_register_member_type( 'foo', array( 109 'has_directory' => true, 110 ) ); 111 112 $this->assertTrue( $object->has_directory ); 113 $this->assertSame( 'foo', $object->directory_slug ); 114 } 115 116 /** 117 * @ticket BP6286 118 */ 119 public function test_bp_register_member_type_should_store_has_directory_false() { 120 $object = bp_register_member_type( 'foo', array( 121 'has_directory' => false, 122 ) ); 123 124 $this->assertFalse( $object->has_directory ); 125 $this->assertSame( '', $object->directory_slug ); 126 } 127 128 /** 129 * @ticket BP6286 130 */ 131 public function test_bp_register_member_type_should_store_has_directory_string() { 132 $object = bp_register_member_type( 'foo', array( 133 'has_directory' => 'foos', 134 ) ); 135 136 $this->assertTrue( $object->has_directory ); 137 $this->assertSame( 'foos', $object->directory_slug ); 138 } 139 140 public function test_bp_get_member_type_object_should_return_null_for_non_existent_member_type() { 141 $this->assertSame( null, bp_get_member_type_object( 'foo' ) ); 142 } 143 144 public function test_bp_get_member_type_object_should_return_type_object() { 145 bp_register_member_type( 'foo' ); 146 $this->assertInternalType( 'object', bp_get_member_type_object( 'foo' ) ); 147 } 148 149 public function test_bp_set_member_type_should_return_false_for_invalid_member_type() { 150 $this->assertFalse( bp_set_member_type( 'foo', 1 ) ); 151 } 152 153 public function test_bp_set_member_type_should_remove_member_type_when_passing_an_empty_value() { 154 $u = self::factory()->user->create(); 155 bp_register_member_type( 'foo' ); 156 bp_set_member_type( $u, 'foo' ); 157 158 // Make sure it's set up. 159 $this->assertSame( 'foo', bp_get_member_type( $u ) ); 160 161 $this->assertSame( array(), bp_set_member_type( $u, '' ) ); 162 $this->assertFalse( bp_get_member_type( $u ) ); 163 } 164 165 public function test_bp_set_member_type_success() { 166 $u = self::factory()->user->create(); 167 bp_register_member_type( 'foo' ); 168 169 $this->assertNotEmpty( bp_set_member_type( $u, 'foo' ) ); 170 } 171 172 public function test_bp_get_member_type_with_default_value_for_single() { 173 $u = self::factory()->user->create(); 174 bp_register_member_type( 'foo' ); 175 bp_register_member_type( 'bar' ); 176 bp_set_member_type( $u, 'foo' ); 177 bp_set_member_type( $u, 'bar', true ); 178 179 $this->assertSame( 'foo', bp_get_member_type( $u ) ); 180 } 181 182 public function test_bp_get_member_type_with_single_true() { 183 $u = self::factory()->user->create(); 184 bp_register_member_type( 'foo' ); 185 bp_register_member_type( 'bar' ); 186 bp_set_member_type( $u, 'foo' ); 187 bp_set_member_type( $u, 'bar', true ); 188 189 $this->assertSame( 'foo', bp_get_member_type( $u, true ) ); 190 } 191 192 public function test_bp_get_member_type_with_single_false() { 193 $u = self::factory()->user->create(); 194 bp_register_member_type( 'foo' ); 195 bp_register_member_type( 'bar' ); 196 bp_set_member_type( $u, 'foo' ); 197 bp_set_member_type( $u, 'bar', true ); 198 199 $this->assertEqualSets( array( 'foo', 'bar' ), bp_get_member_type( $u, false ) ); 200 } 201 202 public function test_bp_get_member_type_should_return_false_when_no_value_is_found() { 203 $u = self::factory()->user->create(); 204 bp_register_member_type( 'foo' ); 205 206 $this->assertFalse( bp_get_member_type( $u ) ); 207 } 208 209 /** 210 * @group cache 211 */ 212 public function test_bp_get_member_type_should_hit_cache() { 213 $u = self::factory()->user->create(); 214 bp_register_member_type( 'foo' ); 215 bp_set_member_type( $u, 'foo' ); 216 217 global $wpdb; 218 219 // Initial query should hit the database. 220 bp_get_member_type( $u ); 221 $num_queries = $wpdb->num_queries; 222 223 // Next query should not hit the database. 224 bp_get_member_type( $u ); 225 $this->assertSame( $num_queries, $wpdb->num_queries ); 226 } 227 228 /** 229 * @group BP6193 230 */ 231 public function test_bp_members_prefetch_member_type_array_cache_set() { 232 $u1 = self::factory()->user->create(); 233 $u2 = self::factory()->user->create(); 234 bp_register_member_type( 'foo' ); 235 bp_register_member_type( 'bar' ); 236 bp_set_member_type( $u1, 'foo' ); 237 bp_set_member_type( $u1, 'bar', true ); 238 239 // Get users so that the 'bp_user_query_populate_extras' is fired 240 // and members type prefetched 241 $users = bp_core_get_users( array( 'include' => array( $u1, $u2 ) ) ); 242 243 // Get single member type 244 $this->assertSame( 'foo', bp_get_member_type( $u1, true ) ); 245 $this->assertEmpty( bp_get_member_type( $u2, true ) ); 246 247 // Get all member types for the user 248 $this->assertEqualSets( array( 'foo', 'bar' ), bp_get_member_type( $u1, false ) ); 249 $this->assertEmpty( bp_get_member_type( $u2, false ) ); 250 } 251 252 /** 253 * @group cache 254 */ 255 public function test_bp_get_member_type_should_return_false_for_deleted_user() { 256 $u = self::factory()->user->create(); 257 bp_register_member_type( 'foo' ); 258 bp_set_member_type( $u, 'foo' ); 259 260 // Prime the cache. 261 bp_get_member_type( $u ); 262 263 if ( is_multisite() ) { 264 wpmu_delete_user( $u ); 265 } else { 266 wp_delete_user( $u ); 267 } 268 269 $this->assertFalse( wp_cache_get( $u, 'bp_member_type' ) ); 270 $this->assertFalse( bp_get_member_type( $u ) ); 271 } 272 273 /** 274 * @group BP6242 275 * @group cache 276 */ 277 public function test_bp_get_member_type_should_not_conflict_with_term_cache() { 278 global $wpdb; 279 280 // Offset IDs. 281 $dummy_terms = self::factory()->tag->create_many( 7 ); 282 283 $u1 = self::factory()->user->create(); 284 bp_register_member_type( 'foo' ); 285 bp_set_member_type( $u1, 'foo' ); 286 287 // Fetch a term ID. 288 $terms = get_terms( bp_get_member_type_tax_name(), array( 'hide_empty' => false, 'fields' => 'all' ) ); 289 290 // Make sure the user's ID matches a term ID, to force a cache confusion. 291 $u2 = self::factory()->user->create(); 292 $new_user_id = $terms[0]->term_id; 293 $wpdb->update( $wpdb->users, array( 'ID' => $new_user_id ), array( 'ID' => $u2 ) ); 294 295 bp_set_member_type( $new_user_id, 'foo' ); 296 297 // Reprime the taxonomy cache. 298 $terms = get_terms( bp_get_member_type_tax_name(), array( 'hide_empty' => false, 'fields' => 'all' ) ); 299 300 $this->assertSame( 'foo', bp_get_member_type( $new_user_id, true ) ); 301 } 302 303 /** 304 * @group BP6188 305 */ 306 public function test_bp_remove_member_type_should_return_false_when_member_type_is_empty() { 307 $this->assertFalse( bp_remove_member_type( 5, '' ) ); 308 } 309 310 /** 311 * @group BP6188 312 */ 313 public function test_bp_remove_member_type_should_return_false_when_member_type_is_invalid() { 314 $this->assertFalse( bp_remove_member_type( 5, 'foo' ) ); 315 } 316 317 /** 318 * @group BP6188 319 */ 320 public function test_bp_remove_member_type_should_return_false_when_member_is_not_of_provided_type() { 321 $u1 = self::factory()->user->create(); 322 bp_register_member_type( 'foo' ); 323 bp_register_member_type( 'bar' ); 324 bp_set_member_type( $u1, 'bar' ); 325 326 $this->assertFalse( bp_remove_member_type( $u1, 'foo' ) ); 327 $types = bp_get_member_type( $u1, false ); 328 $this->assertEquals( array( 'bar' ), $types ); 329 } 330 331 /** 332 * @group BP6188 333 */ 334 public function test_bp_remove_member_type_should_return_true_for_successful_deletion() { 335 $u1 = self::factory()->user->create(); 336 bp_register_member_type( 'foo' ); 337 bp_register_member_type( 'bar' ); 338 bp_set_member_type( $u1, 'foo' ); 339 bp_set_member_type( $u1, 'bar', true ); 340 341 $this->assertTrue( bp_remove_member_type( $u1, 'foo' ) ); 342 $types = bp_get_member_type( $u1, false ); 343 $this->assertEquals( array( 'bar' ), $types ); 344 } 345 346 /** 347 * @ticket BP8639 348 */ 349 public function test_bp_remove_member_type_should_return_false_when_user_has_no_member_type() { 350 $u1 = self::factory()->user->create(); 351 bp_register_member_type( 'random' ); 352 353 $this->assertFalse( bp_remove_member_type( $u1, 'random' ) ); 354 } 355 356 /** 357 * @group BP6138 358 */ 359 function test_bp_has_member_type_should_return_false_when_member_type_is_empty() { 360 $this->assertFalse( bp_has_member_type( 5, '' ) ); 361 } 362 363 /** 364 * @group BP6138 365 */ 366 function test_bp_has_member_type_should_return_false_when_member_type_is_invalid() { 367 $this->assertFalse( bp_has_member_type( 5, 'foo' ) ); 368 } 369 370 /** 371 * @group BP6138 372 */ 373 public function test_bp_has_member_type_should_return_false_when_member_id_is_empty() { 374 bp_register_member_type( 'foo' ); 375 376 $this->assertFalse( bp_has_member_type( '', 'foo' ) ); 377 } 378 379 /** 380 * @group BP6138 381 */ 382 public function test_bp_has_member_type_should_return_false_when_member_is_not_of_provided_type() { 383 $u1 = self::factory()->user->create(); 384 bp_register_member_type( 'foo' ); 385 bp_register_member_type( 'bar' ); 386 bp_set_member_type( $u1, 'bar' ); 387 388 $this->assertFalse( bp_has_member_type( $u1, 'foo' ) ); 389 } 390 391 /** 392 * @group BP6138 393 */ 394 public function test_bp_has_member_type_should_return_true_on_success() { 395 $u1 = self::factory()->user->create(); 396 bp_register_member_type( 'foo' ); 397 bp_register_member_type( 'bar' ); 398 bp_set_member_type( $u1, 'foo' ); 399 bp_set_member_type( $u1, 'bar', true ); 400 401 $this->assertTrue( bp_has_member_type( $u1, 'foo' ) ); 402 $types = bp_get_member_type( $u1, false ); 403 $this->assertEqualSets( array( 'bar', 'foo' ), $types ); 404 } 405 406 public function test_bp_get_registered_by_code_member_type_should_not_return_unregistered_types() { 407 $u1 = self::factory()->user->create(); 408 bp_register_member_type( 'foo' ); 409 bp_set_member_type( $u1, 'foo' ); 410 411 // Directly set a type that hasn't been registered. 412 bp_set_object_terms( $u1, 'ugh', bp_get_member_type_tax_name(), true ); 413 414 $type = bp_get_member_type( $u1, false, false ); 415 $this->assertEquals( array( 'foo' ), $type ); 416 } 417 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Dec 22 01:00:54 2024 | Cross-referenced by PHPXref 0.7.1 |