/tests/phpunit/tests/customize/panel.php

https://github.com/WordPress/wordpress-develop · PHP · 242 lines · 168 code · 26 blank · 48 comment · 1 complexity · d215ea47eea41fc637a7127efd2b5c9f MD5 · raw file

  1. <?php
  2. /**
  3. * Tests for the WP_Customize_Panel class.
  4. *
  5. * @group customize
  6. */
  7. class Tests_WP_Customize_Panel extends WP_UnitTestCase {
  8. /**
  9. * @var WP_Customize_Manager
  10. */
  11. protected $manager;
  12. function setUp() {
  13. parent::setUp();
  14. require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
  15. $GLOBALS['wp_customize'] = new WP_Customize_Manager();
  16. $this->manager = $GLOBALS['wp_customize'];
  17. $this->undefined = new stdClass();
  18. }
  19. function tearDown() {
  20. $this->manager = null;
  21. unset( $GLOBALS['wp_customize'] );
  22. parent::tearDown();
  23. }
  24. /**
  25. * @see WP_Customize_Panel::__construct()
  26. */
  27. function test_construct_default_args() {
  28. $panel = new WP_Customize_Panel( $this->manager, 'foo' );
  29. $this->assertInternalType( 'int', $panel->instance_number );
  30. $this->assertSame( $this->manager, $panel->manager );
  31. $this->assertSame( 'foo', $panel->id );
  32. $this->assertSame( 160, $panel->priority );
  33. $this->assertSame( 'edit_theme_options', $panel->capability );
  34. $this->assertSame( '', $panel->theme_supports );
  35. $this->assertSame( '', $panel->title );
  36. $this->assertSame( '', $panel->description );
  37. $this->assertEmpty( $panel->sections );
  38. $this->assertSame( 'default', $panel->type );
  39. $this->assertSame( array( $panel, 'active_callback' ), $panel->active_callback );
  40. }
  41. /**
  42. * @see WP_Customize_Panel::__construct()
  43. */
  44. function test_construct_custom_args() {
  45. $args = array(
  46. 'priority' => 200,
  47. 'capability' => 'edit_posts',
  48. 'theme_supports' => 'html5',
  49. 'title' => 'Hello World',
  50. 'description' => 'Lorem Ipsum',
  51. 'type' => 'horizontal',
  52. 'active_callback' => '__return_true',
  53. );
  54. $panel = new WP_Customize_Panel( $this->manager, 'foo', $args );
  55. foreach ( $args as $key => $value ) {
  56. $this->assertSame( $value, $panel->$key );
  57. }
  58. }
  59. /**
  60. * @see WP_Customize_Panel::__construct()
  61. */
  62. function test_construct_custom_type() {
  63. $panel = new Custom_Panel_Test( $this->manager, 'foo' );
  64. $this->assertSame( 'titleless', $panel->type );
  65. }
  66. /**
  67. * @see WP_Customize_Panel::active()
  68. * @see WP_Customize_Panel::active_callback()
  69. */
  70. function test_active() {
  71. $panel = new WP_Customize_Panel( $this->manager, 'foo' );
  72. $this->assertTrue( $panel->active() );
  73. $panel = new WP_Customize_Panel(
  74. $this->manager,
  75. 'foo',
  76. array(
  77. 'active_callback' => '__return_false',
  78. )
  79. );
  80. $this->assertFalse( $panel->active() );
  81. add_filter( 'customize_panel_active', array( $this, 'filter_active_test' ), 10, 2 );
  82. $this->assertTrue( $panel->active() );
  83. }
  84. /**
  85. * @param bool $active
  86. * @param WP_Customize_Panel $panel
  87. * @return bool
  88. */
  89. function filter_active_test( $active, $panel ) {
  90. $this->assertFalse( $active );
  91. $this->assertInstanceOf( 'WP_Customize_Panel', $panel );
  92. $active = true;
  93. return $active;
  94. }
  95. /**
  96. * @see WP_Customize_Panel::json()
  97. */
  98. function test_json() {
  99. $args = array(
  100. 'priority' => 200,
  101. 'capability' => 'edit_posts',
  102. 'theme_supports' => 'html5',
  103. 'title' => 'Hello World',
  104. 'description' => 'Lorem Ipsum',
  105. 'type' => 'horizontal',
  106. 'active_callback' => '__return_true',
  107. );
  108. $panel = new WP_Customize_Panel( $this->manager, 'foo', $args );
  109. $data = $panel->json();
  110. $this->assertSame( 'foo', $data['id'] );
  111. foreach ( array( 'title', 'description', 'priority', 'type' ) as $key ) {
  112. $this->assertSame( $args[ $key ], $data[ $key ] );
  113. }
  114. $this->assertEmpty( $data['content'] );
  115. $this->assertTrue( $data['active'] );
  116. $this->assertInternalType( 'int', $data['instanceNumber'] );
  117. }
  118. /**
  119. * @see WP_Customize_Panel::check_capabilities()
  120. */
  121. function test_check_capabilities() {
  122. $user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
  123. wp_set_current_user( $user_id );
  124. $panel = new WP_Customize_Panel( $this->manager, 'foo' );
  125. $this->assertTrue( $panel->check_capabilities() );
  126. $old_cap = $panel->capability;
  127. $panel->capability = 'do_not_allow';
  128. $this->assertFalse( $panel->check_capabilities() );
  129. $panel->capability = $old_cap;
  130. $this->assertTrue( $panel->check_capabilities() );
  131. $panel->theme_supports = 'impossible_feature';
  132. $this->assertFalse( $panel->check_capabilities() );
  133. }
  134. /**
  135. * @see WP_Customize_Panel::get_content()
  136. */
  137. function test_get_content() {
  138. $panel = new WP_Customize_Panel( $this->manager, 'foo' );
  139. $this->assertEmpty( $panel->get_content() );
  140. }
  141. /**
  142. * @see WP_Customize_Panel::maybe_render()
  143. */
  144. function test_maybe_render() {
  145. wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
  146. $panel = new WP_Customize_Panel( $this->manager, 'bar' );
  147. $customize_render_panel_count = did_action( 'customize_render_panel' );
  148. add_action( 'customize_render_panel', array( $this, 'action_customize_render_panel_test' ) );
  149. ob_start();
  150. $panel->maybe_render();
  151. $content = ob_get_clean();
  152. $this->assertTrue( $panel->check_capabilities() );
  153. $this->assertEmpty( $content );
  154. $this->assertSame( $customize_render_panel_count + 1, did_action( 'customize_render_panel' ), 'Unexpected did_action count for customize_render_panel' );
  155. $this->assertSame( 1, did_action( "customize_render_panel_{$panel->id}" ), "Unexpected did_action count for customize_render_panel_{$panel->id}" );
  156. }
  157. /**
  158. * @see WP_Customize_Panel::maybe_render()
  159. * @param WP_Customize_Panel $panel
  160. */
  161. function action_customize_render_panel_test( $panel ) {
  162. $this->assertInstanceOf( 'WP_Customize_Panel', $panel );
  163. }
  164. /**
  165. * @see WP_Customize_Panel::print_template()
  166. */
  167. function test_print_templates_standard() {
  168. wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
  169. $panel = new WP_Customize_Panel( $this->manager, 'baz' );
  170. ob_start();
  171. $panel->print_template();
  172. $content = ob_get_clean();
  173. $this->assertContains( '<script type="text/html" id="tmpl-customize-panel-default-content">', $content );
  174. $this->assertContains( 'accordion-section-title', $content );
  175. $this->assertContains( 'control-panel-content', $content );
  176. $this->assertContains( '<script type="text/html" id="tmpl-customize-panel-default">', $content );
  177. $this->assertContains( 'customize-panel-description', $content );
  178. $this->assertContains( 'preview-notice', $content );
  179. }
  180. /**
  181. * @see WP_Customize_Panel::print_template()
  182. */
  183. function test_print_templates_custom() {
  184. wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
  185. $panel = new Custom_Panel_Test( $this->manager, 'baz' );
  186. ob_start();
  187. $panel->print_template();
  188. $content = ob_get_clean();
  189. $this->assertContains( '<script type="text/html" id="tmpl-customize-panel-titleless-content">', $content );
  190. $this->assertNotContains( 'accordion-section-title', $content );
  191. $this->assertContains( '<script type="text/html" id="tmpl-customize-panel-titleless">', $content );
  192. $this->assertNotContains( 'preview-notice', $content );
  193. }
  194. }
  195. require_once ABSPATH . WPINC . '/class-wp-customize-panel.php';
  196. class Custom_Panel_Test extends WP_Customize_Panel {
  197. public $type = 'titleless';
  198. protected function render_template() {
  199. ?>
  200. <li id="accordion-panel-{{ data.id }}" class="accordion-section control-section control-panel control-panel-{{ data.type }}">
  201. <ul class="accordion-sub-container control-panel-content"></ul>
  202. </li>
  203. <?php
  204. }
  205. protected function content_template() {
  206. ?>
  207. <li class="panel-meta accordion-section control-section<# if ( ! data.description ) { #> cannot-expand<# } #>">
  208. <# if ( data.description ) { #>
  209. <div class="accordion-section-content description">
  210. {{{ data.description }}}
  211. </div>
  212. <# } #>
  213. </li>
  214. <?php
  215. }
  216. }