[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/tests/phpunit/testcases/members/ -> functions.php (source)

   1  <?php
   2  /**
   3   * @group members
   4   */
   5  class BP_Tests_Members_Functions extends BP_UnitTestCase {
   6  
   7      /**
   8       * @ticket BP4915
   9       * @group bp_core_delete_account
  10       */
  11  	public function test_bp_core_delete_account() {
  12          // Stash
  13          $current_user = get_current_user_id();
  14          $deletion_disabled = bp_disable_account_deletion();
  15  
  16          // Create an admin for testing
  17          $admin_user = self::factory()->user->create( array( 'role' => 'administrator' ) );
  18          $this->grant_super_admin( $admin_user );
  19  
  20          // 1. Admin can delete user account
  21          $this->set_current_user( $admin_user );
  22          $user1 = self::factory()->user->create( array( 'role' => 'subscriber' ) );
  23          bp_core_delete_account( $user1 );
  24          $maybe_user = new WP_User( $user1 );
  25          $this->assertEquals( 0, $maybe_user->ID );
  26          unset( $maybe_user );
  27          $this->restore_admins();
  28  
  29          // 2. Admin cannot delete superadmin account
  30          $user2 = self::factory()->user->create( array( 'role' => 'administrator' ) );
  31          $this->grant_super_admin( $user2 );
  32          bp_core_delete_account( $user2 );
  33          $maybe_user = new WP_User( $user2 );
  34          $this->assertNotEquals( 0, $maybe_user->ID );
  35          unset( $maybe_user );
  36  
  37          // User cannot delete other's account
  38          $user3 = self::factory()->user->create( array( 'role' => 'subscriber' ) );
  39          $user4 = self::factory()->user->create( array( 'role' => 'subscriber' ) );
  40          $this->set_current_user( $user3 );
  41          bp_core_delete_account( $user4 );
  42          $maybe_user = new WP_User( $user4 );
  43          $this->assertNotEquals( 0, $maybe_user->ID );
  44          unset( $maybe_user );
  45  
  46          // Cleanup
  47          $this->set_current_user( $current_user );
  48          bp_update_option( 'bp-disable-account-deletion', $deletion_disabled );
  49      }
  50  
  51      /**
  52       * @group object_cache
  53       * @group bp_core_get_directory_pages
  54       */
  55      public function test_bp_core_get_user_domain_after_directory_page_update() {
  56          // Generate user
  57          $user_id = self::factory()->user->create( array( 'role' => 'subscriber' ) );
  58  
  59          // Set object cache first for user domain
  60          $user_domain = bp_core_get_user_domain( $user_id );
  61  
  62          // Now change the members directory slug
  63          $pages = bp_core_get_directory_pages();
  64          $members_page = get_post( $pages->members->id );
  65          $members_page->post_name = 'new-members-slug';
  66          wp_update_post( $members_page );
  67  
  68          // Go back to members directory page and recheck user domain
  69          $this->go_to( trailingslashit( home_url( 'new-members-slug' ) ) );
  70          $user = new WP_User( $user_id );
  71  
  72          $this->assertSame( home_url( 'new-members-slug' ) . '/' . $user->user_nicename . '/', bp_core_get_user_domain( $user_id ) );
  73      }
  74  
  75      /**
  76       * @group bp_core_get_user_displayname
  77       */
  78      public function test_bp_core_get_user_displayname_empty_username() {
  79          $this->assertFalse( bp_core_get_user_displayname( '' ) );
  80      }
  81  
  82      /**
  83       * @group bp_core_get_user_displayname
  84       */
  85      public function test_bp_core_get_user_displayname_translate_username() {
  86          $u = self::factory()->user->create();
  87  
  88          $user = new WP_User( $u );
  89  
  90          $found = bp_core_get_user_displayname( $u );
  91          $this->assertNotEmpty( $found );
  92          $this->assertSame( $found, bp_core_get_user_displayname( $user->user_login ) );
  93      }
  94  
  95      /**
  96       * @group bp_core_get_user_displayname
  97       */
  98      public function test_bp_core_get_user_displayname_bad_username() {
  99          $this->assertFalse( bp_core_get_user_displayname( 'i_dont_exist' ) );
 100      }
 101  
 102      /**
 103       * @group bp_core_get_user_displayname
 104       */
 105      public function test_bp_core_get_user_displayname_xprofile_exists() {
 106          $xprofile_is_active = bp_is_active( 'xprofile' );
 107          buddypress()->active_components['xprofile'] = '1';
 108  
 109          $u = self::factory()->user->create();
 110          xprofile_set_field_data( 1, $u, 'Foo Foo' );
 111  
 112          $this->assertSame( 'Foo Foo', bp_core_get_user_displayname( $u ) );
 113  
 114          if ( ! $xprofile_is_active ) {
 115              unset( buddypress()->active_components['xprofile'] );
 116          }
 117      }
 118  
 119      /**
 120       * @group bp_core_get_user_displaynames
 121       */
 122      public function test_bp_core_get_user_displayname_arrays_all_bad_entries() {
 123          $this->assertSame( array(), bp_core_get_user_displaynames( array( 0, 'foo', ) ) );
 124      }
 125  
 126      /**
 127       * @group bp_core_get_user_displaynames
 128       */
 129      public function test_bp_core_get_user_displaynames_all_uncached() {
 130          $u1 = self::factory()->user->create();
 131          $u2 = self::factory()->user->create();
 132          xprofile_set_field_data( 1, $u1, 'Foo' );
 133          xprofile_set_field_data( 1, $u2, 'Bar' );
 134  
 135          $expected = array(
 136              $u1 => 'Foo',
 137              $u2 => 'Bar',
 138          );
 139  
 140          $this->assertSame( $expected, bp_core_get_user_displaynames( array( $u1, $u2, ) ) );
 141      }
 142  
 143      /**
 144       * @group bp_core_get_user_displaynames
 145       */
 146      public function test_bp_core_get_user_displaynames_one_not_in_xprofile() {
 147          $u1 = self::factory()->user->create();
 148          $u2 = self::factory()->user->create( array(
 149              'display_name' => 'Bar',
 150          ) );
 151          xprofile_set_field_data( 1, $u1, 'Foo' );
 152  
 153          $expected = array(
 154              $u1 => 'Foo',
 155              $u2 => 'Bar',
 156          );
 157  
 158          $this->assertSame( $expected, bp_core_get_user_displaynames( array( $u1, $u2, ) ) );
 159      }
 160  
 161      /**
 162       * @group bp_members_migrate_signups
 163       */
 164  	public function test_bp_members_migrate_signups_standard() {
 165          $u = self::factory()->user->create();
 166          $u_obj = new WP_User( $u );
 167  
 168          // Fake an old-style registration
 169          $key = wp_generate_password( 32, false );
 170          update_user_meta( $u, 'activation_key', $key );
 171  
 172          global $wpdb;
 173          $wpdb->update(
 174              $wpdb->users,
 175              array( 'user_status' => '2', ),
 176              array( 'ID' => $u, ),
 177              array( '%d', ),
 178              array( '%d', )
 179          );
 180          clean_user_cache( $u );
 181  
 182          bp_members_migrate_signups();
 183  
 184          $found = BP_Signup::get();
 185  
 186          // Use email address as a sanity check
 187          $found_email = isset( $found['signups'][0]->user_email ) ? $found['signups'][0]->user_email : '';
 188          $this->assertSame( $u_obj->user_email, $found_email );
 189  
 190          // Check that activation keys match
 191          $found_key = isset( $found['signups'][0]->activation_key ) ? $found['signups'][0]->activation_key : '';
 192          $this->assertSame( $key, $found_key );
 193      }
 194  
 195      /**
 196       * @group bp_members_migrate_signups
 197       */
 198      public function test_bp_members_migrate_signups_activation_key_but_user_status_0() {
 199          $u = self::factory()->user->create();
 200          $u_obj = new WP_User( $u );
 201  
 202          // Fake an old-style registration
 203          $key = wp_generate_password( 32, false );
 204          update_user_meta( $u, 'activation_key', $key );
 205  
 206          // ...but ensure that user_status is 0. This mimics the
 207          // behavior of certain plugins that disrupt the BP registration
 208          // flow
 209          global $wpdb;
 210          $wpdb->update(
 211              $wpdb->users,
 212              array( 'user_status' => '0', ),
 213              array( 'ID' => $u, ),
 214              array( '%d', ),
 215              array( '%d', )
 216          );
 217          clean_user_cache( $u );
 218  
 219          bp_members_migrate_signups();
 220  
 221          // No migrations should have taken place
 222          $found = BP_Signup::get();
 223          $this->assertEmpty( $found['total'] );
 224      }
 225  
 226      /**
 227       * @group bp_members_migrate_signups
 228       */
 229      public function test_bp_members_migrate_signups_no_activation_key_but_user_status_2() {
 230          $u = self::factory()->user->create();
 231          $u_obj = new WP_User( $u );
 232  
 233          // Fake an old-style registration but without an activation key
 234          global $wpdb;
 235          $wpdb->update(
 236              $wpdb->users,
 237              array( 'user_status' => '2', ),
 238              array( 'ID' => $u, ),
 239              array( '%d', ),
 240              array( '%d', )
 241          );
 242          clean_user_cache( $u );
 243  
 244          bp_members_migrate_signups();
 245  
 246          // Use email address as a sanity check
 247          $found = BP_Signup::get();
 248          $found_email = isset( $found['signups'][0]->user_email ) ? $found['signups'][0]->user_email : '';
 249          $this->assertSame( $u_obj->user_email, $found_email );
 250      }
 251  
 252      /**
 253       * @group bp_last_activity_migrate
 254       * @expectedIncorrectUsage update_user_meta( $user_id, 'last_activity' )
 255       * @expectedIncorrectUsage get_user_meta( $user_id, 'last_activity' )
 256       */
 257  	public function test_bp_last_activity_migrate() {
 258          // We explicitly do not want last_activity created, so use the
 259          // WP factory methods
 260          $u1 = self::factory()->user->create();
 261          $u2 = self::factory()->user->create();
 262          $u3 = self::factory()->user->create();
 263  
 264          $time = time();
 265          $t1 = date( 'Y-m-d H:i:s', $time - 50 );
 266          $t2 = date( 'Y-m-d H:i:s', $time - 500 );
 267          $t3 = date( 'Y-m-d H:i:s', $time - 5000 );
 268  
 269          update_user_meta( $u1, 'last_activity', $t1 );
 270          update_user_meta( $u2, 'last_activity', $t2 );
 271          update_user_meta( $u3, 'last_activity', $t3 );
 272  
 273          // Create an existing entry in last_activity to test no dupes
 274          global $wpdb;
 275          $bp = buddypress();
 276          $wpdb->query( $wpdb->prepare(
 277              "INSERT INTO {$bp->members->table_name_last_activity}
 278                  (`user_id`, `component`, `type`, `action`, `content`, `primary_link`, `item_id`, `date_recorded` ) VALUES
 279                  ( %d, %s, %s, %s, %s, %s, %d, %s )",
 280              $u2, $bp->members->id, 'last_activity', '', '', '', 0, $t1
 281          ) );
 282  
 283          bp_last_activity_migrate();
 284  
 285          $expected = array(
 286              $u1 => $t1,
 287              $u2 => $t2,
 288              $u3 => $t3,
 289          );
 290  
 291          $found = array(
 292              $u1 => '',
 293              $u2 => '',
 294              $u3 => '',
 295          );
 296  
 297          foreach ( $found as $uid => $v ) {
 298              $found[ $uid ] = bp_get_user_last_activity( $uid );
 299          }
 300  
 301          $this->assertSame( $expected, $found );
 302      }
 303  
 304      /**
 305       * @group bp_core_get_userid_from_nicename
 306       */
 307  	public function test_bp_core_get_userid_from_nicename_failure() {
 308          $this->assertSame( NULL, bp_core_get_userid_from_nicename( 'non_existent_user' ) );
 309      }
 310  
 311      /**
 312       * @group bp_update_user_last_activity
 313       */
 314  	public function test_bp_last_activity_multi_network() {
 315  
 316          // Filter the usermeta key
 317          add_filter( 'bp_get_user_meta_key', array( $this, 'filter_usermeta_key' ) );
 318  
 319          // We explicitly do not want last_activity created, so use the
 320          // WP factory methods
 321          $user = self::factory()->user->create();
 322          $time = date( 'Y-m-d H:i:s', time() - 50 );
 323  
 324          // Update last user activity
 325          bp_update_user_last_activity( $user, $time );
 326  
 327          // Setup parameters to assert to be the same
 328          $expected = $time;
 329          $found    = bp_get_user_meta( $user, 'last_activity', true );
 330  
 331          $this->assertSame( $expected, $found );
 332      }
 333  
 334      /**
 335       * @group bp_update_user_last_activity
 336       * @global object $wpdb
 337       * @param  string $key
 338       * @return string
 339       */
 340  	public function filter_usermeta_key( $key ) {
 341          global $wpdb;
 342          return $wpdb->prefix . $key;
 343      }
 344  
 345      /**
 346       * @group bp_core_process_spammer_status
 347       */
 348  	public function test_bp_core_process_spammer_status() {
 349          if ( is_multisite() ) {
 350              $this->markTestSkipped();
 351          }
 352  
 353          $bp = buddypress();
 354          $displayed_user = $bp->displayed_user;
 355  
 356          $u1 = self::factory()->user->create();
 357          $bp->displayed_user->id = $u1;
 358  
 359          // Spam the user
 360          bp_core_process_spammer_status( $u1, 'spam' );
 361  
 362          $this->assertTrue( bp_is_user_spammer( $u1 ) );
 363  
 364          // Unspam the user
 365          bp_core_process_spammer_status( $u1, 'ham' );
 366  
 367          $this->assertFalse( bp_is_user_spammer( $u1 ) );
 368  
 369          // Reset displayed user
 370          $bp->displayed_user = $displayed_user;
 371      }
 372  
 373      /**
 374       * @group bp_core_process_spammer_status
 375       */
 376      public function test_bp_core_process_spammer_status_ms_bulk_spam() {
 377          if ( ! is_multisite() ) {
 378              $this->markTestSkipped();
 379          }
 380  
 381          $bp = buddypress();
 382          $displayed_user = $bp->displayed_user;
 383  
 384          $u1 = self::factory()->user->create();
 385          $bp->displayed_user->id = $u1;
 386  
 387          // Bulk spam in network admin uses update_user_status
 388          bp_core_update_member_status( $u1, '1' );
 389  
 390          $this->assertTrue( bp_is_user_spammer( $u1 ) );
 391  
 392          // Unspam the user
 393          bp_core_process_spammer_status( $u1, 'ham' );
 394  
 395          $this->assertFalse( bp_is_user_spammer( $u1 ) );
 396  
 397          // Reset displayed user
 398          $bp->displayed_user = $displayed_user;
 399      }
 400  
 401      /**
 402       * @group bp_core_process_spammer_status
 403       */
 404      public function test_bp_core_process_spammer_status_ms_bulk_ham() {
 405          if ( ! is_multisite() ) {
 406              $this->markTestSkipped();
 407          }
 408  
 409          $bp = buddypress();
 410          $displayed_user = $bp->displayed_user;
 411  
 412          $u1 = self::factory()->user->create();
 413          $bp->displayed_user->id = $u1;
 414  
 415          // Spam the user
 416          bp_core_process_spammer_status( $u1, 'spam' );
 417  
 418          $this->assertTrue( bp_is_user_spammer( $u1 ) );
 419  
 420          // Bulk unspam in network admin uses update_user_status
 421          bp_core_update_member_status( $u1, '0' );
 422  
 423          $this->assertFalse( bp_is_user_spammer( $u1 ) );
 424  
 425          // Reset displayed user
 426          $bp->displayed_user = $displayed_user;
 427      }
 428  
 429      /**
 430       * @group bp_core_process_spammer_status
 431       */
 432      public function test_bp_core_process_spammer_status_make_spam_user_filter() {
 433          add_filter( 'make_spam_user', array( $this, 'notification_filter_callback' ) );
 434  
 435          $u1 = self::factory()->user->create();
 436          $n = bp_core_process_spammer_status( $u1, 'spam' );
 437  
 438          remove_filter( 'make_spam_user', array( $this, 'notification_filter_callback' ) );
 439  
 440          $this->assertSame( 'make_spam_user', $this->filter_fired );
 441  
 442      }
 443  
 444      public function test_bp_core_process_spammer_status_make_ham_user_filter() {
 445          $u1 = self::factory()->user->create();
 446          $s  = bp_core_process_spammer_status( $u1, 'spam' );
 447  
 448          add_filter( 'make_ham_user', array( $this, 'notification_filter_callback' ) );
 449  
 450          $h = bp_core_process_spammer_status( $u1, 'ham' );
 451  
 452          remove_filter( 'make_ham_user', array( $this, 'notification_filter_callback' ) );
 453  
 454          $this->assertSame( 'make_ham_user', $this->filter_fired );
 455  
 456      }
 457  
 458      public function test_bp_core_process_spammer_status_bp_make_spam_user_filter() {
 459          add_filter( 'bp_make_spam_user', array( $this, 'notification_filter_callback' ) );
 460  
 461          $u1 = self::factory()->user->create();
 462          $n = bp_core_process_spammer_status( $u1, 'spam' );
 463  
 464          remove_filter( 'bp_make_spam_user', array( $this, 'notification_filter_callback' ) );
 465  
 466          $this->assertSame( 'bp_make_spam_user', $this->filter_fired );
 467  
 468      }
 469  
 470      public function test_bp_core_process_spammer_status_bp_make_ham_user_filter() {
 471          add_filter( 'bp_make_ham_user', array( $this, 'notification_filter_callback' ) );
 472  
 473          $u1 = self::factory()->user->create();
 474          $n = bp_core_process_spammer_status( $u1, 'ham' );
 475  
 476          remove_filter( 'bp_make_ham_user', array( $this, 'notification_filter_callback' ) );
 477  
 478          $this->assertSame( 'bp_make_ham_user', $this->filter_fired );
 479  
 480      }
 481  
 482  	public function notification_filter_callback( $value ) {
 483          $this->filter_fired = current_filter();
 484          return $value;
 485      }
 486  
 487      /**
 488       * @ticket BP6208
 489       *
 490       * Note - it's not possible to test this when registration is not configured properly,
 491       * because `bp_has_custom_signup_page()` stores its value in a static variable that cannot
 492       * be toggled.
 493       */
 494      public function test_wp_registration_url_should_return_bp_register_page_when_register_page_is_configured_properly() {
 495          $this->assertSame( bp_get_signup_page(), wp_registration_url() );
 496      }
 497  
 498      /**
 499       * @group bp_core_activate_signup
 500       */
 501  	public function test_bp_core_activate_signup_password() {
 502          global $wpdb;
 503  
 504  
 505          $signups = array( 'no-blog' =>
 506              array( 'signup_id' => self::factory()->signup->create( array(
 507                      'user_login'     => 'noblog',
 508                      'user_email'     => 'noblog@example.com',
 509                      'activation_key' => 'no-blog',
 510                      'meta' => array(
 511                          'field_1' => 'Foo Bar',
 512                          'password' => 'foobar',
 513                      ),
 514              ) ),
 515                  'password' => 'foobar',
 516              ),
 517          );
 518  
 519          if ( is_multisite() ) {
 520              if ( function_exists( 'wp_initialize_site' ) ) {
 521                  $this->setExpectedDeprecated( 'wpmu_new_blog' );
 522              }
 523  
 524              $signups['ms-blog'] = array( 'signup_id' => self::factory()->signup->create( array(
 525                      'user_login'     => 'msblog',
 526                      'user_email'     => 'msblog@example.com',
 527                      'domain'         => get_current_site()->domain,
 528                      'path'           => get_current_site()->path . 'ms-blog',
 529                      'title'          => 'Ding Dang',
 530                      'activation_key' => 'ms-blog',
 531                      'meta' => array(
 532                          'field_1'  => 'Ding Dang',
 533                          'password' => 'dingdang',
 534                      ),
 535                  ) ),
 536                  'password' => 'dingdang',
 537              );
 538          }
 539  
 540          // Neutralize db errors
 541          $suppress = $wpdb->suppress_errors();
 542  
 543          foreach ( $signups as $key => $data ) {
 544              $u = bp_core_activate_signup( $key );
 545  
 546              $this->assertEquals( get_userdata( $u )->user_pass, $data['password'] );
 547          }
 548  
 549          $wpdb->suppress_errors( $suppress );
 550      }
 551  
 552      /**
 553       * @ticket BP7461
 554       *
 555       * Test function before and after adding custom illegal names from WordPress.
 556       */
 557  	public function test_bp_core_get_illegal_names() {
 558  
 559          // Making sure BP custom illegals are in the array.
 560          $this->assertTrue( in_array( 'profile', bp_core_get_illegal_names(), true ) );
 561          $this->assertTrue( in_array( 'forums', bp_core_get_illegal_names(), true ) );
 562  
 563          add_filter( 'illegal_user_logins', array( $this, '_illegal_user_logins' ) );
 564  
 565          // Testing fake custom illegal names.
 566          $this->assertTrue( in_array( 'testuser', bp_core_get_illegal_names(), true ) );
 567          $this->assertTrue( in_array( 'admins', bp_core_get_illegal_names(), true ) );
 568          $this->assertFalse( in_array( 'buddypresss', bp_core_get_illegal_names(), true ) );
 569  
 570          // Making sure BP custom illegals are in the array after including the custom ones.
 571          $this->assertTrue( in_array( 'profile', bp_core_get_illegal_names(), true ) );
 572          $this->assertTrue( in_array( 'forums', bp_core_get_illegal_names(), true ) );
 573  
 574          remove_filter( 'illegal_user_logins', array( $this, '_illegal_user_logins' ) );
 575      }
 576  
 577  	public function _illegal_user_logins() {
 578          return array(
 579              'testuser',
 580              'admins',
 581              'buddypress',
 582          );
 583      }
 584  
 585      /**
 586       * @group bp_core_activate_signup
 587       */
 588      public function test_bp_core_activate_signup_should_add_user_role() {
 589          $key = 'test';
 590  
 591          // Create the signup.
 592          $this->factory->signup->create( array(
 593              'user_login'     => 'test',
 594              'user_email'     => 'test@example.com',
 595              'activation_key' => $key,
 596              'meta' => array(
 597                  'field_1' => 'Foo Bar',
 598                  'password' => 'foobar',
 599              ),
 600          ) );
 601  
 602          // Activate user.
 603          $user_id = bp_core_activate_signup( $key );
 604  
 605          // Assert that user has a role.
 606          $user = get_userdata( $user_id );
 607          $this->assertNotEmpty( $user->roles );
 608      }
 609  }


Generated: Mon Nov 18 01:01:37 2019 Cross-referenced by PHPXref 0.7.1