query.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728
  1. <?php
  2. /**
  3. * Test WP_User Query, in wp-includes/user.php
  4. *
  5. * @group user
  6. */
  7. class 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;
  13. protected $user_id;
  14. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  15. self::$author_ids = $factory->user->create_many(
  16. 4,
  17. array(
  18. 'role' => 'author',
  19. )
  20. );
  21. self::$sub_ids = $factory->user->create_many(
  22. 2,
  23. array(
  24. 'role' => 'subscriber',
  25. )
  26. );
  27. self::$editor_ids = $factory->user->create_many(
  28. 3,
  29. array(
  30. 'role' => 'editor',
  31. )
  32. );
  33. self::$contrib_id = $factory->user->create(
  34. array(
  35. 'role' => 'contributor',
  36. )
  37. );
  38. self::$admin_ids = $factory->user->create_many(
  39. 2,
  40. array(
  41. 'role' => 'administrator',
  42. )
  43. );
  44. }
  45. function test_get_and_set() {
  46. $users = new WP_User_Query();
  47. $this->assertEquals( '', $users->get( 'fields' ) );
  48. if ( isset( $users->query_vars['fields'] ) ) {
  49. $this->assertSame( '', $users->query_vars['fields'] );
  50. }
  51. $users->set( 'fields', 'all' );
  52. $this->assertSame( 'all', $users->get( 'fields' ) );
  53. $this->assertSame( 'all', $users->query_vars['fields'] );
  54. $users->set( 'fields', '' );
  55. $this->assertSame( '', $users->get( 'fields' ) );
  56. $this->assertSame( '', $users->query_vars['fields'] );
  57. $this->assertNull( $users->get( 'does-not-exist' ) );
  58. }
  59. public function test_include_single() {
  60. $q = new WP_User_Query(
  61. array(
  62. 'fields' => '',
  63. 'include' => self::$author_ids[0],
  64. )
  65. );
  66. $ids = $q->get_results();
  67. $this->assertEqualSets( array( self::$author_ids[0] ), $ids );
  68. }
  69. public function test_include_comma_separated() {
  70. $q = new WP_User_Query(
  71. array(
  72. 'fields' => '',
  73. 'include' => self::$author_ids[0] . ', ' . self::$author_ids[2],
  74. )
  75. );
  76. $ids = $q->get_results();
  77. $this->assertEqualSets( array( self::$author_ids[0], self::$author_ids[2] ), $ids );
  78. }
  79. public function test_include_array() {
  80. $q = new WP_User_Query(
  81. array(
  82. 'fields' => '',
  83. 'include' => array( self::$author_ids[0], self::$author_ids[2] ),
  84. )
  85. );
  86. $ids = $q->get_results();
  87. $this->assertEqualSets( array( self::$author_ids[0], self::$author_ids[2] ), $ids );
  88. }
  89. public function test_include_array_bad_values() {
  90. $q = new WP_User_Query(
  91. array(
  92. 'fields' => '',
  93. 'include' => array( self::$author_ids[0], 'foo', self::$author_ids[2] ),
  94. )
  95. );
  96. $ids = $q->get_results();
  97. $this->assertEqualSets( array( self::$author_ids[0], self::$author_ids[2] ), $ids );
  98. }
  99. public function test_exclude() {
  100. $q = new WP_User_Query(
  101. array(
  102. 'fields' => '',
  103. 'exclude' => self::$author_ids[1],
  104. )
  105. );
  106. $ids = $q->get_results();
  107. // Indirect test in order to ignore default user created during installation.
  108. $this->assertNotEmpty( $ids );
  109. $this->assertNotContains( self::$author_ids[1], $ids );
  110. }
  111. public function test_get_all() {
  112. $users = new WP_User_Query( array( 'blog_id' => get_current_blog_id() ) );
  113. $users = $users->get_results();
  114. // +1 for the default user created during installation.
  115. $this->assertSame( 13, count( $users ) );
  116. foreach ( $users as $user ) {
  117. $this->assertInstanceOf( 'WP_User', $user );
  118. }
  119. $users = new WP_User_Query(
  120. array(
  121. 'blog_id' => get_current_blog_id(),
  122. 'fields' => 'all_with_meta',
  123. )
  124. );
  125. $users = $users->get_results();
  126. $this->assertSame( 13, count( $users ) );
  127. foreach ( $users as $user ) {
  128. $this->assertInstanceOf( 'WP_User', $user );
  129. }
  130. }
  131. /**
  132. * @ticket 39297
  133. */
  134. public function test_get_total_is_int() {
  135. $users = new WP_User_Query( array( 'blog_id' => get_current_blog_id() ) );
  136. $total_users = $users->get_total();
  137. $this->assertSame( 13, $total_users );
  138. }
  139. /**
  140. * @dataProvider orderby_should_convert_non_prefixed_keys_data
  141. */
  142. public function test_orderby_should_convert_non_prefixed_keys( $short_key, $full_key ) {
  143. $q = new WP_User_Query(
  144. array(
  145. 'orderby' => $short_key,
  146. )
  147. );
  148. $this->assertContains( "ORDER BY $full_key", $q->query_orderby );
  149. }
  150. public function orderby_should_convert_non_prefixed_keys_data() {
  151. return array(
  152. array( 'nicename', 'user_nicename' ),
  153. array( 'email', 'user_email' ),
  154. array( 'url', 'user_url' ),
  155. array( 'registered', 'user_registered' ),
  156. array( 'name', 'display_name' ),
  157. );
  158. }
  159. public function test_orderby_meta_value() {
  160. update_user_meta( self::$author_ids[0], 'last_name', 'Jones' );
  161. update_user_meta( self::$author_ids[1], 'last_name', 'Albert' );
  162. update_user_meta( self::$author_ids[2], 'last_name', 'Zorro' );
  163. $q = new WP_User_Query(
  164. array(
  165. 'include' => self::$author_ids,
  166. 'meta_key' => 'last_name',
  167. 'orderby' => 'meta_value',
  168. 'fields' => 'ids',
  169. )
  170. );
  171. $expected = array( self::$author_ids[3], self::$author_ids[1], self::$author_ids[0], self::$author_ids[2] );
  172. $this->assertEquals( $expected, $q->get_results() );
  173. }
  174. /**
  175. * @ticket 27887
  176. */
  177. public function test_orderby_meta_value_num() {
  178. update_user_meta( self::$author_ids[0], 'user_age', '101' );
  179. update_user_meta( self::$author_ids[1], 'user_age', '20' );
  180. update_user_meta( self::$author_ids[2], 'user_age', '25' );
  181. $q = new WP_User_Query(
  182. array(
  183. 'include' => self::$author_ids,
  184. 'meta_key' => 'user_age',
  185. 'orderby' => 'meta_value_num',
  186. 'fields' => 'ids',
  187. )
  188. );
  189. $expected = array( self::$author_ids[1], self::$author_ids[2], self::$author_ids[0] );
  190. $this->assertEquals( $expected, $q->get_results() );
  191. }
  192. /**
  193. * @ticket 31265
  194. */
  195. public function test_orderby_somekey_where_meta_key_is_somekey() {
  196. update_user_meta( self::$author_ids[0], 'foo', 'zzz' );
  197. update_user_meta( self::$author_ids[1], 'foo', 'aaa' );
  198. update_user_meta( self::$author_ids[2], 'foo', 'jjj' );
  199. $q = new WP_User_Query(
  200. array(
  201. 'include' => self::$author_ids,
  202. 'meta_key' => 'foo',
  203. 'orderby' => 'foo',
  204. 'fields' => 'ids',
  205. )
  206. );
  207. $expected = array( self::$author_ids[1], self::$author_ids[2], self::$author_ids[0] );
  208. $this->assertEquals( $expected, $q->get_results() );
  209. }
  210. /**
  211. * @ticket 31265
  212. */
  213. public function test_orderby_clause_key() {
  214. add_user_meta( self::$author_ids[0], 'foo', 'aaa' );
  215. add_user_meta( self::$author_ids[1], 'foo', 'zzz' );
  216. add_user_meta( self::$author_ids[2], 'foo', 'jjj' );
  217. $q = new WP_User_Query(
  218. array(
  219. 'fields' => 'ids',
  220. 'meta_query' => array(
  221. 'foo_key' => array(
  222. 'key' => 'foo',
  223. 'compare' => 'EXISTS',
  224. ),
  225. ),
  226. 'orderby' => 'foo_key',
  227. 'order' => 'DESC',
  228. )
  229. );
  230. $this->assertEquals( array( self::$author_ids[1], self::$author_ids[2], self::$author_ids[0] ), $q->results );
  231. }
  232. /**
  233. * @ticket 31265
  234. */
  235. public function test_orderby_clause_key_as_secondary_sort() {
  236. $u1 = self::factory()->user->create(
  237. array(
  238. 'user_registered' => '2015-01-28 03:00:00',
  239. )
  240. );
  241. $u2 = self::factory()->user->create(
  242. array(
  243. 'user_registered' => '2015-01-28 05:00:00',
  244. )
  245. );
  246. $u3 = self::factory()->user->create(
  247. array(
  248. 'user_registered' => '2015-01-28 03:00:00',
  249. )
  250. );
  251. add_user_meta( $u1, 'foo', 'jjj' );
  252. add_user_meta( $u2, 'foo', 'zzz' );
  253. add_user_meta( $u3, 'foo', 'aaa' );
  254. $q = new WP_User_Query(
  255. array(
  256. 'fields' => 'ids',
  257. 'meta_query' => array(
  258. 'foo_key' => array(
  259. 'key' => 'foo',
  260. 'compare' => 'EXISTS',
  261. ),
  262. ),
  263. 'orderby' => array(
  264. 'comment_date' => 'asc',
  265. 'foo_key' => 'asc',
  266. ),
  267. )
  268. );
  269. $this->assertEquals( array( $u3, $u1, $u2 ), $q->results );
  270. }
  271. /**
  272. * @ticket 31265
  273. */
  274. public function test_orderby_more_than_one_clause_key() {
  275. add_user_meta( self::$author_ids[0], 'foo', 'jjj' );
  276. add_user_meta( self::$author_ids[1], 'foo', 'zzz' );
  277. add_user_meta( self::$author_ids[2], 'foo', 'jjj' );
  278. add_user_meta( self::$author_ids[0], 'bar', 'aaa' );
  279. add_user_meta( self::$author_ids[1], 'bar', 'ccc' );
  280. add_user_meta( self::$author_ids[2], 'bar', 'bbb' );
  281. $q = new WP_User_Query(
  282. array(
  283. 'fields' => 'ids',
  284. 'meta_query' => array(
  285. 'foo_key' => array(
  286. 'key' => 'foo',
  287. 'compare' => 'EXISTS',
  288. ),
  289. 'bar_key' => array(
  290. 'key' => 'bar',
  291. 'compare' => 'EXISTS',
  292. ),
  293. ),
  294. 'orderby' => array(
  295. 'foo_key' => 'asc',
  296. 'bar_key' => 'desc',
  297. ),
  298. )
  299. );
  300. $this->assertEquals( array( self::$author_ids[2], self::$author_ids[0], self::$author_ids[1] ), $q->results );
  301. }
  302. /**
  303. * @ticket 30064
  304. */
  305. public function test_orderby_include_with_empty_include() {
  306. $q = new WP_User_Query(
  307. array(
  308. 'orderby' => 'include',
  309. )
  310. );
  311. $this->assertContains( 'ORDER BY user_login', $q->query_orderby );
  312. }
  313. /**
  314. * @ticket 30064
  315. */
  316. public function test_orderby_include() {
  317. global $wpdb;
  318. $q = new WP_User_Query(
  319. array(
  320. 'orderby' => 'include',
  321. 'include' => array( self::$author_ids[1], self::$author_ids[0], self::$author_ids[3] ),
  322. 'fields' => '',
  323. )
  324. );
  325. $expected_orderby = 'ORDER BY FIELD( ' . $wpdb->users . '.ID, ' . self::$author_ids[1] . ',' . self::$author_ids[0] . ',' . self::$author_ids[3] . ' )';
  326. $this->assertContains( $expected_orderby, $q->query_orderby );
  327. // assertEquals() respects order but ignores type (get_results() returns numeric strings).
  328. $this->assertEquals( array( self::$author_ids[1], self::$author_ids[0], self::$author_ids[3] ), $q->get_results() );
  329. }
  330. /**
  331. * @ticket 30064
  332. */
  333. public function test_orderby_include_duplicate_values() {
  334. global $wpdb;
  335. $q = new WP_User_Query(
  336. array(
  337. 'orderby' => 'include',
  338. 'include' => array( self::$author_ids[1], self::$author_ids[0], self::$author_ids[1], self::$author_ids[3] ),
  339. 'fields' => '',
  340. )
  341. );
  342. $expected_orderby = 'ORDER BY FIELD( ' . $wpdb->users . '.ID, ' . self::$author_ids[1] . ',' . self::$author_ids[0] . ',' . self::$author_ids[3] . ' )';
  343. $this->assertContains( $expected_orderby, $q->query_orderby );
  344. // assertEquals() respects order but ignores type (get_results() returns numeric strings).
  345. $this->assertEquals( array( self::$author_ids[1], self::$author_ids[0], self::$author_ids[3] ), $q->get_results() );
  346. }
  347. /**
  348. * @ticket 31265
  349. */
  350. public function test_orderby_space_separated() {
  351. $q = new WP_User_Query(
  352. array(
  353. 'orderby' => 'login nicename',
  354. 'order' => 'ASC',
  355. )
  356. );
  357. $this->assertContains( 'ORDER BY user_login ASC, user_nicename ASC', $q->query_orderby );
  358. }
  359. /**
  360. * @ticket 31265
  361. */
  362. public function test_orderby_flat_array() {
  363. $q = new WP_User_Query(
  364. array(
  365. 'orderby' => array( 'login', 'nicename' ),
  366. )
  367. );
  368. $this->assertContains( 'ORDER BY user_login ASC, user_nicename ASC', $q->query_orderby );
  369. }
  370. /**
  371. * @ticket 31265
  372. */
  373. public function test_orderby_array_contains_invalid_item() {
  374. $q = new WP_User_Query(
  375. array(
  376. 'orderby' => array( 'login', 'foo', 'nicename' ),
  377. )
  378. );
  379. $this->assertContains( 'ORDER BY user_login ASC, user_nicename ASC', $q->query_orderby );
  380. }
  381. /**
  382. * @ticket 31265
  383. */
  384. public function test_orderby_array_contains_all_invalid_items() {
  385. $q = new WP_User_Query(
  386. array(
  387. 'orderby' => array( 'foo', 'bar', 'baz' ),
  388. )
  389. );
  390. $this->assertContains( 'ORDER BY user_login', $q->query_orderby );
  391. }
  392. /**
  393. * @ticket 31265
  394. */
  395. public function test_orderby_array() {
  396. $q = new WP_User_Query(
  397. array(
  398. 'orderby' => array(
  399. 'login' => 'DESC',
  400. 'nicename' => 'ASC',
  401. 'email' => 'DESC',
  402. ),
  403. )
  404. );
  405. $this->assertContains( 'ORDER BY user_login DESC, user_nicename ASC, user_email DESC', $q->query_orderby );
  406. }
  407. /**
  408. * @ticket 31265
  409. */
  410. public function test_orderby_array_should_discard_invalid_columns() {
  411. $q = new WP_User_Query(
  412. array(
  413. 'orderby' => array(
  414. 'login' => 'DESC',
  415. 'foo' => 'ASC',
  416. 'email' => 'ASC',
  417. ),
  418. )
  419. );
  420. $this->assertContains( 'ORDER BY user_login DESC, user_email ASC', $q->query_orderby );
  421. }
  422. /**
  423. * @ticket 28631
  424. */
  425. function test_number() {
  426. // +1 for the default user created by the test suite.
  427. $users = new WP_User_Query( array( 'blog_id' => get_current_blog_id() ) );
  428. $users = $users->get_results();
  429. $this->assertSame( 13, count( $users ) );
  430. $users = new WP_User_Query(
  431. array(
  432. 'blog_id' => get_current_blog_id(),
  433. 'number' => 10,
  434. )
  435. );
  436. $users = $users->get_results();
  437. $this->assertSame( 10, count( $users ) );
  438. $users = new WP_User_Query(
  439. array(
  440. 'blog_id' => get_current_blog_id(),
  441. 'number' => 2,
  442. )
  443. );
  444. $users = $users->get_results();
  445. $this->assertSame( 2, count( $users ) );
  446. $users = new WP_User_Query(
  447. array(
  448. 'blog_id' => get_current_blog_id(),
  449. 'number' => -1,
  450. )
  451. );
  452. $users = $users->get_results();
  453. $this->assertSame( 13, count( $users ) );
  454. }
  455. /**
  456. * @ticket 21119
  457. */
  458. function test_prepare_query() {
  459. $query = new WP_User_Query();
  460. $this->assertEmpty( $query->query_fields );
  461. $this->assertEmpty( $query->query_from );
  462. $this->assertEmpty( $query->query_limit );
  463. $this->assertEmpty( $query->query_orderby );
  464. $this->assertEmpty( $query->query_where );
  465. $this->assertEmpty( $query->query_vars );
  466. $_query_vars = $query->query_vars;
  467. $query->prepare_query();
  468. $this->assertNotEmpty( $query->query_fields );
  469. $this->assertNotEmpty( $query->query_from );
  470. $this->assertEmpty( $query->query_limit );
  471. $this->assertNotEmpty( $query->query_orderby );
  472. $this->assertNotEmpty( $query->query_where );
  473. $this->assertNotEmpty( $query->query_vars );
  474. $this->assertNotEquals( $_query_vars, $query->query_vars );
  475. // All values get reset.
  476. $query->prepare_query( array( 'number' => 8 ) );
  477. $this->assertNotEmpty( $query->query_limit );
  478. $this->assertSame( 'LIMIT 0, 8', $query->query_limit );
  479. // All values get reset.
  480. $query->prepare_query( array( 'fields' => 'all' ) );
  481. $this->assertEmpty( $query->query_limit );
  482. $this->assertEquals( '', $query->query_limit );
  483. $_query_vars = $query->query_vars;
  484. $query->prepare_query();
  485. $this->assertSame( $_query_vars, $query->query_vars );
  486. $query->prepare_query( array( 'number' => -1 ) );
  487. $this->assertNotEquals( 'LIMIT -1', $query->query_limit );
  488. $this->assertEmpty( $query->query_limit );
  489. }
  490. public function test_meta_vars_should_be_converted_to_meta_query() {
  491. $q = new WP_User_Query(
  492. array(
  493. 'meta_key' => 'foo',
  494. 'meta_value' => '5',
  495. 'meta_compare' => '>',
  496. 'meta_type' => 'SIGNED',
  497. )
  498. );
  499. // Multisite adds a 'blog_id' clause, so we have to find the 'foo' clause.
  500. $mq_clauses = $q->meta_query->get_clauses();
  501. foreach ( $mq_clauses as $mq_clause ) {
  502. if ( 'foo' === $mq_clause['key'] ) {
  503. $clause = $mq_clause;
  504. }
  505. }
  506. $this->assertSame( 'foo', $clause['key'] );
  507. $this->assertSame( '5', $clause['value'] );
  508. $this->assertSame( '>', $clause['compare'] );
  509. $this->assertSame( 'SIGNED', $clause['type'] );
  510. }
  511. /**
  512. * @ticket 23849
  513. */
  514. function test_meta_query_with_role() {
  515. add_user_meta( self::$author_ids[0], 'foo', 'bar' );
  516. add_user_meta( self::$author_ids[1], 'foo', 'baz' );
  517. // Users with foo = bar or baz restricted to the author role.
  518. $query = new WP_User_Query(
  519. array(
  520. 'fields' => '',
  521. 'role' => 'author',
  522. 'meta_query' => array(
  523. 'relation' => 'OR',
  524. array(
  525. 'key' => 'foo',
  526. 'value' => 'bar',
  527. ),
  528. array(
  529. 'key' => 'foo',
  530. 'value' => 'baz',
  531. ),
  532. ),
  533. )
  534. );
  535. $this->assertEquals( array( self::$author_ids[0], self::$author_ids[1] ), $query->get_results() );
  536. }
  537. public function test_roles_and_caps_should_be_populated_for_default_value_of_blog_id() {
  538. $query = new WP_User_Query(
  539. array(
  540. 'include' => self::$author_ids[0],
  541. )
  542. );
  543. $found = $query->get_results();
  544. $this->assertNotEmpty( $found );
  545. $user = reset( $found );
  546. $this->assertSame( array( 'author' ), $user->roles );
  547. $this->assertSame( array( 'author' => true ), $user->caps );
  548. }
  549. /**
  550. * @group ms-excluded
  551. */
  552. public function test_roles_and_caps_should_be_populated_for_explicit_value_of_blog_id_on_nonms() {
  553. $query = new WP_User_Query(
  554. array(
  555. 'include' => self::$author_ids[0],
  556. 'blog_id' => get_current_blog_id(),
  557. )
  558. );
  559. $found = $query->get_results();
  560. $this->assertNotEmpty( $found );
  561. $user = reset( $found );
  562. $this->assertSame( array( 'author' ), $user->roles );
  563. $this->assertSame( array( 'author' => true ), $user->caps );
  564. }
  565. /**
  566. * @group ms-required
  567. */
  568. public function test_roles_and_caps_should_be_populated_for_explicit_value_of_current_blog_id_on_ms() {
  569. $query = new WP_User_Query(
  570. array(
  571. 'include' => self::$author_ids[0],
  572. 'blog_id' => get_current_blog_id(),
  573. )
  574. );
  575. $found = $query->get_results();
  576. $this->assertNotEmpty( $found );
  577. $user = reset( $found );
  578. $this->assertSame( array( 'author' ), $user->roles );
  579. $this->assertSame( array( 'author' => true ), $user->caps );
  580. }
  581. /**
  582. * @group ms-required
  583. */
  584. public function test_roles_and_caps_should_be_populated_for_explicit_value_of_different_blog_id_on_ms_when_fields_all_with_meta() {
  585. $b = self::factory()->blog->create();
  586. add_user_to_blog( $b, self::$author_ids[0], 'author' );
  587. $query = new WP_User_Query(
  588. array(
  589. 'include' => self::$author_ids[0],
  590. 'blog_id' => $b,
  591. 'fields' => 'all_with_meta',
  592. )
  593. );
  594. $found = $query->get_results();
  595. $this->assertNotEmpty( $found );
  596. $user = reset( $found );
  597. $this->assertSame( array( 'author' ), $user->roles );
  598. $this->assertSame( array( 'author' => true ), $user->caps );
  599. }
  600. /**
  601. * @ticket 31878
  602. * @group ms-required
  603. */
  604. public function test_roles_and_caps_should_be_populated_for_explicit_value_of_different_blog_id_on_ms_when_fields_all() {
  605. $b = self::factory()->blog->create();
  606. add_user_to_blog( $b, self::$author_ids[0], 'author' );
  607. $query = new WP_User_Query(
  608. array(
  609. 'fields' => 'all',
  610. 'include' => self::$author_ids[0],
  611. 'blog_id' => $b,
  612. )
  613. );
  614. $found = $query->get_results();
  615. $this->assertNotEmpty( $found );
  616. $user = reset( $found );
  617. $this->assertSame( array( 'author' ), $user->roles );
  618. $this->assertSame( array( 'author' => true ), $user->caps );
  619. }
  620. /**
  621. * @ticket 32019
  622. * @group ms-required
  623. */
  624. public function test_who_authors() {
  625. $b = self::factory()->blog->create();
  626. add_user_to_blog( $b, self::$author_ids[0], 'subscriber' );
  627. add_user_to_blog( $b, self::$author_ids[1], 'author' );
  628. add_user_to_blog( $b, self::$author_ids[2], 'editor' );
  629. $q = new WP_User_Query(
  630. array(
  631. 'who' => 'authors',
  632. 'blog_id' => $b,
  633. )
  634. );
  635. $found = wp_list_pluck( $q->get_results(), 'ID' );
  636. $this->assertNotContains( self::$author_ids[0], $found );
  637. $this->assertContains( self::$author_ids[1], $found );
  638. $this->assertContains( self::$author_ids[2], $found );
  639. }
  640. /**
  641. * @ticket 32019
  642. * @group ms-required
  643. */
  644. public function test_who_authors_should_work_alongside_meta_query() {
  645. $b = self::factory()->blog->create();
  646. add_user_to_blog( $b, self::$author_ids[0], 'subscriber' );
  647. add_user_to_blog( $b, self::$author_ids[1], 'author' );
  648. add_user_to_blog( $b, self::$author_ids[2], 'editor' );
  649. add_user_meta( self::$author_ids[1], 'foo', 'bar' );
  650. add_user_meta( self::$author_ids[2], 'foo', 'baz' );
  651. $q = new WP_User_Query(
  652. array(
  653. 'who' => 'authors',
  654. 'blog_id' => $b,
  655. 'meta_query' => array(
  656. array(
  657. 'key' => 'foo',
  658. 'value' => 'bar',
  659. ),
  660. ),
  661. )
  662. );
  663. $found = wp_list_pluck( $q->get_results(), 'ID' );
  664. $this->assertNotContains( self::$author_ids[0], $found );
  665. $this->assertContains( self::$author_ids[1], $found );
  666. $this->assertNotContains( self::$author_ids[2], $found );
  667. }
  668. /**
  669. * @ticket 36724
  670. * @group ms-required
  671. */
  672. public function test_who_authors_should_work_alongside_meta_params() {
  673. $b = self::factory()->blog->create();
  674. add_user_to_blog( $b, self::$author_ids[0], 'subscriber' );
  675. add_user_to_blog( $b, self::$author_ids[1], 'author' );
  676. add_user_to_blog( $b, self::$author_ids[2], 'editor' );
  677. add_user_meta( self::$author_ids[1], 'foo', 'bar' );
  678. add_user_meta( self::$author_ids[2], 'foo', 'baz' );
  679. $q = new WP_User_Query(
  680. array(
  681. 'who' => 'authors',
  682. 'blog_id' => $b,
  683. 'meta_key' => 'foo',
  684. 'meta_value' => 'bar',
  685. )
  686. );
  687. $found = wp_list_pluck( $q->get_results(), 'ID' );
  688. $this->assertNotContains( self::$author_ids[0], $found );
  689. $this->assertContains( self::$author_ids[1], $found );
  690. $this->assertNotContains( self::$author_ids[2], $found );
  691. }
  692. /**
  693. * @ticket 32250
  694. */
  695. public function test_has_published_posts_with_value_true_should_show_authors_of_posts_in_public_post_types() {
  696. register_post_type( 'wptests_pt_public', array( 'public' => true ) );
  697. register_post_type( 'wptests_pt_private', array( 'public' => false ) );
  698. self::factory()->post->create(
  699. array(
  700. 'post_author' => self::$author_ids[0],
  701. 'post_status' => 'publish',
  702. 'post_type' => 'wptests_pt_public',
  703. )
  704. );
  705. self::factory()->post->create(
  706. array(
  707. 'post_author' => self::$author_ids[1],
  708. 'post_status' => 'publish',
  709. 'post_type' => 'wptests_pt_private',
  710. )
  711. );
  712. $q = new WP_User_Query(
  713. array(
  714. 'has_published_posts' => true,
  715. )
  716. );
  717. $found = wp_list_pluck( $q->get_results(), 'ID' );
  718. $expected = array( self::$author_ids[0] );
  719. $this->assertSameSets( $expected, $found );
  720. }
  721. /**
  722. * @ticket 32250
  723. */
  724. public function test_has_published_posts_should_obey_post_types() {
  725. register_post_type( 'wptests_pt_public', array( 'public' => true ) );
  726. register_post_type( 'wptests_pt_private', array( 'public' => false ) );
  727. self::factory()->post->create(
  728. array(
  729. 'post_author' => self::$author_ids[0],
  730. 'post_status' => 'publish',
  731. 'post_type' => 'wptests_pt_public',
  732. )
  733. );
  734. self::factory()->post->create(
  735. array(
  736. 'post_author' => self::$author_ids[1],
  737. 'post_status' => 'publish',
  738. 'post_type' => 'wptests_pt_private',
  739. )
  740. );
  741. self::factory()->post->create(
  742. array(
  743. 'post_author' => self::$author_ids[2],
  744. 'post_status' => 'publish',
  745. 'post_type' => 'post',
  746. )
  747. );
  748. $q = new WP_User_Query(
  749. array(
  750. 'has_published_posts' => array( 'wptests_pt_private', 'post' ),
  751. )
  752. );
  753. $found = wp_list_pluck( $q->get_results(), 'ID' );
  754. $expected = array( self::$author_ids[1], self::$author_ids[2] );
  755. $this->assertSameSets( $expected, $found );
  756. }
  757. /**
  758. * @ticket 32250
  759. */
  760. public function test_has_published_posts_should_ignore_non_published_posts() {
  761. register_post_type( 'wptests_pt_public', array( 'public' => true ) );
  762. register_post_type( 'wptests_pt_private', array( 'public' => false ) );
  763. self::factory()->post->create(
  764. array(
  765. 'post_author' => self::$author_ids[0],
  766. 'post_status' => 'draft',
  767. 'post_type' => 'wptests_pt_public',
  768. )
  769. );
  770. self::factory()->post->create(
  771. array(
  772. 'post_author' => self::$author_ids[1],
  773. 'post_status' => 'inherit',
  774. 'post_type' => 'wptests_pt_private',
  775. )
  776. );
  777. self::factory()->post->create(
  778. array(
  779. 'post_author' => self::$author_ids[2],
  780. 'post_status' => 'publish',
  781. 'post_type' => 'post',
  782. )
  783. );
  784. $q = new WP_User_Query(
  785. array(
  786. 'has_published_posts' => array( 'wptests_pt_public', 'wptests_pt_private', 'post' ),
  787. )
  788. );
  789. $found = wp_list_pluck( $q->get_results(), 'ID' );
  790. $expected = array( self::$author_ids[2] );
  791. $this->assertSameSets( $expected, $found );
  792. }
  793. /**
  794. * @ticket 32250
  795. * @group ms-required
  796. */
  797. public function test_has_published_posts_should_respect_blog_id() {
  798. $blogs = self::factory()->blog->create_many( 2 );
  799. add_user_to_blog( $blogs[0], self::$author_ids[0], 'author' );
  800. add_user_to_blog( $blogs[0], self::$author_ids[1], 'author' );
  801. add_user_to_blog( $blogs[1], self::$author_ids[0], 'author' );
  802. add_user_to_blog( $blogs[1], self::$author_ids[1], 'author' );
  803. switch_to_blog( $blogs[0] );
  804. self::factory()->post->create(
  805. array(
  806. 'post_author' => self::$author_ids[0],
  807. 'post_status' => 'publish',
  808. 'post_type' => 'post',
  809. )
  810. );
  811. restore_current_blog();
  812. switch_to_blog( $blogs[1] );
  813. self::factory()->post->create(
  814. array(
  815. 'post_author' => self::$author_ids[1],
  816. 'post_status' => 'publish',
  817. 'post_type' => 'post',
  818. )
  819. );
  820. restore_current_blog();
  821. $q = new WP_User_Query(
  822. array(
  823. 'has_published_posts' => array( 'post' ),
  824. 'blog_id' => $blogs[1],
  825. )
  826. );
  827. $found = wp_list_pluck( $q->get_results(), 'ID' );
  828. $expected = array( self::$author_ids[1] );
  829. $this->assertSameSets( $expected, $found );
  830. }
  831. /**
  832. * @ticket 32592
  833. */
  834. public function test_top_level_or_meta_query_should_eliminate_duplicate_matches() {
  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. $q = new WP_User_Query(
  839. array(
  840. 'meta_query' => array(
  841. 'relation' => 'OR',
  842. array(
  843. 'key' => 'foo',
  844. 'value' => 'bar',
  845. ),
  846. array(
  847. 'key' => 'foo2',
  848. 'value' => 'bar2',
  849. ),
  850. ),
  851. )
  852. );
  853. $found = wp_list_pluck( $q->get_results(), 'ID' );
  854. $expected = array( self::$author_ids[0], self::$author_ids[1] );
  855. $this->assertSameSets( $expected, $found );
  856. }
  857. /**
  858. * @ticket 32592
  859. */
  860. public function test_nested_or_meta_query_should_eliminate_duplicate_matches() {
  861. add_user_meta( self::$author_ids[0], 'foo', 'bar' );
  862. add_user_meta( self::$author_ids[1], 'foo', 'bar' );
  863. add_user_meta( self::$author_ids[0], 'foo2', 'bar2' );
  864. add_user_meta( self::$author_ids[1], 'foo3', 'bar3' );
  865. $q = new WP_User_Query(
  866. array(
  867. 'meta_query' => array(
  868. 'relation' => 'AND',
  869. array(
  870. 'key' => 'foo',
  871. 'value' => 'bar',
  872. ),
  873. array(
  874. 'relation' => 'OR',
  875. array(
  876. 'key' => 'foo',
  877. 'value' => 'bar',
  878. ),
  879. array(
  880. 'key' => 'foo2',
  881. 'value' => 'bar2',
  882. ),
  883. ),
  884. ),
  885. )
  886. );
  887. $found = wp_list_pluck( $q->get_results(), 'ID' );
  888. $expected = array( self::$author_ids[0], self::$author_ids[1] );
  889. $this->assertSameSets( $expected, $found );
  890. }
  891. /**
  892. * @ticket 36624
  893. */
  894. public function test_nicename_returns_user_with_nicename() {
  895. wp_update_user(
  896. array(
  897. 'ID' => self::$author_ids[0],
  898. 'user_nicename' => 'peter',
  899. )
  900. );
  901. $q = new WP_User_Query(
  902. array(
  903. 'nicename' => 'peter',
  904. )
  905. );
  906. $found = wp_list_pluck( $q->get_results(), 'ID' );
  907. $expected = array( self::$author_ids[0] );
  908. $this->assertContains( "AND user_nicename = 'peter'", $q->query_where );
  909. $this->assertSameSets( $expected, $found );
  910. }
  911. /**
  912. * @ticket 36624
  913. */
  914. public function test_nicename__in_returns_users_with_included_nicenames() {
  915. wp_update_user(
  916. array(
  917. 'ID' => self::$author_ids[0],
  918. 'user_nicename' => 'peter',
  919. )
  920. );
  921. wp_update_user(
  922. array(
  923. 'ID' => self::$author_ids[1],
  924. 'user_nicename' => 'paul',
  925. )
  926. );
  927. wp_update_user(
  928. array(
  929. 'ID' => self::$author_ids[2],
  930. 'user_nicename' => 'mary',
  931. )
  932. );
  933. $q = new WP_User_Query(
  934. array(
  935. 'nicename__in' => array( 'peter', 'paul', 'mary' ),
  936. )
  937. );
  938. $found = wp_list_pluck( $q->get_results(), 'ID' );
  939. $expected = array( self::$author_ids[0], self::$author_ids[1], self::$author_ids[2] );
  940. $this->assertContains( "AND user_nicename IN ( 'peter','paul','mary' )", $q->query_where );
  941. $this->assertSameSets( $expected, $found );
  942. }
  943. /**
  944. * @ticket 36624
  945. */
  946. public function test_nicename__not_in_returns_users_without_included_nicenames() {
  947. wp_update_user(
  948. array(
  949. 'ID' => self::$author_ids[0],
  950. 'user_nicename' => 'peter',
  951. )
  952. );
  953. wp_update_user(
  954. array(
  955. 'ID' => self::$author_ids[1],
  956. 'user_nicename' => 'paul',
  957. )
  958. );
  959. wp_update_user(
  960. array(
  961. 'ID' => self::$author_ids[2],
  962. 'user_nicename' => 'mary',
  963. )
  964. );
  965. $q = new WP_User_Query(
  966. array(
  967. 'nicename__not_in' => array( 'peter', 'paul', 'mary' ),
  968. )
  969. );
  970. $found_count = count( $q->get_results() );
  971. $expected_count = 10; // 13 total users minus 3 from query.
  972. $this->assertContains( "AND user_nicename NOT IN ( 'peter','paul','mary' )", $q->query_where );
  973. $this->assertSame( $expected_count, $found_count );
  974. }
  975. /**
  976. * @ticket 36624
  977. */
  978. public function test_orderby_nicename__in() {
  979. wp_update_user(
  980. array(
  981. 'ID' => self::$author_ids[0],
  982. 'user_nicename' => 'peter',
  983. )
  984. );
  985. wp_update_user(
  986. array(
  987. 'ID' => self::$author_ids[1],
  988. 'user_nicename' => 'paul',
  989. )
  990. );
  991. wp_update_user(
  992. array(
  993. 'ID' => self::$author_ids[2],
  994. 'user_nicename' => 'mary',
  995. )
  996. );
  997. $q = new WP_User_Query(
  998. array(
  999. 'nicename__in' => array( 'mary', 'peter', 'paul' ),
  1000. 'orderby' => 'nicename__in',
  1001. )
  1002. );
  1003. $found = wp_list_pluck( $q->get_results(), 'ID' );
  1004. $expected = array( self::$author_ids[2], self::$author_ids[0], self::$author_ids[1] );
  1005. $this->assertContains( "FIELD( user_nicename, 'mary','peter','paul' )", $q->query_orderby );
  1006. $this->assertSame( $expected, $found );
  1007. }
  1008. /**
  1009. * @ticket 36624
  1010. */
  1011. public function test_login_returns_user_with_login() {
  1012. $user_login = get_userdata( self::$author_ids[0] )->user_login;
  1013. $q = new WP_User_Query(
  1014. array(
  1015. 'login' => $user_login,
  1016. )
  1017. );
  1018. $found = wp_list_pluck( $q->get_results(), 'ID' );
  1019. $expected = array( self::$author_ids[0] );
  1020. $this->assertContains( "AND user_login = '$user_login'", $q->query_where );
  1021. $this->assertSameSets( $expected, $found );
  1022. }
  1023. /**
  1024. * @ticket 36624
  1025. */
  1026. public function test_login__in_returns_users_with_included_logins() {
  1027. $user_login1 = get_userdata( self::$author_ids[0] )->user_login;
  1028. $user_login2 = get_userdata( self::$author_ids[1] )->user_login;
  1029. $user_login3 = get_userdata( self::$author_ids[2] )->user_login;
  1030. $q = new WP_User_Query(
  1031. array(
  1032. 'login__in' => array( $user_login1, $user_login2, $user_login3 ),
  1033. )
  1034. );
  1035. $found = wp_list_pluck( $q->get_results(), 'ID' );
  1036. $expected = array( self::$author_ids[0], self::$author_ids[1], self::$author_ids[2] );
  1037. $this->assertContains( "AND user_login IN ( '$user_login1','$user_login2','$user_login3' )", $q->query_where );
  1038. $this->assertSameSets( $expected, $found );
  1039. }
  1040. /**
  1041. * @ticket 36624
  1042. */
  1043. public function test_login__not_in_returns_users_without_included_logins() {
  1044. $user_login1 = get_userdata( self::$author_ids[0] )->user_login;
  1045. $user_login2 = get_userdata( self::$author_ids[1] )->user_login;
  1046. $user_login3 = get_userdata( self::$author_ids[2] )->user_login;
  1047. $q = new WP_User_Query(
  1048. array(
  1049. 'login__not_in' => array( $user_login1, $user_login2, $user_login3 ),
  1050. )
  1051. );
  1052. $found_count = count( $q->get_results() );
  1053. $expected_count = 10; // 13 total users minus 3 from query.
  1054. $this->assertContains( "AND user_login NOT IN ( '$user_login1','$user_login2','$user_login3' )", $q->query_where );
  1055. $this->assertSame( $expected_count, $found_count );
  1056. }
  1057. /**
  1058. * @ticket 36624
  1059. */
  1060. public function test_orderby_login__in() {
  1061. $user_login1 = get_userdata( self::$author_ids[0] )->user_login;
  1062. $user_login2 = get_userdata( self::$author_ids[1] )->user_login;
  1063. $user_login3 = get_userdata( self::$author_ids[2] )->user_login;
  1064. $q = new WP_User_Query(
  1065. array(
  1066. 'login__in' => array( $user_login2, $user_login3, $user_login1 ),
  1067. 'orderby' => 'login__in',
  1068. )
  1069. );
  1070. $found = wp_list_pluck( $q->get_results(), 'ID' );
  1071. $expected = array( self::$author_ids[1], self::$author_ids[2], self::$author_ids[0] );
  1072. $this->assertContains( "FIELD( user_login, '$user_login2','$user_login3','$user_login1' )", $q->query_orderby );
  1073. $this->assertSame( $expected, $found );
  1074. }
  1075. /**
  1076. * @ticket 25145
  1077. */
  1078. public function test_paged() {
  1079. $q = new WP_User_Query(
  1080. array(
  1081. 'number' => 2,
  1082. 'paged' => 2,
  1083. 'orderby' => 'ID',
  1084. 'order' => 'DESC', // Avoid funkiness with user 1.
  1085. 'fields' => 'ids',
  1086. )
  1087. );
  1088. $this->assertEquals( array( self::$contrib_id, self::$editor_ids[2] ), $q->results );
  1089. }
  1090. /**
  1091. * @ticket 33449
  1092. */
  1093. public function test_query_vars_should_be_filled_in_after_pre_get_users() {
  1094. $query_vars = array( 'blog_id', 'role', 'meta_key', 'meta_value', 'meta_compare', 'include', 'exclude', 'search', 'search_columns', 'orderby', 'order', 'offset', 'number', 'paged', 'count_total', 'fields', 'who', 'has_published_posts' );
  1095. add_action( 'pre_get_users', array( $this, 'filter_pre_get_users_args' ) );
  1096. $q = new WP_User_Query( array_fill_keys( $query_vars, '1' ) );
  1097. remove_action( 'pre_get_users', array( $this, 'filter_pre_get_users_args' ) );
  1098. foreach ( $query_vars as $query_var ) {
  1099. $this->assertTrue( array_key_exists( $query_var, $q->query_vars ), "$query_var does not exist." );
  1100. }
  1101. }
  1102. public function filter_pre_get_users_args( $q ) {
  1103. foreach ( $q->query_vars as $k => $v ) {
  1104. unset( $q->query_vars[ $k ] );
  1105. }
  1106. }
  1107. /**
  1108. * @ticket 22212
  1109. */
  1110. public function test_get_single_role_by_user_query() {
  1111. $wp_user_search = new WP_User_Query( array( 'role' => 'subscriber' ) );
  1112. $users = $wp_user_search->get_results();
  1113. $this->assertSame( 2, count( $users ) );
  1114. }
  1115. /**
  1116. * @ticket 22212
  1117. */
  1118. public function test_get_multiple_roles_by_user_query() {
  1119. $wp_user_search = new WP_User_Query( array( 'role__in' => array( 'subscriber', 'editor' ) ) );
  1120. $users = $wp_user_search->get_results();
  1121. $this->assertSame( 5, count( $users ) );
  1122. }
  1123. /**
  1124. * @ticket 22212
  1125. */
  1126. public function test_get_single_role_by_string() {
  1127. $users = get_users(
  1128. array(
  1129. 'role' => 'subscriber',
  1130. )
  1131. );
  1132. $this->assertSame( 2, count( $users ) );
  1133. }
  1134. /**
  1135. * @ticket 22212
  1136. */
  1137. public function test_get_single_role_by_string_which_is_similar() {
  1138. $another_editor = self::factory()->user->create(
  1139. array(
  1140. 'user_email' => 'another_editor@another_editor.com',
  1141. 'user_login' => 'another_editor',
  1142. 'role' => 'another-editor',
  1143. )
  1144. );
  1145. $users = get_users(
  1146. array(
  1147. 'role' => 'editor',
  1148. 'fields' => 'ids',
  1149. )
  1150. );
  1151. $this->assertEqualSets( self::$editor_ids, $users );
  1152. }
  1153. /**
  1154. * @ticket 22212
  1155. */
  1156. public function test_get_single_role_by_array() {
  1157. $users = get_users(
  1158. array(
  1159. 'role' => array( 'subscriber' ),
  1160. )
  1161. );
  1162. $this->assertSame( 2, count( $users ) );
  1163. }
  1164. /**
  1165. * @ticket 22212
  1166. */
  1167. public function test_get_multiple_roles_should_only_match_users_who_have_each_role() {
  1168. $users = new WP_User_Query( array( 'role' => array( 'subscriber', 'editor' ) ) );
  1169. $users = $users->get_results();
  1170. $this->assertEmpty( $users );
  1171. foreach ( self::$sub_ids as $subscriber ) {
  1172. $subscriber = get_user_by( 'ID', $subscriber );
  1173. $subscriber->add_role( 'editor' );
  1174. }
  1175. $users = new WP_User_Query( array( 'role' => array( 'subscriber', 'editor' ) ) );
  1176. $users = $users->get_results();
  1177. $this->assertSame( 2, count( $users ) );
  1178. foreach ( $users as $user ) {
  1179. $this->assertInstanceOf( 'WP_User', $user );
  1180. }
  1181. }
  1182. /**
  1183. * @ticket 22212
  1184. */
  1185. public function test_get_multiple_roles_or() {
  1186. $users = new WP_User_Query( array( 'role__in' => array( 'subscriber', 'editor', 'administrator' ) ) );
  1187. $users = $users->get_results();
  1188. // +1 for the default user created during installation.
  1189. $this->assertSame( 8, count( $users ) );
  1190. foreach ( $users as $user ) {
  1191. $this->assertInstanceOf( 'WP_User', $user );
  1192. }
  1193. }
  1194. /**
  1195. * @ticket 22212
  1196. */
  1197. public function test_get_multiple_roles_by_comma_separated_list() {
  1198. $users = get_users(
  1199. array(
  1200. 'role' => 'subscriber, editor',
  1201. )
  1202. );
  1203. $this->assertEmpty( $users );
  1204. foreach ( self::$sub_ids as $subscriber ) {
  1205. $subscriber = get_user_by( 'ID', $subscriber );
  1206. $subscriber->add_role( 'editor' );
  1207. }
  1208. $users = get_users(
  1209. array(
  1210. 'role' => 'subscriber, editor',
  1211. )
  1212. );
  1213. $this->assertSame( 2, count( $users ) );
  1214. }
  1215. /**
  1216. * @ticket 22212
  1217. */
  1218. public function test_get_multiple_roles_with_meta() {
  1219. // Create administrator user + meta.
  1220. update_user_meta( self::$admin_ids[0], 'mk1', 1 );
  1221. update_user_meta( self::$admin_ids[0], 'mk2', 1 );
  1222. // Create editor user + meta.
  1223. update_user_meta( self::$editor_ids[0], 'mk1', 1 );
  1224. update_user_meta( self::$editor_ids[0], 'mk2', 2 );
  1225. // Create subscriber user + meta.
  1226. update_user_meta( self::$sub_ids[0], 'mk1', 1 );
  1227. update_user_meta( self::$sub_ids[0], 'mk2', 1 );
  1228. // Create contributor user + meta.
  1229. update_user_meta( self::$contrib_id, 'mk1', 1 );
  1230. update_user_meta( self::$contrib_id, 'mk2', 2 );
  1231. // Fetch users.
  1232. $users = get_users(
  1233. array(
  1234. 'role__in' => array( 'administrator', 'editor', 'subscriber' ),
  1235. 'meta_query' => array(
  1236. 'relation' => 'AND',
  1237. array(
  1238. 'key' => 'mk1',
  1239. 'value' => '1',
  1240. 'compare' => '=',
  1241. 'type' => 'numeric',
  1242. ),
  1243. array(
  1244. 'key' => 'mk2',
  1245. 'value' => '2',
  1246. 'compare' => '=',
  1247. 'type' => 'numeric',
  1248. ),
  1249. ),
  1250. )
  1251. );
  1252. // Check results.
  1253. $this->assertSame( 1, count( $users ) );
  1254. $this->assertSame( self::$editor_ids[0], (int) $users[0]->ID );
  1255. }
  1256. /**
  1257. * @ticket 22212
  1258. */
  1259. public function test_role_exclusion() {
  1260. $users = get_users(
  1261. array(
  1262. 'role__not_in' => 'subscriber',
  1263. )
  1264. );
  1265. // +1 for the default user created during installation.
  1266. $this->assertSame( 11, count( $users ) );
  1267. $users = get_users(
  1268. array(
  1269. 'role__not_in' => 'editor',
  1270. )
  1271. );
  1272. // +1 for the default user created during installation.
  1273. $this->assertSame( 10, count( $users ) );
  1274. }
  1275. /**
  1276. * @ticket 22212
  1277. */
  1278. public function test_role__in_role__not_in_combined() {
  1279. foreach ( self::$sub_ids as $subscriber ) {
  1280. $subscriber = get_user_by( 'ID', $subscriber );
  1281. $subscriber->add_role( 'editor' );
  1282. }
  1283. $users = get_users(
  1284. array(
  1285. 'role__in' => 'editor',
  1286. )
  1287. );
  1288. $this->assertSame( 5, count( $users ) );
  1289. $users = get_users(
  1290. array(
  1291. 'role__in' => 'editor',
  1292. 'role__not_in' => 'subscriber',
  1293. )
  1294. );
  1295. $this->assertSame( 3, count( $users ) );
  1296. }
  1297. /**
  1298. * @ticket 22212
  1299. */
  1300. public function test_role__not_in_role_combined() {
  1301. $subscriber = get_user_by( 'ID', self::$sub_ids[0] );
  1302. $subscriber->add_role( 'editor' );
  1303. $users = get_users(
  1304. array(
  1305. 'role' => 'subscriber',
  1306. 'role__not_in' => array( 'editor' ),
  1307. )
  1308. );
  1309. $this->assertSame( 1, count( $users ) );
  1310. }
  1311. /**
  1312. * @ticket 22212
  1313. */
  1314. public function test_role__not_in_user_without_role() {
  1315. $user_without_rule = get_user_by( 'ID', self::$sub_ids[0] );
  1316. $user_without_rule->remove_role( 'subscriber' );
  1317. $users = get_users(
  1318. array(
  1319. 'role__not_in' => 'subscriber',
  1320. )
  1321. );
  1322. // +1 for the default user created during installation.
  1323. $this->assertSame( 12, count( $users ) );
  1324. $users = get_users(
  1325. array(
  1326. 'role__not_in' => 'editor',
  1327. )
  1328. );
  1329. // +1 for the default user created during installation.
  1330. $this->assertSame( 10, count( $users ) );
  1331. }
  1332. /**
  1333. * @ticket 22212
  1334. * @group ms-required
  1335. */
  1336. public function test_blog_id_should_restrict_by_blog_without_requiring_a_named_role() {
  1337. $sites = self::factory()->blog->create_many( 2 );
  1338. add_user_to_blog( $sites[0], self::$author_ids[0], 'author' );
  1339. add_user_to_blog( $sites[1], self::$author_ids[1], 'author' );
  1340. $found = get_users(
  1341. array(
  1342. 'blog_id' => $sites[1],
  1343. 'fields' => 'ID',
  1344. )
  1345. );
  1346. $this->assertEqualSets( array( self::$author_ids[1] ), $found );
  1347. }
  1348. /**
  1349. * @ticket 22212
  1350. * @ticket 21119
  1351. * @group ms-required
  1352. */
  1353. public function test_calling_prepare_query_a_second_time_should_not_add_another_cap_query_on_multisite() {
  1354. $site_id = get_current_blog_id();
  1355. add_user_to_blog( $site_id, self::$author_ids[0], 'author' );
  1356. $q = new WP_User_Query(
  1357. array(
  1358. 'include' => self::$author_ids[0],
  1359. )
  1360. );
  1361. $r1 = $q->request;
  1362. $q->prepare_query(
  1363. array(
  1364. 'include' => self::$author_ids[0],
  1365. )
  1366. );
  1367. $r2 = $q->request;
  1368. $q->prepare_query(
  1369. array(
  1370. 'include' => self::$author_ids[0],
  1371. )
  1372. );
  1373. $r3 = $q->request;
  1374. $this->assertSame( $r1, $r2 );
  1375. $this->assertSame( $r1, $r3 );
  1376. }
  1377. /**
  1378. * @ticket 39643
  1379. */
  1380. public function test_search_by_display_name_only() {
  1381. $new_user1 = $this->factory->user->create(
  1382. array(
  1383. 'user_login' => 'name1',
  1384. 'display_name' => 'Sophia Andresen',
  1385. )
  1386. );
  1387. self::$author_ids[] = $new_user1;
  1388. $q = new WP_User_Query(
  1389. array(
  1390. 'search' => '*Sophia*',
  1391. 'fields' => '',
  1392. 'search_columns' => array( 'display_name' ),
  1393. 'include' => self::$author_ids,
  1394. )
  1395. );
  1396. $ids = $q->get_results();
  1397. // Must include user that has the same string in display_name.
  1398. $this->assertEquals( array( $new_user1 ), $ids );
  1399. }
  1400. /**
  1401. * @ticket 39643
  1402. */
  1403. public function test_search_by_display_name_only_ignore_others() {
  1404. $new_user1 = $this->factory->user->create(
  1405. array(
  1406. 'user_login' => 'Sophia Andresen',
  1407. 'display_name' => 'name1',
  1408. )
  1409. );
  1410. self::$author_ids[] = $new_user1;
  1411. $q = new WP_User_Query(
  1412. array(
  1413. 'search' => '*Sophia*',
  1414. 'fields' => '',
  1415. 'search_columns' => array( 'display_name' ),
  1416. 'include' => self::$author_ids,
  1417. )
  1418. );
  1419. $ids = $q->get_results();
  1420. // Must not include user that has the same string in other fields.
  1421. $this->assertSame( array(), $ids );
  1422. }
  1423. /**
  1424. * @ticket 44169
  1425. */
  1426. public function test_users_pre_query_filter_should_bypass_database_query() {
  1427. global $wpdb;
  1428. add_filter( 'users_pre_query', array( __CLASS__, 'filter_users_pre_query' ), 10, 2 );
  1429. $num_queries = $wpdb->num_queries;
  1430. $q = new WP_User_Query(
  1431. array(
  1432. 'fields' => 'ID',
  1433. )
  1434. );
  1435. remove_filter( 'users_pre_query', array( __CLASS__, 'filter_users_pre_query' ), 10, 2 );
  1436. // Make sure no queries were executed.
  1437. $this->assertSame( $num_queries, $wpdb->num_queries );
  1438. // We manually inserted a non-existing user and overrode the results with it.
  1439. $this->assertSame( array( 555 ), $q->results );
  1440. // Make sure manually setting total_users doesn't get overwritten.
  1441. $this->assertSame( 1, $q->total_users );
  1442. }
  1443. public static function filter_users_pre_query( $posts, $query ) {
  1444. $query->total_users = 1;
  1445. return array( 555 );
  1446. }
  1447. }