control.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * Test_WP_Customize_Control tests.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Tests for the Test_WP_Customize_Control class.
  9. *
  10. * @todo This is missing dedicated tests for all but one of the methods.
  11. *
  12. * @group customize
  13. */
  14. class Test_WP_Customize_Control extends WP_UnitTestCase {
  15. /**
  16. * Manager.
  17. *
  18. * @var WP_Customize_Manager
  19. */
  20. public $wp_customize;
  21. /**
  22. * Set up.
  23. */
  24. function setUp() {
  25. parent::setUp();
  26. wp_set_current_user( $this->factory()->user->create( array( 'role' => 'administrator' ) ) );
  27. require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
  28. $GLOBALS['wp_customize'] = new WP_Customize_Manager();
  29. $this->wp_customize = $GLOBALS['wp_customize'];
  30. }
  31. /**
  32. * Test WP_Customize_Control::check_capabilities().
  33. *
  34. * @see WP_Customize_Control::check_capabilities()
  35. */
  36. function test_check_capabilities() {
  37. do_action( 'customize_register', $this->wp_customize );
  38. $control = new WP_Customize_Control(
  39. $this->wp_customize,
  40. 'blogname',
  41. array(
  42. 'settings' => array( 'blogname' ),
  43. )
  44. );
  45. $this->assertTrue( $control->check_capabilities() );
  46. $control = new WP_Customize_Control(
  47. $this->wp_customize,
  48. 'blogname',
  49. array(
  50. 'settings' => array( 'blogname', 'non_existing' ),
  51. )
  52. );
  53. $this->assertFalse( $control->check_capabilities() );
  54. $this->wp_customize->add_setting(
  55. 'top_secret_message',
  56. array(
  57. 'capability' => 'top_secret_clearance',
  58. )
  59. );
  60. $control = new WP_Customize_Control(
  61. $this->wp_customize,
  62. 'blogname',
  63. array(
  64. 'settings' => array( 'blogname', 'top_secret_clearance' ),
  65. )
  66. );
  67. $this->assertFalse( $control->check_capabilities() );
  68. $control = new WP_Customize_Control(
  69. $this->wp_customize,
  70. 'no_setting',
  71. array(
  72. 'settings' => array(),
  73. )
  74. );
  75. $this->assertTrue( $control->check_capabilities() );
  76. $control = new WP_Customize_Control(
  77. $this->wp_customize,
  78. 'no_setting',
  79. array(
  80. 'settings' => array(),
  81. 'capability' => 'top_secret_clearance',
  82. )
  83. );
  84. $this->assertFalse( $control->check_capabilities() );
  85. $control = new WP_Customize_Control(
  86. $this->wp_customize,
  87. 'no_setting',
  88. array(
  89. 'settings' => array(),
  90. 'capability' => 'edit_theme_options',
  91. )
  92. );
  93. $this->assertTrue( $control->check_capabilities() );
  94. }
  95. /**
  96. * @ticket 38164
  97. */
  98. function test_dropdown_pages() {
  99. do_action( 'customize_register', $this->wp_customize );
  100. $this->assertInstanceOf( 'WP_Customize_Nav_Menus', $this->wp_customize->nav_menus );
  101. $nav_menus_created_posts_setting = $this->wp_customize->get_setting( 'nav_menus_created_posts' );
  102. $this->assertInstanceOf( 'WP_Customize_Filter_Setting', $nav_menus_created_posts_setting );
  103. $page_on_front_control = $this->wp_customize->get_control( 'page_on_front' );
  104. // Ensure the add-new-toggle is absent if allow_addition param is not set.
  105. $page_on_front_control->allow_addition = false;
  106. ob_start();
  107. $page_on_front_control->maybe_render();
  108. $content = ob_get_clean();
  109. $this->assertNotContains( 'add-new-toggle', $content );
  110. // Ensure the add-new-toggle is absent if allow_addition param is set.
  111. $page_on_front_control->allow_addition = true;
  112. ob_start();
  113. $page_on_front_control->maybe_render();
  114. $content = ob_get_clean();
  115. $this->assertContains( 'add-new-toggle', $content );
  116. // Ensure that dropdown-pages delect is rendered even if there are no pages published (yet).
  117. foreach ( get_pages() as $page ) {
  118. wp_delete_post( $page->ID );
  119. }
  120. $page_on_front_control->allow_addition = true;
  121. ob_start();
  122. $page_on_front_control->maybe_render();
  123. $content = ob_get_clean();
  124. $this->assertContains( '<option value="0">', $content, 'Dropdown-pages renders select even without any pages published.' );
  125. // Ensure that auto-draft pages are included if they are among the nav_menus_created_posts.
  126. $auto_draft_page_id = $this->factory()->post->create(
  127. array(
  128. 'post_type' => 'page',
  129. 'post_status' => 'auto-draft',
  130. 'post_title' => 'Auto Draft Page',
  131. )
  132. );
  133. $this->factory()->post->create(
  134. array(
  135. 'post_type' => 'page',
  136. 'post_status' => 'auto-draft',
  137. 'post_title' => 'Orphan Auto Draft Page',
  138. )
  139. );
  140. $auto_draft_post_id = $this->factory()->post->create(
  141. array(
  142. 'post_type' => 'post',
  143. 'post_status' => 'auto-draft',
  144. 'post_title' => 'Auto Draft Post',
  145. )
  146. );
  147. $this->wp_customize->set_post_value( $nav_menus_created_posts_setting->id, array( $auto_draft_page_id, $auto_draft_post_id ) );
  148. $nav_menus_created_posts_setting->preview();
  149. ob_start();
  150. $page_on_front_control->maybe_render();
  151. $content = ob_get_clean();
  152. $this->assertContains( sprintf( '<option value="%d">Auto Draft Page</option>', $auto_draft_page_id ), $content );
  153. $this->assertNotContains( 'Auto Draft Post', $content );
  154. $this->assertNotContains( 'Orphan Auto Draft Page', $content );
  155. }
  156. /**
  157. * Tear down.
  158. */
  159. function tearDown() {
  160. $this->wp_customize = null;
  161. unset( $GLOBALS['wp_customize'] );
  162. parent::tearDown();
  163. }
  164. }