[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  // Include the xProfile Test Type
   3  include_once BP_TESTS_DIR . 'assets/bptest-xprofile-field-type.php';
   4  
   5  /**
   6   * @group xprofile
   7   * @group BP_XProfile_Field_Type
   8   */
   9  class BP_Tests_XProfile_Field_Type extends BP_UnitTestCase {
  10  
  11  	public function setUp() {
  12          parent::setUp();
  13  
  14          add_filter( 'bp_xprofile_get_field_types', array( $this, 'get_field_types' ) );
  15      }
  16  
  17  	public function tearDown() {
  18          parent::tearDown();
  19  
  20          remove_filter( 'bp_xprofile_get_field_types', array( $this, 'get_field_types' ) );
  21      }
  22  
  23  	public function test_unregistered_field_type_returns_textbox() {
  24          $field = bp_xprofile_create_field_type( 'fakeyfield' );
  25          $this->assertEquals( get_class( $field ), 'BP_XProfile_Field_Type_Placeholder' );
  26      }
  27  
  28  	public function test_textbox_validate_empty_string() {
  29          $field = bp_xprofile_create_field_type( 'textbox' );
  30          $this->assertTrue( $field->is_valid( '' ) );
  31      }
  32  
  33  	public function test_textbox_validate_string() {
  34          $field = bp_xprofile_create_field_type( 'textbox' );
  35          $this->assertTrue( $field->is_valid( 'my 117th input string!' ) );
  36      }
  37  
  38  	public function test_textbox_validate_integer() {
  39          $field = bp_xprofile_create_field_type( 'textbox' );
  40          $this->assertTrue( $field->is_valid( 123 ) );
  41      }
  42  
  43  	public function test_textbox_validate_allowed_string() {
  44          $field = bp_xprofile_create_field_type( 'textbox' );
  45  
  46          $this->assertTrue( $field->is_valid( 'a string' ) );
  47          $this->assertFalse( $field->set_allowed_values( 'pizza' )->is_valid( 'pasta' ) );
  48          $this->assertTrue( $field->is_valid( 'pizza' ) );
  49      }
  50  
  51  	public function test_multiselectbox_validate_allowed_array() {
  52          $field = bp_xprofile_create_field_type( 'multiselectbox' );
  53          $field->set_allowed_values( array( 'cheese', 'pepporoni' ) );
  54  
  55          $this->assertTrue( $field->is_valid( array( 'cheese', 'pepporoni' ) ) );
  56          $this->assertTrue( $field->is_valid( array( 'cheese' ) ) );
  57          $this->assertFalse( $field->is_valid( array( 'cheese', 'pinapple', 'pepporoni' ) ) );
  58          $this->assertFalse( $field->is_valid( array( 'pinapple' ) ) );
  59      }
  60  
  61  	public function test_multiselectbox_validate_null_value() {
  62          $field = bp_xprofile_create_field_type( 'multiselectbox' );
  63          $field->set_allowed_values( array( 'cheese', 'pepporoni' ) );
  64  
  65          $this->assertFalse( $field->is_valid( array( '' ) ) );
  66          $this->assertFalse( $field->is_valid( '' ) );
  67          $this->assertTrue( $field->is_valid( array() ) );
  68      }
  69  
  70  	public function test_datebox_do_not_validate_string() {
  71          $field = bp_xprofile_create_field_type( 'datebox' );
  72          $this->assertFalse( $field->is_valid( 'datebox fields only accept strings in: Y-m-d 00:00:00' ) );
  73          $this->assertFalse( $field->is_valid( '' ) );
  74      }
  75  
  76  	public function test_datebox_do_not_validate_integer() {
  77          $field = bp_xprofile_create_field_type( 'datebox' );
  78          $this->assertFalse( $field->is_valid( 221213 ) );
  79      }
  80  
  81  	public function test_datebox_validate_date() {
  82          $field = bp_xprofile_create_field_type( 'datebox' );
  83          $this->assertTrue( $field->is_valid( '2013-12-22 00:00:00' ) );
  84      }
  85  
  86      public function test_datebox_do_not_validate_date_with_timestamp() {
  87          $field = bp_xprofile_create_field_type( 'datebox' );
  88          $this->assertFalse( $field->is_valid( '2013-12-22 19:11:30' ) );
  89          $this->assertFalse( $field->is_valid( '2013-12-22' ) );
  90      }
  91  
  92  	public function test_number_do_not_validate_string() {
  93          $field = bp_xprofile_create_field_type( 'number' );
  94          $this->assertFalse( $field->is_valid( 'number fields only accept integers' ) );
  95          $this->assertFalse( $field->is_valid( '' ) );
  96      }
  97  
  98  	public function test_number_validate_positive_integer() {
  99          $field = bp_xprofile_create_field_type( 'number' );
 100          $this->assertTrue( $field->is_valid( 12345678901 ) );
 101          $this->assertTrue( $field->is_valid( '12345678901' ) );
 102      }
 103  
 104  	public function test_number_validate_negative_integer() {
 105          $field = bp_xprofile_create_field_type( 'number' );
 106          $this->assertTrue( $field->is_valid( -987654321 ) );
 107          $this->assertTrue( $field->is_valid( '-987654321' ) );
 108      }
 109  
 110  	public function test_number_validate_null_value() {
 111          $field = bp_xprofile_create_field_type( 'number' );
 112          $this->assertTrue( $field->is_valid( '0' ) );
 113          $this->assertFalse( $field->is_valid( '-' ) );
 114          $this->assertFalse( $field->is_valid( '' ) );
 115      }
 116  
 117  	public function test_number_validate_allowed_array() {
 118          $field = bp_xprofile_create_field_type( 'number' );
 119          $field->set_allowed_values( array( 123, 456 ) );
 120  
 121          $this->assertTrue( $field->is_valid( array( 123 ) ) );
 122          $this->assertTrue( $field->is_valid( array( 456 ) ) );
 123          $this->assertFalse( $field->is_valid( array( 789, 456, 123 ) ) );
 124          $this->assertFalse( $field->is_valid( array( 789 ) ) );
 125      }
 126  
 127  	public function test_radiobutton_validate_allowed_array() {
 128          $field = bp_xprofile_create_field_type( 'radio' );
 129          $field->set_allowed_values( array( 'cheese', 'pepporoni' ) );
 130  
 131          $this->assertTrue( $field->is_valid( array( 'cheese', 'pepporoni' ) ) );
 132          $this->assertTrue( $field->is_valid( array( 'cheese' ) ) );
 133          $this->assertFalse( $field->is_valid( array(  'pinapple', 'cheese', 'pepporoni' ) ) );
 134          $this->assertFalse( $field->is_valid( array( 'pinapple' ) ) );
 135          $this->assertFalse( $field->is_valid( '' ) );
 136      }
 137  
 138      public function test_radiobutton_do_not_validate_empty_items_in_allowed_list() {
 139          $field = bp_xprofile_create_field_type( 'radio' );
 140          $field->set_allowed_values( array( '' ) );
 141  
 142          $this->assertFalse( $field->is_valid( array( '' ) ) );
 143      }
 144  
 145  	public function test_checkbox_validate_allowed_array() {
 146          $field = bp_xprofile_create_field_type( 'checkbox' );
 147          $field->set_allowed_values( array( 'cheese', 'pepporoni' ) );
 148  
 149          $this->assertTrue( $field->is_valid( array( 'cheese', 'pepporoni' ) ) );
 150          $this->assertTrue( $field->is_valid( array( 'cheese' ) ) );
 151          $this->assertFalse( $field->is_valid( array( 'pepporoni', 'cheese', 'pinapple' ) ) );
 152          $this->assertFalse( $field->is_valid( array( 'pinapple' ) ) );
 153          $this->assertFalse( $field->is_valid( '' ) );
 154      }
 155  
 156  	public function test_checkbox_validate_null_value() {
 157          $field = bp_xprofile_create_field_type( 'checkbox' );
 158          $field->set_allowed_values( array( 'cheese', 'pepporoni' ) );
 159  
 160          $this->assertFalse( $field->is_valid( array( '' ) ) );
 161          $this->assertFalse( $field->is_valid( '' ) );
 162          $this->assertTrue( $field->is_valid( array() ) );
 163      }
 164  
 165      /**
 166       * @group url
 167       */
 168  	public function test_url_validate_url() {
 169          $field = bp_xprofile_create_field_type( 'url' );
 170  
 171          $this->assertTrue( $field->is_valid( 'http://foo.com' ) );
 172          $this->assertTrue( $field->is_valid( 'https://foo.com' ) );
 173          $this->assertTrue( $field->is_valid( 'http://foo.bar.com' ) );
 174          $this->assertTrue( $field->is_valid( 'http://foo.bar.com/' ) );
 175          $this->assertTrue( $field->is_valid( 'http://foo.com/bar' ) );
 176          $this->assertTrue( $field->is_valid( 'http://foo.com/index.php' ) );
 177          $this->assertTrue( $field->is_valid( 'http://foo.com/?bar=baz' ) );
 178  
 179          $this->assertFalse( $field->is_valid( 'htp://foo.com' ) );
 180          $this->assertFalse( $field->is_valid( 'http:/foo.com' ) );
 181          $this->assertFalse( $field->is_valid( 'http//foo.com' ) );
 182          $this->assertFalse( $field->is_valid( 'http://foo' ) );
 183          $this->assertFalse( $field->is_valid( 'foo.com' ) );
 184      }
 185  
 186      /**
 187       * @group BP_XProfile_Field_Group_Type_Placeholder
 188       */
 189  	public function test_placeholder_validate_any_value() {
 190          $field = bp_xprofile_create_field_type( 'foo' );
 191          $this->assertTrue( $field->is_valid( '' ) );
 192          $this->assertTrue( $field->is_valid( 'bar' ) );
 193          $this->assertTrue( $field->is_valid( array( 'bar' ) ) );
 194      }
 195  
 196  	public function test_telephone_validate_number_formats() {
 197          $field = bp_xprofile_create_field_type( 'telephone' );
 198          $this->assertTrue( $field->is_valid( '07700 900461' ) );
 199          $this->assertTrue( $field->is_valid( '555-2368' ) );
 200          $this->assertTrue( $field->is_valid( '(212) 664-7665' ) );
 201      }
 202  
 203      /**
 204       * @ticket BP7162
 205       */
 206  	public function test_xprofile_field_type_test_supports() {
 207          $group_id = self::factory()->xprofile_group->create();
 208          $field_id = self::factory()->xprofile_field->create(
 209              array(
 210                  'field_group_id' => $group_id,
 211                  'type'           => 'test-field-type',
 212                  'name'           => 'Test Supports',
 213              )
 214          );
 215  
 216          $field = xprofile_get_field( $field_id, null, false );
 217  
 218          $this->assertTrue( $field->field_type_supports( 'switch_fieldtype' ) );
 219          $this->assertFalse( $field->field_type_supports( 'do_autolink' ) );
 220          $this->assertFalse( $field->field_type_supports( 'allow_custom_visibility' ) );
 221          $this->assertTrue( $field->field_type_supports( 'required' ) );
 222          $this->assertTrue( $field->field_type_supports( 'member_types' ) );
 223          $this->assertEquals( 'adminsonly', $field->get_default_visibility() );
 224      }
 225  
 226  	public function get_field_types( $types ) {
 227          $types['test-field-type'] = 'BPTest_XProfile_Field_Type';
 228          return $types;
 229      }
 230  }


Generated: Fri Nov 22 01:00:56 2024 Cross-referenced by PHPXref 0.7.1