wpGenerateTagCloud.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /**
  3. * @group taxonomy
  4. */
  5. class Tests_WP_Generate_Tag_Cloud extends WP_UnitTestCase {
  6. protected $terms = array();
  7. /**
  8. * Testing when passed $tags array is empty
  9. *
  10. * @dataProvider empty_tags_data_provider
  11. *
  12. * @param $expected Expected output from `wp_generate_tag_cloud()`.
  13. * @param $args Options for `wp_generate_tag_cloud()`.
  14. */
  15. public function test_empty_tags_passed( $expected, $args ) {
  16. $empty_tags = array();
  17. $this->assertSame( $expected, wp_generate_tag_cloud( $empty_tags, $args ) );
  18. }
  19. /**
  20. * Testing when no tags are found
  21. *
  22. * @dataProvider empty_tags_data_provider
  23. *
  24. * @param $expected Expected output from `wp_generate_tag_cloud()`.
  25. * @param $args Options for `wp_generate_tag_cloud()`.
  26. */
  27. function test_empty_tags_list_returned( $expected, $args ) {
  28. $term_ids = self::factory()->term->create_many( 4, array( 'taxonomy' => 'post_tag' ) );
  29. $this->terms = array();
  30. foreach ( $term_ids as $term_id ) {
  31. $this->terms[] = get_term( $term_id, 'post_tag' );
  32. }
  33. $tags = $this->retrieve_terms( array( 'number' => 4 ) );
  34. $this->assertSame( $expected, wp_generate_tag_cloud( $tags, $args ) );
  35. }
  36. /**
  37. * Provider for test when tags are empty.
  38. *
  39. * @return array
  40. */
  41. function empty_tags_data_provider() {
  42. return array(
  43. // When 'format' => 'array', we should be getting an empty array back.
  44. array(
  45. array(),
  46. array( 'format' => 'array' ),
  47. ),
  48. // List format returns an empty string.
  49. array(
  50. '',
  51. array( 'format' => 'list' ),
  52. ),
  53. // $args can be an array or ''. Either should return an empty string.
  54. array(
  55. '',
  56. array(),
  57. ),
  58. array(
  59. '',
  60. '',
  61. ),
  62. );
  63. }
  64. function test_hide_empty_false() {
  65. $term_id = self::factory()->tag->create();
  66. $term = get_term( $term_id, 'post_tag' );
  67. $tags = $this->retrieve_terms(
  68. array(
  69. 'number' => 1,
  70. 'hide_empty' => false,
  71. )
  72. );
  73. $found = wp_generate_tag_cloud(
  74. $tags,
  75. array(
  76. 'hide_empty' => false,
  77. )
  78. );
  79. $this->assertContains( '>' . $tags[0]->name . '<', $found );
  80. }
  81. function test_hide_empty_false_format_array() {
  82. $term_id = self::factory()->tag->create();
  83. $term = get_term( $term_id, 'post_tag' );
  84. $tags = $this->retrieve_terms(
  85. array(
  86. 'number' => 1,
  87. 'hide_empty' => false,
  88. 'format' => 'array',
  89. )
  90. );
  91. $found = wp_generate_tag_cloud(
  92. $tags,
  93. array(
  94. 'hide_empty' => false,
  95. 'format' => 'array',
  96. )
  97. );
  98. $this->assertInternalType( 'array', $found );
  99. $this->assertContains( '>' . $tags[0]->name . '<', $found[0] );
  100. }
  101. function test_hide_empty_false_format_list() {
  102. $term_id = self::factory()->tag->create();
  103. $term = get_term( $term_id, 'post_tag' );
  104. $tags = $this->retrieve_terms(
  105. array(
  106. 'number' => 1,
  107. 'hide_empty' => false,
  108. )
  109. );
  110. $found = wp_generate_tag_cloud(
  111. $tags,
  112. array(
  113. 'hide_empty' => false,
  114. 'format' => 'list',
  115. )
  116. );
  117. $this->assertRegExp( "|^<ul class='wp-tag-cloud' role='list'>|", $found );
  118. $this->assertRegExp( "|</ul>\n|", $found );
  119. $this->assertContains( '>' . $tags[0]->name . '<', $found );
  120. }
  121. function test_hide_empty_false_multi() {
  122. $term_ids = self::factory()->tag->create_many( 4 );
  123. $terms = array();
  124. foreach ( $term_ids as $term_id ) {
  125. $terms[] = get_term( $term_id, 'post_tag' );
  126. }
  127. $tags = $this->retrieve_terms(
  128. array(
  129. 'number' => 4,
  130. 'order' => 'id',
  131. 'hide_empty' => false,
  132. )
  133. );
  134. $found = wp_generate_tag_cloud(
  135. $tags,
  136. array(
  137. 'hide_empty' => false,
  138. )
  139. );
  140. foreach ( $tags as $tag ) {
  141. $this->assertContains( '>' . $tag->name . '<', $found );
  142. }
  143. }
  144. function test_hide_empty_false_multi_format_list() {
  145. $term_ids = self::factory()->tag->create_many( 4 );
  146. $terms = array();
  147. foreach ( $term_ids as $term_id ) {
  148. $terms[] = get_term( $term_id, 'post_tag' );
  149. }
  150. $tags = $this->retrieve_terms(
  151. array(
  152. 'number' => 4,
  153. 'orderby' => 'id',
  154. 'hide_empty' => false,
  155. )
  156. );
  157. $found = wp_generate_tag_cloud(
  158. $tags,
  159. array(
  160. 'hide_empty' => false,
  161. 'format' => 'list',
  162. )
  163. );
  164. $this->assertRegExp( "|^<ul class='wp-tag-cloud' role='list'>|", $found );
  165. $this->assertRegExp( "|</ul>\n|", $found );
  166. foreach ( $tags as $tag ) {
  167. $this->assertContains( '>' . $tag->name . '<', $found );
  168. }
  169. }
  170. public function test_topic_count_text() {
  171. register_taxonomy( 'wptests_tax', 'post' );
  172. $term_ids = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
  173. $this->terms = array();
  174. foreach ( $term_ids as $term_id ) {
  175. $this->terms[] = get_term( $term_id, 'post_tag' );
  176. }
  177. $posts = self::factory()->post->create_many( 2 );
  178. wp_set_post_terms( $posts[0], $term_ids, 'wptests_tax' );
  179. wp_set_post_terms( $posts[1], array( $term_ids[1] ), 'wptests_tax' );
  180. $term_objects = $this->retrieve_terms(
  181. array(
  182. 'include' => $term_ids,
  183. ),
  184. 'wptests_tax'
  185. );
  186. $actual = wp_generate_tag_cloud(
  187. $term_objects,
  188. array(
  189. 'format' => 'array',
  190. 'topic_count_text' => array(
  191. 'singular' => 'Term has %s post',
  192. 'plural' => 'Term has %s posts',
  193. 'domain' => 'foo',
  194. 'context' => 'bar',
  195. ),
  196. )
  197. );
  198. $this->assertContains( 'aria-label="' . $term_objects[0]->name . ' (Term has 1 post)"', $actual[0] );
  199. $this->assertContains( 'aria-label="' . $term_objects[1]->name . ' (Term has 2 posts)"', $actual[1] );
  200. }
  201. public function test_topic_count_text_callback() {
  202. register_taxonomy( 'wptests_tax', 'post' );
  203. $term_ids = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) );
  204. $this->terms = array();
  205. foreach ( $term_ids as $term_id ) {
  206. $this->terms[] = get_term( $term_id, 'post_tag' );
  207. }
  208. $posts = self::factory()->post->create_many( 2 );
  209. wp_set_post_terms( $posts[0], $term_ids, 'wptests_tax' );
  210. wp_set_post_terms( $posts[1], array( $term_ids[1] ), 'wptests_tax' );
  211. $term_objects = $this->retrieve_terms(
  212. array(
  213. 'include' => $term_ids,
  214. ),
  215. 'wptests_tax'
  216. );
  217. $actual = wp_generate_tag_cloud(
  218. $term_objects,
  219. array(
  220. 'format' => 'array',
  221. 'topic_count_text_callback' => array( $this, 'topic_count_text_callback' ),
  222. )
  223. );
  224. $this->assertContains( 'aria-label="' . $term_objects[0]->name . ' (1 foo)"', $actual[0] );
  225. $this->assertContains( 'aria-label="' . $term_objects[1]->name . ' (2 foo)"', $actual[1] );
  226. }
  227. /**
  228. * @ticket 5172
  229. */
  230. public function test_should_include_tag_link_position_class() {
  231. register_taxonomy( 'wptests_tax', 'post' );
  232. $term_ids = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
  233. $p = self::factory()->post->create();
  234. wp_set_post_terms( $p, $term_ids, 'wptests_tax' );
  235. $term_objects = get_terms(
  236. 'wptests_tax',
  237. array(
  238. 'include' => $term_ids,
  239. )
  240. );
  241. $cloud = wp_generate_tag_cloud( $term_objects );
  242. preg_match_all( '|tag\-link\-position-([0-9]+)|', $cloud, $matches );
  243. $this->assertSame( array( 1, 2, 3 ), array_map( 'intval', $matches[1] ) );
  244. }
  245. /**
  246. * Helper method retrieve the created terms.
  247. *
  248. * @uses get_terms
  249. *
  250. * @param array $get_terms_args Options passed to get_terms()
  251. *
  252. * @return array
  253. */
  254. protected function retrieve_terms( $get_terms_args, $taxonomy = 'post_tag' ) {
  255. $terms = get_terms( array( $taxonomy ), $get_terms_args );
  256. $tags = array();
  257. foreach ( $terms as $term ) {
  258. // Add the link.
  259. $term->link = get_term_link( $term );
  260. $tags[] = $term;
  261. }
  262. return $tags;
  263. }
  264. public function topic_count_text_callback( $real_count, $tag, $args ) {
  265. return sprintf( '%s foo', $real_count );
  266. }
  267. }