Make WordPress Core


Ignore:
Timestamp:
10/16/2015 09:04:12 PM (11 years ago)
Author:
wonderboymusic
Message:

Unit Tests: one $factory to rule them all, and it shall be static.

Using more than one instance of WP_UnitTest_Factory causes all kinds of craziness, due to out-of-sync internal generator sequences. Since we want to use setUpBeforeClass, we were creating ad hoc instances. To avoid that, we were injecting one static instance via Dependency Injection in wpSetUpBeforeClass. All tests should really use the static instance, so we will remove the instance prop $factory.

Replace $this->factory with self::$factory over 2000 times.
Rewrite all of the tests that were hard-coding dynamic values.

#YOLOFriday

File:
1 edited

Legend:

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

    r34172 r35225  
    2626
    2727        function test_remove_user_from_blog() {
    28                 $user1 = $this->factory->user->create_and_get();
    29                 $user2 = $this->factory->user->create_and_get();
    30 
    31                 $post_id = $this->factory->post->create( array( 'post_author' => $user1->ID ) );
     28                $user1 = self::$factory->user->create_and_get();
     29                $user2 = self::$factory->user->create_and_get();
     30
     31                $post_id = self::$factory->post->create( array( 'post_author' => $user1->ID ) );
    3232
    3333                remove_user_from_blog( $user1->ID, 1, $user2->ID );
     
    4343         */
    4444        function test_get_blogs_of_user() {
    45                 $user1_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
     45                $user1_id = self::$factory->user->create( array( 'role' => 'administrator' ) );
    4646
    4747                // Maintain a list of 6 total sites and include the primary network site.
    48                 $blog_ids = $this->factory->blog->create_many( 5, array( 'user_id' => $user1_id ) );
     48                $blog_ids = self::$factory->blog->create_many( 5, array( 'user_id' => $user1_id ) );
    4949                $blog_ids = array_merge( array( 1 ), $blog_ids );
    5050
     
    115115                global $wpdb;
    116116
    117                 $user1_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
     117                $user1_id = self::$factory->user->create( array( 'role' => 'administrator' ) );
    118118
    119119                $old_current = get_current_user_id();
     
    125125                $blog_ids = array();
    126126
    127                 $blog_ids = $this->factory->blog->create_many( 5 );
     127                $blog_ids = self::$factory->blog->create_many( 5 );
    128128                foreach ( $blog_ids as $blog_id ) {
    129129                        $this->assertInternalType( 'int', $blog_id );
     
    139139                global $wpdb;
    140140
    141                 $user1_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
    142                 $user2_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
     141                $user1_id = self::$factory->user->create( array( 'role' => 'administrator' ) );
     142                $user2_id = self::$factory->user->create( array( 'role' => 'administrator' ) );
    143143
    144144                $old_current = get_current_user_id();
     
    157157                $this->assertTrue( is_user_member_of_blog( $user1_id, $wpdb->blogid ) );
    158158
    159                 $blog_ids = $this->factory->blog->create_many( 5 );
     159                $blog_ids = self::$factory->blog->create_many( 5 );
    160160                foreach ( $blog_ids as $blog_id ) {
    161161                        $this->assertInternalType( 'int', $blog_id );
     
    198198         */
    199199        function test_is_user_spammy() {
    200                 $user_id = $this->factory->user->create( array(
     200                $user_id = self::$factory->user->create( array(
    201201                        'role' => 'author',
    202202                        'user_login' => 'testuser1',
     
    204204
    205205                $spam_username = (string) $user_id;
    206                 $spam_user_id = $this->factory->user->create( array(
     206                $spam_user_id = self::$factory->user->create( array(
    207207                        'role' => 'author',
    208208                        'user_login' => $spam_username,
     
    220220                global $wp_rewrite;
    221221
    222                 $this->factory->blog->create();
    223                 $user_id = $this->factory->user->create();
    224                 $this->factory->blog->create( array( 'user_id' => $user_id ) );
     222                self::$factory->blog->create();
     223                $user_id = self::$factory->user->create();
     224                self::$factory->blog->create( array( 'user_id' => $user_id ) );
    225225
    226226                $blogs = get_blogs_of_user( $user_id );
     
    262262                }
    263263
    264                 $user_id = $this->factory->user->create();
     264                $user_id = self::$factory->user->create();
    265265                grant_super_admin( $user_id );
    266266                revoke_super_admin( $user_id );
     
    279279                }
    280280
    281                 $user_id = $this->factory->user->create();
     281                $user_id = self::$factory->user->create();
    282282                grant_super_admin( $user_id );
    283283                revoke_super_admin( $user_id );
     
    298298                }
    299299
    300                 $user_id = $this->factory->user->create();
     300                $user_id = self::$factory->user->create();
    301301                grant_super_admin( $user_id );
    302302
     
    317317                }
    318318
    319                 $user_id = $this->factory->user->create();
     319                $user_id = self::$factory->user->create();
    320320
    321321                $this->assertFalse( is_super_admin( $user_id ) );
     
    330330
    331331                // Try with two users.
    332                 $second_user = $this->factory->user->create();
     332                $second_user = self::$factory->user->create();
    333333                $this->assertTrue( grant_super_admin( $user_id ) );
    334334                $this->assertTrue( grant_super_admin( $second_user ) );
     
    344344
    345345        public function test_numeric_string_user_id() {
    346                 $u = $this->factory->user->create();
     346                $u = self::$factory->user->create();
    347347
    348348                $u_string = (string) $u;
     
    362362         */
    363363        public function test_should_return_false_for_object_user_id() {
    364                 $u_obj = $this->factory->user->create_and_get();
     364                $u_obj = self::$factory->user->create_and_get();
    365365                $this->assertFalse( wpmu_delete_user( $u_obj ) );
    366366                $this->assertEquals( $u_obj->ID, username_exists( $u_obj->user_login ) );
Note: See TracChangeset for help on using the changeset viewer.