Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/27/2015 03:00:42 AM (9 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.