wpDropdownCategories.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * @group taxonomy
  4. * @group category.php
  5. */
  6. class Tests_Category_WpDropdownCategories extends WP_UnitTestCase {
  7. /**
  8. * @ticket 30306
  9. */
  10. public function test_wp_dropdown_categories_value_field_should_default_to_term_id() {
  11. // Create a test category.
  12. $cat_id = self::factory()->category->create(
  13. array(
  14. 'name' => 'Test Category',
  15. 'slug' => 'test_category',
  16. )
  17. );
  18. // Get the default functionality of wp_dropdown_categories().
  19. $dropdown_default = wp_dropdown_categories(
  20. array(
  21. 'echo' => 0,
  22. 'hide_empty' => 0,
  23. )
  24. );
  25. // Test to see if it returns the default with the category ID.
  26. $this->assertContains( 'value="' . $cat_id . '"', $dropdown_default );
  27. }
  28. /**
  29. * @ticket 30306
  30. */
  31. public function test_wp_dropdown_categories_value_field_term_id() {
  32. // Create a test category.
  33. $cat_id = self::factory()->category->create(
  34. array(
  35. 'name' => 'Test Category',
  36. 'slug' => 'test_category',
  37. )
  38. );
  39. // Get the default functionality of wp_dropdown_categories().
  40. $found = wp_dropdown_categories(
  41. array(
  42. 'echo' => 0,
  43. 'hide_empty' => 0,
  44. 'value_field' => 'term_id',
  45. )
  46. );
  47. // Test to see if it returns the default with the category ID.
  48. $this->assertContains( 'value="' . $cat_id . '"', $found );
  49. }
  50. /**
  51. * @ticket 30306
  52. */
  53. public function test_wp_dropdown_categories_value_field_slug() {
  54. // Create a test category.
  55. $cat_id = self::factory()->category->create(
  56. array(
  57. 'name' => 'Test Category',
  58. 'slug' => 'test_category',
  59. )
  60. );
  61. // Get the default functionality of wp_dropdown_categories().
  62. $found = wp_dropdown_categories(
  63. array(
  64. 'echo' => 0,
  65. 'hide_empty' => 0,
  66. 'value_field' => 'slug',
  67. )
  68. );
  69. // Test to see if it returns the default with the category slug.
  70. $this->assertContains( 'value="test_category"', $found );
  71. }
  72. /**
  73. * @ticket 30306
  74. */
  75. public function test_wp_dropdown_categories_value_field_should_fall_back_on_term_id_when_an_invalid_value_is_provided() {
  76. // Create a test category.
  77. $cat_id = self::factory()->category->create(
  78. array(
  79. 'name' => 'Test Category',
  80. 'slug' => 'test_category',
  81. )
  82. );
  83. // Get the default functionality of wp_dropdown_categories().
  84. $found = wp_dropdown_categories(
  85. array(
  86. 'echo' => 0,
  87. 'hide_empty' => 0,
  88. 'value_field' => 'foo',
  89. )
  90. );
  91. // Test to see if it returns the default with the category slug.
  92. $this->assertContains( 'value="' . $cat_id . '"', $found );
  93. }
  94. /**
  95. * @ticket 32330
  96. */
  97. public function test_wp_dropdown_categories_selected_should_respect_custom_value_field() {
  98. $c1 = self::factory()->category->create(
  99. array(
  100. 'name' => 'Test Category 1',
  101. 'slug' => 'test_category_1',
  102. )
  103. );
  104. $c2 = self::factory()->category->create(
  105. array(
  106. 'name' => 'Test Category 2',
  107. 'slug' => 'test_category_2',
  108. )
  109. );
  110. $found = wp_dropdown_categories(
  111. array(
  112. 'echo' => 0,
  113. 'hide_empty' => 0,
  114. 'value_field' => 'slug',
  115. 'selected' => 'test_category_2',
  116. )
  117. );
  118. $this->assertContains( 'value="test_category_2" selected="selected"', $found );
  119. }
  120. /**
  121. * @ticket 33452
  122. */
  123. public function test_wp_dropdown_categories_show_option_all_should_be_selected_if_no_selected_value_is_explicitly_passed_and_value_field_does_not_have_string_values() {
  124. $cats = self::factory()->category->create_many( 3 );
  125. $found = wp_dropdown_categories(
  126. array(
  127. 'echo' => 0,
  128. 'hide_empty' => 0,
  129. 'show_option_all' => 'Foo',
  130. 'value_field' => 'slug',
  131. )
  132. );
  133. $this->assertContains( "value='0' selected='selected'", $found );
  134. foreach ( $cats as $cat ) {
  135. $_cat = get_term( $cat, 'category' );
  136. $this->assertNotContains( 'value="' . $_cat->slug . '" selected="selected"', $found );
  137. }
  138. }
  139. /**
  140. * @ticket 33452
  141. */
  142. public function test_wp_dropdown_categories_show_option_all_should_be_selected_if_selected_value_of_0_string_is_explicitly_passed_and_value_field_does_not_have_string_values() {
  143. $cats = self::factory()->category->create_many( 3 );
  144. $found = wp_dropdown_categories(
  145. array(
  146. 'echo' => 0,
  147. 'hide_empty' => 0,
  148. 'show_option_all' => 'Foo',
  149. 'value_field' => 'slug',
  150. 'selected' => '0',
  151. )
  152. );
  153. $this->assertContains( "value='0' selected='selected'", $found );
  154. foreach ( $cats as $cat ) {
  155. $_cat = get_term( $cat, 'category' );
  156. $this->assertNotContains( 'value="' . $_cat->slug . '" selected="selected"', $found );
  157. }
  158. }
  159. /**
  160. * @ticket 31909
  161. */
  162. public function test_required_true_should_add_required_attribute() {
  163. // Create a test category.
  164. $cat_id = self::factory()->category->create(
  165. array(
  166. 'name' => 'Test Category',
  167. 'slug' => 'test_category',
  168. )
  169. );
  170. $args = array(
  171. 'show_option_none' => __( 'Select one', 'text-domain' ),
  172. 'option_none_value' => '',
  173. 'required' => true,
  174. 'hide_empty' => 0,
  175. 'echo' => 0,
  176. );
  177. $dropdown_categories = wp_dropdown_categories( $args );
  178. // Test to see if it contains the "required" attribute.
  179. $this->assertRegExp( '/<select[^>]+required/', $dropdown_categories );
  180. }
  181. /**
  182. * @ticket 31909
  183. */
  184. public function test_required_false_should_omit_required_attribute() {
  185. // Create a test category.
  186. $cat_id = self::factory()->category->create(
  187. array(
  188. 'name' => 'Test Category',
  189. 'slug' => 'test_category',
  190. )
  191. );
  192. $args = array(
  193. 'show_option_none' => __( 'Select one', 'text-domain' ),
  194. 'option_none_value' => '',
  195. 'required' => false,
  196. 'hide_empty' => 0,
  197. 'echo' => 0,
  198. );
  199. $dropdown_categories = wp_dropdown_categories( $args );
  200. // Test to see if it contains the "required" attribute.
  201. $this->assertNotRegExp( '/<select[^>]+required/', $dropdown_categories );
  202. }
  203. /**
  204. * @ticket 31909
  205. */
  206. public function test_required_should_default_to_false() {
  207. // Create a test category.
  208. $cat_id = self::factory()->category->create(
  209. array(
  210. 'name' => 'Test Category',
  211. 'slug' => 'test_category',
  212. )
  213. );
  214. $args = array(
  215. 'show_option_none' => __( 'Select one', 'text-domain' ),
  216. 'option_none_value' => '',
  217. 'hide_empty' => 0,
  218. 'echo' => 0,
  219. );
  220. $dropdown_categories = wp_dropdown_categories( $args );
  221. // Test to see if it contains the "required" attribute.
  222. $this->assertNotRegExp( '/<select[^>]+required/', $dropdown_categories );
  223. }
  224. }