[ 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          update_user_status( $u1, 'spam', '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          update_user_status( $u1, 'spam', '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          add_filter( 'make_ham_user', array( $this, 'notification_filter_callback' ) );
 446  
 447          $u1 = self::factory()->user->create();
 448          $n = bp_core_process_spammer_status( $u1, 'ham' );
 449  
 450          remove_filter( 'make_ham_user', array( $this, 'notification_filter_callback' ) );
 451  
 452          $this->assertSame( 'make_ham_user', $this->filter_fired );
 453  
 454      }
 455  
 456      public function test_bp_core_process_spammer_status_bp_make_spam_user_filter() {
 457          add_filter( 'bp_make_spam_user', array( $this, 'notification_filter_callback' ) );
 458  
 459          $u1 = self::factory()->user->create();
 460          $n = bp_core_process_spammer_status( $u1, 'spam' );
 461  
 462          remove_filter( 'bp_make_spam_user', array( $this, 'notification_filter_callback' ) );
 463  
 464          $this->assertSame( 'bp_make_spam_user', $this->filter_fired );
 465  
 466      }
 467  
 468      public function test_bp_core_process_spammer_status_bp_make_ham_user_filter() {
 469          add_filter( 'bp_make_ham_user', array( $this, 'notification_filter_callback' ) );
 470  
 471          $u1 = self::factory()->user->create();
 472          $n = bp_core_process_spammer_status( $u1, 'ham' );
 473  
 474          remove_filter( 'bp_make_ham_user', array( $this, 'notification_filter_callback' ) );
 475  
 476          $this->assertSame( 'bp_make_ham_user', $this->filter_fired );
 477  
 478      }
 479  
 480  	public function notification_filter_callback( $value ) {
 481          $this->filter_fired = current_filter();
 482          return $value;
 483      }
 484  
 485      /**
 486       * @ticket BP6208
 487       *
 488       * Note - it's not possible to test this when registration is not configured properly,
 489       * because `bp_has_custom_signup_page()` stores its value in a static variable that cannot
 490       * be toggled.
 491       */
 492      public function test_wp_registration_url_should_return_bp_register_page_when_register_page_is_configured_properly() {
 493          $this->assertSame( bp_get_signup_page(), wp_registration_url() );
 494      }
 495  
 496      /**
 497       * @group bp_core_activate_signup
 498       */
 499  	public function test_bp_core_activate_signup_password() {
 500          global $wpdb;
 501  
 502  
 503          $signups = array( 'no-blog' =>
 504              array( 'signup_id' => self::factory()->signup->create( array(
 505                      'user_login'     => 'noblog',
 506                      'user_email'     => 'noblog@example.com',
 507                      'activation_key' => 'no-blog',
 508                      'meta' => array(
 509                          'field_1' => 'Foo Bar',
 510                          'password' => 'foobar',
 511                      ),
 512              ) ),
 513                  'password' => 'foobar',
 514              ),
 515          );
 516  
 517          if ( is_multisite() ) {
 518              if ( function_exists( 'wp_initialize_site' ) ) {
 519                  $this->setExpectedDeprecated( 'wpmu_new_blog' );
 520              }
 521  
 522              $signups['ms-blog'] = array( 'signup_id' => self::factory()->signup->create( array(
 523                      'user_login'     => 'msblog',
 524                      'user_email'     => 'msblog@example.com',
 525                      'domain'         => get_current_site()->domain,
 526                      'path'           => get_current_site()->path . 'ms-blog',
 527                      'title'          => 'Ding Dang',
 528                      'activation_key' => 'ms-blog',
 529                      'meta' => array(
 530                          'field_1'  => 'Ding Dang',
 531                          'password' => 'dingdang',
 532                      ),
 533                  ) ),
 534                  'password' => 'dingdang',
 535              );
 536          }
 537  
 538          // Neutralize db errors
 539          $suppress = $wpdb->suppress_errors();
 540  
 541          foreach ( $signups as $key => $data ) {
 542              $u = bp_core_activate_signup( $key );
 543  
 544              $this->assertEquals( get_userdata( $u )->user_pass, $data['password'] );
 545          }
 546  
 547          $wpdb->suppress_errors( $suppress );
 548      }
 549  
 550      /**
 551       * @ticket BP7461
 552       *
 553       * Test function before and after adding custom illegal names from WordPress.
 554       */
 555  	public function test_bp_core_get_illegal_names() {
 556  
 557          // Making sure BP custom illegals are in the array.
 558          $this->assertTrue( in_array( 'profile', bp_core_get_illegal_names(), true ) );
 559          $this->assertTrue( in_array( 'forums', bp_core_get_illegal_names(), true ) );
 560  
 561          add_filter( 'illegal_user_logins', array( $this, '_illegal_user_logins' ) );
 562  
 563          // Testing fake custom illegal names.
 564          $this->assertTrue( in_array( 'testuser', bp_core_get_illegal_names(), true ) );
 565          $this->assertTrue( in_array( 'admins', bp_core_get_illegal_names(), true ) );
 566          $this->assertFalse( in_array( 'buddypresss', bp_core_get_illegal_names(), true ) );
 567  
 568          // Making sure BP custom illegals are in the array after including the custom ones.
 569          $this->assertTrue( in_array( 'profile', bp_core_get_illegal_names(), true ) );
 570          $this->assertTrue( in_array( 'forums', bp_core_get_illegal_names(), true ) );
 571  
 572          remove_filter( 'illegal_user_logins', array( $this, '_illegal_user_logins' ) );
 573      }
 574  
 575  	public function _illegal_user_logins() {
 576          return array(
 577              'testuser',
 578              'admins',
 579              'buddypress',
 580          );
 581      }
 582  
 583      /**
 584       * @group bp_core_activate_signup
 585       */
 586      public function test_bp_core_activate_signup_should_add_user_role() {
 587          $key = 'test';
 588  
 589          // Create the signup.
 590          $this->factory->signup->create( array(
 591              'user_login'     => 'test',
 592              'user_email'     => 'test@example.com',
 593              'activation_key' => $key,
 594              'meta' => array(
 595                  'field_1' => 'Foo Bar',
 596                  'password' => 'foobar',
 597              ),
 598          ) );
 599  
 600          // Activate user.
 601          $user_id = bp_core_activate_signup( $key );
 602  
 603          // Assert that user has a role.
 604          $user = get_userdata( $user_id );
 605          $this->assertNotEmpty( $user->roles );
 606      }
 607  }


Generated: Tue Jul 16 01:01:43 2019 Cross-referenced by PHPXref 0.7.1