Skip to:
Content

BuddyPress.org

Changeset 9561


Ignore:
Timestamp:
02/27/2015 03:00:42 AM (7 years ago)
Author:
boonebgorges
Message:

Don't create a current user by default in most unit tests.

The default state of our tests should be as a logged-out user. BP does pretty
different things with different kinds of logged-in users, so individual tests
should decide for themselves what the current user status should be.

Moreover, the creation of fixture users is fairly resource-intensive. Not
creating users when they're not needed makes the test suite run 5-10% faster.

See #6009.

Location:
trunk/tests/phpunit/testcases
Files:
26 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/activity/actions.php

    r9486 r9561  
    44 */
    55class BP_Tests_Activity_Actions extends BP_UnitTestCase {
    6     protected $old_current_user = 0;
    7 
    8     public function setUp() {
    9         parent::setUp();
    10 
    11         $this->old_current_user = get_current_user_id();
    12         $this->set_current_user( $this->factory->user->create( array( 'role' => 'subscriber' ) ) );
    13     }
    14 
    15     public function tearDown() {
    16         parent::tearDown();
    17         $this->set_current_user( $this->old_current_user );
    18     }
    196
    207    /**
  • trunk/tests/phpunit/testcases/activity/class.BP_Activity_Activity.php

    r9139 r9561  
    44 */
    55class BP_Tests_Activity_Class extends BP_UnitTestCase {
    6     protected $old_current_user = 0;
    7 
    8     public function setUp() {
    9         parent::setUp();
    10 
    11         $this->old_current_user = get_current_user_id();
    12         $this->set_current_user( $this->factory->user->create( array(
    13             'role' => 'subscriber',
    14         ) ) );
    15     }
    16 
    17     public function tearDown() {
    18         parent::tearDown();
    19         $this->set_current_user( $this->old_current_user );
    20     }
    216
    227    /**
     
    259244     */
    260245    public function test_get_with_display_comments_threaded() {
     246        $u = $this->factory->user->create();
     247
    261248        $now = time();
    262249        $a1 = $this->factory->activity->create( array(
     250            'user_id' => $u,
    263251            'content' => 'Life Rules',
    264252            'recorded_time' => date( 'Y-m-d H:i:s', $now ),
    265253        ) );
    266254        $a2 = $this->factory->activity->create( array(
     255            'user_id' => $u,
    267256            'content' => 'Life Drools',
    268257            'recorded_time' => date( 'Y-m-d H:i:s', $now - 100 ),
    269258        ) );
    270259        $a3 = bp_activity_new_comment( array(
     260            'user_id' => $u,
    271261            'activity_id' => $a1,
    272262            'content' => 'Candy is good',
  • trunk/tests/phpunit/testcases/activity/functions.php

    r9194 r9561  
    44 */
    55class BP_Tests_Activity_Functions extends BP_UnitTestCase {
    6     protected $old_current_user = 0;
    7 
    8     public function setUp() {
    9         parent::setUp();
    10 
    11         $this->old_current_user = get_current_user_id();
    12         $this->set_current_user( $this->factory->user->create( array( 'role' => 'subscriber' ) ) );
    13     }
    14 
    15     public function tearDown() {
    16         parent::tearDown();
    17         $this->set_current_user( $this->old_current_user );
    18     }
    196
    207    /**
     
    915902     */
    916903    public function test_bp_activity_new_comment_clear_comment_caches() {
    917         $a1 = $this->factory->activity->create();
     904        $u = $this->factory->user->create();
     905        $a1 = $this->factory->activity->create( array(
     906            'user_id' => $u,
     907        ) );
    918908        $a2 = bp_activity_new_comment( array(
    919909            'activity_id' => $a1,
    920910            'parent_id' => $a1,
    921911            'content' => 'foo',
    922             'user_id' => 1,
     912            'user_id' => $u,
    923913        ) );
    924914        $a3 = bp_activity_new_comment( array(
     
    926916            'parent_id' => $a2,
    927917            'content' => 'foo',
    928             'user_id' => 1,
     918            'user_id' => $u,
    929919        ) );
    930920        $a4 = bp_activity_new_comment( array(
     
    932922            'parent_id' => $a3,
    933923            'content' => 'foo',
    934             'user_id' => 1,
     924            'user_id' => $u,
    935925        ) );
    936926        $a5 = bp_activity_new_comment( array(
     
    938928            'parent_id' => $a3,
    939929            'content' => 'foo',
    940             'user_id' => 1,
     930            'user_id' => $u,
    941931        ) );
    942932
     
    954944            'parent_id' => $a4,
    955945            'content' => 'foo',
    956             'user_id' => 1,
     946            'user_id' => $u,
    957947        ) );
    958948
     
    966956     */
    967957    public function test_bp_activity_new_comment_clear_activity_caches() {
    968         $a1 = $this->factory->activity->create();
     958        $u = $this->factory->user->create();
     959        $a1 = $this->factory->activity->create( array(
     960            'user_id' => $u,
     961        ) );
    969962        $a2 = bp_activity_new_comment( array(
    970963            'activity_id' => $a1,
    971964            'parent_id' => $a1,
    972965            'content' => 'foo',
    973             'user_id' => 1,
     966            'user_id' => $u,
    974967        ) );
    975968        $a3 = bp_activity_new_comment( array(
     
    977970            'parent_id' => $a2,
    978971            'content' => 'foo',
    979             'user_id' => 1,
     972            'user_id' => $u,
    980973        ) );
    981974        $a4 = bp_activity_new_comment( array(
     
    983976            'parent_id' => $a3,
    984977            'content' => 'foo',
    985             'user_id' => 1,
     978            'user_id' => $u,
    986979        ) );
    987980        $a5 = bp_activity_new_comment( array(
     
    989982            'parent_id' => $a3,
    990983            'content' => 'foo',
    991             'user_id' => 1,
     984            'user_id' => $u,
    992985        ) );
    993986
     
    10211014            'parent_id' => $a4,
    10221015            'content' => 'foo',
    1023             'user_id' => 1,
     1016            'user_id' => $u,
    10241017        ) );
    10251018
     
    10351028     */
    10361029    public function test_bp_activity_delete_comment_clear_cache() {
     1030        $u = $this->factory->user->create();
    10371031        // add new activity update and comment to this update
    1038         $a1 = $this->factory->activity->create();
     1032        $a1 = $this->factory->activity->create( array(
     1033            'user_id' => $u,
     1034        ) );
    10391035        $a2 = bp_activity_new_comment( array(
    10401036            'activity_id' => $a1,
    10411037            'parent_id' => $a1,
    10421038            'content' => 'foo',
    1043             'user_id' => 1,
     1039            'user_id' => $u,
    10441040        ) );
    10451041
     
    10621058     */
    10631059    public function test_bp_activity_comment_on_deleted_activity() {
     1060        $u = $this->factory->user->create();
    10641061        $a = $this->factory->activity->create();
    10651062
     
    10701067            'parent_id' => $a,
    10711068            'content' => 'foo',
    1072             'user_id' => 1,
     1069            'user_id' => $u,
    10731070        ) );
    10741071
     
    10831080        $u = $this->factory->user->create();
    10841081        $a = $this->factory->activity->create();
     1082
     1083        // bp_activity_add_user_favorite() requires a logged-in user.
     1084        $current_user = bp_loggedin_user_id();
     1085        $this->set_current_user( $u );
    10851086
    10861087        $this->assertTrue( bp_activity_add_user_favorite( $a, $u ) );
     
    10891090        $this->assertSame( array( $a ), bp_activity_get_user_favorites( $u ) );
    10901091        $this->assertEquals( 1, bp_activity_get_meta( $a, 'favorite_count' ) );
     1092
     1093        $this->set_current_user( $current_user );
    10911094    }
    10921095
     
    10981101        $u = $this->factory->user->create();
    10991102        $a = $this->factory->activity->create();
     1103
     1104        // bp_activity_add_user_favorite() requires a logged-in user.
     1105        $current_user = bp_loggedin_user_id();
     1106        $this->set_current_user( $u );
    11001107        $this->assertTrue( bp_activity_add_user_favorite( $a, $u ) );
     1108
     1109        $this->set_current_user( $current_user );
    11011110    }
    11021111
     
    11101119        $a = $this->factory->activity->create();
    11111120
     1121        // bp_activity_add_user_favorite() requires a logged-in user.
     1122        $current_user = bp_loggedin_user_id();
     1123        $this->set_current_user( $u1 );
     1124
    11121125        // Only favorite for user 1
    11131126        bp_activity_add_user_favorite( $a, $u1 );
     
    11161129        $this->assertFalse( bp_activity_remove_user_favorite( $a, $u2 ) );
    11171130        $this->assertEquals( 1, bp_activity_get_meta( $a, 'favorite_count' ) );
     1131
     1132        $this->set_current_user( $current_user );
    11181133    }
    11191134
  • trunk/tests/phpunit/testcases/activity/template.php

    r9475 r9561  
    44 */
    55class BP_Tests_Activity_Template extends BP_UnitTestCase {
    6     protected $old_current_user = 0;
    7 
    8     public function setUp() {
    9         parent::setUp();
    10 
    11         $this->old_current_user = get_current_user_id();
    12         $this->set_current_user( $this->factory->user->create( array( 'role' => 'subscriber' ) ) );
    13     }
    14 
    15     public function tearDown() {
    16         parent::tearDown();
    17         $this->set_current_user( $this->old_current_user );
    18     }
    196
    207    /**
     
    2310    public function test_user_can_delete() {
    2411        $bp = buddypress();
     12        $u = $this->factory->user->create();
     13        $original_user = bp_loggedin_user_id();
     14        $this->set_current_user( $u );
    2515
    2616        $a = $this->factory->activity->create( array(
    2717            'type' => 'activity_update',
     18            'user_id' => $u,
    2819        ) );
    2920
     
    3122        $activity = $this->factory->activity->get_object_by_id( $a );
    3223        $this->assertTrue( bp_activity_user_can_delete( $activity ) );
    33 
    34         // Stash original user
    35         $original_user = get_current_user_id();
    3624
    3725        // Logged-out user can't delete
     
    10795        ) );
    10896
     97        $current_user = bp_loggedin_user_id();
     98        $this->set_current_user( $user_id );
     99
    109100        bp_activity_add_user_favorite( $a1, $user_id );
    110101        bp_activity_add_user_favorite( $a2, $user_id );
     102
     103        $this->set_current_user( $current_user );
    111104
    112105        // groan. It sucks that you have to invoke the global
     
    12241217        add_filter( 'bp_disable_blogforum_comments', '__return_false' );
    12251218
     1219        $u = $this->factory->user->create();
     1220
    12261221        $now = time();
    12271222        $a1 = $this->factory->activity->create( array(
     
    12301225            'type' => 'new_blog_post',
    12311226            'recorded_time' => date( 'Y-m-d H:i:s', $now ),
     1227            'user_id' => $u,
    12321228        ) );
    12331229        $a2 = $this->factory->activity->create( array(
     
    12361232            'type' => 'new_blog_comment',
    12371233            'recorded_time' => date( 'Y-m-d H:i:s', $now - 100 ),
     1234            'user_id' => $u,
    12381235        ) );
    12391236
     
    12441241            'content' => 'Candy is good',
    12451242            'recorded_time' => date( 'Y-m-d H:i:s', $now - 200 ),
     1243            'user_id' => $u,
    12461244        ) );
    12471245
     
    12511249            'type' => 'activity_update',
    12521250            'recorded_time' => date( 'Y-m-d H:i:s', $now - 300 ),
     1251            'user_id' => $u,
    12531252        ) );
    12541253
     
    12591258            'content' => 'Candy is great',
    12601259            'recorded_time' => date( 'Y-m-d H:i:s', $now - 400 ),
     1260            'user_id' => $u,
    12611261        ) );
    12621262        global $activities_template;
  • trunk/tests/phpunit/testcases/blogs/class-bp-blogs-blog.php

    r9354 r9561  
    66 */
    77class BP_Tests_BP_Blogs_Blog_TestCases extends BP_UnitTestCase {
    8     public function setUp() {
    9         parent::setUp();
    10     }
    11 
    12     public function tearDown() {
    13         parent::tearDown();
    14     }
    15 
    168    public function test_get_with_search_terms() {
    179        if ( ! is_multisite() ) {
  • trunk/tests/phpunit/testcases/core/avatars.php

    r9455 r9561  
    55 */
    66class BP_Tests_Avatars extends BP_UnitTestCase {
    7     protected $old_current_user = 0;
    8 
    97    private $params = array();
    10 
    11     public function setUp() {
    12         parent::setUp();
    13 
    14         $this->old_current_user = get_current_user_id();
    15         $this->administrator    = $this->factory->user->create( array( 'role' => 'administrator' ) );
    16         wp_set_current_user( $this->administrator );
    17     }
    18 
    19     public function tearDown() {
    20         parent::tearDown();
    21         wp_set_current_user( $this->old_current_user );
    22     }
    238
    249    private function clean_existing_avatars( $type = 'user' ) {
     
    5944        }
    6045
     46        $u = $this->factory->user->create();
     47
    6148        // get BP root blog's upload directory data
    6249        $upload_dir = wp_upload_dir();
     
    6451        // create new subsite
    6552        $blog_id = $this->factory->blog->create( array(
    66             'user_id' => $this->administrator,
     53            'user_id' => $u,
    6754            'title'   => 'Test Title',
    6855            'path'    => '/path' . rand() . time() . '/',
  • trunk/tests/phpunit/testcases/core/class-bp-button.php

    r9139 r9561  
    66 */
    77class BP_Tests_BP_Button extends BP_UnitTestCase {
    8     protected $old_current_user;
    9 
    10     public function setUp() {
    11         $this->old_current_user = get_current_user_id();
    12         parent::setUp();
    13     }
    14 
    15     public function tearDown() {
    16         $this->set_current_user( $this->old_current_user );
    17         parent::tearDown();
    18     }
    19 
    208    /**
    219     * @group block_self
  • trunk/tests/phpunit/testcases/core/class-bp-core-user.php

    r9139 r9561  
    66 */
    77class BP_Tests_BP_Core_User_TestCases extends BP_UnitTestCase {
    8     protected $old_current_user = 0;
    9 
    10     public function setUp() {
    11         parent::setUp();
    12     }
    13 
    14     public function tearDown() {
    15         parent::tearDown();
    16     }
    17 
    188    /**
    199     * @expectedDeprecated BP_Core_User::get_users
  • trunk/tests/phpunit/testcases/core/class-bp-user-query.php

    r9533 r9561  
    66 */
    77class BP_Tests_BP_User_Query_TestCases extends BP_UnitTestCase {
    8     protected $old_current_user = 0;
    9 
    10     public function setUp() {
    11         parent::setUp();
    12 
    13         $this->old_current_user = get_current_user_id();
    14         $this->set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
    15     }
    16 
    17     public function tearDown() {
    18         parent::tearDown();
    19         $this->set_current_user( $this->old_current_user );
    20     }
    21 
    228    /**
    239     * Checks that user_id returns friends
  • trunk/tests/phpunit/testcases/friends/class-bp-friends-friendship.php

    r9525 r9561  
    55 */
    66class BP_Tests_BP_Friends_Friendship_TestCases extends BP_UnitTestCase {
    7     protected $old_current_user = 0;
    8 
    9     public function setUp() {
    10         parent::setUp();
    11 
    12         $this->old_current_user = get_current_user_id();
    13         $this->set_current_user( $this->factory->user->create( array( 'role' => 'subscriber' ) ) );
    14     }
    15 
    16     public function tearDown() {
    17         parent::tearDown();
    18         $this->set_current_user( $this->old_current_user );
    19     }
    20 
    217    public function test_search_friends() {
    228        $u1 = $this->factory->user->create();
  • trunk/tests/phpunit/testcases/friends/functions.php

    r9173 r9561  
    1212        parent::setUp();
    1313        $this->filter_fired = '';
    14     }
    15 
    16     public function tearDown() {
    17         parent::tearDown();
    1814    }
    1915
  • trunk/tests/phpunit/testcases/groups/class-bp-group-extension.php

    r9139 r9561  
    88 */
    99class BP_Tests_Group_Extension_TestCases extends BP_UnitTestCase {
    10     public function setUp() {
    11         parent::setUp();
    12     }
    13 
    14     public function tearDown() {
    15         parent::tearDown();
    16     }
    17 
    1810    public function test_parse_legacy_properties() {
    1911        $class_name = 'BPTest_Group_Extension_Parse_Legacy_Properties';
  • trunk/tests/phpunit/testcases/groups/class-bp-group-member-query.php

    r9139 r9561  
    55 */
    66class BP_Tests_BP_Group_Member_Query_TestCases extends BP_UnitTestCase {
    7     public function setUp() {
    8         $this->current_user = get_current_user_id();
    9         $this->set_current_user( 0 );
    10         parent::setUp();
    11     }
    12 
    13     public function tearDown() {
    14         $this->set_current_user( $this->current_user );
    15         parent::tearDown();
    16     }
    177
    188    /**
  • trunk/tests/phpunit/testcases/groups/class-bp-groups-group.php

    r9483 r9561  
    55 */
    66class BP_Tests_BP_Groups_Group_TestCases extends BP_UnitTestCase {
    7     public function setUp() {
    8         parent::setUp();
    9     }
    10 
    11     public function tearDown() {
    12         parent::tearDown();
    13     }
    147
    158    /** __construct()  ***************************************************/
  • trunk/tests/phpunit/testcases/groups/class-bp-groups-member.php

    r9300 r9561  
    55 */
    66class BP_Tests_BP_Groups_Member_TestCases extends BP_UnitTestCase {
    7     public function setUp() {
    8         parent::setUp();
    9     }
    10 
    11     public function tearDown() {
    12         parent::tearDown();
    13     }
    14 
    157    public static function invite_user_to_group( $user_id, $group_id, $inviter_id ) {
    168        $invite                = new BP_Groups_Member;
  • trunk/tests/phpunit/testcases/groups/functions.php

    r9471 r9561  
    66 */
    77class BP_Tests_Groups_Functions extends BP_UnitTestCase {
    8     protected $old_current_user_id = 0;
    9 
    10     public function setUp() {
    11         parent::setUp();
    12         $this->old_current_user = get_current_user_id();
    13     }
    14 
    15     public function tearDown() {
    16         parent::tearDown();
    17         $this->set_current_user( $this->old_current_user );
    18     }
    19 
    208    public function test_creating_new_group_as_authenticated_user() {
    219        $u = $this->factory->user->create();
  • trunk/tests/phpunit/testcases/groups/template.php

    r9417 r9561  
    55 */
    66class BP_Tests_Groups_Template extends BP_UnitTestCase {
    7     public function setUp() {
    8         parent::setUp();
    9     }
    10 
    11     public function tearDown() {
    12         parent::tearDown();
    13     }
    14 
    157    /**
    168     * Integration test to make sure meta_query is getting passed through
  • trunk/tests/phpunit/testcases/members/functions.php

    r9471 r9561  
    44 */
    55class BP_Tests_Members_Functions extends BP_UnitTestCase {
    6     protected $old_current_user = 0;
    7 
    8     public function setUp() {
    9         parent::setUp();
    10 
    11         $this->old_current_user = get_current_user_id();
    12         $this->set_current_user( $this->factory->user->create( array( 'role' => 'subscriber' ) ) );
    13     }
    14 
    15     public function tearDown() {
    16         parent::tearDown();
    17         $this->set_current_user( $this->old_current_user );
    18     }
    196
    207    /**
  • trunk/tests/phpunit/testcases/members/template.php

    r9417 r9561  
    44 */
    55class BP_Tests_Members_Template extends BP_UnitTestCase {
    6     protected $old_current_user = 0;
    7 
    8     public function setUp() {
    9         parent::setUp();
    10 
    11         $this->old_current_user = get_current_user_id();
    12         $new_user = $this->factory->user->create( array( 'role' => 'administrator' ) );
    13         $this->set_current_user( $new_user );
    14 
    15     }
    16 
    17     public function tearDown() {
    18         parent::tearDown();
    19         $this->set_current_user( $this->old_current_user );
    20     }
    21 
    226    public function test_bp_has_members_include_on_user_page() {
    237        $u1 = $this->factory->user->create();
  • trunk/tests/phpunit/testcases/notifications/template.php

    r9486 r9561  
    55 */
    66class BP_Tests_Notifications_Template extends BP_UnitTestCase {
    7     public function setUp() {
    8         parent::setUp();
    9     }
    10 
    11     public function tearDown() {
    12         parent::tearDown();
    13     }
    14 
    157    /**
    168     * @group pagination
  • trunk/tests/phpunit/testcases/routing/anonymous.php

    r8958 r9561  
    44 */
    55class BP_Tests_Routing_Anonymous extends BP_UnitTestCase {
    6     protected $old_current_user = 0;
    7 
    8     public function setUp() {
    9         parent::setUp();
    10 
    11         $this->old_current_user = get_current_user_id();
    12         $this->set_current_user( 0 );
    13     }
    14 
    15     public function tearDown() {
    16         parent::tearDown();
    17         $this->set_current_user( $this->old_current_user );
    18     }
    19 
    206    function test_wordpress_page() {
    217        $this->go_to( '/' );
  • trunk/tests/phpunit/testcases/routing/core.php

    r8958 r9561  
    1818        $this->set_current_user( $this->old_current_user );
    1919    }
    20 
    2120    function test_wordpress_page() {
    2221        $this->go_to( '/' );
  • trunk/tests/phpunit/testcases/routing/url.php

    r9364 r9561  
    44 */
    55class BP_Tests_URL extends BP_UnitTestCase {
    6     protected $old_current_user = 0;
    7 
    8     public function setUp() {
    9         parent::setUp();
    10 
    11         $this->old_current_user = get_current_user_id();
    12         $this->set_current_user( $this->factory->user->create( array( 'role' => 'subscriber' ) ) );
    13     }
    14 
    15     public function tearDown() {
    16         parent::tearDown();
    17         $this->set_current_user( $this->old_current_user );
    18     }
    19 
    206    function test_bp_core_ajax_url() {
    217        $forced = force_ssl_admin();
  • trunk/tests/phpunit/testcases/xprofile/class-bp-xprofile-field-type.php

    r8958 r9561  
    55 */
    66class BP_Tests_XProfile_Field_Type extends BP_UnitTestCase {
    7     public function setUp() {
    8         parent::setUp();
    9     }
    10 
    11     public function tearDown() {
    12         parent::tearDown();
    13     }
    14 
    157    public function test_unregistered_field_type_returns_textbox() {
    168        $field = bp_xprofile_create_field_type( 'fakeyfield' );
  • trunk/tests/phpunit/testcases/xprofile/class-bp-xprofile-field.php

    r9324 r9561  
    55 */
    66class BP_Tests_BP_XProfile_Field_TestCases extends BP_UnitTestCase {
    7     public function setUp() {
    8         parent::setUp();
    9     }
    10 
    11     public function tearDown() {
    12         parent::tearDown();
    13     }
    14 
    157    public function test_can_delete_save() {
    168        $group = $this->factory->xprofile_group->create();
  • trunk/tests/phpunit/testcases/xprofile/functions.php

    r9139 r9561  
    66 */
    77class BP_Tests_XProfile_Functions extends BP_UnitTestCase {
    8     public function setUp() {
    9         parent::setUp();
    10     }
    11 
    12     public function tearDown() {
    13         parent::tearDown();
    14     }
    15 
    168    public function test_get_hidden_field_types_for_user_loggedout() {
    179        $duser = $this->factory->user->create();
Note: See TracChangeset for help on using the changeset viewer.