Make WordPress Core

Changeset 35197


Ignore:
Timestamp:
10/15/2015 07:28:40 AM (11 years ago)
Author:
wonderboymusic
Message:

Unit Tests: add/upgrade the fixtures in Tests_User_Query.

See #30017, #33968.

File:
1 edited

Legend:

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

    r35123 r35197  
    66 */
    77class Tests_User_Query extends WP_UnitTestCase {
     8        protected static $author_ids;
     9        protected static $sub_ids;
     10        protected static $editor_ids;
     11        protected static $contrib_id;
     12        protected static $admin_ids;
    813
    914        protected $user_id;
    1015
    11         function setUp() {
    12                 parent::setUp();
     16        public static function wpSetUpBeforeClass( $factory ) {
     17                self::$author_ids = $factory->user->create_many( 4, array(
     18                        'role' => 'author'
     19                ) );
     20
     21                self::$sub_ids = $factory->user->create_many( 2, array(
     22                        'role' => 'subscriber',
     23                ) );
     24
     25                self::$editor_ids = $factory->user->create_many( 3, array(
     26                        'role' => 'editor',
     27                ) );
     28
     29                self::$contrib_id = $factory->user->create( array(
     30                        'role' => 'contributor',
     31                ) );
     32
     33                self::$admin_ids = $factory->user->create_many( 2, array(
     34                        'role' => 'administrator',
     35                ) );
     36        }
     37
     38        public static function wpTearDownAfterClass() {
     39                $sets = array(
     40                        self::$author_ids,
     41                        self::$sub_ids,
     42                        self::$editor_ids,
     43                        array( self::$contrib_id ),
     44                        self::$admin_ids
     45                );
     46
     47                foreach ( $sets as $set ) {
     48                        foreach ( $set as $id ) {
     49                                if ( is_multisite() ) {
     50                                        wpmu_delete_user( $id );
     51                                } else {
     52                                        wp_delete_user( $id );
     53                                }
     54                        }
     55                }
    1356        }
    1457
     
    3275
    3376        public function test_include_single() {
    34                 $users = $this->factory->user->create_many( 2 );
    3577                $q = new WP_User_Query( array(
    3678                        'fields' => '',
    37                         'include' => $users[0],
     79                        'include' => self::$author_ids[0],
    3880                ) );
    3981                $ids = $q->get_results();
    4082
    41                 $this->assertEquals( array( $users[0] ), $ids );
     83                $this->assertEquals( array( self::$author_ids[0] ), $ids );
    4284        }
    4385
    4486        public function test_include_comma_separated() {
    45                 $users = $this->factory->user->create_many( 3 );
    4687                $q = new WP_User_Query( array(
    4788                        'fields' => '',
    48                         'include' => $users[0] . ', ' . $users[2],
     89                        'include' => self::$author_ids[0] . ', ' . self::$author_ids[2],
    4990                ) );
    5091                $ids = $q->get_results();
    5192
    52                 $this->assertEqualSets( array( $users[0], $users[2] ), $ids );
     93                $this->assertEqualSets( array( self::$author_ids[0], self::$author_ids[2] ), $ids );
    5394        }
    5495
    5596        public function test_include_array() {
    56                 $users = $this->factory->user->create_many( 3 );
    5797                $q = new WP_User_Query( array(
    5898                        'fields' => '',
    59                         'include' => array( $users[0], $users[2] ),
     99                        'include' => array( self::$author_ids[0], self::$author_ids[2] ),
    60100                ) );
    61101                $ids = $q->get_results();
    62102
    63                 $this->assertEqualSets( array( $users[0], $users[2] ), $ids );
     103                $this->assertEqualSets( array( self::$author_ids[0], self::$author_ids[2] ), $ids );
    64104        }
    65105
    66106        public function test_include_array_bad_values() {
    67                 $users = $this->factory->user->create_many( 3 );
    68107                $q = new WP_User_Query( array(
    69108                        'fields' => '',
    70                         'include' => array( $users[0], 'foo', $users[2] ),
     109                        'include' => array( self::$author_ids[0], 'foo', self::$author_ids[2] ),
    71110                ) );
    72111                $ids = $q->get_results();
    73112
    74                 $this->assertEqualSets( array( $users[0], $users[2] ), $ids );
     113                $this->assertEqualSets( array( self::$author_ids[0], self::$author_ids[2] ), $ids );
    75114        }
    76115
    77116        public function test_exclude() {
    78                 $users = $this->factory->user->create_many( 3, array(
    79                         'role' => 'author',
    80                 ) );
    81 
    82117                $q = new WP_User_Query( array(
    83118                        'fields' => '',
    84                         'exclude' => $users[1],
     119                        'exclude' => self::$author_ids[1],
    85120                ) );
    86121
     
    89124                // Indirect test in order to ignore default user created during installation.
    90125                $this->assertNotEmpty( $ids );
    91                 $this->assertNotContains( $users[1], $ids );
     126                $this->assertNotContains( self::$author_ids[1], $ids );
    92127        }
    93128
    94129        public function test_get_all() {
    95                 $this->factory->user->create_many( 3, array(
    96                         'role' => 'author'
    97                 ) );
    98 
    99130                $users = new WP_User_Query( array( 'blog_id' => get_current_blog_id() ) );
    100131                $users = $users->get_results();
    101132
    102133                // +1 for the default user created during installation.
    103                 $this->assertEquals( 4, count( $users ) );
     134                $this->assertEquals( 13, count( $users ) );
    104135                foreach ( $users as $user ) {
    105136                        $this->assertInstanceOf( 'WP_User', $user );
     
    108139                $users = new WP_User_Query( array( 'blog_id' => get_current_blog_id(), 'fields' => 'all_with_meta' ) );
    109140                $users = $users->get_results();
    110                 $this->assertEquals( 4, count( $users ) );
     141                $this->assertEquals( 13, count( $users ) );
    111142                foreach ( $users as $user ) {
    112143                        $this->assertInstanceOf( 'WP_User', $user );
     
    136167
    137168        public function test_orderby_meta_value() {
    138                 $users = $this->factory->user->create_many( 3, array(
    139                         'role' => 'author'
    140                 ) );
    141 
    142                 update_user_meta( $users[0], 'last_name', 'Jones' );
    143                 update_user_meta( $users[1], 'last_name', 'Albert' );
    144                 update_user_meta( $users[2], 'last_name', 'Zorro' );
    145 
    146                 $q = new WP_User_Query( array(
    147                         'include' => $users,
     169                update_user_meta( self::$author_ids[0], 'last_name', 'Jones' );
     170                update_user_meta( self::$author_ids[1], 'last_name', 'Albert' );
     171                update_user_meta( self::$author_ids[2], 'last_name', 'Zorro' );
     172
     173                $q = new WP_User_Query( array(
     174                        'include' => self::$author_ids,
    148175                        'meta_key' => 'last_name',
    149176                        'orderby' => 'meta_value',
     
    151178                ) );
    152179
    153                 $expected = array( $users[1], $users[0], $users[2] );
     180                $expected = array( self::$author_ids[3], self::$author_ids[1], self::$author_ids[0], self::$author_ids[2] );
    154181
    155182                $this->assertEquals( $expected, $q->get_results() );
     
    160187         */
    161188        public function test_orderby_meta_value_num() {
    162                 $users = $this->factory->user->create_many( 3, array(
    163                         'role' => 'author'
    164                 ) );
    165 
    166                 update_user_meta( $users[0], 'user_age', '101' );
    167                 update_user_meta( $users[1], 'user_age', '20' );
    168                 update_user_meta( $users[2], 'user_age', '25' );
    169 
    170                 $q = new WP_User_Query( array(
    171                         'include' => $users,
     189                update_user_meta( self::$author_ids[0], 'user_age', '101' );
     190                update_user_meta( self::$author_ids[1], 'user_age', '20' );
     191                update_user_meta( self::$author_ids[2], 'user_age', '25' );
     192
     193                $q = new WP_User_Query( array(
     194                        'include' => self::$author_ids,
    172195                        'meta_key' => 'user_age',
    173196                        'orderby' => 'meta_value_num',
     
    175198                ) );
    176199
    177                 $expected = array( $users[1], $users[2], $users[0] );
     200                $expected = array( self::$author_ids[1], self::$author_ids[2], self::$author_ids[0] );
    178201
    179202                $this->assertEquals( $expected, $q->get_results() );
     
    184207         */
    185208        public function test_orderby_somekey_where_meta_key_is_somekey() {
    186                 $users = $this->factory->user->create_many( 3, array(
    187                         'role' => 'author'
    188                 ) );
    189 
    190                 update_user_meta( $users[0], 'foo', 'zzz' );
    191                 update_user_meta( $users[1], 'foo', 'aaa' );
    192                 update_user_meta( $users[2], 'foo', 'jjj' );
    193 
    194                 $q = new WP_User_Query( array(
    195                         'include' => $users,
     209                update_user_meta( self::$author_ids[0], 'foo', 'zzz' );
     210                update_user_meta( self::$author_ids[1], 'foo', 'aaa' );
     211                update_user_meta( self::$author_ids[2], 'foo', 'jjj' );
     212
     213                $q = new WP_User_Query( array(
     214                        'include' => self::$author_ids,
    196215                        'meta_key' => 'foo',
    197216                        'orderby' => 'foo',
     
    199218                ) );
    200219
    201                 $expected = array( $users[1], $users[2], $users[0] );
     220                $expected = array( self::$author_ids[1], self::$author_ids[2], self::$author_ids[0] );
    202221
    203222                $this->assertEquals( $expected, $q->get_results() );
     
    208227         */
    209228        public function test_orderby_clause_key() {
    210                 $users = $this->factory->user->create_many( 3 );
    211                 add_user_meta( $users[0], 'foo', 'aaa' );
    212                 add_user_meta( $users[1], 'foo', 'zzz' );
    213                 add_user_meta( $users[2], 'foo', 'jjj' );
     229                add_user_meta( self::$author_ids[0], 'foo', 'aaa' );
     230                add_user_meta( self::$author_ids[1], 'foo', 'zzz' );
     231                add_user_meta( self::$author_ids[2], 'foo', 'jjj' );
    214232
    215233                $q = new WP_User_Query( array(
     
    225243                ) );
    226244
    227                 $this->assertEquals( array( $users[1], $users[2], $users[0] ), $q->results );
     245                $this->assertEquals( array( self::$author_ids[1], self::$author_ids[2], self::$author_ids[0] ), $q->results );
    228246        }
    229247
     
    232250         */
    233251        public function test_orderby_clause_key_as_secondary_sort() {
    234                 $u1 = $this->factory->user->create( array(
     252                $u1 = self::$static_factory->user->create( array(
    235253                        'user_registered' => '2015-01-28 03:00:00',
    236254                ) );
    237                 $u2 = $this->factory->user->create( array(
     255                $u2 = self::$static_factory->user->create( array(
    238256                        'user_registered' => '2015-01-28 05:00:00',
    239257                ) );
    240                 $u3 = $this->factory->user->create( array(
     258                $u3 = self::$static_factory->user->create( array(
    241259                        'user_registered' => '2015-01-28 03:00:00',
    242260                ) );
     
    267285         */
    268286        public function test_orderby_more_than_one_clause_key() {
    269                 $users = $this->factory->user->create_many( 3 );
    270 
    271                 add_user_meta( $users[0], 'foo', 'jjj' );
    272                 add_user_meta( $users[1], 'foo', 'zzz' );
    273                 add_user_meta( $users[2], 'foo', 'jjj' );
    274                 add_user_meta( $users[0], 'bar', 'aaa' );
    275                 add_user_meta( $users[1], 'bar', 'ccc' );
    276                 add_user_meta( $users[2], 'bar', 'bbb' );
     287                add_user_meta( self::$author_ids[0], 'foo', 'jjj' );
     288                add_user_meta( self::$author_ids[1], 'foo', 'zzz' );
     289                add_user_meta( self::$author_ids[2], 'foo', 'jjj' );
     290                add_user_meta( self::$author_ids[0], 'bar', 'aaa' );
     291                add_user_meta( self::$author_ids[1], 'bar', 'ccc' );
     292                add_user_meta( self::$author_ids[2], 'bar', 'bbb' );
    277293
    278294                $q = new WP_User_Query( array(
     
    294310                ) );
    295311
    296                 $this->assertEquals( array( $users[2], $users[0], $users[1] ), $q->results );
     312                $this->assertEquals( array( self::$author_ids[2], self::$author_ids[0], self::$author_ids[1] ), $q->results );
    297313        }
    298314
     
    314330                global $wpdb;
    315331
    316                 $users = $this->factory->user->create_many( 4 );
    317332                $q = new WP_User_Query( array(
    318333                        'orderby' => 'include',
    319                         'include' => array( $users[1], $users[0], $users[3] ),
     334                        'include' => array( self::$author_ids[1], self::$author_ids[0], self::$author_ids[3] ),
    320335                        'fields' => '',
    321336                ) );
    322337
    323                 $expected_orderby = 'ORDER BY FIELD( ' . $wpdb->users . '.ID, ' . $users[1] . ',' . $users[0] . ',' . $users[3] . ' )';
     338                $expected_orderby = 'ORDER BY FIELD( ' . $wpdb->users . '.ID, ' . self::$author_ids[1] . ',' . self::$author_ids[0] . ',' . self::$author_ids[3] . ' )';
    324339                $this->assertContains( $expected_orderby, $q->query_orderby );
    325340
    326341                // assertEquals() respects order but ignores type (get_results() returns numeric strings).
    327                 $this->assertEquals( array( $users[1], $users[0], $users[3] ), $q->get_results() );
     342                $this->assertEquals( array( self::$author_ids[1], self::$author_ids[0], self::$author_ids[3] ), $q->get_results() );
    328343        }
    329344
     
    334349                global $wpdb;
    335350
    336                 $users = $this->factory->user->create_many( 4 );
    337351                $q = new WP_User_Query( array(
    338352                        'orderby' => 'include',
    339                         'include' => array( $users[1], $users[0], $users[1], $users[3] ),
     353                        'include' => array( self::$author_ids[1], self::$author_ids[0], self::$author_ids[1], self::$author_ids[3] ),
    340354                        'fields' => '',
    341355                ) );
    342356
    343                 $expected_orderby = 'ORDER BY FIELD( ' . $wpdb->users . '.ID, ' . $users[1] . ',' . $users[0] . ',' . $users[3] . ' )';
     357                $expected_orderby = 'ORDER BY FIELD( ' . $wpdb->users . '.ID, ' . self::$author_ids[1] . ',' . self::$author_ids[0] . ',' . self::$author_ids[3] . ' )';
    344358                $this->assertContains( $expected_orderby, $q->query_orderby );
    345359
    346360                // assertEquals() respects order but ignores type (get_results() returns numeric strings).
    347                 $this->assertEquals( array( $users[1], $users[0], $users[3] ), $q->get_results() );
     361                $this->assertEquals( array( self::$author_ids[1], self::$author_ids[0], self::$author_ids[3] ), $q->get_results() );
    348362        }
    349363
     
    352366         */
    353367        public function test_orderby_space_separated() {
    354                 global $wpdb;
    355 
    356368                $q = new WP_User_Query( array(
    357369                        'orderby' => 'login nicename',
     
    366378         */
    367379        public function test_orderby_flat_array() {
    368                 global $wpdb;
    369 
    370380                $q = new WP_User_Query( array(
    371381                        'orderby' => array( 'login', 'nicename' ),
     
    379389         */
    380390        public function test_orderby_array_contains_invalid_item() {
    381                 global $wpdb;
    382 
    383391                $q = new WP_User_Query( array(
    384392                        'orderby' => array( 'login', 'foo', 'nicename' ),
     
    392400         */
    393401        public function test_orderby_array_contains_all_invalid_items() {
    394                 global $wpdb;
    395 
    396402                $q = new WP_User_Query( array(
    397403                        'orderby' => array( 'foo', 'bar', 'baz' ),
     
    405411         */
    406412        public function test_orderby_array() {
    407                 global $wpdb;
    408 
    409413                $q = new WP_User_Query( array(
    410414                        'orderby' => array(
     
    422426         */
    423427        public function test_orderby_array_should_discard_invalid_columns() {
    424                 global $wpdb;
    425 
    426428                $q = new WP_User_Query( array(
    427429                        'orderby' => array(
     
    440442        function test_number() {
    441443                // +1 for the default user created by the test suite.
    442                 $user_ids = $this->factory->user->create_many( 3 );
    443 
    444444                $users = new WP_User_Query( array( 'blog_id' => get_current_blog_id() ) );
    445445                $users = $users->get_results();
    446                 $this->assertEquals( 4, count( $users ) );
     446                $this->assertEquals( 13, count( $users ) );
    447447
    448448                $users = new WP_User_Query( array( 'blog_id' => get_current_blog_id(), 'number' => 10 ) );
    449449                $users = $users->get_results();
    450                 $this->assertEquals( 4, count( $users ) );
     450                $this->assertEquals( 10, count( $users ) );
    451451
    452452                $users = new WP_User_Query( array( 'blog_id' => get_current_blog_id(), 'number' => 2 ) );
     
    456456                $users = new WP_User_Query( array( 'blog_id' => get_current_blog_id(), 'number' => -1 ) );
    457457                $users = $users->get_results();
    458                 $this->assertEquals( 4, count( $users ) );
     458                $this->assertEquals( 13, count( $users ) );
    459459        }
    460460
     
    526526         */
    527527        function test_meta_query_with_role() {
    528                 $author_ids = $this->factory->user->create_many( 4, array( 'role' => 'author' ) );
    529 
    530                 add_user_meta( $author_ids[0], 'foo', 'bar' );
    531                 add_user_meta( $author_ids[1], 'foo', 'baz' );
     528                add_user_meta( self::$author_ids[0], 'foo', 'bar' );
     529                add_user_meta( self::$author_ids[1], 'foo', 'baz' );
    532530
    533531                // Users with foo = bar or baz restricted to the author role.
     
    548546                ) );
    549547
    550                 $this->assertEquals( array( $author_ids[0], $author_ids[1] ), $query->get_results() );
     548                $this->assertEquals( array( self::$author_ids[0], self::$author_ids[1] ), $query->get_results() );
    551549        }
    552550
    553551        public function test_roles_and_caps_should_be_populated_for_default_value_of_blog_id() {
    554                 $u = $this->factory->user->create( array( 'role' => 'author' ) );
    555 
    556552                $query = new WP_User_Query( array(
    557                         'include' => $u,
     553                        'include' => self::$author_ids[0],
    558554                ) );
    559555
     
    571567                }
    572568
    573                 $u = $this->factory->user->create( array( 'role' => 'author' ) );
    574 
    575569                $query = new WP_User_Query( array(
    576                         'include' => $u,
     570                        'include' => self::$author_ids[0],
    577571                        'blog_id' => get_current_blog_id(),
    578572                ) );
     
    591585                }
    592586
    593                 $u = $this->factory->user->create( array( 'role' => 'author' ) );
    594 
    595587                $query = new WP_User_Query( array(
    596                         'include' => $u,
     588                        'include' => self::$author_ids[0],
    597589                        'blog_id' => get_current_blog_id(),
    598590                ) );
     
    612604
    613605                $b = $this->factory->blog->create();
    614                 $u = $this->factory->user->create();
    615                 add_user_to_blog( $b, $u, 'author' );
     606
     607                add_user_to_blog( $b, self::$author_ids[0], 'author' );
    616608
    617609                $query = new WP_User_Query( array(
    618                         'include' => $u,
     610                        'include' => self::$author_ids[0],
    619611                        'blog_id' => $b,
    620612                        'fields' => 'all_with_meta',
     
    638630
    639631                $b = $this->factory->blog->create();
    640                 $u = $this->factory->user->create();
    641                 add_user_to_blog( $b, $u, 'author' );
     632                add_user_to_blog( $b, self::$author_ids[0], 'author' );
    642633
    643634                $query = new WP_User_Query( array(
    644635                        'fields' => 'all',
    645                         'include' => $u,
     636                        'include' => self::$author_ids[0],
    646637                        'blog_id' => $b,
    647638                ) );
     
    664655
    665656                $b = $this->factory->blog->create();
    666                 $users = $this->factory->user->create_many( 3 );
    667 
    668                 add_user_to_blog( $b, $users[0], 'subscriber' );
    669                 add_user_to_blog( $b, $users[1], 'author' );
    670                 add_user_to_blog( $b, $users[2], 'editor' );
     657
     658                add_user_to_blog( $b, self::$author_ids[0], 'subscriber' );
     659                add_user_to_blog( $b, self::$author_ids[1], 'author' );
     660                add_user_to_blog( $b, self::$author_ids[2], 'editor' );
    671661
    672662                $q = new WP_User_Query( array(
     
    677667                $found = wp_list_pluck( $q->get_results(), 'ID' );
    678668
    679                 $this->assertNotContains( $users[0], $found );
    680                 $this->assertContains( $users[1], $found );
    681                 $this->assertContains( $users[2], $found );
     669                $this->assertNotContains( self::$author_ids[0], $found );
     670                $this->assertContains( self::$author_ids[1], $found );
     671                $this->assertContains( self::$author_ids[2], $found );
    682672        }
    683673
     
    691681
    692682                $b = $this->factory->blog->create();
    693                 $users = $this->factory->user->create_many( 3 );
    694 
    695                 add_user_to_blog( $b, $users[0], 'subscriber' );
    696                 add_user_to_blog( $b, $users[1], 'author' );
    697                 add_user_to_blog( $b, $users[2], 'editor' );
    698 
    699                 add_user_meta( $users[1], 'foo', 'bar' );
    700                 add_user_meta( $users[2], 'foo', 'baz' );
     683
     684                add_user_to_blog( $b, self::$author_ids[0], 'subscriber' );
     685                add_user_to_blog( $b, self::$author_ids[1], 'author' );
     686                add_user_to_blog( $b, self::$author_ids[2], 'editor' );
     687
     688                add_user_meta( self::$author_ids[1], 'foo', 'bar' );
     689                add_user_meta( self::$author_ids[2], 'foo', 'baz' );
    701690
    702691                $q = new WP_User_Query( array(
     
    711700                $found = wp_list_pluck( $q->get_results(), 'ID' );
    712701
    713                 $this->assertNotContains( $users[0], $found );
    714                 $this->assertContains( $users[1], $found );
    715                 $this->assertNotContains( $users[2], $found );
     702                $this->assertNotContains( self::$author_ids[0], $found );
     703                $this->assertContains( self::$author_ids[1], $found );
     704                $this->assertNotContains( self::$author_ids[2], $found );
    716705        }
    717706
     
    723712                register_post_type( 'wptests_pt_private', array( 'public' => false ) );
    724713
    725                 $users = $this->factory->user->create_many( 3 );
    726 
    727                 $this->factory->post->create( array( 'post_author' => $users[0], 'post_status' => 'publish', 'post_type' => 'wptests_pt_public' ) );
    728                 $this->factory->post->create( array( 'post_author' => $users[1], 'post_status' => 'publish', 'post_type' => 'wptests_pt_private' ) );
     714                $this->factory->post->create( array( 'post_author' => self::$author_ids[0], 'post_status' => 'publish', 'post_type' => 'wptests_pt_public' ) );
     715                $this->factory->post->create( array( 'post_author' => self::$author_ids[1], 'post_status' => 'publish', 'post_type' => 'wptests_pt_private' ) );
    729716
    730717                $q = new WP_User_Query( array(
     
    733720
    734721                $found = wp_list_pluck( $q->get_results(), 'ID' );
    735                 $expected = array( $users[0] );
     722                $expected = array( self::$author_ids[0] );
    736723
    737724                $this->assertEqualSets( $expected, $found );
     
    745732                register_post_type( 'wptests_pt_private', array( 'public' => false ) );
    746733
    747                 $users = $this->factory->user->create_many( 3 );
    748 
    749                 $this->factory->post->create( array( 'post_author' => $users[0], 'post_status' => 'publish', 'post_type' => 'wptests_pt_public' ) );
    750                 $this->factory->post->create( array( 'post_author' => $users[1], 'post_status' => 'publish', 'post_type' => 'wptests_pt_private' ) );
    751                 $this->factory->post->create( array( 'post_author' => $users[2], 'post_status' => 'publish', 'post_type' => 'post' ) );
     734                $this->factory->post->create( array( 'post_author' => self::$author_ids[0], 'post_status' => 'publish', 'post_type' => 'wptests_pt_public' ) );
     735                $this->factory->post->create( array( 'post_author' => self::$author_ids[1], 'post_status' => 'publish', 'post_type' => 'wptests_pt_private' ) );
     736                $this->factory->post->create( array( 'post_author' => self::$author_ids[2], 'post_status' => 'publish', 'post_type' => 'post' ) );
    752737
    753738                $q = new WP_User_Query( array(
     
    756741
    757742                $found = wp_list_pluck( $q->get_results(), 'ID' );
    758                 $expected = array( $users[1], $users[2] );
     743                $expected = array( self::$author_ids[1], self::$author_ids[2] );
    759744
    760745                $this->assertEqualSets( $expected, $found );
     
    768753                register_post_type( 'wptests_pt_private', array( 'public' => false ) );
    769754
    770                 $users = $this->factory->user->create_many( 3 );
    771 
    772                 $this->factory->post->create( array( 'post_author' => $users[0], 'post_status' => 'draft', 'post_type' => 'wptests_pt_public' ) );
    773                 $this->factory->post->create( array( 'post_author' => $users[1], 'post_status' => 'inherit', 'post_type' => 'wptests_pt_private' ) );
    774                 $this->factory->post->create( array( 'post_author' => $users[2], 'post_status' => 'publish', 'post_type' => 'post' ) );
     755                $this->factory->post->create( array( 'post_author' => self::$author_ids[0], 'post_status' => 'draft', 'post_type' => 'wptests_pt_public' ) );
     756                $this->factory->post->create( array( 'post_author' => self::$author_ids[1], 'post_status' => 'inherit', 'post_type' => 'wptests_pt_private' ) );
     757                $this->factory->post->create( array( 'post_author' => self::$author_ids[2], 'post_status' => 'publish', 'post_type' => 'post' ) );
    775758
    776759                $q = new WP_User_Query( array(
     
    779762
    780763                $found = wp_list_pluck( $q->get_results(), 'ID' );
    781                 $expected = array( $users[2] );
     764                $expected = array( self::$author_ids[2] );
    782765
    783766                $this->assertEqualSets( $expected, $found );
     
    792775                }
    793776
    794                 $users = $this->factory->user->create_many( 3 );
    795777                $blogs = $this->factory->blog->create_many( 2 );
    796778
    797                 add_user_to_blog( $blogs[0], $users[0], 'author' );
    798                 add_user_to_blog( $blogs[0], $users[1], 'author' );
    799                 add_user_to_blog( $blogs[1], $users[0], 'author' );
    800                 add_user_to_blog( $blogs[1], $users[1], 'author' );
     779                add_user_to_blog( $blogs[0], self::$author_ids[0], 'author' );
     780                add_user_to_blog( $blogs[0], self::$author_ids[1], 'author' );
     781                add_user_to_blog( $blogs[1], self::$author_ids[0], 'author' );
     782                add_user_to_blog( $blogs[1], self::$author_ids[1], 'author' );
    801783
    802784                switch_to_blog( $blogs[0] );
    803                 $this->factory->post->create( array( 'post_author' => $users[0], 'post_status' => 'publish', 'post_type' => 'post' ) );
     785                $this->factory->post->create( array( 'post_author' => self::$author_ids[0], 'post_status' => 'publish', 'post_type' => 'post' ) );
    804786                restore_current_blog();
    805787
    806788                switch_to_blog( $blogs[1] );
    807                 $this->factory->post->create( array( 'post_author' => $users[1], 'post_status' => 'publish', 'post_type' => 'post' ) );
     789                $this->factory->post->create( array( 'post_author' => self::$author_ids[1], 'post_status' => 'publish', 'post_type' => 'post' ) );
    808790                restore_current_blog();
    809791
     
    814796
    815797                $found = wp_list_pluck( $q->get_results(), 'ID' );
    816                 $expected = array( $users[1] );
     798                $expected = array( self::$author_ids[1] );
    817799
    818800                $this->assertEqualSets( $expected, $found );
     
    823805         */
    824806        public function test_top_level_or_meta_query_should_eliminate_duplicate_matches() {
    825                 $users = $this->factory->user->create_many( 3 );
    826 
    827                 add_user_meta( $users[0], 'foo', 'bar' );
    828                 add_user_meta( $users[1], 'foo', 'bar' );
    829                 add_user_meta( $users[0], 'foo2', 'bar2' );
     807                add_user_meta( self::$author_ids[0], 'foo', 'bar' );
     808                add_user_meta( self::$author_ids[1], 'foo', 'bar' );
     809                add_user_meta( self::$author_ids[0], 'foo2', 'bar2' );
    830810
    831811                $q = new WP_User_Query( array(
     
    844824
    845825                $found = wp_list_pluck( $q->get_results(), 'ID' );
    846                 $expected = array( $users[0], $users[1] );
     826                $expected = array( self::$author_ids[0], self::$author_ids[1] );
    847827
    848828                $this->assertEqualSets( $expected, $found );
     
    853833         */
    854834        public function test_nested_or_meta_query_should_eliminate_duplicate_matches() {
    855                 $users = $this->factory->user->create_many( 3 );
    856 
    857                 add_user_meta( $users[0], 'foo', 'bar' );
    858                 add_user_meta( $users[1], 'foo', 'bar' );
    859                 add_user_meta( $users[0], 'foo2', 'bar2' );
    860                 add_user_meta( $users[1], 'foo3', 'bar3' );
     835                add_user_meta( self::$author_ids[0], 'foo', 'bar' );
     836                add_user_meta( self::$author_ids[1], 'foo', 'bar' );
     837                add_user_meta( self::$author_ids[0], 'foo2', 'bar2' );
     838                add_user_meta( self::$author_ids[1], 'foo3', 'bar3' );
    861839
    862840                $q = new WP_User_Query( array(
     
    882860
    883861                $found = wp_list_pluck( $q->get_results(), 'ID' );
    884                 $expected = array( $users[0], $users[1] );
     862                $expected = array( self::$author_ids[0], self::$author_ids[1] );
    885863
    886864                $this->assertEqualSets( $expected, $found );
     
    891869         */
    892870        public function test_paged() {
    893                 $users = $this->factory->user->create_many( 5 );
    894 
    895871                $q = new WP_User_Query( array(
    896872                        'number' => 2,
     
    901877                ) );
    902878
    903                 $this->assertEquals( array( $users[2], $users[1] ), $q->results );
     879                $this->assertEquals( array( self::$contrib_id, self::$editor_ids[2] ), $q->results );
    904880        }
    905881
     
    930906         */
    931907        public function test_get_single_role_by_user_query() {
    932                 $this->factory->user->create_many( 2, array(
    933                         'role' => 'subscriber',
    934                 ) );
    935 
    936                 $this->factory->user->create( array(
    937                         'role' => 'contributor',
    938                 ) );
    939 
    940908                $wp_user_search = new WP_User_Query( array( 'role' => 'subscriber' ) );
    941909                $users          = $wp_user_search->get_results();
     
    948916         */
    949917        public function test_get_multiple_roles_by_user_query() {
    950                 $this->factory->user->create_many( 2, array(
    951                         'role' => 'subscriber',
    952                 ) );
    953 
    954                 $this->factory->user->create_many( 3, array(
    955                         'role' => 'editor',
    956                 ) );
    957 
    958                 $this->factory->user->create( array(
    959                         'role' => 'contributor',
    960                 ) );
    961 
    962918                $wp_user_search = new WP_User_Query( array( 'role__in' => array( 'subscriber', 'editor' ) ) );
    963919                $users          = $wp_user_search->get_results();
     
    969925         */
    970926        public function test_get_single_role_by_string() {
    971                 $this->factory->user->create_many( 2, array(
    972                         'role' => 'subscriber',
    973                 ) );
    974 
    975                 $this->factory->user->create( array(
    976                         'role' => 'contributor',
    977                 ) );
    978 
    979927                $users = get_users( array(
    980928                        'role' => 'subscriber',
     
    988936         */
    989937        public function test_get_single_role_by_string_which_is_similar() {
    990                 $editors = $this->factory->user->create_many( 2, array(
    991                         'role' => 'editor',
    992                 ) );
    993 
    994938                $another_editor = $this->factory->user->create( array(
    995939                        'role' => 'another-editor',
     
    1001945                ) );
    1002946
    1003                 $this->assertEqualSets( $editors, $users );
     947                $this->assertEqualSets( self::$editor_ids, $users );
    1004948        }
    1005949
     
    1009953         */
    1010954        public function test_get_single_role_by_array() {
    1011                 $this->factory->user->create_many( 2, array(
    1012                         'role' => 'subscriber',
    1013                 ) );
    1014 
    1015                 $this->factory->user->create( array(
    1016                         'role' => 'contributor',
    1017                 ) );
    1018 
    1019955                $users = get_users( array(
    1020956                        'role' => array( 'subscriber' ),
     
    1028964         */
    1029965        public function test_get_multiple_roles_should_only_match_users_who_have_each_role() {
    1030                 $subscribers = $this->factory->user->create_many( 2, array(
    1031                         'role' => 'subscriber',
    1032                 ) );
    1033 
    1034                 $this->factory->user->create_many( 3, array(
    1035                         'role' => 'editor',
    1036                 ) );
    1037 
    1038                 $this->factory->user->create_many( 2, array(
    1039                         'role' => 'administrator',
    1040                 ) );
    1041 
    1042966                $users = new WP_User_Query( array( 'role' => array( 'subscriber', 'editor' ) ) );
    1043967                $users = $users->get_results();
     
    1045969                $this->assertEmpty( $users );
    1046970
    1047                 foreach ( $subscribers as $subscriber ) {
     971                foreach ( self::$sub_ids as $subscriber ) {
    1048972                        $subscriber = get_user_by( 'ID', $subscriber );
    1049973                        $subscriber->add_role( 'editor' );
     
    1064988         */
    1065989        public function test_get_multiple_roles_or() {
    1066                 $this->factory->user->create_many( 2, array(
    1067                         'role' => 'subscriber',
    1068                 ) );
    1069 
    1070                 $this->factory->user->create_many( 3, array(
    1071                         'role' => 'editor',
    1072                 ) );
    1073 
    1074                 $this->factory->user->create_many( 2, array(
    1075                         'role' => 'administrator',
    1076                 ) );
    1077 
    1078                 $this->factory->user->create_many( 1, array(
    1079                         'role' => 'contributor',
    1080                 ) );
    1081 
    1082990                $users = new WP_User_Query( array( 'role__in' => array( 'subscriber', 'editor', 'administrator' ) ) );
    1083991                $users = $users->get_results();
     
    10941002         */
    10951003        public function test_get_multiple_roles_by_comma_separated_list() {
    1096                 $subscribers = $this->factory->user->create_many( 2, array(
    1097                         'role' => 'subscriber',
    1098                 ) );
    1099 
    1100                 $this->factory->user->create_many( 3, array(
    1101                         'role' => 'editor',
    1102                 ) );
    1103 
    11041004                $users = get_users( array(
    11051005                        'role' => 'subscriber, editor',
     
    11081008                $this->assertEmpty( $users );
    11091009
    1110                 foreach ( $subscribers as $subscriber ) {
     1010                foreach ( self::$sub_ids as $subscriber ) {
    11111011                        $subscriber = get_user_by( 'ID', $subscriber );
    11121012                        $subscriber->add_role( 'editor' );
     
    11251025        public function test_get_multiple_roles_with_meta() {
    11261026                // Create administrator user + meta
    1127                 $administrator_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
    1128                 update_user_meta( $administrator_id, 'mk1', 1 );
    1129                 update_user_meta( $administrator_id, 'mk2', 1 );
     1027                update_user_meta( self::$admin_ids[0], 'mk1', 1 );
     1028                update_user_meta( self::$admin_ids[0], 'mk2', 1 );
    11301029
    11311030                // Create editor user + meta
    1132                 $editor_id = $this->factory->user->create( array( 'role' => 'editor' ) );
    1133                 update_user_meta( $editor_id, 'mk1', 1 );
    1134                 update_user_meta( $editor_id, 'mk2', 2 );
     1031                update_user_meta( self::$editor_ids[0], 'mk1', 1 );
     1032                update_user_meta( self::$editor_ids[0], 'mk2', 2 );
    11351033
    11361034                // Create subscriber user + meta
    1137                 $subscriber_id = $this->factory->user->create( array( 'role' => 'subscriber' ) );
    1138                 update_user_meta( $subscriber_id, 'mk1', 1 );
    1139                 update_user_meta( $subscriber_id, 'mk2', 1 );
     1035                update_user_meta( self::$sub_ids[0], 'mk1', 1 );
     1036                update_user_meta( self::$sub_ids[0], 'mk2', 1 );
    11401037
    11411038                // Create contributor user + meta
    1142                 $contributor_id = $this->factory->user->create( array( 'role' => 'contributor' ) );
    1143                 update_user_meta( $contributor_id, 'mk1', 1 );
    1144                 update_user_meta( $contributor_id, 'mk2', 2 );
     1039                update_user_meta( self::$contrib_id, 'mk1', 1 );
     1040                update_user_meta( self::$contrib_id, 'mk2', 2 );
    11451041
    11461042                // Fetch users
     
    11661062                // Check results
    11671063                $this->assertEquals( 1, count( $users ) );
    1168                 $this->assertSame( $editor_id, (int) $users[0]->ID );
     1064                $this->assertSame( self::$editor_ids[0], (int) $users[0]->ID );
    11691065        }
    11701066
     
    11731069         */
    11741070        public function test_role_exclusion() {
    1175                 $this->factory->user->create_many( 2, array(
    1176                         'role' => 'subscriber',
    1177                 ) );
    1178 
    1179                 $this->factory->user->create_many( 3, array(
    1180                         'role' => 'editor',
    1181                 ) );
    1182 
    11831071                $users = get_users( array(
    11841072                        'role__not_in' => 'subscriber',
     
    11861074
    11871075                // +1 for the default user created during installation.
    1188                 $this->assertEquals( 4, count( $users ) );
     1076                $this->assertEquals( 11, count( $users ) );
    11891077
    11901078                $users = get_users( array(
     
    11931081
    11941082                // +1 for the default user created during installation.
    1195                 $this->assertEquals( 3, count( $users ) );
     1083                $this->assertEquals( 10, count( $users ) );
    11961084        }
    11971085
     
    12001088         */
    12011089        public function test_role__in_role__not_in_combined() {
    1202                 $subscribers = $this->factory->user->create_many( 2, array(
    1203                         'role' => 'subscriber',
    1204                 ) );
    1205 
    1206                 $this->factory->user->create_many( 3, array(
    1207                         'role' => 'editor',
    1208                 ) );
    1209 
    1210                 foreach ( $subscribers as $subscriber ) {
     1090                foreach ( self::$sub_ids as $subscriber ) {
    12111091                        $subscriber = get_user_by( 'ID', $subscriber );
    12121092                        $subscriber->add_role( 'editor' );
     
    12311111         */
    12321112        public function test_role__not_in_role_combined() {
    1233                 $subscribers = $this->factory->user->create_many( 2, array(
    1234                         'role' => 'subscriber',
    1235                 ) );
    1236 
    1237                 $this->factory->user->create_many( 3, array(
    1238                         'role' => 'editor',
    1239                 ) );
    1240 
    1241                 $subscriber = get_user_by( 'ID', $subscribers[0] );
     1113                $subscriber = get_user_by( 'ID', self::$sub_ids[0] );
    12421114                $subscriber->add_role( 'editor' );
    12431115
     
    12541126         */
    12551127        public function test_role__not_in_user_without_role() {
    1256                 $user_without_rule = $this->factory->user->get_object_by_id( $this->factory->user->create( array(
    1257                         'role' => 'subscriber',
    1258                 ) ) );
     1128                $user_without_rule = get_user_by( 'ID', self::$sub_ids[0] );
    12591129
    12601130                $user_without_rule->remove_role( 'subscriber' );
    1261 
    1262                 $this->factory->user->create_many( 3, array(
    1263                         'role' => 'editor',
    1264                 ) );
    12651131
    12661132                $users = get_users( array(
     
    12691135
    12701136                // +1 for the default user created during installation.
    1271                 $this->assertEquals( 5, count( $users ) );
     1137                $this->assertEquals( 12, count( $users ) );
    12721138
    12731139                $users = get_users( array(
     
    12761142
    12771143                // +1 for the default user created during installation.
    1278                 $this->assertEquals( 2, count( $users ) );
     1144                $this->assertEquals( 10, count( $users ) );
    12791145        }
    12801146
     
    12881154
    12891155                $sites = $this->factory->blog->create_many( 2 );
    1290                 $users = $this->factory->user->create_many( 2 );
    1291 
    1292                 add_user_to_blog( $sites[0], $users[0], 'author' );
    1293                 add_user_to_blog( $sites[1], $users[1], 'author' );
     1156
     1157                add_user_to_blog( self::$author_ids[0], self::$author_ids[0], 'author' );
     1158                add_user_to_blog( self::$author_ids[1], self::$author_ids[1], 'author' );
    12941159
    12951160                $found = get_users( array(
     
    12981163                ) );
    12991164
    1300                 $this->assertEqualSets( array( $users[1] ), $found );
     1165                $this->assertEqualSets( array( self::$author_ids[1] ), $found );
    13011166        }
    13021167
     
    13111176
    13121177                $site_id = get_current_blog_id();
    1313                 $u = $this->factory->user->create();
    1314                 add_user_to_blog( $site_id, $u, 'author' );
    1315 
    1316                 $q = new WP_User_Query( array(
    1317                         'include' => $u,
     1178                add_user_to_blog( $site_id, self::$author_ids[0], 'author' );
     1179
     1180                $q = new WP_User_Query( array(
     1181                        'include' => self::$author_ids[0],
    13181182                ) );
    13191183
     
    13211185
    13221186                $q->prepare_query( array(
    1323                         'include' => $u,
     1187                        'include' => self::$author_ids[0],
    13241188                ) );
    13251189
     
    13271191
    13281192                $q->prepare_query( array(
    1329                         'include' => $u,
     1193                        'include' => self::$author_ids[0],
    13301194                ) );
    13311195
Note: See TracChangeset for help on using the changeset viewer.