WordPress.org

Make WordPress Core

Changeset 30017


Ignore:
Timestamp:
10/24/2014 07:51:01 PM (6 years ago)
Author:
boonebgorges
Message:

Streamline WP_User_Query unit tests.

  • Don't create user during setUp(), as it's not used in every test.
  • Create fewer users in get_all and orderby tests.

See #30017.

File:
1 edited

Legend:

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

    r30016 r30017  
    1111    function setUp() {
    1212        parent::setUp();
    13 
    14         $this->user_id = $this->factory->user->create( array(
    15             'role' => 'author'
    16         ) );
    1713    }
    1814
     
    7975    }
    8076
    81     function test_exclude() {
    82         $users = new WP_User_Query();
    83         $users->set( 'fields', '' );
    84         $users->set( 'exclude', $this->user_id );
    85         $users->prepare_query();
    86         $users->query();
    87 
    88         $ids = $users->get_results();
    89         $this->assertNotContains( $this->user_id, $ids );
    90     }
    91 
    92     function test_get_all() {
    93         $this->factory->user->create_many( 10, array(
     77    public function test_exclude() {
     78        $users = $this->factory->user->create_many( 3, array(
     79            'role' => 'author',
     80        ) );
     81
     82        $q = new WP_User_Query( array(
     83            'fields' => '',
     84            'exclude' => $users[1],
     85        ) );
     86
     87        $ids = $q->get_results();
     88
     89        // Indirect test in order to ignore default user created during installation.
     90        $this->assertNotEmpty( $ids );
     91        $this->assertNotContains( $users[1], $ids );
     92    }
     93
     94    public function test_get_all() {
     95        $this->factory->user->create_many( 3, array(
    9496            'role' => 'author'
    9597        ) );
     
    9799        $users = new WP_User_Query( array( 'blog_id' => get_current_blog_id() ) );
    98100        $users = $users->get_results();
    99         $this->assertEquals( 12, count( $users ) );
     101
     102        // +1 for the default user created during installation.
     103        $this->assertEquals( 4, count( $users ) );
    100104        foreach ( $users as $user ) {
    101105            $this->assertInstanceOf( 'WP_User', $user );
     
    104108        $users = new WP_User_Query( array( 'blog_id' => get_current_blog_id(), 'fields' => 'all_with_meta' ) );
    105109        $users = $users->get_results();
    106         $this->assertEquals( 12, count( $users ) );
     110        $this->assertEquals( 4, count( $users ) );
    107111        foreach ( $users as $user ) {
    108112            $this->assertInstanceOf( 'WP_User', $user );
     
    110114    }
    111115
    112     function test_orderby() {
    113         $user_ids = $this->factory->user->create_many( 10, array(
     116    public function test_orderby_meta_value() {
     117        $users = $this->factory->user->create_many( 3, array(
    114118            'role' => 'author'
    115119        ) );
    116120
    117         $names = array( 'd', 'f', 'n', 'f', 'd', 'j', 'r', 'p', 'h', 'g' );
    118 
    119         foreach ( $names as $i => $name )
    120             update_user_meta( $user_ids[$i], 'last_name', $name );
    121 
    122         $u = new WP_User_Query( array(
    123             'include' => $user_ids,
     121        update_user_meta( $users[0], 'last_name', 'Jones' );
     122        update_user_meta( $users[1], 'last_name', 'Albert' );
     123        update_user_meta( $users[2], 'last_name', 'Zorro' );
     124
     125        $q = new WP_User_Query( array(
     126            'include' => $users,
    124127            'meta_key' => 'last_name',
    125128            'orderby' => 'meta_value',
    126129            'fields' => 'ids'
    127130        ) );
    128         $values = array();
    129         foreach ( $u->get_results() as $user )
    130             $values[] = get_user_meta( $user, 'last_name', true );
    131 
    132         sort( $names );
    133 
    134         $this->assertEquals( $names, $values );
     131
     132        $expected = array( $users[1], $users[0], $users[2] );
     133
     134        $this->assertEquals( $expected, $q->get_results() );
    135135    }
    136136
Note: See TracChangeset for help on using the changeset viewer.