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


Generated: Sun Apr 5 01:01:30 2020 Cross-referenced by PHPXref 0.7.1