[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/tests/phpunit/testcases/members/ -> class-bp-signup.php (source)

   1  <?php
   2  
   3  /**
   4   * @group members
   5   * @group signups
   6   * @group BP_Signup
   7   */
   8  class BP_Tests_BP_Signup extends BP_UnitTestCase {
   9      protected $signup_allowed;
  10  
  11  	public function setUp() {
  12  
  13          if ( is_multisite() ) {
  14              $this->signup_allowed = get_site_option( 'registration' );
  15              update_site_option( 'registration', 'all' );
  16          } else {
  17              bp_get_option( 'users_can_register' );
  18              bp_update_option( 'users_can_register', 1 );
  19          }
  20  
  21          parent::setUp();
  22      }
  23  
  24  	public function tearDown() {
  25          if ( is_multisite() ) {
  26              update_site_option( 'registration', $this->signup_allowed );
  27          } else {
  28              bp_update_option( 'users_can_register', $this->signup_allowed );
  29          }
  30  
  31          parent::tearDown();
  32      }
  33  
  34      /**
  35       * @group add
  36       */
  37  	public function test_add() {
  38          $time = bp_core_current_time();
  39          $args = array(
  40              'domain' => 'foo',
  41              'path' => 'bar',
  42              'title' => 'Foo bar',
  43              'user_login' => 'user1',
  44              'user_email' => 'user1@example.com',
  45              'registered' => $time,
  46              'activation_key' => '12345',
  47              'meta' => array(
  48                  'field_1' => 'Foo Bar',
  49                  'meta1' => 'meta2',
  50              ),
  51          );
  52  
  53          $signup = BP_Signup::add( $args );
  54          $this->assertNotEmpty( $signup );
  55  
  56          $s = new BP_Signup( $signup );
  57  
  58          // spot check
  59          $this->assertSame( $signup, $s->id );
  60          $this->assertSame( 'user1', $s->user_login );
  61          $this->assertSame( '12345', $s->activation_key );
  62      }
  63  
  64      /**
  65       * @group add
  66       */
  67      public function test_add_no_visibility_level_set_should_use_default_visiblity_level() {
  68          // Update field_1's default visibility to 'adminsonly'.
  69          bp_xprofile_update_field_meta( 1, 'default_visibility', 'adminsonly' );
  70  
  71          // Add new signup without a custom field visibility set for field_1.
  72          $signup = BP_Signup::add( array(
  73              'title' => 'Foo bar',
  74              'user_login' => 'user1',
  75              'user_email' => 'user1@example.com',
  76              'registered' => bp_core_current_time(),
  77              'activation_key' => '12345',
  78              'meta' => array(
  79                  'field_1' => 'Foo Bar',
  80                  'meta1' => 'meta2',
  81                  'password' => 'password',
  82  
  83                  /*
  84                   * Ensure we pass the field ID.
  85                   *
  86                   * See bp_core_activate_signup() and BP_Signup::add_backcompat().
  87                   */
  88                  'profile_field_ids' => '1'
  89              ),
  90          ) );
  91  
  92          // Activate the signup.
  93          $activate = BP_Signup::activate( (array) $signup );
  94  
  95          // Assert that field 1's visibility for the signup is still 'adminsonly'
  96          $vis = xprofile_get_field_visibility_level( 1, $activate['activated'][0] );
  97          $this->assertSame( 'adminsonly', $vis );
  98      }
  99  
 100      /**
 101       * @group get
 102       */
 103  	public function test_get_with_offset() {
 104          $s1 = self::factory()->signup->create();
 105          $s2 = self::factory()->signup->create();
 106          $s3 = self::factory()->signup->create();
 107  
 108          $ss = BP_Signup::get( array(
 109              'offset' => 1,
 110              'fields' => 'ids',
 111          ) );
 112  
 113          $this->assertEquals( array( $s2 ), $ss['signups'] );
 114      }
 115  
 116      /**
 117       * @group get
 118       */
 119  	public function test_get_with_number() {
 120          $s1 = self::factory()->signup->create();
 121          $s2 = self::factory()->signup->create();
 122          $s3 = self::factory()->signup->create();
 123  
 124          $ss = BP_Signup::get( array(
 125              'number' => 2,
 126              'fields' => 'ids',
 127          ) );
 128  
 129          $this->assertEquals( array( $s3, $s2 ), $ss['signups'] );
 130      }
 131  
 132      /**
 133       * @group get
 134       */
 135  	public function test_get_with_usersearch() {
 136          $s1 = self::factory()->signup->create( array(
 137              'user_email' => 'fghij@example.com',
 138          ) );
 139          $s2 = self::factory()->signup->create();
 140          $s3 = self::factory()->signup->create();
 141  
 142          $ss = BP_Signup::get( array(
 143              'usersearch' => 'ghi',
 144              'fields' => 'ids',
 145          ) );
 146  
 147          $this->assertEquals( array( $s1 ), $ss['signups'] );
 148      }
 149  
 150      /**
 151       * @group get
 152       */
 153  	public function test_get_with_orderby_email() {
 154          $s1 = self::factory()->signup->create( array(
 155              'user_email' => 'fghij@example.com',
 156          ) );
 157          $s2 = self::factory()->signup->create( array(
 158              'user_email' => 'abcde@example.com',
 159          ) );
 160          $s3 = self::factory()->signup->create( array(
 161              'user_email' => 'zzzzz@example.com',
 162          ) );
 163  
 164          $ss = BP_Signup::get( array(
 165              'orderby' => 'email',
 166              'number' => 3,
 167              'fields' => 'ids',
 168          ) );
 169  
 170          // default order is DESC.
 171          $this->assertEquals( array( $s3, $s1, $s2 ), $ss['signups'] );
 172      }
 173  
 174      /**
 175       * @group get
 176       */
 177  	public function test_get_with_orderby_email_asc() {
 178          $s1 = self::factory()->signup->create( array(
 179              'user_email' => 'fghij@example.com',
 180          ) );
 181          $s2 = self::factory()->signup->create( array(
 182              'user_email' => 'abcde@example.com',
 183          ) );
 184          $s3 = self::factory()->signup->create( array(
 185              'user_email' => 'zzzzz@example.com',
 186          ) );
 187  
 188          $ss = BP_Signup::get( array(
 189              'orderby' => 'email',
 190              'number' => 3,
 191              'order' => 'ASC',
 192              'fields' => 'ids',
 193          ) );
 194  
 195          $this->assertEquals( array( $s2, $s1, $s3 ), $ss['signups'] );
 196      }
 197  
 198      /**
 199       * @group get
 200       */
 201  	public function test_get_with_orderby_login_asc() {
 202          $s1 = self::factory()->signup->create( array(
 203              'user_login' => 'fghij',
 204          ) );
 205          $s2 = self::factory()->signup->create( array(
 206              'user_login' => 'abcde',
 207          ) );
 208          $s3 = self::factory()->signup->create( array(
 209              'user_login' => 'zzzzz',
 210          ) );
 211  
 212          $ss = BP_Signup::get( array(
 213              'orderby' => 'login',
 214              'number' => 3,
 215              'order' => 'ASC',
 216              'fields' => 'ids',
 217          ) );
 218  
 219          $this->assertEquals( array( $s2, $s1, $s3 ), $ss['signups'] );
 220      }
 221  
 222      /**
 223       * @group get
 224       */
 225  	public function test_get_with_orderby_registered_asc() {
 226          $now = time();
 227  
 228          $s1 = self::factory()->signup->create( array(
 229              'registered' => date( 'Y-m-d H:i:s', $now - 50 ),
 230          ) );
 231          $s2 = self::factory()->signup->create( array(
 232              'registered' => date( 'Y-m-d H:i:s', $now - 100 ),
 233          ) );
 234          $s3 = self::factory()->signup->create( array(
 235              'registered' => date( 'Y-m-d H:i:s', $now - 10 ),
 236          ) );
 237  
 238          $ss = BP_Signup::get( array(
 239              'orderby' => 'registered',
 240              'number' => 3,
 241              'order' => 'ASC',
 242              'fields' => 'ids',
 243          ) );
 244  
 245          $this->assertEquals( array( $s2, $s1, $s3 ), $ss['signups'] );
 246      }
 247  
 248      /**
 249       * @group get
 250       */
 251  	public function test_get_with_include() {
 252          $s1 = self::factory()->signup->create();
 253          $s2 = self::factory()->signup->create();
 254          $s3 = self::factory()->signup->create();
 255  
 256          $ss = BP_Signup::get( array(
 257              'include' => array( $s1, $s3 ),
 258              'fields' => 'ids',
 259          ) );
 260  
 261          $this->assertEquals( array( $s1, $s3 ), $ss['signups'] );
 262      }
 263  
 264      /**
 265       * @group get
 266       */
 267  	public function test_get_with_activation_key() {
 268          $s1 = self::factory()->signup->create( array(
 269              'activation_key' => 'foo',
 270          ) );
 271          $s2 = self::factory()->signup->create( array(
 272              'activation_key' => 'bar',
 273          ) );
 274          $s3 = self::factory()->signup->create( array(
 275              'activation_key' => 'baz',
 276          ) );
 277  
 278          $ss = BP_Signup::get( array(
 279              'activation_key' => 'bar',
 280              'fields' => 'ids',
 281          ) );
 282  
 283          $this->assertEquals( array( $s2 ), $ss['signups'] );
 284      }
 285  
 286      /**
 287       * @group get
 288       */
 289  	public function test_get_with_user_login() {
 290          $s1 = self::factory()->signup->create( array(
 291              'user_login' => 'aaaafoo',
 292          ) );
 293          $s2 = self::factory()->signup->create( array(
 294              'user_login' => 'zzzzfoo',
 295          ) );
 296          $s3 = self::factory()->signup->create( array(
 297              'user_login' => 'jjjjfoo',
 298          ) );
 299  
 300          $ss = BP_Signup::get( array(
 301              'user_login' => 'zzzzfoo',
 302              'fields' => 'ids',
 303          ) );
 304  
 305          $this->assertEquals( array( $s2 ), $ss['signups'] );
 306      }
 307  
 308      /**
 309       * @group activate
 310       */
 311  	public function test_activate_user_accounts() {
 312          $signups = array();
 313  
 314          $signups['accountone'] = self::factory()->signup->create( array(
 315              'user_login'     => 'accountone',
 316              'user_email'     => 'accountone@example.com',
 317              'activation_key' => 'activationkeyone',
 318          ) );
 319  
 320          $signups['accounttwo'] = self::factory()->signup->create( array(
 321              'user_login'     => 'accounttwo',
 322              'user_email'     => 'accounttwo@example.com',
 323              'activation_key' => 'activationkeytwo',
 324          ) );
 325  
 326          $signups['accountthree'] = self::factory()->signup->create( array(
 327              'user_login'     => 'accountthree',
 328              'user_email'     => 'accountthree@example.com',
 329              'activation_key' => 'activationkeythree',
 330          ) );
 331  
 332          $results = BP_Signup::activate( $signups );
 333          $this->assertNotEmpty( $results['activated'] );
 334  
 335          $users = array();
 336  
 337          foreach ( $signups as $login => $signup_id  ) {
 338              $users[ $login ] = get_user_by( 'login', $login );
 339          }
 340  
 341          $this->assertEqualSets( $results['activated'], wp_list_pluck( $users, 'ID' ) );
 342      }
 343  
 344      /**
 345       * @group get
 346       */
 347  	public function test_get_signup_ids_only() {
 348          $s1 = self::factory()->signup->create();
 349          $s2 = self::factory()->signup->create();
 350          $s3 = self::factory()->signup->create();
 351  
 352          $ss = BP_Signup::get( array(
 353              'number' => 3,
 354              'fields' => 'ids',
 355          ) );
 356  
 357          $this->assertEquals( array( $s3, $s2, $s1 ), $ss['signups'] );
 358      }
 359  
 360      /**
 361       * @group cache
 362       */
 363  	public function test_get_queries_should_be_cached() {
 364          global $wpdb;
 365  
 366          $s = self::factory()->signup->create();
 367  
 368          $found1 = BP_Signup::get(
 369              array(
 370                  'fields' => 'ids',
 371              )
 372          );
 373  
 374          $num_queries = $wpdb->num_queries;
 375  
 376          $found2 = BP_Signup::get(
 377              array(
 378                  'fields' => 'ids',
 379              )
 380          );
 381  
 382          $this->assertEqualSets( $found1, $found2 );
 383          $this->assertSame( $num_queries, $wpdb->num_queries );
 384      }
 385  
 386      /**
 387       * @group cache
 388       */
 389  	public function test_get_query_caches_should_be_busted_by_add() {
 390          $s1 = self::factory()->signup->create();
 391  
 392          $found1 = BP_Signup::get(
 393              array(
 394                  'fields' => 'ids',
 395              )
 396          );
 397          $this->assertEqualSets( array( $s1 ), $found1['signups'] );
 398  
 399          $s2 = self::factory()->signup->create();
 400          $found2 = BP_Signup::get(
 401              array(
 402                  'fields' => 'ids',
 403              )
 404          );
 405          $this->assertEqualSets( array( $s2 ), $found2['signups'] );
 406      }
 407  
 408      /**
 409       * @group cache
 410       */
 411      public function test_get_query_caches_should_be_busted_by_meta_update() {
 412          $time = bp_core_current_time();
 413  
 414          $args = array(
 415              'domain' => 'foo',
 416              'path' => 'bar',
 417              'title' => 'Foo bar',
 418              'user_login' => 'user1',
 419              'user_email' => 'user1@example.com',
 420              'registered' => $time,
 421              'activation_key' => '12345',
 422              'meta' => array(
 423                  'field_1' => 'Fozzie',
 424                  'meta1' => 'meta2',
 425              ),
 426          );
 427          $s1 = BP_Signup::add( $args );
 428  
 429          $args['meta']['field_1'] = 'Fozz';
 430          $s2 = BP_Signup::add( $args );
 431  
 432          // Should find both.
 433          $found1 = BP_Signup::get( array(
 434              'fields' => 'ids',
 435              'number'  => -1,
 436              'usersearch' => 'Fozz',
 437          ) );
 438          $this->assertEqualSets( array( $s1, $s2 ), $found1['signups'] );
 439  
 440          BP_Signup::update( array(
 441              'signup_id'  => $s1,
 442              'meta'       => array(
 443                  'field_1' => 'Fonzie'
 444              ),
 445          ) );
 446  
 447          $found2 = BP_Signup::get( array(
 448              'fields' => 'ids',
 449              'number'  => -1,
 450              'usersearch' => 'Fozz',
 451          ) );
 452  
 453          $this->assertEqualSets( array( $s2 ), $found2['signups'] );
 454      }
 455  
 456      /**
 457       * @group cache
 458       */
 459      public function test_get_query_caches_should_be_busted_by_delete() {
 460          global $wpdb;
 461          $time = bp_core_current_time();
 462  
 463          $args = array(
 464              'domain' => 'foo',
 465              'path' => 'bar',
 466              'title' => 'Foo bar',
 467              'user_login' => 'user1',
 468              'user_email' => 'user1@example.com',
 469              'registered' => $time,
 470              'activation_key' => '12345',
 471              'meta' => array(
 472                  'field_1' => 'Fozzie',
 473                  'meta1' => 'meta2',
 474              ),
 475          );
 476          $s1 = BP_Signup::add( $args );
 477  
 478          $args['meta']['field_1'] = 'Fozz';
 479          $s2 = BP_Signup::add( $args );
 480  
 481          // Should find both.
 482          $found1 = BP_Signup::get( array(
 483              'fields' => 'ids',
 484              'number'  => -1,
 485              'usersearch' => 'Fozz',
 486          ) );
 487          $this->assertEqualSets( array( $s1, $s2 ), $found1['signups'] );
 488  
 489          BP_Signup::delete( array( $s1 ) );
 490  
 491          $found2 = BP_Signup::get( array(
 492              'fields' => 'ids',
 493              'number'  => -1,
 494              'usersearch' => 'Fozz',
 495          ) );
 496  
 497          $this->assertEqualSets( array( $s2 ), $found2['signups'] );
 498      }
 499  
 500      /**
 501       * @group cache
 502       */
 503      public function test_get_query_caches_should_be_busted_by_activation() {
 504          $s1 = self::factory()->signup->create( array(
 505              'user_login'     => 'accountone',
 506              'user_email'     => 'accountone@example.com',
 507              'activation_key' => 'activationkeyone',
 508          ) );
 509  
 510          $s2 = self::factory()->signup->create( array(
 511              'user_login'     => 'accounttwo',
 512              'user_email'     => 'accounttwo@example.com',
 513              'activation_key' => 'activationkeytwo',
 514          ) );
 515          $found1 = BP_Signup::get(
 516              array(
 517                  'number' => -1,
 518                  'fields' => 'ids',
 519              )
 520          );
 521          $this->assertEqualSets( array( $s1, $s2 ), $found1['signups'] );
 522  
 523          $activate = BP_Signup::activate( (array) $s2 );
 524  
 525          $found2 = BP_Signup::get(
 526              array(
 527                  'number' => -1,
 528                  'fields' => 'ids',
 529              )
 530          );
 531          $this->assertEqualSets( array( $s1 ), $found2['signups'] );
 532      }
 533  
 534      /**
 535       * @group cache
 536       */
 537  	public function signup_objects_should_be_cached() {
 538          global $wpdb;
 539  
 540          $s1 = self::factory()->signup->create( array(
 541              'user_login'     => 'accountone',
 542              'user_email'     => 'accountone@example.com',
 543              'activation_key' => 'activationkeyone',
 544          ) );
 545  
 546          $found1 = new BP_Signup( $s1 );
 547  
 548          $num_queries = $wpdb->num_queries;
 549  
 550          // Object should be rebuilt from cache.
 551          $found2 = new BP_Signup( $s1 );
 552  
 553          // @TODO: This fails because "get_avatar()" in populate() results in db queries.
 554          $this->assertEquals( $found1, $found2 );
 555          $this->assertEquals( $num_queries, $wpdb->num_queries );
 556      }
 557  
 558      /**
 559       * @group cache
 560       */
 561      public function test_signup_object_caches_should_be_busted_by_activation() {
 562          $s1 = self::factory()->signup->create( array(
 563              'user_login'     => 'accountone',
 564              'user_email'     => 'accountone@example.com',
 565              'activation_key' => 'activationkeyone',
 566          ) );
 567  
 568          $found1 = new BP_Signup( $s1 );
 569          $this->assertEquals( $s1, $found1->id );
 570          $this->assertFalse( $found1->active );
 571  
 572          $activate = BP_Signup::activate( (array) $s1 );
 573  
 574          $found2 = new BP_Signup( $s1 );
 575          $this->assertEquals( $s1, $found2->id );
 576          $this->assertTrue( $found2->active );
 577  
 578      }
 579  }


Generated: Tue Sep 21 01:01:37 2021 Cross-referenced by PHPXref 0.7.1