Make WordPress Core

Ticket #24656: wp_generate_tag_cloud.tests

File wp_generate_tag_cloud.tests, 5.5 KB (added by welcher, 10 years ago)

First pass of unit tests for wp_generate_tag_cloud

Line 
1Index: phpunit/tests/term/wpGenerateTagCloud.php
2===================================================================
3--- phpunit/tests/term/wpGenerateTagCloud.php   (revision 0)
4+++ phpunit/tests/term/wpGenerateTagCloud.php   (working copy)
5@@ -0,0 +1,199 @@
6+<?php
7+/**
8+ * @group ryan
9+ */
10+class Tests_WP_Generate_Tag_Cloud extends WP_UnitTestCase {
11+
12+       public function setUp() {
13+               parent::setUp();
14+       }
15+
16+       public function tearDown() {
17+               parent::tearDown();
18+               // This code will run after each test
19+       }
20+
21+       /**
22+        * Testing when passed $tags array is empty
23+        *
24+        * @dataProvider empty_tags_data_provider
25+        *
26+        * @param $expected. Expected output from wp_generate_tag_cloud
27+        * @param $args. Options for wp_generate_tag_cloud.
28+        */
29+       public function test_empty_tags_passed( $expected, $args ) {
30+
31+               $empty_tags = array();
32+               $this->assertSame( $expected, wp_generate_tag_cloud( $empty_tags, $args ) );
33+       }
34+
35+       /**
36+        * Testing when no tags are found
37+        *
38+        * @dataProvider empty_tags_data_provider
39+        *
40+        * @param $expected. Expected output from wp_generate_tag_cloud
41+        * @param $args. Options for wp_generate_tag_cloud.
42+        */
43+       function test_empty_tags_list_returned( $expected, $args ) {
44+               $this->factory->term->create_many( 4, array( 'taxonomy' => 'post_tag' ) );
45+               $tags = $this->retrieve_terms( array( 'number' => 4 ) );
46+               $this->assertSame( $expected, wp_generate_tag_cloud( $tags, $args ) );
47+       }
48+
49+       /**
50+        * Provider for test when tags are empty.
51+        * @return array
52+        */
53+       function empty_tags_data_provider ( ) {
54+               return array(
55+                       /**
56+                        * when format => array, we should be getting an empty array back
57+                        */
58+                       array(
59+                               array(),
60+                               array( 'format' => 'array' ),
61+                       ),
62+                       /**
63+                        * List format returns an empty string
64+                        */
65+                       array(
66+                               '',
67+                               array( 'format' => 'list' ),
68+                       ),
69+                       /**
70+                        * $args can be an array or ''. Either should return an empty string
71+                        */
72+                       array(
73+                               '',
74+                               array(),
75+                       ),
76+                       array(
77+                               '',
78+                               '',
79+                       ),
80+               );
81+       }
82+
83+
84+       /**
85+        * Testing the various output for a single link
86+        * in various formats
87+        *
88+        * @dataProvider single_link_data_provider
89+        *
90+        * @param int $create How many tags to create
91+        * @param array $get_terms_args. What args we want to pass to retreve_terms
92+        * @param mixed $expected. Expected output from wp_generate_tag_cloud
93+        * @param array $args. Options for wp_generate_tag_cloud.
94+        *
95+        */
96+       function test_wp_generate_tag_cloud( $create, $get_terms_args, $expected, $args ) {
97+               $this->factory->term->create_many( $create, array( 'taxonomy' => 'post_tag' ) );
98+               $tags = $this->retrieve_terms( $get_terms_args );
99+
100+               $this->assertEquals( $expected, wp_generate_tag_cloud( $tags, $args ) );
101+       }
102+
103+
104+       function single_link_data_provider() {
105+               return array(
106+                       array(
107+                               1,
108+                               array(
109+                                       'number' => 1,
110+                                       'hide_empty' => false,
111+                               ),
112+                               "<a href='http://example.org/?tag=term-1' class='tag-link-0' title='0 topics' style='font-size: 8pt;'>Term 1</a>",
113+                               array(
114+                                       'hide_empty' => false,
115+                               ),
116+                       ),
117+
118+                       array(
119+                               1,
120+                               array(
121+                                       'number' => 1,
122+                                       'hide_empty' => false,
123+                               ),
124+                               array(
125+                                       "<a href='http://example.org/?tag=term-1' class='tag-link-0' title='0 topics' style='font-size: 8pt;'>Term 1</a>",
126+                               ),
127+                               array(
128+                                       'hide_empty' => false,
129+                                       'format'     => 'array',
130+                               ),
131+                       ),
132+
133+                       array(
134+                               1,
135+                               array(
136+                                       'number' => 1,
137+                                       'hide_empty' => false,
138+                               ),
139+                               "<ul class='wp-tag-cloud'>\n\t<li><a href='http://example.org/?tag=term-1' class='tag-link-0' title='0 topics' style='font-size: 8pt;'>Term 1</a></li>\n</ul>\n",
140+                               array(
141+                                       'hide_empty' => false,
142+                                       'format'     => 'list',
143+                               ),
144+                       ),
145+                       array(
146+                               4,
147+                               array(
148+                                       'number' => 4,
149+                                       'hide_empty' => false,
150+                               ),
151+                               "<a href='http://example.org/?tag=term-1' class='tag-link-0' title='0 topics' style='font-size: 8pt;'>Term 1</a>\n".
152+                               "<a href='http://example.org/?tag=term-2' class='tag-link-1' title='0 topics' style='font-size: 8pt;'>Term 2</a>\n".
153+                               "<a href='http://example.org/?tag=term-3' class='tag-link-2' title='0 topics' style='font-size: 8pt;'>Term 3</a>\n".
154+                               "<a href='http://example.org/?tag=term-4' class='tag-link-3' title='0 topics' style='font-size: 8pt;'>Term 4</a>",
155+                               array(
156+                                       'hide_empty' => false,
157+                               ),
158+                       ),
159+                       array(
160+                               4,
161+                               array(
162+                                       'number' => 4,
163+                                       'hide_empty' => false,
164+                               ),
165+                               "<ul class='wp-tag-cloud'>\n\t<li>".
166+                               "<a href='http://example.org/?tag=term-1' class='tag-link-0' title='0 topics' style='font-size: 8pt;'>Term 1</a></li>\n\t<li>".
167+                               "<a href='http://example.org/?tag=term-2' class='tag-link-1' title='0 topics' style='font-size: 8pt;'>Term 2</a></li>\n\t<li>".
168+                               "<a href='http://example.org/?tag=term-3' class='tag-link-2' title='0 topics' style='font-size: 8pt;'>Term 3</a></li>\n\t<li>".
169+                               "<a href='http://example.org/?tag=term-4' class='tag-link-3' title='0 topics' style='font-size: 8pt;'>Term 4</a>".
170+                               "</li>\n</ul>\n",
171+                               array(
172+                                       'hide_empty' => false,
173+                                       'format'     => 'list',
174+                               ),
175+                       ),
176+               );
177+       }
178+
179+
180+
181+       /**
182+        * Helper method retrieve the created terms.
183+        *
184+        * @uses get_terms
185+        *
186+        * @param array $get_terms_args Options passed to get_terms()
187+        *
188+        * @return array
189+        */
190+       protected function retrieve_terms( $get_terms_args ) {
191+
192+               $terms = get_terms( array( 'post_tag' ), $get_terms_args );
193+
194+               $tags = array();
195+               foreach ( $terms as $term ) {
196+                       //add the link
197+                       $term->link = get_term_link( $term );
198+                       $tags[] = $term;
199+
200+               }
201+
202+               return $tags;
203+       }
204+}
205\ No newline at end of file