[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/tests/phpunit/testcases/groups/ -> types.php (source)

   1  <?php
   2  
   3  /**
   4   * @group groups
   5   * @group group_types
   6   */
   7  class BP_Tests_Groups_Types extends BP_UnitTestCase {
   8      protected static $u1 = null;
   9  
  10  	public function setUp() {
  11          parent::setUp();
  12  
  13          buddypress()->groups->types = array();
  14      }
  15  
  16  	public static function wpSetUpBeforeClass( $f ) {
  17          self::$u1 = $f->user->create( array(
  18              'user_email' => 'group-types-tests@example.com',
  19              'user_login' => 'grouptypestests',
  20          ) );
  21      }
  22  
  23  	public static function wpTearDownAfterClass() {
  24          self::delete_user( self::$u1 );
  25      }
  26  
  27      public function test_groups_register_type_should_fail_for_existing_group_type() {
  28          bp_groups_register_group_type( 'foo' );
  29          $this->assertWPError( bp_groups_register_group_type( 'foo' ) );
  30      }
  31  
  32      /**
  33       * @dataProvider illegal_names
  34       */
  35  	public function test_illegal_names( $name ) {
  36          $this->assertWPError( bp_groups_register_group_type( $name ) );
  37      }
  38  
  39  	public function illegal_names() {
  40          return array(
  41              array( 'any' ),
  42              array( 'null' ),
  43              array( '_none' ),
  44          );
  45      }
  46  
  47      public function test_groups_register_type_should_sanitize_group_type_key() {
  48          $key = 'F//oo% -Bar';
  49          $sanitized_key = 'foo-bar';
  50  
  51          $object = bp_groups_register_group_type( $key );
  52          $this->assertSame( $sanitized_key, $object->name );
  53      }
  54  
  55      public function test_groups_register_type_should_store_group_type_string_as_name_property() {
  56          $object = bp_groups_register_group_type( 'foo' );
  57          $this->assertSame( 'foo', $object->name );
  58      }
  59  
  60      public function test_groups_register_type_should_fill_missing_labels_with_ucfirst_group_type() {
  61          $object = bp_groups_register_group_type( 'foo' );
  62          foreach ( $object->labels as $label ) {
  63              $this->assertSame( 'Foo', $label );
  64          }
  65      }
  66  
  67      public function test_groups_register_type_should_respect_custom_name_labels() {
  68          $object = bp_groups_register_group_type( 'foo', array(
  69              'labels' => array(
  70                  'name' => 'Bar',
  71              ),
  72          ) );
  73  
  74          $this->assertSame( 'Bar', $object->labels['name'] );
  75          $this->assertSame( 'Bar', $object->labels['singular_name'] );
  76      }
  77  
  78      public function test_groups_register_type_should_respect_custom_singular_name_labels() {
  79          $object = bp_groups_register_group_type( 'foo', array(
  80              'labels' => array(
  81                  'singular_name' => 'Bar',
  82              ),
  83          ) );
  84  
  85          $this->assertSame( 'Foo', $object->labels['name'] );
  86          $this->assertSame( 'Bar', $object->labels['singular_name'] );
  87      }
  88  
  89      public function test_groups_get_type_object_should_return_null_for_non_existing_group_type() {
  90          $this->assertSame( null, bp_groups_get_group_type_object( 'foo' ) );
  91      }
  92  
  93      public function test_groups_get_type_object_should_return_type_object() {
  94          bp_groups_register_group_type( 'foo' );
  95          $this->assertInternalType( 'object', bp_groups_register_group_type( 'foo' ) );
  96      }
  97  
  98      public function test_groups_set_type_should_return_false_for_invalid_group_type() {
  99          $this->assertFalse( bp_groups_set_group_type( 1, 'foo' ) );
 100      }
 101  
 102  	public function test_groups_set_type_success() {
 103          $g = self::factory()->group->create( array( 'creator_id' => self::$u1 ) );
 104          bp_groups_register_group_type( 'foo' );
 105  
 106          $this->assertNotEmpty( bp_groups_set_group_type( $g, 'foo' ) );
 107      }
 108  
 109      public function test_groups_set_type_should_remove_type_when_passing_an_empty_value() {
 110          $g = self::factory()->group->create( array( 'creator_id' => self::$u1 ) );
 111          bp_groups_register_group_type( 'foo' );
 112          bp_groups_set_group_type( $g, 'foo' );
 113  
 114          // Make sure group type is set.
 115          $this->assertSame( 'foo', bp_groups_get_group_type( $g ) );
 116  
 117          $this->assertSame( array(), bp_groups_set_group_type( $g, '' ) );
 118          $this->assertFalse( bp_groups_get_group_type( $g ) );
 119      }
 120  
 121      public function test_groups_get_type_with_default_value_for_single() {
 122          $g = self::factory()->group->create( array( 'creator_id' => self::$u1 ) );
 123          bp_groups_register_group_type( 'foo' );
 124          bp_groups_register_group_type( 'bar' );
 125          bp_groups_set_group_type( $g, 'foo' );
 126          bp_groups_set_group_type( $g, 'bar', true );
 127  
 128          $this->assertSame( 'foo', bp_groups_get_group_type( $g ) );
 129      }
 130  
 131  	public function test_groups_get_type_with_single_true() {
 132          $g = self::factory()->group->create( array( 'creator_id' => self::$u1 ) );
 133          bp_groups_register_group_type( 'foo' );
 134          bp_groups_register_group_type( 'bar' );
 135          bp_groups_set_group_type( $g, 'foo' );
 136          bp_groups_set_group_type( $g, 'bar', true );
 137  
 138          $this->assertSame( 'foo', bp_groups_get_group_type( $g, true ) );
 139      }
 140  
 141  	public function test_groups_get_type_with_single_false() {
 142          $g = self::factory()->group->create( array( 'creator_id' => self::$u1 ) );
 143          bp_groups_register_group_type( 'foo' );
 144          bp_groups_register_group_type( 'bar' );
 145          bp_groups_set_group_type( $g, 'foo' );
 146          bp_groups_set_group_type( $g, 'bar', true );
 147  
 148          $this->assertEqualSets( array( 'foo', 'bar' ), bp_groups_get_group_type( $g, false ) );
 149      }
 150  
 151      public function test_groups_get_type_should_return_false_when_no_value_is_found() {
 152          $g = self::factory()->group->create( array( 'creator_id' => self::$u1 ) );
 153          bp_groups_register_group_type( 'foo' );
 154  
 155          $this->assertFalse( bp_groups_get_group_type( $g ) );
 156      }
 157  
 158      public function test_groups_remove_type_should_return_false_when_group_type_is_empty() {
 159          $this->assertFalse( bp_groups_remove_group_type( 9, '' ) );
 160      }
 161  
 162      public function test_groups_remove_type_should_return_false_when_group_type_is_invalid() {
 163          $this->assertFalse( bp_groups_remove_group_type( 9, 'foo' ) );
 164      }
 165  
 166      public function test_groups_remove_type_should_return_false_when_group_is_not_of_provided_type() {
 167          $g = self::factory()->group->create( array( 'creator_id' => self::$u1 ) );
 168          bp_groups_register_group_type( 'foo' );
 169          bp_groups_register_group_type( 'bar' );
 170          bp_groups_set_group_type( $g, 'foo' );
 171  
 172          $this->assertFalse( bp_groups_remove_group_type( $g, 'bar' ) );
 173          $this->assertEquals( array( 'foo' ), bp_groups_get_group_type( $g, false ) );
 174      }
 175  
 176      public function tests_groups_remove_type_should_return_true_on_successful_deletion() {
 177          $g = self::factory()->group->create( array( 'creator_id' => self::$u1 ) );
 178          bp_groups_register_group_type( 'foo' );
 179          bp_groups_register_group_type( 'bar' );
 180          bp_groups_set_group_type( $g, 'foo' );
 181          bp_groups_set_group_type( $g, 'bar', true );
 182  
 183          $this->assertTrue( bp_groups_remove_group_type( $g, 'foo' ) );
 184          $this->assertEquals( array( 'bar' ), bp_groups_get_group_type( $g, false ) );
 185      }
 186  
 187      public function test_groups_has_type_should_return_false_when_group_type_is_empty() {
 188          $this->assertFalse( bp_groups_has_group_type( 9, '' ) );
 189      }
 190  
 191      public function test_groups_has_type_should_return_false_when_group_type_is_invalid() {
 192          $this->assertFalse( bp_groups_has_group_type( 9, 'foo' ) );
 193      }
 194  
 195      public function test_groups_has_type_should_return_false_when_group_id_is_empty() {
 196          bp_groups_register_group_type( 'foo' );
 197  
 198          $this->assertFalse( bp_groups_has_group_type( '', 'foo' ) );
 199      }
 200  
 201      public function test_groups_has_type_should_return_false_when_group_is_not_of_provided_type() {
 202          $g = self::factory()->group->create( array( 'creator_id' => self::$u1 ) );
 203          bp_groups_register_group_type( 'foo' );
 204          bp_groups_register_group_type( 'bar' );
 205          bp_groups_set_group_type( $g, 'foo' );
 206  
 207          $this->assertFalse( bp_groups_has_group_type( $g, 'bar' ) );
 208      }
 209  
 210      public function test_groups_has_type_should_return_true_on_success() {
 211          $g = self::factory()->group->create( array( 'creator_id' => self::$u1 ) );
 212          bp_groups_register_group_type( 'foo' );
 213          bp_groups_register_group_type( 'bar' );
 214          bp_groups_set_group_type( $g, 'foo' );
 215          bp_groups_set_group_type( $g, 'bar', true );
 216  
 217          $this->assertTrue( bp_groups_has_group_type( $g, 'foo' ) );
 218          $this->assertEqualSets( array( 'bar', 'foo' ), bp_groups_get_group_type( $g, false ) );
 219      }
 220  
 221      /**
 222       * @group cache
 223       */
 224  	public function test_groups_get_type_should_hit_cache() {
 225          $g = self::factory()->group->create( array( 'creator_id' => self::$u1 ) );
 226          bp_groups_register_group_type( 'foo' );
 227          bp_groups_set_group_type( $g, 'foo' );
 228  
 229          global $wpdb;
 230  
 231          // Initial query. Should hit DB.
 232          bp_groups_get_group_type( $g );
 233          $num_queries = $wpdb->num_queries;
 234  
 235          // Next query should hit cache
 236          bp_groups_get_group_type( $g );
 237          $this->assertSame( $num_queries, $wpdb->num_queries );
 238      }
 239  
 240  	public function test_bp_groups_get_group_types_filter() {
 241          bp_groups_register_group_type( 'foo' );
 242          bp_groups_register_group_type( 'bar' );
 243  
 244          $types = bp_groups_get_group_types( array( 'name' => 'bar' ) );
 245  
 246          $this->assertEqualSets( array( 'bar' ), $types );
 247      }
 248  
 249      public function test_groups_get_type_should_not_return_unregistered_types() {
 250          $g = self::factory()->group->create( array( 'creator_id' => self::$u1 ) );
 251          bp_groups_register_group_type( 'foo' );
 252          bp_groups_set_group_type( $g, 'foo' );
 253  
 254          // Directly set a type that hasn't been registered.
 255          bp_set_object_terms( $g, 'ugh', 'bp_group_type', true );
 256  
 257          $type = bp_groups_get_group_type( $g, false );
 258          $this->assertEquals( array( 'foo' ), $type );
 259      }
 260  
 261      public function test_bp_groups_register_group_type_show_in_list_true_when_show_in_create_screen_true() {
 262          $object = bp_groups_register_group_type( 'foo', array(
 263              'show_in_create_screen' => true,
 264          ) );
 265  
 266          $this->assertTrue( $object->show_in_list );
 267      }
 268  
 269      public function test_bp_groups_register_group_type_show_in_list_false_when_show_in_create_screen_false() {
 270          $object = bp_groups_register_group_type( 'foo', array(
 271              'show_in_create_screen' => false,
 272          ) );
 273  
 274          $this->assertFalse( $object->show_in_list );
 275      }
 276  
 277      public function test_bp_groups_register_group_type_show_in_list_false_and_show_in_create_screen_true() {
 278          $object = bp_groups_register_group_type( 'foo', array(
 279              'show_in_create_screen' => true,
 280              'show_in_list' => false,
 281          ) );
 282  
 283          $this->assertFalse( $object->show_in_list );
 284      }
 285  
 286      public function test_bp_groups_set_group_type_should_remove_types_when_passing_an_empty_value() {
 287          $g = self::factory()->group->create( array( 'creator_id' => self::$u1 ) );
 288          bp_groups_register_group_type( 'foo' );
 289          bp_groups_set_group_type( $g, 'foo' );
 290  
 291          // Make sure it's set up.
 292          $this->assertSame( 'foo', bp_groups_get_group_type( $g ) );
 293  
 294          $this->assertSame( array(), bp_groups_set_group_type( $g, '' ) );
 295          $this->assertFalse( bp_groups_get_group_type( $g ) );
 296      }
 297  
 298      public function test_bp_groups_set_group_type_should_set_multiple_types_when_passing_array_of_types() {
 299          $g = self::factory()->group->create( array( 'creator_id' => self::$u1 ) );
 300          bp_groups_register_group_type( 'foo' );
 301          bp_groups_register_group_type( 'bar' );
 302  
 303          // Set multiple group types.
 304          $types = array( 'foo', 'bar' );
 305          bp_groups_set_group_type( $g, $types );
 306  
 307          // Assert!
 308          $this->assertEqualSets( $types, bp_groups_get_group_type( $g, false ) );
 309      }
 310  }


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