#41871 closed defect (bug) (fixed)
Code Editor: Add unit tests
Reported by: | westonruter | Owned by: | ryotsun |
---|---|---|---|
Milestone: | 4.9 | Priority: | high |
Severity: | normal | Version: | |
Component: | General | Keywords: | needs-patch |
Focuses: | Cc: |
Attachments (5)
Change History (19)
#2
@
7 years ago
Although I would like to work on this ticket, I need your some assistance.
What does the test expect for?
For example,
- confirm its content by using
assertNonEmptyMultidimensionalArray()
- just compared
json formated string
with the result ofwp_json_encode()
.
<?php function test_wp_enqueue_code_editor() { $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 ); $settings = array( 'codeEditor' => $wp_enqueue_code_editor, ); $expected = '{"codeEditor":{"codemirror":{"indentUnit":4,"indentWithTabs":true,"inputStyle":"contenteditable","lineNumbers":true,"lineWrapping":true,"styleActiveLine":true,"continueComments":true,"extraKeys":{"Ctrl-Space":"autocomplete","Ctrl-\/":"toggleComment","Cmd-\/":"toggleComment","Alt-F":"findPersistent"},"direction":"ltr","mode":"php","autoCloseBrackets":true,"autoCloseTags":true,"matchBrackets":true,"matchTags":{"bothTags":true}},"csslint":{"errors":true,"box-model":true,"display-property-grouping":true,"duplicate-properties":true,"known-properties":true,"outline-none":true},"jshint":{"boss":true,"curly":true,"eqeqeq":true,"eqnull":true,"es3":true,"expr":true,"immed":true,"noarg":true,"nonbsp":true,"onevar":true,"quotmark":"single","trailing":true,"undef":true,"unused":true,"browser":true,"globals":{"_":false,"Backbone":false,"jQuery":false,"JSON":false,"wp":false}},"htmlhint":{"tagname-lowercase":true,"attr-lowercase":true,"attr-value-double-quotes":true,"doctype-first":false,"tag-pair":true,"spec-char-escape":true,"id-unique":true,"src-not-empty":true,"attr-no-duplication":true,"alt-require":true,"space-tab-mixed-disabled":"tab","attr-unsafe-chars":true}}}'; $this->assertEquals( $expected, wp_json_encode( $settings ) ); }
Are something like these okay for tests?
#3
follow-up:
↓ 4
@
7 years ago
@ryotsun I'd be looking for tests on wp_enqueue_code_editor()
that passing it the various args it supports (type, file, settings) then get result in the expected settings on output, as well as the expected scripts and styles being enqueued (note: you'll need to nullify $wp_scripts
and $wp_styles
for each test.
As for how exactly to look at the resulting $settings
, I'd not suggest comparing the entire array but rather look for specific keys and values in the array. For example: https://github.com/WordPress/wordpress-develop/blob/32224a5c2a7abe7353fd16519e0686aa6603a992/tests/phpunit/tests/customize/manager.php#L2593-L2611
#4
in reply to:
↑ 3
@
7 years ago
Replying to westonruter:
Thank you for your advise.
I'll work on this as soon as possible if it's all right.
@ryotsun I'd be looking for tests on
wp_enqueue_code_editor()
that passing it the various args it supports (type, file, settings) then get result in the expected settings on output, as well as the expected scripts and styles being enqueued (note: you'll need to nullify$wp_scripts
and$wp_styles
for each test.
As for how exactly to look at the resulting
$settings
, I'd not suggest comparing the entire array but rather look for specific keys and values in the array. For example: https://github.com/WordPress/wordpress-develop/blob/32224a5c2a7abe7353fd16519e0686aa6603a992/tests/phpunit/tests/customize/manager.php#L2593-L2611
#6
@
7 years ago
I just wrote test only for wp_enqueue_code_editor()
.
And now working on the other one.
WP_Widget_Custom_HTML
is a bit confusing for me how should I define test code...
Anyway, I'll resume working tomorrow.
#7
@
7 years ago
There are already tests for the Custom HTML widget in https://github.com/WordPress/wordpress-develop/blob/master/tests/phpunit/tests/widgets/custom-html-widget.php
Here are the new changes since the widget was introduced that tests could be added for: https://gist.github.com/westonruter/412ae02d7b87156b03dcd7068bd6440e
#8
@
7 years ago
Hello, I've just added all test for this ticket.
Please kindly check the attached file.
Thank you.
This ticket was mentioned in Slack in #core by ryotsun. View the logs.
7 years ago
#10
@
7 years ago
@ryotsun thank you, it's looking good.
Three more things I'd like you to add tests for:
- When you call
wp_enqueue_code_editor()
add calls towp_script_is()
to actually confirm that all of the expected scripts got enqueued, like you did for the Custom HTML widget. - Add assertion in
\Test_WP_Widget_Custom_HTML::test_enqueue_admin_scripts()
to confirm that thecode-editor
sript gets enqueued, and thus thatwp_enqueue_code_editor()
was called. - Add tests for when a user turns off the
syntax_highlighting
option for their profile, to ensure thatwp-codemirror
and related scripts do not get enqueued.
#11
@
7 years ago
@westonruter
Thank you for your review.
I fixed some test but I'm not sure it's correct. :(
Deleted test_enqueue_admin_scripts()
method and added the following methods.
test_enqueue_admin_scripts_when_logged_in_and_syntax_highlighting_on()
test_enqueue_admin_scripts_when_logged_in_and_syntax_highlighting_off()
Bumping priority to high for visibility and alignment with 4.9 goals, and given proximity to beta 1 deadline.