Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixture Tests: Correctly generate fixture files for form-related blocks #56719

Merged
merged 3 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions packages/block-library/src/form-input/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
__experimentalGetBorderClassesAndStyles as getBorderClassesAndStyles,
__experimentalGetColorClassesAndStyles as getColorClassesAndStyles,
} from '@wordpress/block-editor';
import { __unstableStripHTML as stripHTML } from '@wordpress/dom';

/**
* Get the name attribute from a content string.
Expand All @@ -21,11 +22,8 @@ import {
* @return {string} Returns the slug.
*/
const getNameFromLabel = ( content ) => {
const dummyElement = document.createElement( 'div' );
dummyElement.innerHTML = content;
// Get the slug.
return (
removeAccents( dummyElement.innerText )
removeAccents( stripHTML( content ) )
// Convert anything that's not a letter or number to a hyphen.
.replace( /[^\p{L}\p{N}]+/gu, '-' )
// Convert to lowercase
Expand Down
8 changes: 8 additions & 0 deletions phpunit/class-block-fixture-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

class Block_Fixture_Test extends WP_UnitTestCase {

public function filter_allowed_html( $tags ) {
$tags['form']['class'] = true;
$tags['form']['enctype'] = true;
return $tags;
}
Comment on lines +10 to +14
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wp_kses_post() function does not allow form tag by default. So we need to allow attributes to be included in the serialized form block.


/**
* Tests that running the serialised block content through KSES doesn't cause the
* HTML to change.
Expand All @@ -20,7 +26,9 @@ public function test_kses_doesnt_change_fixtures( $block, $filename ) {
$block = preg_replace( "/href=['\"]data:[^'\"]+['\"]/", 'href="https://wordpress.org/foo.jpg"', $block );
$block = preg_replace( '/url\(data:[^)]+\)/', 'url(https://wordpress.org/foo.jpg)', $block );

add_filter( 'wp_kses_allowed_html', array( $this, 'filter_allowed_html' ) );
$kses_block = wp_kses_post( $block );
remove_filter( 'wp_kses_allowed_html', array( $this, 'filter_allowed_html' ) );

// KSES adds a space at the end of self-closing tags, add it to the original to match.
$block = preg_replace( '|([^ ])/>|', '$1 />', $block );
Expand Down
4 changes: 2 additions & 2 deletions test/integration/fixtures/blocks/core__form-input.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:form-input {"label":"Name","required":true} -->
<label class="wp-block-form-input-label"><span class="wp-block-form-input-label__content">Name</span><input class="wp-block-form-input" type="text" name="Name" required aria-required="true"/></label>
<!-- wp:form-input -->
<label class="wp-block-form-input__label"><span class="wp-block-form-input__label-content">Label</span><input class="wp-block-form-input__input" type="text" name="label" aria-required="false"/></label>
<!-- /wp:form-input -->
11 changes: 7 additions & 4 deletions test/integration/fixtures/blocks/core__form-input.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
[
{
"name": "core/missing",
"name": "core/form-input",
"isValid": true,
"attributes": {
"originalName": "core/form-input",
"originalUndelimitedContent": "<label class=\"wp-block-form-input-label\"><span class=\"wp-block-form-input-label__content\">Name</span><input class=\"wp-block-form-input\" type=\"text\" name=\"Name\" required aria-required=\"true\"/></label>",
"originalContent": "<!-- wp:form-input {\"label\":\"Name\",\"required\":true} -->\n<label class=\"wp-block-form-input-label\"><span class=\"wp-block-form-input-label__content\">Name</span><input class=\"wp-block-form-input\" type=\"text\" name=\"Name\" required aria-required=\"true\"/></label>\n<!-- /wp:form-input -->"
"type": "text",
"label": "Label",
"inlineLabel": false,
"required": false,
"value": "",
"visibilityPermissions": "all"
},
"innerBlocks": []
}
Expand Down
9 changes: 3 additions & 6 deletions test/integration/fixtures/blocks/core__form-input.parsed.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
[
{
"blockName": "core/form-input",
"attrs": {
"label": "Name",
"required": true
},
"attrs": {},
"innerBlocks": [],
"innerHTML": "\n<label class=\"wp-block-form-input-label\"><span class=\"wp-block-form-input-label__content\">Name</span><input class=\"wp-block-form-input\" type=\"text\" name=\"Name\" required aria-required=\"true\"/></label>\n",
"innerHTML": "\n<label class=\"wp-block-form-input__label\"><span class=\"wp-block-form-input__label-content\">Label</span><input class=\"wp-block-form-input__input\" type=\"text\" name=\"label\" aria-required=\"false\"/></label>\n",
"innerContent": [
"\n<label class=\"wp-block-form-input-label\"><span class=\"wp-block-form-input-label__content\">Name</span><input class=\"wp-block-form-input\" type=\"text\" name=\"Name\" required aria-required=\"true\"/></label>\n"
"\n<label class=\"wp-block-form-input__label\"><span class=\"wp-block-form-input__label-content\">Label</span><input class=\"wp-block-form-input__input\" type=\"text\" name=\"label\" aria-required=\"false\"/></label>\n"
]
}
]
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:form-input {"label":"Name","required":true} -->
<label class="wp-block-form-input-label"><span class="wp-block-form-input-label__content">Name</span><input class="wp-block-form-input" type="text" name="Name" required aria-required="true"/></label>
<!-- wp:form-input -->
<label class="wp-block-form-input__label"><span class="wp-block-form-input__label-content">Label</span><input class="wp-block-form-input__input" type="text" name="label" aria-required="false"/></label>
<!-- /wp:form-input -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- wp:form-submission-notification -->
<div class="wp-block-form-submission-notification form-notification-type-success"><!-- wp:paragraph {"style":{"elements":{"link":{"color":{"text":"#000000"}}}},"backgroundColor":"#00D084","textColor":"#000000"} -->
<p class="has-000000-color has-00-d-084-background-color has-text-color has-background has-link-color">Your form has been submitted successfully.</p>
<!-- /wp:paragraph --></div>
<!-- /wp:form-submission-notification -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[
{
"name": "core/form-submission-notification",
"isValid": true,
"attributes": {
"type": "success"
},
"innerBlocks": [
{
"name": "core/paragraph",
"isValid": true,
"attributes": {
"content": "Your form has been submitted successfully.",
"dropCap": false,
"backgroundColor": "#00D084",
"textColor": "#000000",
"style": {
"elements": {
"link": {
"color": {
"text": "#000000"
}
}
}
}
},
"innerBlocks": []
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[
{
"blockName": "core/form-submission-notification",
"attrs": {},
"innerBlocks": [
{
"blockName": "core/paragraph",
"attrs": {
"style": {
"elements": {
"link": {
"color": {
"text": "#000000"
}
}
}
},
"backgroundColor": "#00D084",
"textColor": "#000000"
},
"innerBlocks": [],
"innerHTML": "\n<p class=\"has-000000-color has-00-d-084-background-color has-text-color has-background has-link-color\">Your form has been submitted successfully.</p>\n",
"innerContent": [
"\n<p class=\"has-000000-color has-00-d-084-background-color has-text-color has-background has-link-color\">Your form has been submitted successfully.</p>\n"
]
}
],
"innerHTML": "\n<div class=\"wp-block-form-submission-notification form-notification-type-success\"></div>\n",
"innerContent": [
"\n<div class=\"wp-block-form-submission-notification form-notification-type-success\">",
null,
"</div>\n"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- wp:form-submission-notification -->
<div class="wp-block-form-submission-notification form-notification-type-success"><!-- wp:paragraph {"backgroundColor":"#00D084","textColor":"#000000","style":{"elements":{"link":{"color":{"text":"#000000"}}}}} -->
<p class="has-000000-color has-00-d-084-background-color has-text-color has-background has-link-color">Your form has been submitted successfully.</p>
<!-- /wp:paragraph --></div>
<!-- /wp:form-submission-notification -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!-- wp:form-submit-button -->
<div class="wp-block-form-submit-button"><!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button {"tagName":"button","type":"submit"} -->
<div class="wp-block-button"><button type="submit" class="wp-block-button__link wp-element-button">Submit</button></div>
<!-- /wp:button --></div>
<!-- /wp:buttons --></div>
<!-- /wp:form-submit-button -->
26 changes: 26 additions & 0 deletions test/integration/fixtures/blocks/core__form-submit-button.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"name": "core/form-submit-button",
"isValid": true,
"attributes": {},
"innerBlocks": [
{
"name": "core/buttons",
"isValid": true,
"attributes": {},
"innerBlocks": [
{
"name": "core/button",
"isValid": true,
"attributes": {
"tagName": "button",
"type": "submit",
"text": "Submit"
},
"innerBlocks": []
}
]
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"blockName": "core/form-submit-button",
"attrs": {},
"innerBlocks": [
{
"blockName": "core/buttons",
"attrs": {},
"innerBlocks": [
{
"blockName": "core/button",
"attrs": {
"tagName": "button",
"type": "submit"
},
"innerBlocks": [],
"innerHTML": "\n<div class=\"wp-block-button\"><button type=\"submit\" class=\"wp-block-button__link wp-element-button\">Submit</button></div>\n",
"innerContent": [
"\n<div class=\"wp-block-button\"><button type=\"submit\" class=\"wp-block-button__link wp-element-button\">Submit</button></div>\n"
]
}
],
"innerHTML": "\n<div class=\"wp-block-buttons\"></div>\n",
"innerContent": [
"\n<div class=\"wp-block-buttons\">",
null,
"</div>\n"
]
}
],
"innerHTML": "\n<div class=\"wp-block-form-submit-button\"></div>\n",
"innerContent": [
"\n<div class=\"wp-block-form-submit-button\">",
null,
"</div>\n"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!-- wp:form-submit-button -->
<div class="wp-block-form-submit-button"><!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button {"tagName":"button","type":"submit"} -->
<div class="wp-block-button"><button type="submit" class="wp-block-button__link wp-element-button">Submit</button></div>
<!-- /wp:button --></div>
<!-- /wp:buttons --></div>
<!-- /wp:form-submit-button -->
22 changes: 14 additions & 8 deletions test/integration/fixtures/blocks/core__form.html
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
<!-- wp:form -->
<form><!-- wp:form-input {"label":"Name","required":true} -->
<label class="wp-block-form-input-label"><span class="wp-block-form-input-label__content">Name</span><input class="wp-block-form-input" type="text" name="Name" required aria-required="true"/></label>
<form class="wp-block-form" enctype="text/plain">
<!-- wp:form-input {"label":"Name","required":true} -->
<label class="wp-block-form-input__label"><span class="wp-block-form-input__label-content">Name</span><input class="wp-block-form-input__input" type="text" name="name" required aria-required="true"/></label>
<!-- /wp:form-input -->

<!-- wp:form-input {"type":"email","label":"Email","required":true} -->
<label class="wp-block-form-input-label"><span class="wp-block-form-input-label__content">Email</span><input class="wp-block-form-input" type="email" name="Email" required aria-required="true"/></label>
<label class="wp-block-form-input__label"><span class="wp-block-form-input__label-content">Email</span><input class="wp-block-form-input__input" type="email" name="email" required aria-required="true"/></label>
<!-- /wp:form-input -->

<!-- wp:form-input {"type":"url","label":"Website"} -->
<label class="wp-block-form-input-label"><span class="wp-block-form-input-label__content">Website</span><input class="wp-block-form-input" type="url" name="Website" aria-required="false"/></label>
<label class="wp-block-form-input__label"><span class="wp-block-form-input__label-content">Website</span><input class="wp-block-form-input__input" type="url" name="website" aria-required="false"/></label>
<!-- /wp:form-input -->

<!-- wp:form-input {"type":"textarea","label":"Comment","required":true} -->
<label class="wp-block-form-input-label"><span class="wp-block-form-input-label__content">Comment</span><textarea class="wp-block-form-input" name="Comment" required aria-required="true"></textarea></label>
<label class="wp-block-form-input__label"><span class="wp-block-form-input__label-content">Comment</span><textarea class="wp-block-form-input__input" name="comment" required aria-required="true"></textarea></label>
<!-- /wp:form-input -->

<!-- wp:form-input {"type":"submit","label":"Submit"} -->
<div class="wp-block-buttons"><div class="wp-block-button"><button class="wp-block-button__link wp-element-button">Submit</button></div></div>
<!-- /wp:form-input --></form>
<!-- wp:form-submit-button -->
<div class="wp-block-form-submit-button"><!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button {"tagName":"button","type":"submit"} -->
<div class="wp-block-button"><button type="submit" class="wp-block-button__link wp-element-button">Submit</button></div>
<!-- /wp:button --></div>
<!-- /wp:buttons --></div>
<!-- /wp:form-submit-button -->
</form>
<!-- /wp:form -->
Loading
Loading