From faeac43629090d40c116a93a241ba9e1973edba9 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 13 Oct 2017 17:57:38 +0000 Subject: [PATCH] Code Editor: Add unit tests for `wp_enqueue_code_editor()` and `WP_Widget_Custom_HTML`. Props ryotsun. See #12423. Fixes #41871. git-svn-id: https://develop.svn.wordpress.org/trunk@41855 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/dependencies/scripts.php | 356 ++++++++++++++++++ .../tests/widgets/custom-html-widget.php | 117 +++++- 2 files changed, 464 insertions(+), 9 deletions(-) diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index efe5a7ba6f..c7d3f813ef 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -724,4 +724,360 @@ class Tests_Dependencies_Scripts extends WP_UnitTestCase { $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); } + + /** + * Testing `wp_enqueue_code_editor` with file path. + * + * @ticket 41871 + * @covers wp_enqueue_code_editor() + */ + public function test_wp_enqueue_code_editor_when_php_file_will_be_passed() { + $real_file = WP_PLUGIN_DIR . '/hello.php'; + $wp_enqueue_code_editor = wp_enqueue_code_editor( array( 'file' => $real_file ) ); + $this->assertNonEmptyMultidimensionalArray( $wp_enqueue_code_editor ); + + $this->assertEqualSets( array( 'codemirror', 'csslint', 'jshint', 'htmlhint' ), array_keys( $wp_enqueue_code_editor ) ); + $this->assertEqualSets( + array( + 'autoCloseBrackets', + 'autoCloseTags', + 'continueComments', + 'direction', + 'extraKeys', + 'indentUnit', + 'indentWithTabs', + 'inputStyle', + 'lineNumbers', + 'lineWrapping', + 'matchBrackets', + 'matchTags', + 'mode', + 'styleActiveLine', + ), + array_keys( $wp_enqueue_code_editor['codemirror'] ) + ); + + $this->assertEqualSets( + array( + 'errors', + 'box-model', + 'display-property-grouping', + 'duplicate-properties', + 'known-properties', + 'outline-none', + ), + array_keys( $wp_enqueue_code_editor['csslint'] ) + ); + + $this->assertEqualSets( + array( + 'boss', + 'curly', + 'eqeqeq', + 'eqnull', + 'es3', + 'expr', + 'immed', + 'noarg', + 'nonbsp', + 'onevar', + 'quotmark', + 'trailing', + 'undef', + 'unused', + 'browser', + 'globals', + ), + array_keys( $wp_enqueue_code_editor['jshint'] ) + ); + + $this->assertEqualSets( + array( + 'tagname-lowercase', + 'attr-lowercase', + 'attr-value-double-quotes', + 'doctype-first', + 'tag-pair', + 'spec-char-escape', + 'id-unique', + 'src-not-empty', + 'attr-no-duplication', + 'alt-require', + 'space-tab-mixed-disabled', + 'attr-unsafe-chars', + ), + array_keys( $wp_enqueue_code_editor['htmlhint'] ) + ); + } + + /** + * Testing `wp_enqueue_code_editor` with `compact`. + * + * @ticket 41871 + * @covers wp_enqueue_code_editor() + */ + public function test_wp_enqueue_code_editor_when_generated_array_by_compact_will_be_passed() { + $wp_enqueue_code_editor = wp_enqueue_code_editor( compact( 'file' ) ); + $this->assertNonEmptyMultidimensionalArray( $wp_enqueue_code_editor ); + + $this->assertEqualSets( array( 'codemirror', 'csslint', 'jshint', 'htmlhint' ), array_keys( $wp_enqueue_code_editor ) ); + $this->assertEqualSets( + array( + 'continueComments', + 'direction', + 'extraKeys', + 'indentUnit', + 'indentWithTabs', + 'inputStyle', + 'lineNumbers', + 'lineWrapping', + 'mode', + 'styleActiveLine', + ), + array_keys( $wp_enqueue_code_editor['codemirror'] ) + ); + + $this->assertEqualSets( + array( + 'errors', + 'box-model', + 'display-property-grouping', + 'duplicate-properties', + 'known-properties', + 'outline-none', + ), + array_keys( $wp_enqueue_code_editor['csslint'] ) + ); + + $this->assertEqualSets( + array( + 'boss', + 'curly', + 'eqeqeq', + 'eqnull', + 'es3', + 'expr', + 'immed', + 'noarg', + 'nonbsp', + 'onevar', + 'quotmark', + 'trailing', + 'undef', + 'unused', + 'browser', + 'globals', + ), + array_keys( $wp_enqueue_code_editor['jshint'] ) + ); + + $this->assertEqualSets( + array( + 'tagname-lowercase', + 'attr-lowercase', + 'attr-value-double-quotes', + 'doctype-first', + 'tag-pair', + 'spec-char-escape', + 'id-unique', + 'src-not-empty', + 'attr-no-duplication', + 'alt-require', + 'space-tab-mixed-disabled', + 'attr-unsafe-chars', + ), + array_keys( $wp_enqueue_code_editor['htmlhint'] ) + ); + } + + /** + * Testing `wp_enqueue_code_editor` with `array_merge`. + * + * @ticket 41871 + * @covers wp_enqueue_code_editor() + */ + public function test_wp_enqueue_code_editor_when_generated_array_by_array_merge_will_be_passed() { + $wp_enqueue_code_editor = wp_enqueue_code_editor( + array_merge( + array( + 'type' => 'text/css', + 'codemirror' => array( + 'indentUnit' => 2, + 'tabSize' => 2, + ), + ), + array() + ) + ); + + $this->assertNonEmptyMultidimensionalArray( $wp_enqueue_code_editor ); + + $this->assertEqualSets( array( 'codemirror', 'csslint', 'jshint', 'htmlhint' ), array_keys( $wp_enqueue_code_editor ) ); + $this->assertEqualSets( + array( + 'autoCloseBrackets', + 'continueComments', + 'direction', + 'extraKeys', + 'gutters', + 'indentUnit', + 'indentWithTabs', + 'inputStyle', + 'lineNumbers', + 'lineWrapping', + 'lint', + 'matchBrackets', + 'mode', + 'styleActiveLine', + 'tabSize', + ), + array_keys( $wp_enqueue_code_editor['codemirror'] ) + ); + + $this->assertEqualSets( + array( + 'errors', + 'box-model', + 'display-property-grouping', + 'duplicate-properties', + 'known-properties', + 'outline-none', + ), + array_keys( $wp_enqueue_code_editor['csslint'] ) + ); + + $this->assertEqualSets( + array( + 'boss', + 'curly', + 'eqeqeq', + 'eqnull', + 'es3', + 'expr', + 'immed', + 'noarg', + 'nonbsp', + 'onevar', + 'quotmark', + 'trailing', + 'undef', + 'unused', + 'browser', + 'globals', + ), + array_keys( $wp_enqueue_code_editor['jshint'] ) + ); + + $this->assertEqualSets( + array( + 'tagname-lowercase', + 'attr-lowercase', + 'attr-value-double-quotes', + 'doctype-first', + 'tag-pair', + 'spec-char-escape', + 'id-unique', + 'src-not-empty', + 'attr-no-duplication', + 'alt-require', + 'space-tab-mixed-disabled', + 'attr-unsafe-chars', + ), + array_keys( $wp_enqueue_code_editor['htmlhint'] ) + ); + } + + /** + * Testing `wp_enqueue_code_editor` with `array`. + * + * @ticket 41871 + * @covers wp_enqueue_code_editor() + */ + public function test_wp_enqueue_code_editor_when_simple_array_will_be_passed() { + $wp_enqueue_code_editor = wp_enqueue_code_editor( + array( + 'type' => 'text/css', + 'codemirror' => array( + 'indentUnit' => 2, + 'tabSize' => 2, + ), + ) + ); + + $this->assertNonEmptyMultidimensionalArray( $wp_enqueue_code_editor ); + + $this->assertEqualSets( array( 'codemirror', 'csslint', 'jshint', 'htmlhint' ), array_keys( $wp_enqueue_code_editor ) ); + $this->assertEqualSets( + array( + 'autoCloseBrackets', + 'continueComments', + 'direction', + 'extraKeys', + 'gutters', + 'indentUnit', + 'indentWithTabs', + 'inputStyle', + 'lineNumbers', + 'lineWrapping', + 'lint', + 'matchBrackets', + 'mode', + 'styleActiveLine', + 'tabSize', + ), + array_keys( $wp_enqueue_code_editor['codemirror'] ) + ); + + $this->assertEqualSets( + array( + 'errors', + 'box-model', + 'display-property-grouping', + 'duplicate-properties', + 'known-properties', + 'outline-none', + ), + array_keys( $wp_enqueue_code_editor['csslint'] ) + ); + + $this->assertEqualSets( + array( + 'boss', + 'curly', + 'eqeqeq', + 'eqnull', + 'es3', + 'expr', + 'immed', + 'noarg', + 'nonbsp', + 'onevar', + 'quotmark', + 'trailing', + 'undef', + 'unused', + 'browser', + 'globals', + ), + array_keys( $wp_enqueue_code_editor['jshint'] ) + ); + + $this->assertEqualSets( + array( + 'tagname-lowercase', + 'attr-lowercase', + 'attr-value-double-quotes', + 'doctype-first', + 'tag-pair', + 'spec-char-escape', + 'id-unique', + 'src-not-empty', + 'attr-no-duplication', + 'alt-require', + 'space-tab-mixed-disabled', + 'attr-unsafe-chars', + ), + array_keys( $wp_enqueue_code_editor['htmlhint'] ) + ); + } } diff --git a/tests/phpunit/tests/widgets/custom-html-widget.php b/tests/phpunit/tests/widgets/custom-html-widget.php index 35dac8462a..69623fbd65 100644 --- a/tests/phpunit/tests/widgets/custom-html-widget.php +++ b/tests/phpunit/tests/widgets/custom-html-widget.php @@ -28,23 +28,54 @@ class Test_WP_Widget_Custom_HTML extends WP_UnitTestCase { protected $widget_text_args; /** - * Test constructor. + * Clean up global scope. * - * @covers WP_Widget_Custom_HTML::__constructor + * @global WP_Scripts $wp_scripts + * @global WP_Styles $wp_style */ - function test_constructor() { + public function clean_up_global_scope() { + global $wp_scripts, $wp_styles; + parent::clean_up_global_scope(); + $wp_scripts = null; + $wp_styles = null; + } + + /** + * Test construct. + * + * @covers WP_Widget_Custom_HTML::__construct + */ + public function test_construct() { $widget = new WP_Widget_Custom_HTML(); $this->assertEquals( 'custom_html', $widget->id_base ); $this->assertEquals( 'widget_custom_html', $widget->widget_options['classname'] ); + $this->assertEquals( 400, $widget->control_options['width'] ); + $this->assertEquals( 350, $widget->control_options['height'] ); $this->assertTrue( $widget->widget_options['customize_selective_refresh'] ); } + /** + * Test enqueue_admin_scripts method. + * + * @covers WP_Widget_Custom_HTML::_register + */ + public function test__register() { + set_current_screen( 'widgets.php' ); + $widget = new WP_Widget_Custom_HTML(); + $widget->_register(); + + $this->assertEquals( 10, has_action( 'admin_print_scripts-widgets.php', array( $widget, 'enqueue_admin_scripts' ) ) ); + $this->assertEquals( 10, has_action( 'admin_footer-widgets.php', array( 'WP_Widget_Custom_HTML', 'render_control_template_scripts' ) ) ); + $this->assertEquals( 10, has_action( 'admin_head-widgets.php', array( 'WP_Widget_Custom_HTML', 'add_help_text' ) ) ); + $this->assertContains( 'wp.customHtmlWidgets.idBases.push( "custom_html" );', wp_scripts()->registered['custom-html-widgets']->extra['after'] ); + } + /** * Test widget method. * * @covers WP_Widget_Custom_HTML::widget */ - function test_widget() { + public function test_widget() { $widget = new WP_Widget_Custom_HTML(); $content = "Custom HTML\n\nCODE\nLast line.unclosed"; @@ -105,7 +136,7 @@ class Test_WP_Widget_Custom_HTML extends WP_UnitTestCase { * @param WP_Widget_Custom_HTML $widget Current widget instance. * @return string Widget content. */ - function filter_widget_text( $text, $instance, $widget ) { + public function filter_widget_text( $text, $instance, $widget ) { $this->widget_text_args = array( $text, $instance, $widget ); $text .= '[filter:widget_text]'; return $text; @@ -119,7 +150,7 @@ class Test_WP_Widget_Custom_HTML extends WP_UnitTestCase { * @param WP_Widget_Custom_HTML $widget Current Custom HTML widget instance. * @return string Widget content. */ - function filter_widget_custom_html_content( $widget_content, $instance, $widget ) { + public function filter_widget_custom_html_content( $widget_content, $instance, $widget ) { $this->widget_custom_html_content_args = array( $widget_content, $instance, $widget ); $widget_content .= '[filter:widget_custom_html_content]'; return $widget_content; @@ -130,7 +161,7 @@ class Test_WP_Widget_Custom_HTML extends WP_UnitTestCase { * * @covers WP_Widget_Custom_HTML::update */ - function test_update() { + public function test_update() { $widget = new WP_Widget_Custom_HTML(); $instance = array( 'title' => "The\nTitle", @@ -174,7 +205,7 @@ class Test_WP_Widget_Custom_HTML extends WP_UnitTestCase { * @param string $cap Capability name. * @return array Caps. */ - function grant_unfiltered_html_cap( $caps, $cap ) { + public function grant_unfiltered_html_cap( $caps, $cap ) { if ( 'unfiltered_html' === $cap ) { $caps = array_diff( $caps, array( 'do_not_allow' ) ); $caps[] = 'unfiltered_html'; @@ -189,11 +220,79 @@ class Test_WP_Widget_Custom_HTML extends WP_UnitTestCase { * @param string $cap Capability name. * @return array Caps. */ - function revoke_unfiltered_html_cap( $caps, $cap ) { + public function revoke_unfiltered_html_cap( $caps, $cap ) { if ( 'unfiltered_html' === $cap ) { $caps = array_diff( $caps, array( 'unfiltered_html' ) ); $caps[] = 'do_not_allow'; } return $caps; } + + /** + * Test enqueue_admin_scripts method. Condition: logged_in, syntax_highlighting is on. + * + * @covers WP_Widget_Custom_HTML::enqueue_admin_scripts + */ + public function test_enqueue_admin_scripts_when_logged_in_and_syntax_highlighting_on() { + $user = $this->factory()->user->create(); + wp_set_current_user( $user ); + wp_get_current_user()->syntax_highlighting = 'true'; + set_current_screen( 'widgets.php' ); + $widget = new WP_Widget_Custom_HTML(); + $widget->enqueue_admin_scripts(); + + $this->assertTrue( wp_script_is( 'custom-html-widgets', 'enqueued' ) ); + $this->assertTrue( wp_script_is( 'code-editor', 'enqueued' ) ); + $this->assertTrue( wp_script_is( 'wp-codemirror', 'enqueued' ) ); + $this->assertTrue( wp_script_is( 'csslint', 'enqueued' ) ); + $this->assertTrue( wp_script_is( 'jshint', 'enqueued' ) ); + $this->assertTrue( wp_script_is( 'htmlhint', 'enqueued' ) ); + } + + /** + * Test enqueue_admin_scripts method. Condition: logged_in, syntax_highlighting is off. + * + * @covers WP_Widget_Custom_HTML::enqueue_admin_scripts + */ + public function test_enqueue_admin_scripts_when_logged_in_and_syntax_highlighting_off() { + $user = $this->factory()->user->create(); + wp_set_current_user( $user ); + update_user_meta( $user, 'syntax_highlighting', 'false' ); + set_current_screen( 'widgets.php' ); + $widget = new WP_Widget_Custom_HTML(); + $widget->enqueue_admin_scripts(); + + $this->assertTrue( wp_script_is( 'custom-html-widgets', 'enqueued' ) ); + $this->assertTrue( wp_script_is( 'code-editor', 'enqueued' ) ); + $this->assertTrue( wp_script_is( 'wp-codemirror', 'enqueued' ) ); + $this->assertFalse( wp_script_is( 'csslint', 'enqueued' ) ); + $this->assertFalse( wp_script_is( 'jshint', 'enqueued' ) ); + $this->assertFalse( wp_script_is( 'htmlhint', 'enqueued' ) ); + } + + /** + * Test render_control_template_scripts method. + * + * @covers WP_Widget_Custom_HTML::render_control_template_scripts + */ + public function test_render_control_template_scripts() { + ob_start(); + WP_Widget_Custom_HTML::render_control_template_scripts(); + $output = ob_get_clean(); + + $this->assertContains( '