listAuthors.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. /**
  3. * @group author
  4. * @group user
  5. */
  6. class Tests_User_ListAuthors extends WP_UnitTestCase {
  7. public static $user_ids = array();
  8. public static $fred_id;
  9. public static $posts = array();
  10. public static $user_urls = array();
  11. /* Defaults
  12. 'orderby' => 'name',
  13. 'order' => 'ASC',
  14. 'number' => null,
  15. 'optioncount' => false,
  16. 'exclude_admin' => true,
  17. 'show_fullname' => false,
  18. 'hide_empty' => true,
  19. 'echo' => true,
  20. 'feed' => [empty string],
  21. 'feed_image' => [empty string],
  22. 'feed_type' => [empty string],
  23. 'style' => 'list',
  24. 'html' => true );
  25. */
  26. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  27. self::$user_ids[] = $factory->user->create(
  28. array(
  29. 'user_login' => 'zack',
  30. 'display_name' => 'zack',
  31. 'role' => 'author',
  32. 'first_name' => 'zack',
  33. 'last_name' => 'moon',
  34. )
  35. );
  36. self::$user_ids[] = $factory->user->create(
  37. array(
  38. 'user_login' => 'bob',
  39. 'display_name' => 'bob',
  40. 'role' => 'author',
  41. 'first_name' => 'bob',
  42. 'last_name' => 'reno',
  43. )
  44. );
  45. self::$user_ids[] = $factory->user->create(
  46. array(
  47. 'user_login' => 'paul',
  48. 'display_name' => 'paul',
  49. 'role' => 'author',
  50. 'first_name' => 'paul',
  51. 'last_name' => 'norris',
  52. )
  53. );
  54. self::$fred_id = $factory->user->create(
  55. array(
  56. 'user_login' => 'fred',
  57. 'role' => 'author',
  58. )
  59. );
  60. $count = 0;
  61. foreach ( self::$user_ids as $userid ) {
  62. $count = $count + 1;
  63. for ( $i = 0; $i < $count; $i++ ) {
  64. self::$posts[] = $factory->post->create(
  65. array(
  66. 'post_type' => 'post',
  67. 'post_author' => $userid,
  68. )
  69. );
  70. }
  71. self::$user_urls[] = get_author_posts_url( $userid );
  72. }
  73. }
  74. function test_wp_list_authors_default() {
  75. $expected['default'] =
  76. '<li><a href="' . self::$user_urls[1] . '" title="Posts by bob">bob</a></li>' .
  77. '<li><a href="' . self::$user_urls[2] . '" title="Posts by paul">paul</a></li>' .
  78. '<li><a href="' . self::$user_urls[0] . '" title="Posts by zack">zack</a></li>';
  79. $this->assertSame( $expected['default'], wp_list_authors( array( 'echo' => false ) ) );
  80. }
  81. function test_wp_list_authors_orderby() {
  82. $expected['post_count'] =
  83. '<li><a href="' . self::$user_urls[0] . '" title="Posts by zack">zack</a></li>' .
  84. '<li><a href="' . self::$user_urls[1] . '" title="Posts by bob">bob</a></li>' .
  85. '<li><a href="' . self::$user_urls[2] . '" title="Posts by paul">paul</a></li>';
  86. $this->assertSame(
  87. $expected['post_count'],
  88. wp_list_authors(
  89. array(
  90. 'echo' => false,
  91. 'orderby' => 'post_count',
  92. )
  93. )
  94. );
  95. }
  96. function test_wp_list_authors_order() {
  97. $expected['id'] =
  98. '<li><a href="' . self::$user_urls[2] . '" title="Posts by paul">paul</a></li>' .
  99. '<li><a href="' . self::$user_urls[1] . '" title="Posts by bob">bob</a></li>' .
  100. '<li><a href="' . self::$user_urls[0] . '" title="Posts by zack">zack</a></li>';
  101. $this->assertSame(
  102. $expected['id'],
  103. wp_list_authors(
  104. array(
  105. 'echo' => false,
  106. 'orderby' => 'id',
  107. 'order' => 'DESC',
  108. )
  109. )
  110. );
  111. }
  112. function test_wp_list_authors_optioncount() {
  113. $expected['optioncount'] =
  114. '<li><a href="' . self::$user_urls[1] . '" title="Posts by bob">bob</a> (2)</li>' .
  115. '<li><a href="' . self::$user_urls[2] . '" title="Posts by paul">paul</a> (3)</li>' .
  116. '<li><a href="' . self::$user_urls[0] . '" title="Posts by zack">zack</a> (1)</li>';
  117. $this->assertSame(
  118. $expected['optioncount'],
  119. wp_list_authors(
  120. array(
  121. 'echo' => false,
  122. 'optioncount' => 1,
  123. )
  124. )
  125. );
  126. }
  127. function test_wp_list_authors_exclude_admin() {
  128. self::factory()->post->create(
  129. array(
  130. 'post_type' => 'post',
  131. 'post_author' => 1,
  132. )
  133. );
  134. $expected['exclude_admin'] =
  135. '<li><a href="' . get_author_posts_url( 1 ) . '" title="Posts by admin">admin</a></li>' .
  136. '<li><a href="' . self::$user_urls[1] . '" title="Posts by bob">bob</a></li>' .
  137. '<li><a href="' . self::$user_urls[2] . '" title="Posts by paul">paul</a></li>' .
  138. '<li><a href="' . self::$user_urls[0] . '" title="Posts by zack">zack</a></li>';
  139. $this->assertSame(
  140. $expected['exclude_admin'],
  141. wp_list_authors(
  142. array(
  143. 'echo' => false,
  144. 'exclude_admin' => 0,
  145. )
  146. )
  147. );
  148. }
  149. function test_wp_list_authors_show_fullname() {
  150. $expected['show_fullname'] =
  151. '<li><a href="' . self::$user_urls[1] . '" title="Posts by bob">bob reno</a></li>' .
  152. '<li><a href="' . self::$user_urls[2] . '" title="Posts by paul">paul norris</a></li>' .
  153. '<li><a href="' . self::$user_urls[0] . '" title="Posts by zack">zack moon</a></li>';
  154. $this->assertSame(
  155. $expected['show_fullname'],
  156. wp_list_authors(
  157. array(
  158. 'echo' => false,
  159. 'show_fullname' => 1,
  160. )
  161. )
  162. );
  163. }
  164. function test_wp_list_authors_hide_empty() {
  165. $fred_id = self::$fred_id;
  166. $expected['hide_empty'] =
  167. '<li><a href="' . self::$user_urls[1] . '" title="Posts by bob">bob</a></li>' .
  168. '<li><a href="' . get_author_posts_url( $fred_id ) . '" title="Posts by fred">fred</a></li>' .
  169. '<li><a href="' . self::$user_urls[2] . '" title="Posts by paul">paul</a></li>' .
  170. '<li><a href="' . self::$user_urls[0] . '" title="Posts by zack">zack</a></li>';
  171. $this->assertSame(
  172. $expected['hide_empty'],
  173. wp_list_authors(
  174. array(
  175. 'echo' => false,
  176. 'hide_empty' => 0,
  177. )
  178. )
  179. );
  180. }
  181. function test_wp_list_authors_echo() {
  182. $expected['echo'] =
  183. '<li><a href="' . self::$user_urls[1] . '" title="Posts by bob">bob</a></li>' .
  184. '<li><a href="' . self::$user_urls[2] . '" title="Posts by paul">paul</a></li>' .
  185. '<li><a href="' . self::$user_urls[0] . '" title="Posts by zack">zack</a></li>';
  186. $this->expectOutputString( $expected['echo'] );
  187. wp_list_authors( array( 'echo' => true ) );
  188. }
  189. function test_wp_list_authors_feed() {
  190. $url0 = get_author_feed_link( self::$user_ids[0] );
  191. $url1 = get_author_feed_link( self::$user_ids[1] );
  192. $url2 = get_author_feed_link( self::$user_ids[2] );
  193. $expected['feed'] =
  194. '<li><a href="' . self::$user_urls[1] . '" title="Posts by bob">bob</a> (<a href="' . $url1 . '">link to feed</a>)</li>' .
  195. '<li><a href="' . self::$user_urls[2] . '" title="Posts by paul">paul</a> (<a href="' . $url2 . '">link to feed</a>)</li>' .
  196. '<li><a href="' . self::$user_urls[0] . '" title="Posts by zack">zack</a> (<a href="' . $url0 . '">link to feed</a>)</li>';
  197. $this->assertSame(
  198. $expected['feed'],
  199. wp_list_authors(
  200. array(
  201. 'echo' => false,
  202. 'feed' => 'link to feed',
  203. )
  204. )
  205. );
  206. }
  207. function test_wp_list_authors_feed_image() {
  208. $url0 = get_author_feed_link( self::$user_ids[0] );
  209. $url1 = get_author_feed_link( self::$user_ids[1] );
  210. $url2 = get_author_feed_link( self::$user_ids[2] );
  211. $expected['feed_image'] =
  212. '<li><a href="' . self::$user_urls[1] . '" title="Posts by bob">bob</a> <a href="' . $url1 . '"><img src="http://' . WP_TESTS_DOMAIN . '/path/to/a/graphic.png" style="border: none;" /></a></li>' .
  213. '<li><a href="' . self::$user_urls[2] . '" title="Posts by paul">paul</a> <a href="' . $url2 . '"><img src="http://' . WP_TESTS_DOMAIN . '/path/to/a/graphic.png" style="border: none;" /></a></li>' .
  214. '<li><a href="' . self::$user_urls[0] . '" title="Posts by zack">zack</a> <a href="' . $url0 . '"><img src="http://' . WP_TESTS_DOMAIN . '/path/to/a/graphic.png" style="border: none;" /></a></li>';
  215. $this->assertSame(
  216. $expected['feed_image'],
  217. wp_list_authors(
  218. array(
  219. 'echo' => false,
  220. 'feed_image' => WP_TESTS_DOMAIN . '/path/to/a/graphic.png',
  221. )
  222. )
  223. );
  224. }
  225. /**
  226. * @ticket 26538
  227. */
  228. function test_wp_list_authors_feed_type() {
  229. $url0 = get_author_feed_link( self::$user_ids[0], 'atom' );
  230. $url1 = get_author_feed_link( self::$user_ids[1], 'atom' );
  231. $url2 = get_author_feed_link( self::$user_ids[2], 'atom' );
  232. $expected['feed_type'] =
  233. '<li><a href="' . self::$user_urls[1] . '" title="Posts by bob">bob</a> (<a href="' . $url1 . '">link to feed</a>)</li>' .
  234. '<li><a href="' . self::$user_urls[2] . '" title="Posts by paul">paul</a> (<a href="' . $url2 . '">link to feed</a>)</li>' .
  235. '<li><a href="' . self::$user_urls[0] . '" title="Posts by zack">zack</a> (<a href="' . $url0 . '">link to feed</a>)</li>';
  236. $this->assertSame(
  237. $expected['feed_type'],
  238. wp_list_authors(
  239. array(
  240. 'echo' => false,
  241. 'feed' => 'link to feed',
  242. 'feed_type' => 'atom',
  243. )
  244. )
  245. );
  246. }
  247. function test_wp_list_authors_style() {
  248. $expected['style'] =
  249. '<a href="' . self::$user_urls[1] . '" title="Posts by bob">bob</a>, ' .
  250. '<a href="' . self::$user_urls[2] . '" title="Posts by paul">paul</a>, ' .
  251. '<a href="' . self::$user_urls[0] . '" title="Posts by zack">zack</a>';
  252. $this->assertSame(
  253. $expected['style'],
  254. wp_list_authors(
  255. array(
  256. 'echo' => false,
  257. 'style' => 'none',
  258. )
  259. )
  260. );
  261. }
  262. function test_wp_list_authors_html() {
  263. $expected['html'] = 'bob, paul, zack';
  264. $this->assertSame(
  265. $expected['html'],
  266. wp_list_authors(
  267. array(
  268. 'echo' => false,
  269. 'html' => 0,
  270. )
  271. )
  272. );
  273. }
  274. }