[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @group member_types 5 * @group xprofile 6 * @ticket BP5192 7 */ 8 class BP_Tests_XProfile_BpXprofileField_MemberTypes extends BP_UnitTestCase { 9 protected $field_group_id; 10 protected $field_id; 11 protected $field; 12 13 public function setUp() { 14 parent::setUp(); 15 bp_register_member_type( 'foo' ); 16 bp_register_member_type( 'bar' ); 17 18 $this->field_group_id = self::factory()->xprofile_group->create(); 19 $this->field_id = self::factory()->xprofile_field->create( array( 'field_group_id' => $this->field_group_id ) ); 20 $this->field = new BP_XProfile_Field( $this->field_id ); 21 } 22 23 public function tearDown() { 24 buddypress()->members->types = array(); 25 parent::tearDown(); 26 } 27 28 public function test_get_single_member_type() { 29 bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'foo' ); 30 $this->assertEqualSets( array( 'foo' ), $this->field->get_member_types() ); 31 } 32 33 public function test_get_multiple_member_types() { 34 bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'foo' ); 35 bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'bar' ); 36 $this->assertEqualSets( array( 'foo', 'bar' ), $this->field->get_member_types() ); 37 } 38 39 public function test_invalid_member_types_should_not_be_returned() { 40 bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'foo' ); 41 bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'phony' ); 42 $this->assertEquals( array( 'foo' ), $this->field->get_member_types() ); 43 } 44 45 public function test_when_no_stored_types_are_found_all_registered_member_types_as_well_as_null_type_should_be_returned() { 46 $this->assertEqualSets( array( 'null', 'foo', 'bar' ), $this->field->get_member_types() ); 47 } 48 49 public function test__none_meta_should_result_in_empty_array() { 50 bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', '_none' ); 51 $this->assertEquals( array(), $this->field->get_member_types() ); 52 } 53 54 public function test__none_meta_should_override_other_values() { 55 bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', '_none' ); 56 bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'foo' ); 57 $this->assertEquals( array(), $this->field->get_member_types() ); 58 } 59 60 public function test_set_should_not_append_by_default() { 61 bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'foo' ); 62 $this->assertEquals( array( 'bar' ), $this->field->set_member_types( array( 'bar' ) ) ); 63 } 64 65 public function test_set_should_not_append_when_append_is_set_to_false() { 66 bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'foo' ); 67 $this->assertEquals( array( 'bar' ), $this->field->set_member_types( array( 'bar', false ) ) ); 68 } 69 70 public function test_set_should_append_when_append_is_set_to_true() { 71 bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'foo' ); 72 $this->assertEqualSets( array( 'foo', 'bar' ), $this->field->set_member_types( array( 'bar' ), true ) ); 73 } 74 75 public function test_set_empty_array_with_append_true_should_have_no_effect_on_saved_types() { 76 bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'foo' ); 77 $this->assertEqualSets( array( 'foo' ), $this->field->set_member_types( array(), true ) ); 78 } 79 80 public function test_set_empty_array_with_append_false_should_result_in_field_being_associated_with_no_member_types() { 81 bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'foo' ); 82 $this->assertEqualSets( array(), $this->field->set_member_types( array() ) ); 83 } 84 85 public function test_set_should_interpret_null_flag_properly() { 86 $this->assertEqualSets( array( 'null' ), $this->field->set_member_types( array( 'null' ) ) ); 87 } 88 89 public function test_set_all_types_plus_null_should_result_in_nothing_stored_in_db() { 90 $types = array( 'null', 'foo', 'bar' ); 91 $this->assertEqualSets( $types, $this->field->set_member_types( $types ) ); 92 93 $types_db = bp_xprofile_get_meta( $this->field_id, 'field', 'member_type', false ); 94 $this->assertEqualSets( array(), $types_db ); 95 } 96 97 public function test_get_fields_for_member_type_should_get_field_with_explicit_member_type() { 98 bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'foo' ); 99 $found = BP_XProfile_Field::get_fields_for_member_type( 'foo' ); 100 $this->assertEqualSets( array( 1, $this->field_id ), array_keys( $found ) ); 101 } 102 103 public function test_get_fields_for_member_type_should_ignore_field_that_applies_to_no_member_types() { 104 bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', '_none' ); 105 $found = BP_XProfile_Field::get_fields_for_member_type( 'foo' ); 106 $this->assertEqualSets( array( 1 ), array_keys( $found ) ); 107 } 108 109 public function test_get_fields_for_member_type_should_ignore_field_that_applies_to_different_member_types() { 110 bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'bar' ); 111 $found = BP_XProfile_Field::get_fields_for_member_type( 'foo' ); 112 $this->assertEqualSets( array( 1 ), array_keys( $found ) ); 113 } 114 115 public function test_get_fields_for_member_type_should_include_fields_with_no_member_type_restrictions() { 116 // A field with no member_type metadata applies to all member types. 117 $found = BP_XProfile_Field::get_fields_for_member_type( 'foo' ); 118 $this->assertEqualSets( array( 1, $this->field_id ), array_keys( $found ) ); 119 } 120 121 public function test_passing_member_type_any_to_get_fields_for_member_type_should_return_unrestricted_fields() { 122 $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $this->field_group_id ) ); 123 124 $field_2 = new BP_XProfile_Field( $f2 ); 125 $field_2->set_member_types( array( 'foo' ) ); 126 127 $found = BP_XProfile_Field::get_fields_for_member_type( 'any' ); 128 $this->assertEqualSets( array( 1, $this->field_id ), array_keys( $found ) ); 129 } 130 131 public function test_passing_empty_member_type_to_get_fields_for_member_type_should_return_unrestricted_fields() { 132 $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $this->field_group_id ) ); 133 134 $field_2 = new BP_XProfile_Field( $f2 ); 135 $field_2->set_member_types( array( 'foo' ) ); 136 137 $found = BP_XProfile_Field::get_fields_for_member_type( '' ); 138 $this->assertEqualSets( array( 1, $this->field_id ), array_keys( $found ) ); 139 } 140 141 public function test_passing_member_type_none_to_get_fields_for_member_type_should_return_fields_unavailable_to_all_member_types() { 142 bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', '_none' ); 143 $found = BP_XProfile_Field::get_fields_for_member_type( '_none' ); 144 $this->assertEqualSets( array( $this->field_id ), array_keys( $found ) ); 145 } 146 147 public function test_get_fields_for_member_type_should_accept_an_array_of_member_types() { 148 $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $this->field_group_id ) ); 149 bp_xprofile_add_meta( $this->field_id, 'field', 'member_type', 'foo' ); 150 bp_xprofile_add_meta( $f2, 'field', 'member_type', 'bar' ); 151 152 $found = BP_XProfile_Field::get_fields_for_member_type( array( 'foo', 'bar' ) ); 153 $this->assertEqualSets( array( 1, $this->field_id, $f2 ), array_keys( $found ) ); 154 } 155 156 /** 157 * @group cache 158 */ 159 public function test_get_fields_for_member_type_should_fetch_restricted_fields_from_cache() { 160 global $wpdb; 161 162 $this->field->set_member_types( array( 'foo' ) ); 163 164 // Prime cache. 165 BP_XProfile_Field::get_fields_for_member_type( 'foo' ); 166 167 $num_queries = $wpdb->num_queries; 168 169 BP_XProfile_Field::get_fields_for_member_type( 'foo' ); 170 171 // get_fields_for_member_type() always does at least one query. 172 $num_queries++; 173 $this->assertSame( $num_queries, $wpdb->num_queries ); 174 } 175 176 /** 177 * @group cache 178 */ 179 public function test_get_fields_for_member_type_should_skip_cache_after_a_fields_member_type_is_modified() { 180 global $wpdb; 181 182 $this->field->set_member_types( array( 'foo' ) ); 183 184 // Prime cache. 185 BP_XProfile_Field::get_fields_for_member_type( 'foo' ); 186 187 $num_queries = $wpdb->num_queries; 188 189 $this->field->set_member_types( array( 'none' ) ); 190 191 $found = BP_XProfile_Field::get_fields_for_member_type( 'foo' ); 192 193 $this->assertTrue( $num_queries + 2 <= $wpdb->num_queries ); 194 $this->assertEqualSets( array( 1 ), array_keys( $found ) ); 195 } 196 197 public function test_get_member_type_label_order() { 198 // Should be alphabetical, with the null option last. 199 bp_register_member_type( 'aaa', array( 200 'labels' => array( 201 'name' => 'AAA', 202 ), 203 ) ); 204 bp_register_member_type( 'zzz', array( 205 'labels' => array( 206 'name' => 'ZZZ', 207 ), 208 ) ); 209 210 $this->field->set_member_types( array( 'aaa', 'zzz', 'null' ) ); 211 212 $expected = '(Member types: AAA, ZZZ, Users with no member type)'; 213 $this->assertSame( $expected, $this->field->get_member_type_label() ); 214 } 215 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 18 01:00:54 2024 | Cross-referenced by PHPXref 0.7.1 |