[ 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->assertContains( $s1, $ss['signups'] );
 262          $this->assertContains( $s3, $ss['signups'] );
 263      }
 264  
 265      /**
 266       * @group get
 267       */
 268  	public function test_get_with_activation_key() {
 269          $s1 = self::factory()->signup->create( array(
 270              'activation_key' => 'foo',
 271          ) );
 272          $s2 = self::factory()->signup->create( array(
 273              'activation_key' => 'bar',
 274          ) );
 275          $s3 = self::factory()->signup->create( array(
 276              'activation_key' => 'baz',
 277          ) );
 278  
 279          $ss = BP_Signup::get( array(
 280              'activation_key' => 'bar',
 281              'fields' => 'ids',
 282          ) );
 283  
 284          $this->assertEquals( array( $s2 ), $ss['signups'] );
 285      }
 286  
 287      /**
 288       * @group get
 289       */
 290  	public function test_get_with_user_login() {
 291          $s1 = self::factory()->signup->create( array(
 292              'user_login' => 'aaaafoo',
 293          ) );
 294          $s2 = self::factory()->signup->create( array(
 295              'user_login' => 'zzzzfoo',
 296          ) );
 297          $s3 = self::factory()->signup->create( array(
 298              'user_login' => 'jjjjfoo',
 299          ) );
 300  
 301          $ss = BP_Signup::get( array(
 302              'user_login' => 'zzzzfoo',
 303              'fields' => 'ids',
 304          ) );
 305  
 306          $this->assertEquals( array( $s2 ), $ss['signups'] );
 307      }
 308  
 309      /**
 310       * @group activate
 311       */
 312  	public function test_activate_user_accounts() {
 313          $signups = array();
 314  
 315          $signups['accountone'] = self::factory()->signup->create( array(
 316              'user_login'     => 'accountone',
 317              'user_email'     => 'accountone@example.com',
 318              'activation_key' => 'activationkeyone',
 319          ) );
 320  
 321          $signups['accounttwo'] = self::factory()->signup->create( array(
 322              'user_login'     => 'accounttwo',
 323              'user_email'     => 'accounttwo@example.com',
 324              'activation_key' => 'activationkeytwo',
 325          ) );
 326  
 327          $signups['accountthree'] = self::factory()->signup->create( array(
 328              'user_login'     => 'accountthree',
 329              'user_email'     => 'accountthree@example.com',
 330              'activation_key' => 'activationkeythree',
 331          ) );
 332  
 333          $results = BP_Signup::activate( $signups );
 334          $this->assertNotEmpty( $results['activated'] );
 335  
 336          $users = array();
 337  
 338          foreach ( $signups as $login => $signup_id  ) {
 339              $users[ $login ] = get_user_by( 'login', $login );
 340          }
 341  
 342          $this->assertEqualSets( $results['activated'], wp_list_pluck( $users, 'ID' ) );
 343      }
 344  
 345      /**
 346       * @group get
 347       */
 348  	public function test_get_signup_ids_only() {
 349          $s1 = self::factory()->signup->create();
 350          $s2 = self::factory()->signup->create();
 351          $s3 = self::factory()->signup->create();
 352  
 353          $ss = BP_Signup::get( array(
 354              'number' => 3,
 355              'fields' => 'ids',
 356          ) );
 357  
 358          $this->assertEquals( array( $s3, $s2, $s1 ), $ss['signups'] );
 359      }
 360  
 361      /**
 362       * @group cache
 363       */
 364  	public function test_get_queries_should_be_cached() {
 365          global $wpdb;
 366  
 367          $s = self::factory()->signup->create();
 368  
 369          $found1 = BP_Signup::get(
 370              array(
 371                  'fields' => 'ids',
 372              )
 373          );
 374  
 375          $num_queries = $wpdb->num_queries;
 376  
 377          $found2 = BP_Signup::get(
 378              array(
 379                  'fields' => 'ids',
 380              )
 381          );
 382  
 383          $this->assertEqualSets( $found1, $found2 );
 384          $this->assertSame( $num_queries, $wpdb->num_queries );
 385      }
 386  
 387      /**
 388       * @group cache
 389       */
 390  	public function test_get_query_caches_should_be_busted_by_add() {
 391          $s1 = self::factory()->signup->create();
 392  
 393          $found1 = BP_Signup::get(
 394              array(
 395                  'fields' => 'ids',
 396              )
 397          );
 398          $this->assertEqualSets( array( $s1 ), $found1['signups'] );
 399  
 400          $s2 = self::factory()->signup->create();
 401          $found2 = BP_Signup::get(
 402              array(
 403                  'fields' => 'ids',
 404              )
 405          );
 406          $this->assertEqualSets( array( $s2 ), $found2['signups'] );
 407      }
 408  
 409      /**
 410       * @group cache
 411       */
 412      public function test_get_query_caches_should_be_busted_by_meta_update() {
 413          $time = bp_core_current_time();
 414  
 415          $args = array(
 416              'domain' => 'foo',
 417              'path' => 'bar',
 418              'title' => 'Foo bar',
 419              'user_login' => 'user1',
 420              'user_email' => 'user1@example.com',
 421              'registered' => $time,
 422              'activation_key' => '12345',
 423              'meta' => array(
 424                  'field_1' => 'Fozzie',
 425                  'meta1' => 'meta2',
 426              ),
 427          );
 428          $s1 = BP_Signup::add( $args );
 429  
 430          $args['meta']['field_1'] = 'Fozz';
 431          $s2 = BP_Signup::add( $args );
 432  
 433          // Should find both.
 434          $found1 = BP_Signup::get( array(
 435              'fields' => 'ids',
 436              'number'  => -1,
 437              'usersearch' => 'Fozz',
 438          ) );
 439          $this->assertEqualSets( array( $s1, $s2 ), $found1['signups'] );
 440  
 441          BP_Signup::update( array(
 442              'signup_id'  => $s1,
 443              'meta'       => array(
 444                  'field_1' => 'Fonzie'
 445              ),
 446          ) );
 447  
 448          $found2 = BP_Signup::get( array(
 449              'fields' => 'ids',
 450              'number'  => -1,
 451              'usersearch' => 'Fozz',
 452          ) );
 453  
 454          $this->assertEqualSets( array( $s2 ), $found2['signups'] );
 455      }
 456  
 457      /**
 458       * @group cache
 459       */
 460      public function test_get_query_caches_should_be_busted_by_delete() {
 461          global $wpdb;
 462          $time = bp_core_current_time();
 463  
 464          $args = array(
 465              'domain' => 'foo',
 466              'path' => 'bar',
 467              'title' => 'Foo bar',
 468              'user_login' => 'user1',
 469              'user_email' => 'user1@example.com',
 470              'registered' => $time,
 471              'activation_key' => '12345',
 472              'meta' => array(
 473                  'field_1' => 'Fozzie',
 474                  'meta1' => 'meta2',
 475              ),
 476          );
 477          $s1 = BP_Signup::add( $args );
 478  
 479          $args['meta']['field_1'] = 'Fozz';
 480          $s2 = BP_Signup::add( $args );
 481  
 482          // Should find both.
 483          $found1 = BP_Signup::get( array(
 484              'fields' => 'ids',
 485              'number'  => -1,
 486              'usersearch' => 'Fozz',
 487          ) );
 488          $this->assertEqualSets( array( $s1, $s2 ), $found1['signups'] );
 489  
 490          BP_Signup::delete( array( $s1 ) );
 491  
 492          $found2 = BP_Signup::get( array(
 493              'fields' => 'ids',
 494              'number'  => -1,
 495              'usersearch' => 'Fozz',
 496          ) );
 497  
 498          $this->assertEqualSets( array( $s2 ), $found2['signups'] );
 499      }
 500  
 501      /**
 502       * @group cache
 503       */
 504      public function test_get_query_caches_should_be_busted_by_activation() {
 505          $s1 = self::factory()->signup->create( array(
 506              'user_login'     => 'accountone',
 507              'user_email'     => 'accountone@example.com',
 508              'activation_key' => 'activationkeyone',
 509          ) );
 510  
 511          $s2 = self::factory()->signup->create( array(
 512              'user_login'     => 'accounttwo',
 513              'user_email'     => 'accounttwo@example.com',
 514              'activation_key' => 'activationkeytwo',
 515          ) );
 516          $found1 = BP_Signup::get(
 517              array(
 518                  'number' => -1,
 519                  'fields' => 'ids',
 520              )
 521          );
 522          $this->assertEqualSets( array( $s1, $s2 ), $found1['signups'] );
 523  
 524          $activate = BP_Signup::activate( (array) $s2 );
 525  
 526          $found2 = BP_Signup::get(
 527              array(
 528                  'number' => -1,
 529                  'fields' => 'ids',
 530              )
 531          );
 532          $this->assertEqualSets( array( $s1 ), $found2['signups'] );
 533      }
 534  
 535      /**
 536       * @group cache
 537       */
 538  	public function signup_objects_should_be_cached() {
 539          global $wpdb;
 540  
 541          $s1 = self::factory()->signup->create( array(
 542              'user_login'     => 'accountone',
 543              'user_email'     => 'accountone@example.com',
 544              'activation_key' => 'activationkeyone',
 545          ) );
 546  
 547          $found1 = new BP_Signup( $s1 );
 548  
 549          $num_queries = $wpdb->num_queries;
 550  
 551          // Object should be rebuilt from cache.
 552          $found2 = new BP_Signup( $s1 );
 553  
 554          // @TODO: This fails because "get_avatar()" in populate() results in db queries.
 555          $this->assertEquals( $found1, $found2 );
 556          $this->assertEquals( $num_queries, $wpdb->num_queries );
 557      }
 558  
 559      /**
 560       * @group cache
 561       */
 562      public function test_signup_object_caches_should_be_busted_by_activation() {
 563          $s1 = self::factory()->signup->create( array(
 564              'user_login'     => 'accountone',
 565              'user_email'     => 'accountone@example.com',
 566              'activation_key' => 'activationkeyone',
 567          ) );
 568  
 569          $found1 = new BP_Signup( $s1 );
 570          $this->assertEquals( $s1, $found1->id );
 571          $this->assertFalse( $found1->active );
 572  
 573          $activate = BP_Signup::activate( (array) $s1 );
 574  
 575          $found2 = new BP_Signup( $s1 );
 576          $this->assertEquals( $s1, $found2->id );
 577          $this->assertTrue( $found2->active );
 578  
 579      }
 580  
 581      public function test_bp_core_signup_send_validation_email_should_increment_sent_count() {
 582          $activation_key = wp_generate_password( 32, false );
 583          $user_email     = 'accountone@example.com';
 584          $s1             = self::factory()->signup->create( array(
 585              'user_login'     => 'accountone',
 586              'user_email'     => $user_email,
 587              'activation_key' => $activation_key
 588          ) );
 589  
 590          $signup = new BP_Signup( $s1 );
 591          $this->assertEquals( 0, $signup->count_sent );
 592  
 593          bp_core_signup_send_validation_email( 0, $user_email, $activation_key );
 594  
 595          $signup = new BP_Signup( $s1 );
 596          $this->assertEquals( 1, $signup->count_sent );
 597      }
 598  }


Generated: Thu Nov 21 01:00:57 2024 Cross-referenced by PHPXref 0.7.1