Skip to content

Commit 46850a2

Browse files
authored
Fixture Tests: Correctly generate fixture files for form-related blocks (#56719)
* Fixture Tests: Correctly generate fixture files for form-related blocks * Fix unit test * Try to fix unit tests
1 parent dcb8a43 commit 46850a2

19 files changed

+324
-85
lines changed

packages/block-library/src/form-input/save.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
__experimentalGetBorderClassesAndStyles as getBorderClassesAndStyles,
1313
__experimentalGetColorClassesAndStyles as getColorClassesAndStyles,
1414
} from '@wordpress/block-editor';
15+
import { __unstableStripHTML as stripHTML } from '@wordpress/dom';
1516

1617
/**
1718
* Get the name attribute from a content string.
@@ -21,11 +22,8 @@ import {
2122
* @return {string} Returns the slug.
2223
*/
2324
const getNameFromLabel = ( content ) => {
24-
const dummyElement = document.createElement( 'div' );
25-
dummyElement.innerHTML = content;
26-
// Get the slug.
2725
return (
28-
removeAccents( dummyElement.innerText )
26+
removeAccents( stripHTML( content ) )
2927
// Convert anything that's not a letter or number to a hyphen.
3028
.replace( /[^\p{L}\p{N}]+/gu, '-' )
3129
// Convert to lowercase

phpunit/class-block-fixture-test.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77

88
class Block_Fixture_Test extends WP_UnitTestCase {
99

10+
public function filter_allowed_html( $tags ) {
11+
$tags['form']['class'] = true;
12+
$tags['form']['enctype'] = true;
13+
return $tags;
14+
}
15+
1016
/**
1117
* Tests that running the serialised block content through KSES doesn't cause the
1218
* HTML to change.
@@ -20,7 +26,9 @@ public function test_kses_doesnt_change_fixtures( $block, $filename ) {
2026
$block = preg_replace( "/href=['\"]data:[^'\"]+['\"]/", 'href="https://wordpress.org/foo.jpg"', $block );
2127
$block = preg_replace( '/url\(data:[^)]+\)/', 'url(https://wordpress.org/foo.jpg)', $block );
2228

29+
add_filter( 'wp_kses_allowed_html', array( $this, 'filter_allowed_html' ) );
2330
$kses_block = wp_kses_post( $block );
31+
remove_filter( 'wp_kses_allowed_html', array( $this, 'filter_allowed_html' ) );
2432

2533
// KSES adds a space at the end of self-closing tags, add it to the original to match.
2634
$block = preg_replace( '|([^ ])/>|', '$1 />', $block );
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<!-- wp:form-input {"label":"Name","required":true} -->
2-
<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>
1+
<!-- wp:form-input -->
2+
<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>
33
<!-- /wp:form-input -->

test/integration/fixtures/blocks/core__form-input.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
[
22
{
3-
"name": "core/missing",
3+
"name": "core/form-input",
44
"isValid": true,
55
"attributes": {
6-
"originalName": "core/form-input",
7-
"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>",
8-
"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 -->"
6+
"type": "text",
7+
"label": "Label",
8+
"inlineLabel": false,
9+
"required": false,
10+
"value": "",
11+
"visibilityPermissions": "all"
912
},
1013
"innerBlocks": []
1114
}
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
[
22
{
33
"blockName": "core/form-input",
4-
"attrs": {
5-
"label": "Name",
6-
"required": true
7-
},
4+
"attrs": {},
85
"innerBlocks": [],
9-
"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",
6+
"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",
107
"innerContent": [
11-
"\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"
8+
"\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"
129
]
1310
}
1411
]
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<!-- wp:form-input {"label":"Name","required":true} -->
2-
<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>
1+
<!-- wp:form-input -->
2+
<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>
33
<!-- /wp:form-input -->
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!-- wp:form-submission-notification -->
2+
<div class="wp-block-form-submission-notification form-notification-type-success"><!-- wp:paragraph {"style":{"elements":{"link":{"color":{"text":"#000000"}}}},"backgroundColor":"#00D084","textColor":"#000000"} -->
3+
<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>
4+
<!-- /wp:paragraph --></div>
5+
<!-- /wp:form-submission-notification -->
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[
2+
{
3+
"name": "core/form-submission-notification",
4+
"isValid": true,
5+
"attributes": {
6+
"type": "success"
7+
},
8+
"innerBlocks": [
9+
{
10+
"name": "core/paragraph",
11+
"isValid": true,
12+
"attributes": {
13+
"content": "Your form has been submitted successfully.",
14+
"dropCap": false,
15+
"backgroundColor": "#00D084",
16+
"textColor": "#000000",
17+
"style": {
18+
"elements": {
19+
"link": {
20+
"color": {
21+
"text": "#000000"
22+
}
23+
}
24+
}
25+
}
26+
},
27+
"innerBlocks": []
28+
}
29+
]
30+
}
31+
]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[
2+
{
3+
"blockName": "core/form-submission-notification",
4+
"attrs": {},
5+
"innerBlocks": [
6+
{
7+
"blockName": "core/paragraph",
8+
"attrs": {
9+
"style": {
10+
"elements": {
11+
"link": {
12+
"color": {
13+
"text": "#000000"
14+
}
15+
}
16+
}
17+
},
18+
"backgroundColor": "#00D084",
19+
"textColor": "#000000"
20+
},
21+
"innerBlocks": [],
22+
"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",
23+
"innerContent": [
24+
"\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"
25+
]
26+
}
27+
],
28+
"innerHTML": "\n<div class=\"wp-block-form-submission-notification form-notification-type-success\"></div>\n",
29+
"innerContent": [
30+
"\n<div class=\"wp-block-form-submission-notification form-notification-type-success\">",
31+
null,
32+
"</div>\n"
33+
]
34+
}
35+
]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!-- wp:form-submission-notification -->
2+
<div class="wp-block-form-submission-notification form-notification-type-success"><!-- wp:paragraph {"backgroundColor":"#00D084","textColor":"#000000","style":{"elements":{"link":{"color":{"text":"#000000"}}}}} -->
3+
<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>
4+
<!-- /wp:paragraph --></div>
5+
<!-- /wp:form-submission-notification -->

0 commit comments

Comments
 (0)