[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/tests/phpunit/testcases/xprofile/ -> class-bp-xprofile-field.php (source)

   1  <?php
   2  /**
   3   * @group xprofile
   4   * @group BP_XProfile_Field
   5   */
   6  class BP_Tests_BP_XProfile_Field_TestCases extends BP_UnitTestCase {
   7  
   8      /**
   9       * @group xprofile_field_save
  10       */
  11  	public function test_can_delete_save() {
  12          $group = self::factory()->xprofile_group->create();
  13          $field = self::factory()->xprofile_field->create( array(
  14              'field_group_id' => $group,
  15          ) );
  16  
  17          $f = new BP_XProfile_Field( $field );
  18          $f->can_delete = 0;
  19          $f->save();
  20  
  21          $f2 = new BP_XProfile_Field( $field );
  22          $this->assertEquals( '0', $f2->can_delete );
  23      }
  24  
  25      /**
  26       * @group xprofile_get_field_id_from_name
  27       */
  28      public function test_get_id_from_name_field_name_option_value_conflict() {
  29          $group = self::factory()->xprofile_group->create();
  30  
  31          // force some checkbox options for our profile field
  32          $_POST['checkbox_option'] = array(
  33              1 => 'BuddyPress',
  34              2 => 'WordPress'
  35          );
  36  
  37          // checkbox field
  38          $f1 = self::factory()->xprofile_field->create( array(
  39              'field_group_id' => $group,
  40              'type' => 'checkbox',
  41              'name' => 'Interests'
  42          ) );
  43  
  44          // textbox field with the same name as our checkbox value
  45          $f2 = self::factory()->xprofile_field->create( array(
  46              'field_group_id' => $group,
  47              'type' => 'textbox',
  48              'name' => 'BuddyPress'
  49          ) );
  50  
  51          $this->assertEquals( $f2, xprofile_get_field_id_from_name( 'BuddyPress' ) );
  52  
  53          // cleanup!
  54          unset( $_POST['checkbox_option'] );
  55      }
  56  
  57      /**
  58       * @group xprofile_field_admin_validate
  59       */
  60  	public function test_admin_validate_zero_field_name() {
  61  
  62          // Mock POST global values
  63          $_POST['title']     = '0';
  64          $_POST['required']  = false;
  65          $_POST['fieldtype'] = 'textbox';
  66  
  67          // Validate the mocked POST radio button options
  68          $result = BP_XProfile_Field::admin_validate();
  69  
  70          // Assert valid
  71          $this->assertEquals( $result, true );
  72  
  73          // cleanup
  74          unset(
  75              $_POST['title'],
  76              $_POST['required'],
  77              $_POST['fieldtype']
  78          );
  79      }
  80  
  81      /**
  82       * @group xprofile_field_admin_validate
  83       */
  84  	public function test_admin_validate_field_options() {
  85  
  86          // Mock POST global values
  87          $_POST['title']        = 'Foo';
  88          $_POST['required']     = false;
  89          $_POST['fieldtype']    = 'radio';
  90          $_POST['radio_option'] = array(
  91              1 => '0',
  92              2 => '1',
  93              3 => '4',
  94          );
  95  
  96          // Validate the mocked POST radio button options
  97          $result = BP_XProfile_Field::admin_validate();
  98  
  99          // Assert valid
 100          $this->assertEquals( $result, true );
 101  
 102          // cleanup
 103          unset(
 104              $_POST['title'],
 105              $_POST['required'],
 106              $_POST['fieldtype'],
 107              $_POST['radio_option' ]
 108          );
 109      }
 110  
 111      /**
 112       * @ticket BP6545
 113       */
 114      public function test_newly_created_field_should_have_field_id_property_set() {
 115          $field = new BP_XProfile_Field();
 116          $field->group_id = 1;
 117          $field->name = 'Foo';
 118  
 119          $new_field_id = $field->save();
 120  
 121          $this->assertSame( $new_field_id, $field->id );
 122      }
 123  
 124      /**
 125       * @ticket BP6638
 126       */
 127  	public function test_default_visibility_should_be_lazy_loaded() {
 128          global $wpdb;
 129  
 130          $group = self::factory()->xprofile_group->create();
 131          $field = self::factory()->xprofile_field->create( array(
 132              'field_group_id' => $group,
 133          ) );
 134  
 135          bp_xprofile_update_meta( $field, 'field', 'default_visibility', 'loggedin' );
 136  
 137          // Initial setup takes just one query.
 138          $num_queries = $wpdb->num_queries;
 139          $field_obj = new BP_XProfile_Field( $field );
 140          $num_queries++;
 141  
 142          $this->assertSame( $num_queries, $wpdb->num_queries );
 143  
 144          // Fetching the default_visibility should cause another query.
 145          $this->assertSame( 'loggedin', $field_obj->default_visibility );
 146          $num_queries++;
 147  
 148          $this->assertSame( $num_queries, $wpdb->num_queries );
 149      }
 150  
 151      /**
 152       * @ticket BP6638
 153       */
 154      public function test_allow_custom_visibility_should_be_lazy_loaded() {
 155          global $wpdb;
 156  
 157          $group = self::factory()->xprofile_group->create();
 158          $field = self::factory()->xprofile_field->create( array(
 159              'field_group_id' => $group,
 160          ) );
 161  
 162          bp_xprofile_update_meta( $field, 'field', 'allow_custom_visibility', 'disabled' );
 163  
 164          // Initial setup takes just one query.
 165          $num_queries = $wpdb->num_queries;
 166          $field_obj = new BP_XProfile_Field( $field );
 167          $num_queries++;
 168  
 169          $this->assertSame( $num_queries, $wpdb->num_queries );
 170  
 171          // Fetching the allow_custom_visibility should cause another query.
 172          $this->assertSame( 'disabled', $field_obj->allow_custom_visibility );
 173          $num_queries++;
 174  
 175          $this->assertSame( $num_queries, $wpdb->num_queries );
 176      }
 177  
 178      /**
 179       * @ticket BP7073
 180       */
 181  	public function test_bad_field_id_should_not_be_cached() {
 182          BP_XProfile_Field::get_instance( 12345 );
 183  
 184          $this->assertFalse( wp_cache_get( 12345, 'bp_xprofile_fields' ) );
 185      }
 186  
 187      /**
 188       * @ticket BP7112
 189       */
 190  	public function test_update_position_should_invalidate_cache() {
 191          $group = self::factory()->xprofile_group->create();
 192          $field = self::factory()->xprofile_field->create( array(
 193              'field_group_id' => $group,
 194          ) );
 195  
 196          // Prime cache.
 197          $fetched_field = xprofile_get_field( $field );
 198          $new_field_order = 12345;
 199  
 200          // Update field position.
 201          BP_XProfile_Field::update_position( $field, $new_field_order, $group );
 202  
 203          // Cache call should miss; fresh data should be fetched.
 204          $updated_fetched_field = xprofile_get_field( $field );
 205          $this->assertEquals( $new_field_order, $updated_fetched_field->field_order );
 206      }
 207  
 208      /**
 209       * @ticket BP7351
 210       */
 211      public function test_empty_datebox_fields_should_not_return_unix_epoch() {
 212          $user  = self::factory()->user->create( array( 'role' => 'subscriber' ) );
 213          $group = self::factory()->xprofile_group->create();
 214          $field = self::factory()->xprofile_field->create( array(
 215              'field_group_id' => $group,
 216              'type' => 'datebox',
 217          ) );
 218  
 219          $old_user = get_current_user_id();
 220          $this->set_current_user( $user );
 221  
 222          $value = bp_get_profile_field_data( array( 'user_id' => $user, 'field' => $field ) );
 223          $this->assertEmpty( $value );
 224  
 225          $this->set_current_user( $old_user );
 226      }
 227  
 228      /**
 229       * @ticket BP6658
 230       */
 231      public function test_delete_field_should_delete_default_field_metadata() {
 232          $group = self::factory()->xprofile_group->create();
 233          $field = self::factory()->xprofile_field->create( array(
 234              'field_group_id' => $group
 235          ) );
 236  
 237          $field_obj = new BP_XProfile_Field( $field );
 238          $field_obj->delete();
 239  
 240          $value = bp_xprofile_get_meta( $field, 'field', 'default_visibility' );
 241          $this->assertEmpty( $value );
 242      }
 243  
 244      /**
 245       * @ticket BP6658
 246       */
 247      public function test_delete_field_should_delete_custom_field_metadata() {
 248          $group = self::factory()->xprofile_group->create();
 249          $field = self::factory()->xprofile_field->create( array(
 250              'field_group_id' => $group
 251          ) );
 252  
 253          bp_xprofile_update_meta( $field, 'field', 'custom', 'metadata' );
 254  
 255          $field_obj = new BP_XProfile_Field( $field );
 256          $field_obj->delete();
 257  
 258          $value = bp_xprofile_get_meta( $field, 'field', 'custom' );
 259          $this->assertEmpty( $value );
 260      }
 261  }


Generated: Mon Jul 22 01:01:43 2019 Cross-referenced by PHPXref 0.7.1