Make WordPress Core

Changeset 41855


Ignore:
Timestamp:
10/13/2017 05:57:38 PM (7 years 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.

Location:
trunk/tests/phpunit/tests
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/dependencies/scripts.php

    r37171 r41855  
    725725        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
    726726    }
     727
     728    /**
     729     * Testing `wp_enqueue_code_editor` with file path.
     730     *
     731     * @ticket 41871
     732     * @covers wp_enqueue_code_editor()
     733     */
     734    public function test_wp_enqueue_code_editor_when_php_file_will_be_passed() {
     735        $real_file = WP_PLUGIN_DIR . '/hello.php';
     736        $wp_enqueue_code_editor = wp_enqueue_code_editor( array( 'file' => $real_file ) );
     737        $this->assertNonEmptyMultidimensionalArray( $wp_enqueue_code_editor );
     738
     739        $this->assertEqualSets( array( 'codemirror', 'csslint', 'jshint', 'htmlhint' ), array_keys( $wp_enqueue_code_editor ) );
     740        $this->assertEqualSets(
     741            array(
     742                'autoCloseBrackets',
     743                'autoCloseTags',
     744                'continueComments',
     745                'direction',
     746                'extraKeys',
     747                'indentUnit',
     748                'indentWithTabs',
     749                'inputStyle',
     750                'lineNumbers',
     751                'lineWrapping',
     752                'matchBrackets',
     753                'matchTags',
     754                'mode',
     755                'styleActiveLine',
     756            ),
     757            array_keys( $wp_enqueue_code_editor['codemirror'] )
     758        );
     759
     760        $this->assertEqualSets(
     761            array(
     762                'errors',
     763                'box-model',
     764                'display-property-grouping',
     765                'duplicate-properties',
     766                'known-properties',
     767                'outline-none',
     768            ),
     769            array_keys( $wp_enqueue_code_editor['csslint'] )
     770        );
     771
     772        $this->assertEqualSets(
     773            array(
     774                'boss',
     775                'curly',
     776                'eqeqeq',
     777                'eqnull',
     778                'es3',
     779                'expr',
     780                'immed',
     781                'noarg',
     782                'nonbsp',
     783                'onevar',
     784                'quotmark',
     785                'trailing',
     786                'undef',
     787                'unused',
     788                'browser',
     789                'globals',
     790            ),
     791            array_keys( $wp_enqueue_code_editor['jshint'] )
     792        );
     793
     794        $this->assertEqualSets(
     795            array(
     796                'tagname-lowercase',
     797                'attr-lowercase',
     798                'attr-value-double-quotes',
     799                'doctype-first',
     800                'tag-pair',
     801                'spec-char-escape',
     802                'id-unique',
     803                'src-not-empty',
     804                'attr-no-duplication',
     805                'alt-require',
     806                'space-tab-mixed-disabled',
     807                'attr-unsafe-chars',
     808            ),
     809            array_keys( $wp_enqueue_code_editor['htmlhint'] )
     810        );
     811    }
     812
     813    /**
     814     * Testing `wp_enqueue_code_editor` with `compact`.
     815     *
     816     * @ticket 41871
     817     * @covers wp_enqueue_code_editor()
     818     */
     819    public function test_wp_enqueue_code_editor_when_generated_array_by_compact_will_be_passed() {
     820        $wp_enqueue_code_editor = wp_enqueue_code_editor( compact( 'file' ) );
     821        $this->assertNonEmptyMultidimensionalArray( $wp_enqueue_code_editor );
     822
     823        $this->assertEqualSets( array( 'codemirror', 'csslint', 'jshint', 'htmlhint' ), array_keys( $wp_enqueue_code_editor ) );
     824        $this->assertEqualSets(
     825            array(
     826                'continueComments',
     827                'direction',
     828                'extraKeys',
     829                'indentUnit',
     830                'indentWithTabs',
     831                'inputStyle',
     832                'lineNumbers',
     833                'lineWrapping',
     834                'mode',
     835                'styleActiveLine',
     836            ),
     837            array_keys( $wp_enqueue_code_editor['codemirror'] )
     838        );
     839
     840        $this->assertEqualSets(
     841            array(
     842                'errors',
     843                'box-model',
     844                'display-property-grouping',
     845                'duplicate-properties',
     846                'known-properties',
     847                'outline-none',
     848            ),
     849            array_keys( $wp_enqueue_code_editor['csslint'] )
     850        );
     851
     852        $this->assertEqualSets(
     853            array(
     854                'boss',
     855                'curly',
     856                'eqeqeq',
     857                'eqnull',
     858                'es3',
     859                'expr',
     860                'immed',
     861                'noarg',
     862                'nonbsp',
     863                'onevar',
     864                'quotmark',
     865                'trailing',
     866                'undef',
     867                'unused',
     868                'browser',
     869                'globals',
     870            ),
     871            array_keys( $wp_enqueue_code_editor['jshint'] )
     872        );
     873
     874        $this->assertEqualSets(
     875            array(
     876                'tagname-lowercase',
     877                'attr-lowercase',
     878                'attr-value-double-quotes',
     879                'doctype-first',
     880                'tag-pair',
     881                'spec-char-escape',
     882                'id-unique',
     883                'src-not-empty',
     884                'attr-no-duplication',
     885                'alt-require',
     886                'space-tab-mixed-disabled',
     887                'attr-unsafe-chars',
     888            ),
     889            array_keys( $wp_enqueue_code_editor['htmlhint'] )
     890        );
     891    }
     892
     893    /**
     894     * Testing `wp_enqueue_code_editor` with `array_merge`.
     895     *
     896     * @ticket 41871
     897     * @covers wp_enqueue_code_editor()
     898     */
     899    public function test_wp_enqueue_code_editor_when_generated_array_by_array_merge_will_be_passed() {
     900        $wp_enqueue_code_editor = wp_enqueue_code_editor(
     901            array_merge(
     902                array(
     903                    'type' => 'text/css',
     904                    'codemirror' => array(
     905                        'indentUnit' => 2,
     906                        'tabSize' => 2,
     907                    ),
     908                ),
     909                array()
     910            )
     911        );
     912
     913        $this->assertNonEmptyMultidimensionalArray( $wp_enqueue_code_editor );
     914
     915        $this->assertEqualSets( array( 'codemirror', 'csslint', 'jshint', 'htmlhint' ), array_keys( $wp_enqueue_code_editor ) );
     916        $this->assertEqualSets(
     917            array(
     918                'autoCloseBrackets',
     919                'continueComments',
     920                'direction',
     921                'extraKeys',
     922                'gutters',
     923                'indentUnit',
     924                'indentWithTabs',
     925                'inputStyle',
     926                'lineNumbers',
     927                'lineWrapping',
     928                'lint',
     929                'matchBrackets',
     930                'mode',
     931                'styleActiveLine',
     932                'tabSize',
     933            ),
     934            array_keys( $wp_enqueue_code_editor['codemirror'] )
     935        );
     936
     937        $this->assertEqualSets(
     938            array(
     939                'errors',
     940                'box-model',
     941                'display-property-grouping',
     942                'duplicate-properties',
     943                'known-properties',
     944                'outline-none',
     945            ),
     946            array_keys( $wp_enqueue_code_editor['csslint'] )
     947        );
     948
     949        $this->assertEqualSets(
     950            array(
     951                'boss',
     952                'curly',
     953                'eqeqeq',
     954                'eqnull',
     955                'es3',
     956                'expr',
     957                'immed',
     958                'noarg',
     959                'nonbsp',
     960                'onevar',
     961                'quotmark',
     962                'trailing',
     963                'undef',
     964                'unused',
     965                'browser',
     966                'globals',
     967            ),
     968            array_keys( $wp_enqueue_code_editor['jshint'] )
     969        );
     970
     971        $this->assertEqualSets(
     972            array(
     973                'tagname-lowercase',
     974                'attr-lowercase',
     975                'attr-value-double-quotes',
     976                'doctype-first',
     977                'tag-pair',
     978                'spec-char-escape',
     979                'id-unique',
     980                'src-not-empty',
     981                'attr-no-duplication',
     982                'alt-require',
     983                'space-tab-mixed-disabled',
     984                'attr-unsafe-chars',
     985            ),
     986            array_keys( $wp_enqueue_code_editor['htmlhint'] )
     987        );
     988    }
     989
     990    /**
     991     * Testing `wp_enqueue_code_editor` with `array`.
     992     *
     993     * @ticket 41871
     994     * @covers wp_enqueue_code_editor()
     995     */
     996    public function test_wp_enqueue_code_editor_when_simple_array_will_be_passed() {
     997        $wp_enqueue_code_editor = wp_enqueue_code_editor(
     998            array(
     999                'type' => 'text/css',
     1000                'codemirror' => array(
     1001                    'indentUnit' => 2,
     1002                    'tabSize' => 2,
     1003                ),
     1004            )
     1005        );
     1006
     1007        $this->assertNonEmptyMultidimensionalArray( $wp_enqueue_code_editor );
     1008
     1009        $this->assertEqualSets( array( 'codemirror', 'csslint', 'jshint', 'htmlhint' ), array_keys( $wp_enqueue_code_editor ) );
     1010        $this->assertEqualSets(
     1011            array(
     1012                'autoCloseBrackets',
     1013                'continueComments',
     1014                'direction',
     1015                'extraKeys',
     1016                'gutters',
     1017                'indentUnit',
     1018                'indentWithTabs',
     1019                'inputStyle',
     1020                'lineNumbers',
     1021                'lineWrapping',
     1022                'lint',
     1023                'matchBrackets',
     1024                'mode',
     1025                'styleActiveLine',
     1026                'tabSize',
     1027            ),
     1028            array_keys( $wp_enqueue_code_editor['codemirror'] )
     1029        );
     1030
     1031        $this->assertEqualSets(
     1032            array(
     1033                'errors',
     1034                'box-model',
     1035                'display-property-grouping',
     1036                'duplicate-properties',
     1037                'known-properties',
     1038                'outline-none',
     1039            ),
     1040            array_keys( $wp_enqueue_code_editor['csslint'] )
     1041        );
     1042
     1043        $this->assertEqualSets(
     1044            array(
     1045                'boss',
     1046                'curly',
     1047                'eqeqeq',
     1048                'eqnull',
     1049                'es3',
     1050                'expr',
     1051                'immed',
     1052                'noarg',
     1053                'nonbsp',
     1054                'onevar',
     1055                'quotmark',
     1056                'trailing',
     1057                'undef',
     1058                'unused',
     1059                'browser',
     1060                'globals',
     1061            ),
     1062            array_keys( $wp_enqueue_code_editor['jshint'] )
     1063        );
     1064
     1065        $this->assertEqualSets(
     1066            array(
     1067                'tagname-lowercase',
     1068                'attr-lowercase',
     1069                'attr-value-double-quotes',
     1070                'doctype-first',
     1071                'tag-pair',
     1072                'spec-char-escape',
     1073                'id-unique',
     1074                'src-not-empty',
     1075                'attr-no-duplication',
     1076                'alt-require',
     1077                'space-tab-mixed-disabled',
     1078                'attr-unsafe-chars',
     1079            ),
     1080            array_keys( $wp_enqueue_code_editor['htmlhint'] )
     1081        );
     1082    }
    7271083}
  • 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.