WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/13/2017 05:57:38 PM (21 months ago)
Author:
westonruter
Message:

Code Editor: Add unit tests for wp_enqueue_code_editor() and WP_Widget_Custom_HTML.

Props ryotsun.
See #12423.
Fixes #41871.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/widgets/custom-html-widget.php

    r41132 r41855  
    2929
    3030    /**
    31      * Test constructor.
    32      *
    33      * @covers WP_Widget_Custom_HTML::__constructor
    34      */
    35     function test_constructor() {
     31     * Clean up global scope.
     32     *
     33     * @global WP_Scripts $wp_scripts
     34     * @global WP_Styles  $wp_style
     35     */
     36    public function clean_up_global_scope() {
     37        global $wp_scripts, $wp_styles;
     38        parent::clean_up_global_scope();
     39        $wp_scripts = null;
     40        $wp_styles = null;
     41    }
     42
     43    /**
     44     * Test construct.
     45     *
     46     * @covers WP_Widget_Custom_HTML::__construct
     47     */
     48    public function test_construct() {
    3649        $widget = new WP_Widget_Custom_HTML();
    3750        $this->assertEquals( 'custom_html', $widget->id_base );
    3851        $this->assertEquals( 'widget_custom_html', $widget->widget_options['classname'] );
     52        $this->assertEquals( 400, $widget->control_options['width'] );
     53        $this->assertEquals( 350, $widget->control_options['height'] );
    3954        $this->assertTrue( $widget->widget_options['customize_selective_refresh'] );
    4055    }
    4156
    4257    /**
     58     * Test enqueue_admin_scripts method.
     59     *
     60     * @covers WP_Widget_Custom_HTML::_register
     61     */
     62    public function test__register() {
     63        set_current_screen( 'widgets.php' );
     64        $widget = new WP_Widget_Custom_HTML();
     65        $widget->_register();
     66
     67        $this->assertEquals( 10, has_action( 'admin_print_scripts-widgets.php', array( $widget, 'enqueue_admin_scripts' ) ) );
     68        $this->assertEquals( 10, has_action( 'admin_footer-widgets.php', array( 'WP_Widget_Custom_HTML', 'render_control_template_scripts' ) ) );
     69        $this->assertEquals( 10, has_action( 'admin_head-widgets.php', array( 'WP_Widget_Custom_HTML', 'add_help_text' ) ) );
     70        $this->assertContains( 'wp.customHtmlWidgets.idBases.push( "custom_html" );', wp_scripts()->registered['custom-html-widgets']->extra['after'] );
     71    }
     72
     73    /**
    4374     * Test widget method.
    4475     *
    4576     * @covers WP_Widget_Custom_HTML::widget
    4677     */
    47     function test_widget() {
     78    public function test_widget() {
    4879        $widget = new WP_Widget_Custom_HTML();
    4980        $content = "<i>Custom HTML</i>\n\n<b>CODE</b>\nLast line.<u>unclosed";
     
    106137     * @return string Widget content.
    107138     */
    108     function filter_widget_text( $text, $instance, $widget ) {
     139    public function filter_widget_text( $text, $instance, $widget ) {
    109140        $this->widget_text_args = array( $text, $instance, $widget );
    110141        $text .= '[filter:widget_text]';
     
    120151     * @return string Widget content.
    121152     */
    122     function filter_widget_custom_html_content( $widget_content, $instance, $widget ) {
     153    public function filter_widget_custom_html_content( $widget_content, $instance, $widget ) {
    123154        $this->widget_custom_html_content_args = array( $widget_content, $instance, $widget );
    124155        $widget_content .= '[filter:widget_custom_html_content]';
     
    131162     * @covers WP_Widget_Custom_HTML::update
    132163     */
    133     function test_update() {
     164    public function test_update() {
    134165        $widget = new WP_Widget_Custom_HTML();
    135166        $instance = array(
     
    175206     * @return array Caps.
    176207     */
    177     function grant_unfiltered_html_cap( $caps, $cap ) {
     208    public function grant_unfiltered_html_cap( $caps, $cap ) {
    178209        if ( 'unfiltered_html' === $cap ) {
    179210            $caps = array_diff( $caps, array( 'do_not_allow' ) );
     
    190221     * @return array Caps.
    191222     */
    192     function revoke_unfiltered_html_cap( $caps, $cap ) {
     223    public function revoke_unfiltered_html_cap( $caps, $cap ) {
    193224        if ( 'unfiltered_html' === $cap ) {
    194225            $caps = array_diff( $caps, array( 'unfiltered_html' ) );
     
    197228        return $caps;
    198229    }
     230
     231    /**
     232     * Test enqueue_admin_scripts method. Condition: logged_in, syntax_highlighting is on.
     233     *
     234     * @covers WP_Widget_Custom_HTML::enqueue_admin_scripts
     235     */
     236    public function test_enqueue_admin_scripts_when_logged_in_and_syntax_highlighting_on() {
     237        $user = $this->factory()->user->create();
     238        wp_set_current_user( $user );
     239        wp_get_current_user()->syntax_highlighting = 'true';
     240        set_current_screen( 'widgets.php' );
     241        $widget = new WP_Widget_Custom_HTML();
     242        $widget->enqueue_admin_scripts();
     243
     244        $this->assertTrue( wp_script_is( 'custom-html-widgets', 'enqueued' ) );
     245        $this->assertTrue( wp_script_is( 'code-editor', 'enqueued' ) );
     246        $this->assertTrue( wp_script_is( 'wp-codemirror', 'enqueued' ) );
     247        $this->assertTrue( wp_script_is( 'csslint', 'enqueued' ) );
     248        $this->assertTrue( wp_script_is( 'jshint', 'enqueued' ) );
     249        $this->assertTrue( wp_script_is( 'htmlhint', 'enqueued' ) );
     250    }
     251
     252    /**
     253     * Test enqueue_admin_scripts method. Condition: logged_in, syntax_highlighting is off.
     254     *
     255     * @covers WP_Widget_Custom_HTML::enqueue_admin_scripts
     256     */
     257    public function test_enqueue_admin_scripts_when_logged_in_and_syntax_highlighting_off() {
     258        $user = $this->factory()->user->create();
     259        wp_set_current_user( $user );
     260        update_user_meta( $user, 'syntax_highlighting', 'false' );
     261        set_current_screen( 'widgets.php' );
     262        $widget = new WP_Widget_Custom_HTML();
     263        $widget->enqueue_admin_scripts();
     264
     265        $this->assertTrue( wp_script_is( 'custom-html-widgets', 'enqueued' ) );
     266        $this->assertTrue( wp_script_is( 'code-editor', 'enqueued' ) );
     267        $this->assertTrue( wp_script_is( 'wp-codemirror', 'enqueued' ) );
     268        $this->assertFalse( wp_script_is( 'csslint', 'enqueued' ) );
     269        $this->assertFalse( wp_script_is( 'jshint', 'enqueued' ) );
     270        $this->assertFalse( wp_script_is( 'htmlhint', 'enqueued' ) );
     271    }
     272
     273    /**
     274     * Test render_control_template_scripts method.
     275     *
     276     * @covers WP_Widget_Custom_HTML::render_control_template_scripts
     277     */
     278    public function test_render_control_template_scripts() {
     279        ob_start();
     280        WP_Widget_Custom_HTML::render_control_template_scripts();
     281        $output = ob_get_clean();
     282
     283        $this->assertContains( '<script type="text/html" id="tmpl-widget-custom-html-control-fields">', $output );
     284    }
     285
     286    /**
     287     * Test add_help_text method.
     288     *
     289     * @covers WP_Widget_Custom_HTML::add_help_text
     290     */
     291    public function test_add_help_text() {
     292        set_current_screen( 'widgets.php' );
     293        WP_Widget_Custom_HTML::add_help_text();
     294        $content = get_current_screen()->get_help_tab( 'custom_html_widget' )['content'];
     295
     296        $this->assertContains( 'Use the Custom HTML widget to add arbitrary HTML code to your widget areas.', $content );
     297    }
    199298}
Note: See TracChangeset for help on using the changeset viewer.