Make WordPress Core


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/user.php

    r34118 r33115  
    336336
    337337    /**
     338     * Test that usermeta cache is cleared after user deletion.
     339     *
     340     * @ticket 19500
     341     */
     342    function test_get_blogs_of_user() {
     343        // Logged out users don't have blogs.
     344        $this->assertEquals( array(), get_blogs_of_user( 0 ) );
     345
     346        $user_id = $this->factory->user->create( array( 'role' => 'subscriber' ) );
     347        $blogs = get_blogs_of_user( $user_id );
     348        $this->assertEquals( array( 1 ), array_keys( $blogs ) );
     349
     350        // Non-existent users don't have blogs.
     351        if ( is_multisite() )
     352            wpmu_delete_user( $user_id );
     353        else
     354            wp_delete_user( $user_id );
     355
     356        $user = new WP_User( $user_id );
     357        $this->assertFalse( $user->exists(), 'WP_User->exists' );
     358        $this->assertEquals( array(), get_blogs_of_user( $user_id ) );
     359    }
     360
     361    /**
     362     * Test that usermeta cache is cleared after user deletion.
     363     *
     364     * @ticket 19500
     365     */
     366    function test_is_user_member_of_blog() {
     367        $old_current = get_current_user_id();
     368
     369        $user_id = $this->factory->user->create( array( 'role' => 'subscriber' ) );
     370        wp_set_current_user( $user_id );
     371
     372        $this->assertTrue( is_user_member_of_blog() );
     373        $this->assertTrue( is_user_member_of_blog( 0, 0 ) );
     374        $this->assertTrue( is_user_member_of_blog( 0, get_current_blog_id() ) );
     375        $this->assertTrue( is_user_member_of_blog( $user_id ) );
     376        $this->assertTrue( is_user_member_of_blog( $user_id, get_current_blog_id() ) );
     377
     378        // Will only remove the user from the current site in multisite; this is desired
     379        // and will achieve the desired effect with is_user_member_of_blog().
     380        wp_delete_user( $user_id );
     381
     382        $this->assertFalse( is_user_member_of_blog( $user_id ) );
     383        $this->assertFalse( is_user_member_of_blog( $user_id, get_current_blog_id() ) );
     384
     385        wp_set_current_user( $old_current );
     386    }
     387
     388    /**
    338389     * ticket 19595
    339390     */
     
    406457    }
    407458
     459    function test_delete_user() {
     460        $user_id = $this->factory->user->create( array( 'role' => 'author' ) );
     461        $user = new WP_User( $user_id );
     462
     463        $post = array(
     464            'post_author' => $user_id,
     465            'post_status' => 'publish',
     466            'post_content' => rand_str(),
     467            'post_title' => rand_str(),
     468            'post_type' => 'post',
     469        );
     470
     471        // insert a post and make sure the ID is ok
     472        $post_id = wp_insert_post($post);
     473        $this->assertTrue(is_numeric($post_id));
     474        $this->assertTrue($post_id > 0);
     475
     476        $post = get_post( $post_id );
     477        $this->assertEquals( $post_id, $post->ID );
     478
     479        $post = array(
     480            'post_author' => $user_id,
     481            'post_status' => 'publish',
     482            'post_content' => rand_str(),
     483            'post_title' => rand_str(),
     484            'post_type' => 'nav_menu_item',
     485        );
     486
     487        // insert a post and make sure the ID is ok
     488        $nav_id = wp_insert_post($post);
     489        $this->assertTrue(is_numeric($nav_id));
     490        $this->assertTrue($nav_id > 0);
     491
     492        $post = get_post( $nav_id );
     493        $this->assertEquals( $nav_id, $post->ID );
     494
     495        wp_delete_user( $user_id );
     496        $user = new WP_User( $user_id );
     497        if ( is_multisite() )
     498            $this->assertTrue( $user->exists() );
     499        else
     500            $this->assertFalse( $user->exists() );
     501
     502        $this->assertNotNull( get_post( $post_id ) );
     503        $this->assertEquals( 'trash', get_post( $post_id )->post_status );
     504        // nav_menu_item is delete_with_user = false so the nav post should remain published.
     505        $this->assertNotNull( get_post( $nav_id ) );
     506        $this->assertEquals( 'publish', get_post( $nav_id )->post_status );
     507        wp_delete_post( $nav_id, true );
     508        $this->assertNull( get_post( $nav_id ) );
     509        wp_delete_post( $post_id, true );
     510        $this->assertNull( get_post( $post_id ) );
     511    }
     512
    408513    /**
    409514     * @ticket 13317
     
    444549        $user = WP_User::get_data_by( 'id', 99999 );
    445550        $this->assertEquals( false, $user );
     551    }
     552
     553    /**
     554     * @ticket 20447
     555     */
     556    function test_wp_delete_user_reassignment_clears_post_caches() {
     557        $user_id   = $this->factory->user->create();
     558        $reassign  = $this->factory->user->create();
     559        $post_id   = $this->factory->post->create( array( 'post_author' => $user_id ) );
     560
     561        get_post( $post_id ); // Ensure this post is in the cache.
     562
     563        wp_delete_user( $user_id, $reassign );
     564
     565        $post = get_post( $post_id );
     566        $this->assertEquals( $reassign, $post->post_author );
    446567    }
    447568
     
    680801    }
    681802
    682     /**
    683      * Testing wp_new_user_notification email statuses.
    684      *
    685      * @dataProvider data_wp_new_user_notifications
    686      * @ticket 33654
    687      */
    688     function test_wp_new_user_notification( $notify, $admin_email_sent_expected, $user_email_sent_expected ) {
    689         unset( $GLOBALS['phpmailer']->mock_sent );
    690 
    691         $was_admin_email_sent = false;
    692         $was_user_email_sent = false;
    693 
    694         $user = $this->factory->user->create( $this->user_data );
    695 
    696         wp_new_user_notification( $user, null, $notify );
    697 
    698         /*
    699          * Check to see if a notification email was sent to the
    700          * post author `blackburn@battlefield3.com` and and site admin `admin@example.org`.
    701          */
    702         if ( ! empty( $GLOBALS['phpmailer']->mock_sent ) ) {
    703             $was_admin_email_sent = ( isset( $GLOBALS['phpmailer']->mock_sent[0] ) && WP_TESTS_EMAIL == $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0] );
    704             $was_user_email_sent = ( isset( $GLOBALS['phpmailer']->mock_sent[1] ) && 'blackburn@battlefield3.com' == $GLOBALS['phpmailer']->mock_sent[1]['to'][0][0] );
    705         }
    706 
    707         $this->assertSame( $admin_email_sent_expected, $was_admin_email_sent, 'Admin email result was not as expected in test_wp_new_user_notification' );
    708         $this->assertSame( $user_email_sent_expected , $was_user_email_sent, 'User email result was not as expected in test_wp_new_user_notification' );
    709     }
    710 
    711     /**
    712      * Data provider for test_wp_new_user_notification().
    713      *
    714      * Passes the three available options for the $notify parameter and the expected email
    715      * emails sent status as a bool.
    716      *
    717      * @return array {
    718      *     @type array {
    719      *         @type string $post_args               The arguments that will merged with the $_POST array.
    720      *         @type bool $admin_email_sent_expected The expected result of whether an email was sent to the admin.
    721      *         @type bool $user_email_sent_expected  The expected result of whether an email was sent to the user.
    722      *     }
    723      * }
    724      */
    725     function data_wp_new_user_notifications() {
    726         return array(
    727             array(
    728                 '',
    729                 true,
    730                 false,
    731             ),
    732             array(
    733                 'admin',
    734                 true,
    735                 false,
    736             ),
    737             array(
    738                 'both',
    739                 true,
    740                 true,
    741             ),
    742         );
    743     }
    744 
    745     /**
    746      * Set up a user and try sending a notification using the old, deprecated
    747      * function signature `wp_new_user_notification( $user, 'plaintext_password' );`.
    748      *
    749      * @ticket 33654
    750      * @expectedDeprecated wp_new_user_notification
    751      */
    752     function test_wp_new_user_notification_old_signature_throws_deprecated_warning() {
    753         $user = $this->factory->user->create(
    754             array(
    755                 'role'       => 'author',
    756                 'user_login' => 'test_wp_new_user_notification',
    757                 'user_pass'  => 'password',
    758                 'user_email' => 'test@test.com',
    759             )
    760         );
    761 
    762         wp_new_user_notification( $user, 'this_is_deprecated' );
    763     }
    764803}
Note: See TracChangeset for help on using the changeset viewer.