| | 1 | <?php |
| | 2 | /** |
| | 3 | * @group user |
| | 4 | * @ticket 15145 |
| | 5 | */ |
| | 6 | class Tests_User_ListUsers extends WP_UnitTestCase { |
| | 7 | private static $user_ids = array(); |
| | 8 | |
| | 9 | public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { |
| | 10 | self::$user_ids[] = $factory->user->create( |
| | 11 | array( |
| | 12 | 'user_login' => 'zack', |
| | 13 | 'display_name' => 'zack', |
| | 14 | 'role' => 'subscriber', |
| | 15 | 'first_name' => 'zack', |
| | 16 | 'last_name' => 'moon', |
| | 17 | 'user_email' => 'm.zack@example.com', |
| | 18 | 'user_url' => 'http://moonzack.fake', |
| | 19 | ) |
| | 20 | ); |
| | 21 | |
| | 22 | self::$user_ids[] = $factory->user->create( |
| | 23 | array( |
| | 24 | 'user_login' => 'jane', |
| | 25 | 'display_name' => 'jane', |
| | 26 | 'role' => 'contributor', |
| | 27 | 'first_name' => 'jane', |
| | 28 | 'last_name' => 'reno', |
| | 29 | 'user_email' => 'r.jane@example.com', |
| | 30 | 'user_url' => 'http://janereno.fake', |
| | 31 | ) |
| | 32 | ); |
| | 33 | |
| | 34 | self::$user_ids[] = $factory->user->create( |
| | 35 | array( |
| | 36 | 'user_login' => 'michelle', |
| | 37 | 'display_name' => 'michelle', |
| | 38 | 'role' => 'subscriber', |
| | 39 | 'first_name' => 'michelle', |
| | 40 | 'last_name' => 'jones', |
| | 41 | 'user_email' => 'j.michelle@example.com', |
| | 42 | 'user_url' => 'http://lemichellejones.fake', |
| | 43 | ) |
| | 44 | ); |
| | 45 | |
| | 46 | self::$user_ids[] = $factory->user->create( |
| | 47 | array( |
| | 48 | 'user_login' => 'paul', |
| | 49 | 'display_name' => 'paul', |
| | 50 | 'role' => 'subscriber', |
| | 51 | 'first_name' => 'paul', |
| | 52 | 'last_name' => 'norris', |
| | 53 | 'user_email' => 'n.paul@example.com', |
| | 54 | 'user_url' => 'http://awildpaulappeared.fake', |
| | 55 | ) |
| | 56 | ); |
| | 57 | |
| | 58 | foreach ( self::$user_ids as $user ) { |
| | 59 | $factory->post->create( |
| | 60 | array( |
| | 61 | 'post_type' => 'post', |
| | 62 | 'post_author' => $user, |
| | 63 | ) |
| | 64 | ); |
| | 65 | } |
| | 66 | } |
| | 67 | |
| | 68 | /** |
| | 69 | * Test that wp_list_users() creates the expected list of users. |
| | 70 | * |
| | 71 | * @dataProvider data_should_create_a_user_list |
| | 72 | * @covers ::wp_list_users |
| | 73 | * |
| | 74 | * @param array $args The arguments to create a list of users. |
| | 75 | * @param string $expected The expected result. |
| | 76 | */ |
| | 77 | public function test_should_create_a_user_list( $args, $expected ) { |
| | 78 | $actual = wp_list_users( $args ); |
| | 79 | |
| | 80 | if ( null === $actual ) { |
| | 81 | $this->expectOutputString( $expected ); |
| | 82 | } else { |
| | 83 | $this->assertSame( $expected, $actual ); |
| | 84 | } |
| | 85 | } |
| | 86 | |
| | 87 | /** |
| | 88 | * Data provider. |
| | 89 | * |
| | 90 | * @return array |
| | 91 | */ |
| | 92 | public function data_should_create_a_user_list() { |
| | 93 | return array( |
| | 94 | 'defaults when no args are supplied' => array( |
| | 95 | 'args' => array(), |
| | 96 | 'expected' => '<li>jane</li><li>michelle</li><li>paul</li><li>zack</li>', |
| | 97 | ), |
| | 98 | 'the admin account included' => array( |
| | 99 | 'args' => array( |
| | 100 | 'exclude_admin' => false, |
| | 101 | ), |
| | 102 | 'expected' => '<li>admin</li><li>jane</li><li>michelle</li><li>paul</li><li>zack</li>', |
| | 103 | ), |
| | 104 | 'the full name of each user' => array( |
| | 105 | 'args' => array( |
| | 106 | 'show_fullname' => true, |
| | 107 | ), |
| | 108 | 'expected' => '<li>jane reno</li><li>michelle jones</li><li>paul norris</li><li>zack moon</li>', |
| | 109 | ), |
| | 110 | 'the feed of each user' => array( |
| | 111 | 'args' => array( |
| | 112 | 'feed' => 'User feed', |
| | 113 | ), |
| | 114 | 'expected' => '<li>jane (<a href="http://example.org/?feed=rss2&author=3">User feed</a>)</li>' . |
| | 115 | '<li>michelle (<a href="http://example.org/?feed=rss2&author=4">User feed</a>)</li>' . |
| | 116 | '<li>paul (<a href="http://example.org/?feed=rss2&author=5">User feed</a>)</li>' . |
| | 117 | '<li>zack (<a href="http://example.org/?feed=rss2&author=2">User feed</a>)</li>', |
| | 118 | ), |
| | 119 | 'the feed of each user and an image' => array( |
| | 120 | 'args' => array( |
| | 121 | 'feed' => 'User feed with image', |
| | 122 | 'feed_image' => 'http://example.org/image.jpg', |
| | 123 | ), |
| | 124 | 'expected' => '<li>jane <a href="http://example.org/?feed=rss2&author=3"><img src="http://example.org/image.jpg" style="border: none;" alt="User feed with image" /></a></li>' . |
| | 125 | '<li>michelle <a href="http://example.org/?feed=rss2&author=4"><img src="http://example.org/image.jpg" style="border: none;" alt="User feed with image" /></a></li>' . |
| | 126 | '<li>paul <a href="http://example.org/?feed=rss2&author=5"><img src="http://example.org/image.jpg" style="border: none;" alt="User feed with image" /></a></li>' . |
| | 127 | '<li>zack <a href="http://example.org/?feed=rss2&author=2"><img src="http://example.org/image.jpg" style="border: none;" alt="User feed with image" /></a></li>', |
| | 128 | ), |
| | 129 | 'a feed of the specified type' => array( |
| | 130 | 'args' => array( |
| | 131 | 'feed' => 'User feed as atom', |
| | 132 | 'feed_type' => 'atom', |
| | 133 | ), |
| | 134 | 'expected' => '<li>jane (<a href="http://example.org/?feed=atom&author=3">User feed as atom</a>)</li>' . |
| | 135 | '<li>michelle (<a href="http://example.org/?feed=atom&author=4">User feed as atom</a>)</li>' . |
| | 136 | '<li>paul (<a href="http://example.org/?feed=atom&author=5">User feed as atom</a>)</li>' . |
| | 137 | '<li>zack (<a href="http://example.org/?feed=atom&author=2">User feed as atom</a>)</li>', |
| | 138 | ), |
| | 139 | 'no output via echo' => array( |
| | 140 | 'args' => array( |
| | 141 | 'echo' => false, |
| | 142 | ), |
| | 143 | 'expected' => '<li>jane</li><li>michelle</li><li>paul</li><li>zack</li>', |
| | 144 | ), |
| | 145 | 'commas separating each user' => array( |
| | 146 | 'args' => array( |
| | 147 | 'style' => '', |
| | 148 | ), |
| | 149 | 'expected' => 'jane, michelle, paul, zack', |
| | 150 | ), |
| | 151 | 'plain text format' => array( |
| | 152 | 'args' => array( |
| | 153 | 'html' => false, |
| | 154 | ), |
| | 155 | 'expected' => 'jane, michelle, paul, zack', |
| | 156 | ), |
| | 157 | ); |
| | 158 | } |
| | 159 | |
| | 160 | /** |
| | 161 | * Tests that wp_list_users() does not create a user list. |
| | 162 | * |
| | 163 | * @dataProvider data_should_not_create_a_user_list |
| | 164 | * @covers ::wp_list_users |
| | 165 | * |
| | 166 | * @param array $args The arguments to create a list of users. |
| | 167 | */ |
| | 168 | public function test_should_not_create_a_user_list( $args ) { |
| | 169 | $actual = wp_list_users( $args ); |
| | 170 | |
| | 171 | if ( null === $actual ) { |
| | 172 | $this->expectOutputString( '', 'wp_list_users() did not output an empty string.' ); |
| | 173 | } else { |
| | 174 | $this->assertSame( $actual, 'wp_list_users() did not return an empty string.' ); |
| | 175 | } |
| | 176 | } |
| | 177 | |
| | 178 | /** |
| | 179 | * Data provider. |
| | 180 | * |
| | 181 | * @return array |
| | 182 | */ |
| | 183 | public function data_should_not_create_a_user_list() { |
| | 184 | return array( |
| | 185 | 'an empty user query result' => array( |
| | 186 | 'args' => array( |
| | 187 | 'include' => array( 9999 ), |
| | 188 | ), |
| | 189 | 'expected' => '', |
| | 190 | ), |
| | 191 | ); |
| | 192 | } |
| | 193 | } |