includesTemplate.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * @group admin
  4. */
  5. class Tests_Admin_includesTemplate extends WP_UnitTestCase {
  6. /**
  7. * @ticket 51147
  8. * @dataProvider data_wp_terms_checklist_with_selected_cats
  9. */
  10. public function test_wp_terms_checklist_with_selected_cats( $term_id ) {
  11. $output = wp_terms_checklist(
  12. 0,
  13. array(
  14. 'selected_cats' => array( $term_id ),
  15. 'echo' => false,
  16. )
  17. );
  18. $this->assertContains( "checked='checked'", $output );
  19. }
  20. /**
  21. * @ticket 51147
  22. * @dataProvider data_wp_terms_checklist_with_selected_cats
  23. */
  24. public function test_wp_terms_checklist_with_popular_cats( $term_id ) {
  25. $output = wp_terms_checklist(
  26. 0,
  27. array(
  28. 'popular_cats' => array( $term_id ),
  29. 'echo' => false,
  30. )
  31. );
  32. $this->assertContains( 'class="popular-category"', $output );
  33. }
  34. public function data_wp_terms_checklist_with_selected_cats() {
  35. return array(
  36. array( '1' ),
  37. array( 1 ),
  38. );
  39. }
  40. public function test_add_meta_box() {
  41. global $wp_meta_boxes;
  42. add_meta_box( 'testbox1', 'Test Metabox', '__return_false', 'post' );
  43. $this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['post']['advanced']['default'] );
  44. }
  45. public function test_remove_meta_box() {
  46. global $wp_meta_boxes;
  47. // Add a meta box to remove.
  48. add_meta_box( 'testbox1', 'Test Metabox', '__return_false', $current_screen = 'post' );
  49. // Confirm it's there.
  50. $this->assertArrayHasKey( 'testbox1', $wp_meta_boxes[ $current_screen ]['advanced']['default'] );
  51. // Remove the meta box.
  52. remove_meta_box( 'testbox1', $current_screen, 'advanced' );
  53. // Check that it was removed properly (the meta box should be set to false once that it has been removed).
  54. $this->assertFalse( $wp_meta_boxes[ $current_screen ]['advanced']['default']['testbox1'] );
  55. }
  56. /**
  57. * @ticket 15000
  58. */
  59. public function test_add_meta_box_on_multiple_screens() {
  60. global $wp_meta_boxes;
  61. // Add a meta box to three different post types.
  62. add_meta_box( 'testbox1', 'Test Metabox', '__return_false', array( 'post', 'comment', 'attachment' ) );
  63. $this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['post']['advanced']['default'] );
  64. $this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['comment']['advanced']['default'] );
  65. $this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['attachment']['advanced']['default'] );
  66. }
  67. /**
  68. * @ticket 15000
  69. */
  70. public function test_remove_meta_box_from_multiple_screens() {
  71. global $wp_meta_boxes;
  72. // Add a meta box to three different screens.
  73. add_meta_box( 'testbox1', 'Test Metabox', '__return_false', array( 'post', 'comment', 'attachment' ) );
  74. // Remove meta box from posts.
  75. remove_meta_box( 'testbox1', 'post', 'advanced' );
  76. // Check that we have removed the meta boxes only from posts.
  77. $this->assertFalse( $wp_meta_boxes['post']['advanced']['default']['testbox1'] );
  78. $this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['comment']['advanced']['default'] );
  79. $this->assertArrayHasKey( 'testbox1', $wp_meta_boxes['attachment']['advanced']['default'] );
  80. // Remove the meta box from the other screens.
  81. remove_meta_box( 'testbox1', array( 'comment', 'attachment' ), 'advanced' );
  82. $this->assertFalse( $wp_meta_boxes['comment']['advanced']['default']['testbox1'] );
  83. $this->assertFalse( $wp_meta_boxes['attachment']['advanced']['default']['testbox1'] );
  84. }
  85. /**
  86. * @ticket 50019
  87. */
  88. public function test_add_meta_box_with_previously_removed_box_and_sorted_priority() {
  89. global $wp_meta_boxes;
  90. // Add a meta box to remove.
  91. add_meta_box( 'testbox1', 'Test Metabox', '__return_false', $current_screen = 'post' );
  92. // Remove the meta box.
  93. remove_meta_box( 'testbox1', $current_screen, 'advanced' );
  94. // Attempt to re-add the meta box with the 'sorted' priority.
  95. add_meta_box( 'testbox1', null, null, $current_screen, 'advanced', 'sorted' );
  96. // Check that the meta box was not re-added.
  97. $this->assertFalse( $wp_meta_boxes[ $current_screen ]['advanced']['default']['testbox1'] );
  98. }
  99. /**
  100. * Test calling get_settings_errors() with variations on where it gets errors from.
  101. *
  102. * @ticket 42498
  103. * @covers ::get_settings_errors
  104. * @global array $wp_settings_errors
  105. */
  106. public function test_get_settings_errors_sources() {
  107. global $wp_settings_errors;
  108. $blogname_error = array(
  109. 'setting' => 'blogname',
  110. 'code' => 'blogname',
  111. 'message' => 'Capital P dangit!',
  112. 'type' => 'error',
  113. );
  114. $blogdescription_error = array(
  115. 'setting' => 'blogdescription',
  116. 'code' => 'blogdescription',
  117. 'message' => 'Too short',
  118. 'type' => 'error',
  119. );
  120. $wp_settings_errors = null;
  121. $this->assertSame( array(), get_settings_errors( 'blogname' ) );
  122. // Test getting errors from transient.
  123. $_GET['settings-updated'] = '1';
  124. set_transient( 'settings_errors', array( $blogname_error ) );
  125. $wp_settings_errors = null;
  126. $this->assertSame( array( $blogname_error ), get_settings_errors( 'blogname' ) );
  127. // Test getting errors from transient and from global.
  128. $_GET['settings-updated'] = '1';
  129. set_transient( 'settings_errors', array( $blogname_error ) );
  130. $wp_settings_errors = null;
  131. add_settings_error( $blogdescription_error['setting'], $blogdescription_error['code'], $blogdescription_error['message'], $blogdescription_error['type'] );
  132. $this->assertSameSets( array( $blogname_error, $blogdescription_error ), get_settings_errors() );
  133. $wp_settings_errors = null;
  134. }
  135. /**
  136. * @ticket 44941
  137. * @covers ::settings_errors
  138. * @global array $wp_settings_errors
  139. * @dataProvider settings_errors_css_classes_provider
  140. */
  141. public function test_settings_errors_css_classes( $type, $expected ) {
  142. global $wp_settings_errors;
  143. add_settings_error( 'foo', 'bar', 'Capital P dangit!', $type );
  144. ob_start();
  145. settings_errors();
  146. $output = ob_get_clean();
  147. $wp_settings_errors = null;
  148. $expected = sprintf( 'notice %s settings-error is-dismissible', $expected );
  149. $this->assertContains( $expected, $output );
  150. $this->assertNotContains( 'notice-notice-', $output );
  151. }
  152. public function settings_errors_css_classes_provider() {
  153. return array(
  154. array( 'error', 'notice-error' ),
  155. array( 'success', 'notice-success' ),
  156. array( 'warning', 'notice-warning' ),
  157. array( 'info', 'notice-info' ),
  158. array( 'updated', 'notice-success' ),
  159. array( 'notice-error', 'notice-error' ),
  160. array( 'error my-own-css-class hello world', 'error my-own-css-class hello world' ),
  161. );
  162. }
  163. /**
  164. * @ticket 42791
  165. */
  166. public function test_wp_add_dashboard_widget() {
  167. global $wp_meta_boxes;
  168. set_current_screen( 'dashboard' );
  169. if ( ! function_exists( 'wp_add_dashboard_widget' ) ) {
  170. require_once ABSPATH . 'wp-admin/includes/dashboard.php';
  171. }
  172. // Some hardcoded defaults for core widgets.
  173. wp_add_dashboard_widget( 'dashboard_quick_press', 'Quick', '__return_false' );
  174. wp_add_dashboard_widget( 'dashboard_browser_nag', 'Nag', '__return_false' );
  175. $this->assertArrayHasKey( 'dashboard_quick_press', $wp_meta_boxes['dashboard']['side']['core'] );
  176. $this->assertArrayHasKey( 'dashboard_browser_nag', $wp_meta_boxes['dashboard']['normal']['high'] );
  177. // Location and priority defaults.
  178. wp_add_dashboard_widget( 'dashboard1', 'Widget 1', '__return_false', null, null, 'foo' );
  179. wp_add_dashboard_widget( 'dashboard2', 'Widget 2', '__return_false', null, null, null, 'bar' );
  180. $this->assertArrayHasKey( 'dashboard1', $wp_meta_boxes['dashboard']['foo']['core'] );
  181. $this->assertArrayHasKey( 'dashboard2', $wp_meta_boxes['dashboard']['normal']['bar'] );
  182. // Cleanup.
  183. remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
  184. remove_meta_box( 'dashboard_browser_nag', 'dashboard', 'normal' );
  185. remove_meta_box( 'dashboard1', 'dashboard', 'foo' );
  186. // This doesn't actually get removed due to the invalid priority.
  187. remove_meta_box( 'dashboard2', 'dashboard', 'normal' );
  188. set_current_screen( 'front' );
  189. }
  190. }