@wordpress/e2e-tests 9.22.0 → 10.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 10.0.0 (2026-10-20)
6
+
7
+ ### Breaking Changes
8
+
9
+ - Raise the minimum required Node.js version to `>=22.13.0`, for stable `require(esm)` support ([#84313](https://github.com/WordPress/gutenberg/pull/84313)).
10
+
5
11
  ## 9.22.0 (2026-10-07)
6
12
 
7
13
  ### Enhancements
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/e2e-tests",
3
- "version": "9.22.0",
3
+ "version": "10.0.0",
4
4
  "description": "Test plugins and mu-plugins for E2E tests in WordPress.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -20,11 +20,11 @@
20
20
  "url": "https://github.com/WordPress/gutenberg/issues"
21
21
  },
22
22
  "engines": {
23
- "node": ">=18.12.0"
23
+ "node": ">=22.13.0"
24
24
  },
25
25
  "dependencies": {
26
- "@wordpress/interactivity": "^7.0.0",
27
- "@wordpress/interactivity-router": "^2.57.0"
26
+ "@wordpress/interactivity": "^8.0.0",
27
+ "@wordpress/interactivity-router": "^3.0.0"
28
28
  },
29
29
  "devDependencies": {
30
30
  "y-websocket": "^3.0.0"
@@ -36,5 +36,5 @@
36
36
  "publishConfig": {
37
37
  "access": "public"
38
38
  },
39
- "gitHead": "12441e2520c78b2254a792a8cba40c34110be060"
39
+ "gitHead": "8d30f92cbbde3ddf781c8799443c649befbbcb88"
40
40
  }
@@ -0,0 +1,10 @@
1
+ <?php
2
+ /**
3
+ * Plugin Name: Gutenberg Test Animated Image Subsizes
4
+ * Plugin URI: https://github.com/WordPress/gutenberg
5
+ * Author: Gutenberg Team
6
+ *
7
+ * @package gutenberg-test-animated-image-subsizes
8
+ */
9
+
10
+ add_filter( 'wp_generate_animated_image_subsizes', '__return_false' );
@@ -0,0 +1,144 @@
1
+ <?php
2
+ /**
3
+ * Plugin Name: Gutenberg Test Autosave Failures
4
+ * Plugin URI: https://github.com/WordPress/gutenberg
5
+ * Author: Gutenberg Team
6
+ *
7
+ * @package gutenberg-test-autosave-failures
8
+ */
9
+
10
+ /**
11
+ * Makes autosave requests fail in the way named by the
12
+ * `gutenberg_test_autosave_failure` cookie.
13
+ *
14
+ * @param mixed $result Response to replace the requested version with.
15
+ * @param WP_REST_Server $server Server instance.
16
+ * @param WP_REST_Request $request Request used to generate the response.
17
+ * @return mixed
18
+ */
19
+ function gutenberg_test_autosave_failure( $result, $server, $request ) {
20
+ if (
21
+ 'POST' !== $request->get_method() ||
22
+ ! str_ends_with( $request->get_route(), '/autosaves' ) ||
23
+ empty( $_COOKIE['gutenberg_test_autosave_failure'] )
24
+ ) {
25
+ return $result;
26
+ }
27
+
28
+ global $wpdb;
29
+
30
+ switch ( $_COOKIE['gutenberg_test_autosave_failure'] ) {
31
+ case 'fatal-error':
32
+ ini_set( 'display_errors', '0' );
33
+ gutenberg_test_autosave_undefined_function();
34
+ break;
35
+
36
+ case 'fatal-error-displayed':
37
+ ini_set( 'display_errors', '1' );
38
+ gutenberg_test_autosave_undefined_function();
39
+ break;
40
+
41
+ case 'database-connection-lost':
42
+ $wpdb->query( 'KILL CONNECTION_ID()' );
43
+ $wpdb->dbhost = '127.0.0.1:1';
44
+ $wpdb->reconnect_retries = 1;
45
+ break;
46
+
47
+ case 'database-write':
48
+ add_filter(
49
+ 'query',
50
+ static function ( $query ) use ( $wpdb ) {
51
+ if ( str_starts_with( $query, "UPDATE `{$wpdb->posts}`" ) ) {
52
+ return "UPDATE `{$wpdb->posts}_missing` SET ID = 0";
53
+ }
54
+ return $query;
55
+ }
56
+ );
57
+ break;
58
+
59
+ case 'permission':
60
+ add_filter(
61
+ 'map_meta_cap',
62
+ static function ( $caps, $cap ) {
63
+ return 'edit_post' === $cap ? array( 'do_not_allow' ) : $caps;
64
+ },
65
+ 10,
66
+ 2
67
+ );
68
+ break;
69
+
70
+ case 'plain-message':
71
+ return new WP_Error( 'test_error', 'Something went wrong on the server.', array( 'status' => 500 ) );
72
+
73
+ case 'link-in-message':
74
+ return new WP_Error( 'test_error', 'Blocked. <a href="https://example.com">Learn more</a>.', array( 'status' => 403 ) );
75
+
76
+ case 'no-message':
77
+ status_header( 500 );
78
+ header( 'Content-Type: application/json' );
79
+ echo '{}';
80
+ exit;
81
+
82
+ case 'zero':
83
+ status_header( 500 );
84
+ header( 'Content-Type: application/json' );
85
+ echo '0';
86
+ exit;
87
+
88
+ case 'too-many-requests':
89
+ status_header( 429 );
90
+ header( 'Content-Type: text/html' );
91
+ echo '<html><body><h1>429 Too Many Requests</h1></body></html>';
92
+ exit;
93
+
94
+ case 'bad-gateway':
95
+ status_header( 502 );
96
+ header( 'Content-Type: text/html' );
97
+ echo '<html><body><h1>502 Bad Gateway</h1></body></html>';
98
+ exit;
99
+
100
+ case 'dropped-connection':
101
+ posix_kill( getmypid(), 9 );
102
+ break;
103
+
104
+ case 'warning-displayed':
105
+ ini_set( 'display_errors', '1' );
106
+ trigger_error( 'A plugin printed a warning.', E_USER_WARNING );
107
+ break;
108
+
109
+ case 'slow':
110
+ sleep( 3 );
111
+ break;
112
+
113
+ case 'redirect-to-login':
114
+ wp_redirect( wp_login_url() );
115
+ exit;
116
+
117
+ case 'redirect-to-other-site':
118
+ wp_redirect( str_replace( '://localhost', '://127.0.0.1', wp_login_url() ) );
119
+ exit;
120
+ }
121
+
122
+ return $result;
123
+ }
124
+ add_filter( 'rest_pre_dispatch', 'gutenberg_test_autosave_failure', 10, 3 );
125
+
126
+ /**
127
+ * Turns on maintenance mode for requests that ask for it. WordPress includes
128
+ * this file on every request, before plugins load.
129
+ */
130
+ function gutenberg_test_autosave_failures_activate() {
131
+ file_put_contents(
132
+ ABSPATH . '.maintenance',
133
+ "<?php\n\$upgrading = 'maintenance' === ( \$_COOKIE['gutenberg_test_autosave_failure'] ?? '' ) ? time() : 0;\n"
134
+ );
135
+ }
136
+ register_activation_hook( __FILE__, 'gutenberg_test_autosave_failures_activate' );
137
+
138
+ /**
139
+ * Removes the maintenance file.
140
+ */
141
+ function gutenberg_test_autosave_failures_deactivate() {
142
+ wp_delete_file( ABSPATH . '.maintenance' );
143
+ }
144
+ register_deactivation_hook( __FILE__, 'gutenberg_test_autosave_failures_deactivate' );
@@ -0,0 +1,49 @@
1
+ /**
2
+ * The JavaScript parts of the `gutenberg-test-entity-actions-api/close-comments`
3
+ * action the test plugin registers for Pages.
4
+ *
5
+ * The default export maps action ids to the properties of the action that PHP
6
+ * cannot serialize. The module applies to the actions it was registered with:
7
+ * `gutenberg-test-entity-actions-api/close-comments`. The
8
+ * `gutenberg-test-entity-actions-api/archive` entry is a whole action no PHP
9
+ * registration names: the editor ignores it, as a module only augments the
10
+ * actions registered with it.
11
+ *
12
+ * No build step: the callback dispatches through the data registry it
13
+ * receives, so the module imports nothing.
14
+ */
15
+
16
+ const closeComments = {
17
+ isEligible: ( item ) => item.comment_status === 'open',
18
+ callback: async ( items, { registry, onActionPerformed } ) => {
19
+ const { saveEntityRecord } = registry.dispatch( 'core' );
20
+ await Promise.all(
21
+ items.map( ( item ) =>
22
+ saveEntityRecord( 'postType', 'page', {
23
+ id: item.id,
24
+ comment_status: 'closed',
25
+ } )
26
+ )
27
+ );
28
+ registry
29
+ .dispatch( 'core/notices' )
30
+ .createSuccessNotice( 'Comments closed.', { type: 'snackbar' } );
31
+ onActionPerformed?.( items );
32
+ },
33
+ };
34
+
35
+ /**
36
+ * A complete action definition, id and label included, with no registration
37
+ * in PHP naming it. The editor merges a module into the actions the server
38
+ * registered the module with, so this entry never becomes an action.
39
+ */
40
+ const archive = {
41
+ id: 'gutenberg-test-entity-actions-api/archive',
42
+ label: 'Archive',
43
+ callback: () => {},
44
+ };
45
+
46
+ export default {
47
+ 'gutenberg-test-entity-actions-api/close-comments': closeComments,
48
+ 'gutenberg-test-entity-actions-api/archive': archive,
49
+ };
@@ -0,0 +1,106 @@
1
+ <?php
2
+ /**
3
+ * Plugin Name: Gutenberg Test Entity Actions API
4
+ * Plugin URI: https://github.com/WordPress/gutenberg
5
+ * Author: Gutenberg Team
6
+ *
7
+ * Extends the actions of Pages through the entity actions API, one self-contained
8
+ * function per case. Every case registers its actions on
9
+ * `wp_entity_actions_api_init`, the action the registry fires the first time its
10
+ * actions are read, after `init`, once the core actions are registered.
11
+ *
12
+ * 1. `add_action_with_script_module`: an action
13
+ * (`gutenberg-test-entity-actions-api/close-comments`) whose data is registered
14
+ * here and whose JavaScript parts come from
15
+ * `entity-actions-api/close-comments.js`. The same module exports a complete
16
+ * `gutenberg-test-entity-actions-api/archive` action no registration names: the
17
+ * module augments the actions it was registered with, so the entry is
18
+ * ignored and the action is not shown.
19
+ * 2. `replace_action`: a substitute for a core action (`move-to-trash`),
20
+ * unregistered and registered again with a new label, merged into the core
21
+ * definition, and with the script module of the core action, which keeps
22
+ * its JavaScript parts.
23
+ * 3. `remove_action`: the removal of a core action (`duplicate-post`).
24
+ *
25
+ * @package gutenberg-test-entity-actions-api
26
+ */
27
+
28
+ /**
29
+ * Case 1: an action (`gutenberg-test-entity-actions-api/close-comments`) whose
30
+ * eligibility and callback come from a script module. Two functions:
31
+ *
32
+ * - The module, on `init` like any plugin asset. It is registered, not
33
+ * enqueued: Gutenberg adds the modules of the registered actions to the
34
+ * import map of the editor pages.
35
+ * - The action, on `wp_entity_actions_api_init`. The action fires whenever the
36
+ * registry is first read, during REST requests too, so its callback
37
+ * registers the action and nothing else.
38
+ */
39
+ function gutenberg_test_entity_actions_api_register_close_comments_module() {
40
+ wp_register_script_module(
41
+ 'gutenberg-test-entity-actions-api/close-comments',
42
+ plugins_url( 'entity-actions-api/close-comments.js', __FILE__ ),
43
+ array(),
44
+ filemtime( __DIR__ . '/entity-actions-api/close-comments.js' )
45
+ );
46
+ }
47
+ add_action( 'init', 'gutenberg_test_entity_actions_api_register_close_comments_module' );
48
+
49
+ function gutenberg_test_entity_actions_api_add_action_with_script_module() {
50
+ wp_register_entity_actions(
51
+ 'gutenberg-test-entity-actions-api',
52
+ 'postType',
53
+ 'page',
54
+ array(
55
+ array(
56
+ 'id' => 'gutenberg-test-entity-actions-api/close-comments',
57
+ 'label' => 'Close comments',
58
+ 'supportsBulk' => true,
59
+ ),
60
+ ),
61
+ 'gutenberg-test-entity-actions-api/close-comments'
62
+ );
63
+ }
64
+ add_action( 'wp_entity_actions_api_init', 'gutenberg_test_entity_actions_api_add_action_with_script_module' );
65
+
66
+ /**
67
+ * Case 2: a substitute for a core action (`move-to-trash`). Unregistering
68
+ * drops its definition and detaches its script module, so the registration
69
+ * that follows is the whole action: the core definition it returns, with a new
70
+ * label, and not primary, so it moves from the row to the actions menu.
71
+ * Registered with the script module of the core action, it keeps its
72
+ * eligibility and its confirmation modal. It is only registered again when it
73
+ * was registered, as WordPress registers some actions only for the users who
74
+ * can perform them. The default priority runs after the core actions: there is
75
+ * nothing to unregister before.
76
+ */
77
+ function gutenberg_test_entity_actions_api_replace_action() {
78
+ $removed = wp_unregister_entity_actions( 'postType', 'page', array( 'move-to-trash' ) );
79
+ if ( ! $removed ) {
80
+ return;
81
+ }
82
+ wp_register_entity_actions(
83
+ 'gutenberg-test-entity-actions-api',
84
+ 'postType',
85
+ 'page',
86
+ array(
87
+ array_merge(
88
+ $removed[0],
89
+ array(
90
+ 'label' => 'Move to bin…',
91
+ 'isPrimary' => false,
92
+ )
93
+ ),
94
+ ),
95
+ '@wordpress/core-actions/post_type_supports'
96
+ );
97
+ }
98
+ add_action( 'wp_entity_actions_api_init', 'gutenberg_test_entity_actions_api_replace_action' );
99
+
100
+ /**
101
+ * Case 3: the removal of a core action (`duplicate-post`).
102
+ */
103
+ function gutenberg_test_entity_actions_api_remove_action() {
104
+ wp_unregister_entity_actions( 'postType', 'page', array( 'duplicate-post' ) );
105
+ }
106
+ add_action( 'wp_entity_actions_api_init', 'gutenberg_test_entity_actions_api_remove_action' );
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Styles for the patched `comment_status` field. Only fields with a
3
+ * JavaScript `render` can be targeted: it is the render that emits the class
4
+ * name.
5
+ */
6
+
7
+ .gutenberg-test-comment-status::before {
8
+ content: "";
9
+ display: inline-block;
10
+ width: 8px;
11
+ height: 8px;
12
+ margin-right: 6px;
13
+ border-radius: 50%;
14
+ background: #757575;
15
+ vertical-align: middle;
16
+ }
17
+
18
+ .gutenberg-test-comment-status.is-open::before {
19
+ background: #00a32a;
20
+ }
21
+
22
+ .gutenberg-test-comment-status.is-closed::before {
23
+ background: #d63638;
24
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * The JavaScript parts of the patch of the `comment_status` field the test
3
+ * plugin registers for Pages: a render replacing the default one.
4
+ *
5
+ * No build step: script modules cannot import the `@wordpress/*` scripts, so
6
+ * the `wp.*` globals the editor already loaded are used instead.
7
+ */
8
+ const { createElement } = window.wp.element;
9
+
10
+ const commentStatus = {
11
+ render: ( { item, field } ) => {
12
+ const value = field.getValue( { item } );
13
+ const option = ( field.elements ?? [] ).find(
14
+ ( element ) => element.value === value
15
+ );
16
+ return createElement(
17
+ 'span',
18
+ { className: `gutenberg-test-comment-status is-${ value }` },
19
+ option ? option.label : value
20
+ );
21
+ },
22
+ };
23
+
24
+ export default {
25
+ comment_status: commentStatus,
26
+ };
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Styles for the `reading_time` field. Only fields with a JavaScript `render`
3
+ * can be targeted: it is the render that emits the class name.
4
+ */
5
+
6
+ .gutenberg-test-reading-time {
7
+ display: inline-block;
8
+ padding: 0 6px;
9
+ border-radius: 2px;
10
+ background: rgb(56, 88, 233);
11
+ color: #fff;
12
+ line-height: 20px;
13
+ }
14
+
15
+ .gutenberg-test-reading-time.is-empty {
16
+ background: rgb(148, 148, 148);
17
+ }
18
+
19
+ .gutenberg-test-reading-time.is-long {
20
+ background: rgb(204, 24, 24);
21
+ }
@@ -0,0 +1,82 @@
1
+ /**
2
+ * The JavaScript parts of the `reading_time` field the test plugin registers
3
+ * for Pages.
4
+ *
5
+ * The default export maps field ids to the properties of the field that PHP
6
+ * cannot serialize. The module applies to the fields it was registered with:
7
+ * `reading_time`. The `word_count` entry is a whole field no PHP registration
8
+ * names: the editor ignores it, as a module only augments the fields
9
+ * registered with it.
10
+ *
11
+ * No build step: script modules cannot import the `@wordpress/*` scripts, so
12
+ * the `wp.*` globals the editor already loaded are used instead.
13
+ */
14
+ const { createElement } = window.wp.element;
15
+
16
+ const WORDS_PER_MINUTE = 200;
17
+ const LONG_READ_MINUTES = 10;
18
+
19
+ const readingTime = {
20
+ getValue: ( { item } ) => {
21
+ const content =
22
+ typeof item.content === 'string'
23
+ ? item.content
24
+ : ( item.content?.raw ?? item.content?.rendered ?? '' );
25
+ const words = content
26
+ .replace( /<[^>]*>/g, ' ' )
27
+ .split( /\s+/ )
28
+ .filter( Boolean ).length;
29
+ return Math.ceil( words / WORDS_PER_MINUTE );
30
+ },
31
+ render: ( { item, field } ) => {
32
+ const minutes = field.getValue( { item } );
33
+ const className = [
34
+ 'gutenberg-test-reading-time',
35
+ minutes === 0 && 'is-empty',
36
+ minutes >= LONG_READ_MINUTES && 'is-long',
37
+ ]
38
+ .filter( Boolean )
39
+ .join( ' ' );
40
+ return createElement(
41
+ 'span',
42
+ { className },
43
+ minutes === 0 ? 'Empty' : `${ minutes } min`
44
+ );
45
+ },
46
+ };
47
+
48
+ /**
49
+ * A complete field definition, id and label included, with no registration
50
+ * in PHP naming it. The editor merges a module into the fields the server
51
+ * registered the module with, so this entry never becomes a field.
52
+ */
53
+ const wordCount = {
54
+ id: 'word_count',
55
+ type: 'integer',
56
+ label: 'Word count',
57
+ enableSorting: false,
58
+ enableHiding: true,
59
+ filterBy: false,
60
+ readOnly: true,
61
+ getValue: ( { item } ) => {
62
+ const content =
63
+ typeof item.content === 'string'
64
+ ? item.content
65
+ : ( item.content?.raw ?? item.content?.rendered ?? '' );
66
+ return content
67
+ .replace( /<[^>]*>/g, ' ' )
68
+ .split( /\s+/ )
69
+ .filter( Boolean ).length;
70
+ },
71
+ render: ( { item, field } ) =>
72
+ createElement(
73
+ 'span',
74
+ { className: 'gutenberg-test-word-count' },
75
+ `${ field.getValue( { item } ) } words`
76
+ ),
77
+ };
78
+
79
+ export default {
80
+ reading_time: readingTime,
81
+ word_count: wordCount,
82
+ };
@@ -0,0 +1,291 @@
1
+ <?php
2
+ /**
3
+ * Plugin Name: Gutenberg Test Entity Fields API
4
+ * Plugin URI: https://github.com/WordPress/gutenberg
5
+ * Author: Gutenberg Team
6
+ *
7
+ * Extends the fields of Pages through the Entity fields API, one
8
+ * self-contained function per case. Every server case registers its fields on
9
+ * `wp_entity_fields_api_init`, the action the registry fires the first time
10
+ * its fields are read, after `init`, once the default fields are registered.
11
+ *
12
+ * 1. `add_field_declarative`: a declarative field (`menu_order`), plain data,
13
+ * no JavaScript.
14
+ * 2. `add_field_with_script_module`: a field backed by a script module and a
15
+ * stylesheet (`reading_time`), its value and render coming from
16
+ * `entity-fields-api/reading-time.js`. The same module exports a complete
17
+ * `word_count` field no registration names: the module augments the fields
18
+ * it was registered with, so the entry is ignored and the field is not
19
+ * registered.
20
+ * 3. `update_field`: an update of a default field (`comment_status`), which
21
+ * becomes sortable and hideable, and gets a render from
22
+ * `entity-fields-api/comment-status.js`, keeping the rest of its
23
+ * definition.
24
+ * 4. `replace_field`: a substitute for a default field (`author`),
25
+ * unregistered and registered again as a plain integer with a new label.
26
+ * 5. `add_field_with_data`: a field whose value is data the plugin adds to
27
+ * the Pages REST endpoint (`subtitle`), a REST field backed by post meta,
28
+ * readable in the list and writable from the Quick Edit form, which the
29
+ * function adds the field to.
30
+ *
31
+ * @package gutenberg-test-entity-fields-api
32
+ */
33
+
34
+ /**
35
+ * Case 1: a declarative field (`menu_order`), whose value is a property of
36
+ * the record. No JavaScript, no styles.
37
+ */
38
+ function gutenberg_test_entity_fields_api_add_field_declarative() {
39
+ wp_register_entity_fields(
40
+ 'gutenberg-test-entity-fields-api',
41
+ 'postType',
42
+ 'page',
43
+ array(
44
+ array(
45
+ 'id' => 'menu_order',
46
+ 'type' => 'integer',
47
+ 'label' => 'Order',
48
+ 'description' => 'Position of the page among its siblings.',
49
+ 'enableSorting' => false,
50
+ 'filterBy' => false,
51
+ ),
52
+ )
53
+ );
54
+ }
55
+ add_action( 'wp_entity_fields_api_init', 'gutenberg_test_entity_fields_api_add_field_declarative' );
56
+
57
+ /**
58
+ * Case 2: a field (`reading_time`) whose value and render come from a script
59
+ * module, styled by a stylesheet. Two functions:
60
+ *
61
+ * - The assets, on `init` like any plugin asset. The module is registered,
62
+ * not enqueued: Gutenberg adds the modules of the registered fields to the
63
+ * import map of the editor pages. A stylesheet cannot ride along with a
64
+ * script module, so the function enqueues it on every screen where the
65
+ * field can show.
66
+ * - The field, on `wp_entity_fields_api_init`. The action fires whenever the
67
+ * registry is first read, during REST requests too, so its callback
68
+ * registers the field and nothing else.
69
+ *
70
+ * The module also exports a complete `word_count` field, id and label
71
+ * included, that no registration names. A module only
72
+ * augments the fields it was registered with, so the entry is ignored and
73
+ * the field is not registered.
74
+ */
75
+ function gutenberg_test_entity_fields_api_register_reading_time_assets() {
76
+ wp_register_script_module(
77
+ 'gutenberg-test-entity-fields-api/reading-time',
78
+ plugins_url( 'entity-fields-api/reading-time.js', __FILE__ ),
79
+ array(),
80
+ filemtime( __DIR__ . '/entity-fields-api/reading-time.js' )
81
+ );
82
+
83
+ wp_register_style(
84
+ 'gutenberg-test-entity-fields-api-reading-time',
85
+ plugins_url( 'entity-fields-api/reading-time.css', __FILE__ ),
86
+ array(),
87
+ filemtime( __DIR__ . '/entity-fields-api/reading-time.css' )
88
+ );
89
+ $enqueue_style = function () {
90
+ wp_enqueue_style( 'gutenberg-test-entity-fields-api-reading-time' );
91
+ };
92
+ // The post editor and the site editor.
93
+ add_action( 'enqueue_block_editor_assets', $enqueue_style );
94
+ // The extensible site editor renders its own document and exits on
95
+ // `admin_init`, before `enqueue_block_editor_assets` fires, so styles are
96
+ // enqueued from its `{page}_init` action instead.
97
+ add_action( 'site-editor-v2_init', $enqueue_style );
98
+ // The extensible site editor rendered inside the wp-admin chrome.
99
+ add_action( 'site-editor-v2-wp-admin_init', $enqueue_style );
100
+ }
101
+ add_action( 'init', 'gutenberg_test_entity_fields_api_register_reading_time_assets' );
102
+
103
+ function gutenberg_test_entity_fields_api_add_field_with_script_module() {
104
+ wp_register_entity_fields(
105
+ 'gutenberg-test-entity-fields-api',
106
+ 'postType',
107
+ 'page',
108
+ array(
109
+ array(
110
+ 'id' => 'reading_time',
111
+ 'type' => 'integer',
112
+ 'label' => 'Reading time',
113
+ 'enableSorting' => false,
114
+ 'filterBy' => false,
115
+ 'readOnly' => true,
116
+ ),
117
+ ),
118
+ 'gutenberg-test-entity-fields-api/reading-time'
119
+ );
120
+ }
121
+ add_action( 'wp_entity_fields_api_init', 'gutenberg_test_entity_fields_api_add_field_with_script_module' );
122
+
123
+ /**
124
+ * Case 3: an update of a default field (`comment_status`). Only the properties
125
+ * given change, and the module applies to the field on top of the modules it
126
+ * has. Hideable, so the field can be shown from the view options.
127
+ *
128
+ * Only a registered field can be updated, so the update must come after the
129
+ * default fields, registered on `wp_entity_fields_api_init` at priority 0: the
130
+ * default priority does. An update before would be refused. The assets go
131
+ * on `init`, split from the field as in case 2.
132
+ */
133
+ function gutenberg_test_entity_fields_api_register_comment_status_assets() {
134
+ wp_register_script_module(
135
+ 'gutenberg-test-entity-fields-api/comment-status',
136
+ plugins_url( 'entity-fields-api/comment-status.js', __FILE__ ),
137
+ array(),
138
+ filemtime( __DIR__ . '/entity-fields-api/comment-status.js' )
139
+ );
140
+
141
+ wp_register_style(
142
+ 'gutenberg-test-entity-fields-api-comment-status',
143
+ plugins_url( 'entity-fields-api/comment-status.css', __FILE__ ),
144
+ array(),
145
+ filemtime( __DIR__ . '/entity-fields-api/comment-status.css' )
146
+ );
147
+ $enqueue_style = function () {
148
+ wp_enqueue_style( 'gutenberg-test-entity-fields-api-comment-status' );
149
+ };
150
+ // The same screens as case 2: see the comments there.
151
+ add_action( 'enqueue_block_editor_assets', $enqueue_style );
152
+ add_action( 'site-editor-v2_init', $enqueue_style );
153
+ add_action( 'site-editor-v2-wp-admin_init', $enqueue_style );
154
+ }
155
+ add_action( 'init', 'gutenberg_test_entity_fields_api_register_comment_status_assets' );
156
+
157
+ function gutenberg_test_entity_fields_api_update_field() {
158
+ wp_update_entity_fields(
159
+ 'gutenberg-test-entity-fields-api',
160
+ 'postType',
161
+ 'page',
162
+ array(
163
+ array(
164
+ 'id' => 'comment_status',
165
+ 'enableSorting' => true,
166
+ 'enableHiding' => true,
167
+ ),
168
+ ),
169
+ 'gutenberg-test-entity-fields-api/comment-status'
170
+ );
171
+ }
172
+ add_action( 'wp_entity_fields_api_init', 'gutenberg_test_entity_fields_api_update_field' );
173
+
174
+ /**
175
+ * Case 4: a substitute for a default field (`author`). Unregistering drops
176
+ * its definition and detaches its script modules, so the registration that
177
+ * follows is the whole field. The default priority runs after the default
178
+ * fields: there is nothing to unregister before.
179
+ */
180
+ function gutenberg_test_entity_fields_api_replace_field() {
181
+ wp_unregister_entity_fields( 'postType', 'page', array( 'author' ) );
182
+ wp_register_entity_fields(
183
+ 'gutenberg-test-entity-fields-api',
184
+ 'postType',
185
+ 'page',
186
+ array(
187
+ array(
188
+ 'id' => 'author',
189
+ 'type' => 'integer',
190
+ 'label' => 'Written by',
191
+ 'filterBy' => false,
192
+ ),
193
+ )
194
+ );
195
+ }
196
+ add_action( 'wp_entity_fields_api_init', 'gutenberg_test_entity_fields_api_replace_field' );
197
+
198
+ /**
199
+ * Case 5: a field (`subtitle`) whose value is data the plugin adds to the
200
+ * Pages REST endpoint. Three parts:
201
+ *
202
+ * - A REST field reading and writing post meta the plugin owns, which the
203
+ * endpoint does not expose otherwise, so the record of every page carries
204
+ * `subtitle` and a request updating a page can set it. Hooked to
205
+ * `rest_api_init` on its own: a request to the Pages endpoint does not read
206
+ * the fields registry, so `wp_entity_fields_api_init` may never fire during it.
207
+ * - A declarative field like case 1. The field API reads the value from the
208
+ * record and, as the field is not read-only, the Quick Edit form saves the
209
+ * edits with the record, through the REST field.
210
+ * - An entry in the Quick Edit form of Pages. The form lists its fields
211
+ * explicitly in the view configuration of the entity, so a new field is
212
+ * not offered for editing until it is added there. Merging appends the
213
+ * field to the end of the form. Filtered from the moment the plugin
214
+ * loads, whenever the view configuration is read.
215
+ */
216
+ function gutenberg_test_entity_fields_api_add_field_with_data() {
217
+ wp_register_entity_fields(
218
+ 'gutenberg-test-entity-fields-api',
219
+ 'postType',
220
+ 'page',
221
+ array(
222
+ array(
223
+ 'id' => 'subtitle',
224
+ 'type' => 'text',
225
+ 'label' => 'Subtitle',
226
+ 'description' => 'A secondary title shown under the title.',
227
+ 'enableSorting' => false,
228
+ 'filterBy' => false,
229
+ ),
230
+ )
231
+ );
232
+ }
233
+ add_action( 'wp_entity_fields_api_init', 'gutenberg_test_entity_fields_api_add_field_with_data' );
234
+
235
+ add_action(
236
+ 'rest_api_init',
237
+ function () {
238
+ register_rest_field(
239
+ 'page',
240
+ 'subtitle',
241
+ array(
242
+ 'schema' => array(
243
+ 'description' => 'A secondary title shown under the title.',
244
+ 'type' => 'string',
245
+ 'context' => array( 'view', 'edit' ),
246
+ ),
247
+ 'get_callback' => function ( $item ) {
248
+ return (string) get_post_meta( $item['id'], '_gutenberg_test_subtitle', true );
249
+ },
250
+ 'update_callback' => function ( $value, $post ) {
251
+ update_post_meta( $post->ID, '_gutenberg_test_subtitle', sanitize_text_field( $value ) );
252
+ return true;
253
+ },
254
+ )
255
+ );
256
+ }
257
+ );
258
+
259
+ add_filter(
260
+ 'get_entity_view_config_posttype_page',
261
+ function ( $data ) {
262
+ return $data->merge(
263
+ array(
264
+ 'form' => array(
265
+ 'fields' => array( 'subtitle' ),
266
+ ),
267
+ ),
268
+ 1
269
+ );
270
+ }
271
+ );
272
+
273
+ /**
274
+ * Registers a submenu rendering the extensible site editor inside the
275
+ * wp-admin chrome, so the `site-editor-v2-wp-admin_init` enqueues above can
276
+ * be exercised. The page has no menu entry of its own.
277
+ */
278
+ function gutenberg_test_entity_fields_api_register_embedded_page() {
279
+ if ( ! function_exists( 'gutenberg_site_editor_v2_wp_admin_render_page' ) ) {
280
+ return;
281
+ }
282
+ add_submenu_page(
283
+ 'themes.php',
284
+ 'Pages (embedded)',
285
+ 'Pages (embedded)',
286
+ 'edit_theme_options',
287
+ 'site-editor-v2-wp-admin',
288
+ 'gutenberg_site_editor_v2_wp_admin_render_page'
289
+ );
290
+ }
291
+ add_action( 'admin_menu', 'gutenberg_test_entity_fields_api_register_embedded_page' );
@@ -4,12 +4,31 @@
4
4
  *
5
5
  * @package gutenberg-test-interactive-blocks
6
6
  */
7
+
8
+ wp_interactivity_state(
9
+ 'directive-html',
10
+ array(
11
+ 'serverHtml' => array( 'wpServerHtml' => true ),
12
+ )
13
+ );
7
14
  ?>
8
15
 
9
16
  <div data-wp-interactive="directive-html">
17
+ <span data-wp-text="state.hydration" data-testid="hydration">not hydrated</span>
18
+ <button data-wp-on--click="actions.setTrustedHtml" data-testid="set trusted html">
19
+ Set Trusted HTML
20
+ </button>
21
+
10
22
  <div data-wp-html="state.html" data-testid="show state html">
11
23
  <p data-testid="fallback">Fallback content</p>
12
24
  </div>
25
+ <div data-wp-html="state.rejected" data-testid="rejected html"><p>Rejected fallback</p></div>
26
+ <div data-wp-html="state.serverHtml" data-testid="server html"><p>Server-rendered HTML</p></div>
27
+ <button data-wp-on--click="actions.setServerHtml" data-testid="set server html">Set Server HTML</button>
28
+ <ul data-testid="created items">
29
+ <template data-wp-each="state.list"><li><div data-wp-html="state.empty"></div></li></template>
30
+ </ul>
31
+ <button data-wp-on--click="actions.setRejected" data-testid="set rejected html">Set Rejected HTML</button>
13
32
 
14
33
  <button data-wp-on--click="actions.setHtml" data-testid="set html">
15
34
  Set HTML
@@ -22,13 +41,6 @@
22
41
  Set Loading
23
42
  </button>
24
43
 
25
- <button
26
- data-wp-on--click="actions.setPlainString"
27
- data-testid="set plain string"
28
- >
29
- Set Plain String
30
- </button>
31
-
32
44
  <div data-wp-html--suffix="state.html" data-testid="ignores suffixes">
33
45
  <span data-testid="suffix-fallback">Suffix fallback</span>
34
46
  </div>
@@ -1,8 +1,21 @@
1
1
  import { store, asDangerousHTML } from '@wordpress/interactivity';
2
2
 
3
+ const policy = window.trustedTypes?.createPolicy( 'directive-html', {
4
+ createHTML: ( html ) => html,
5
+ } );
6
+
3
7
  const { state } = store( 'directive-html', {
4
8
  state: {
5
9
  html: null,
10
+ // Matches the server-rendered fallback, so it renders before the
11
+ // rejected value replaces it.
12
+ rejected: asDangerousHTML(
13
+ policy?.createHTML( '<p>Rejected fallback</p>' ) ??
14
+ '<p>Rejected fallback</p>'
15
+ ),
16
+ empty: null,
17
+ list: [ 'a', 'b' ],
18
+ hydration: 'hydrated',
6
19
  },
7
20
  actions: {
8
21
  setHtml() {
@@ -13,8 +26,14 @@ const { state } = store( 'directive-html', {
13
26
  setLoading() {
14
27
  state.html = null;
15
28
  },
16
- setPlainString() {
17
- state.html = '<strong>Not rendered</strong>';
29
+ setTrustedHtml() {
30
+ state.html = asDangerousHTML( policy.createHTML( '<p>Late</p>' ) );
31
+ },
32
+ setRejected() {
33
+ state.rejected = asDangerousHTML( '<b>x</b>' );
34
+ },
35
+ setServerHtml() {
36
+ state.serverHtml = asDangerousHTML( '<p>Client HTML</p>' );
18
37
  },
19
38
  },
20
39
  } );
@@ -1,5 +1,5 @@
1
1
  /**
2
- * This module dynamically imports @wordpress/interactivity on DOMContentLoaded.
2
+ * This module dynamically imports `@wordpress/interactivity` on DOMContentLoaded.
3
3
  *
4
4
  * This simulates a lazy-loading scenario where the Interactivity API is not
5
5
  * statically imported. The test verifies that hydration still occurs even
@@ -0,0 +1,19 @@
1
+ {
2
+ "$schema": "https://schemas.wp.org/trunk/block.json",
3
+ "apiVersion": 3,
4
+ "name": "test/router-html",
5
+ "title": "E2E Interactivity tests - router HTML",
6
+ "category": "text",
7
+ "icon": "heart",
8
+ "attributes": {
9
+ "page": { "type": "string" },
10
+ "value": { "type": "string", "default": "null" },
11
+ "next": { "type": "string" }
12
+ },
13
+ "supports": {
14
+ "interactivity": true
15
+ },
16
+ "textdomain": "e2e-interactivity",
17
+ "viewScriptModule": "file:./view.js",
18
+ "render": "file:./render.php"
19
+ }
@@ -0,0 +1,64 @@
1
+ <?php
2
+ /**
3
+ * HTML for testing HTML elements kept across router navigation.
4
+ *
5
+ * @package gutenberg-test-interactive-blocks
6
+ */
7
+
8
+ $attributes = get_defined_vars()['attributes'];
9
+ $page = $attributes['page'];
10
+
11
+ if ( 'pending' === $attributes['value'] ) {
12
+ wp_interactivity_state(
13
+ 'router-html',
14
+ array(
15
+ /**
16
+ * Supplies the server-only derived HTML placeholder.
17
+ *
18
+ * @return null The client implementation is deferred.
19
+ */
20
+ 'late' => function () {
21
+ return null;
22
+ },
23
+ )
24
+ );
25
+ add_filter(
26
+ 'script_module_data_@wordpress/interactivity',
27
+ /**
28
+ * Declares a derived value whose client implementation loads later.
29
+ *
30
+ * @param array|null $data Serialized Interactivity API server data.
31
+ * @return array Server data with the pending HTML getter declaration.
32
+ */
33
+ function ( $data ) {
34
+ $data = $data ?? array();
35
+ $data['derivedStateClosures']['router-html'][] = 'state.late';
36
+ return $data;
37
+ }
38
+ );
39
+ }
40
+ ?>
41
+
42
+ <div data-wp-interactive="router-html">
43
+ <div
44
+ data-wp-interactive="router-html"
45
+ data-wp-router-region="router-html-kept"
46
+ <?php
47
+ echo wp_interactivity_data_wp_context(
48
+ array(
49
+ 'value' => $attributes['value'],
50
+ 'resolved' => false,
51
+ )
52
+ );
53
+ ?>
54
+ >
55
+ <p data-testid="page-content">content from <?php echo esc_html( $page ); ?></p>
56
+ <div data-testid="kept-html" data-wp-html="state.html"><i><?php echo 'one' === $page ? 'one' : 'two'; ?></i></div>
57
+ <div data-testid="never-html" data-wp-html="state.never"><i><?php echo 'one' === $page ? 'never one' : 'never two'; ?></i></div>
58
+ <p data-testid="value" data-wp-text="state.value">not hydrated</p>
59
+ <button data-wp-on--click="actions.resolve">Resolve HTML</button>
60
+ <?php if ( ! empty( $attributes['next'] ) ) : ?>
61
+ <a href="<?php echo esc_url( $attributes['next'] ); ?>" data-wp-on--click="actions.navigate">Next</a>
62
+ <?php endif; ?>
63
+ </div>
64
+ </div>
@@ -0,0 +1,9 @@
1
+ <?php return array(
2
+ 'dependencies' => array(
3
+ '@wordpress/interactivity',
4
+ array(
5
+ 'id' => '@wordpress/interactivity-router',
6
+ 'import' => 'dynamic',
7
+ ),
8
+ ),
9
+ );
@@ -0,0 +1,56 @@
1
+ import {
2
+ asDangerousHTML,
3
+ getContext,
4
+ getServerContext,
5
+ store,
6
+ withSyncEvent,
7
+ } from '@wordpress/interactivity';
8
+
9
+ /** HTML tokens selected by each page's server context. */
10
+ const tokens = {
11
+ /** Initial or matching destination HTML. */
12
+ T: asDangerousHTML( '<p>T</p>' ),
13
+ /** Differing or late destination HTML. */
14
+ U: asDangerousHTML( '<p>U</p>' ),
15
+ };
16
+
17
+ const { state } = store( 'router-html', {
18
+ state: {
19
+ /** Always empty, including on a kept node. */
20
+ never: null,
21
+ /** Returns the destination token, null, or a pending derived value. */
22
+ get html() {
23
+ if ( getServerContext().value === 'pending' ) {
24
+ return state.late;
25
+ }
26
+ return getContext().resolved
27
+ ? tokens.U
28
+ : ( tokens[ getServerContext().value ] ?? null );
29
+ },
30
+ /** Reports the destination's own serialized value selection. */
31
+ get value() {
32
+ return getServerContext().value;
33
+ },
34
+ },
35
+ actions: {
36
+ /** Navigates using the router without a document reload. */
37
+ navigate: withSyncEvent( function* ( event ) {
38
+ event.preventDefault();
39
+ const { actions } =
40
+ yield import( '@wordpress/interactivity-router' );
41
+ yield actions.navigate( event.target.href );
42
+ } ),
43
+ /** Resolves null HTML or registers the pending derived getter. */
44
+ resolve() {
45
+ getContext().resolved = true;
46
+ store( 'router-html', {
47
+ state: {
48
+ /** Supplies a late token after the pending destination loads. */
49
+ get late() {
50
+ return tokens.U;
51
+ },
52
+ },
53
+ } );
54
+ },
55
+ },
56
+ } );
@@ -36,3 +36,15 @@ add_action(
36
36
  }
37
37
  }
38
38
  );
39
+
40
+ add_action(
41
+ 'send_headers',
42
+ function () {
43
+ if (
44
+ isset( $_GET['require_trusted_types'] ) &&
45
+ 'true' === $_GET['require_trusted_types']
46
+ ) {
47
+ header( "Content-Security-Policy: require-trusted-types-for 'script'" );
48
+ }
49
+ }
50
+ );
@@ -1,6 +1,7 @@
1
1
  ( function () {
2
2
  const { registerBlockType } = wp.blocks;
3
3
  const { useBlockProps } = wp.blockEditor;
4
+ const { createElement, memo } = wp.element;
4
5
 
5
6
  // The externalized React JSX runtime (the `react-jsx-runtime` script
6
7
  // handle). When the React 19 experiment is enabled this is React 19's
@@ -89,6 +90,23 @@
89
90
  label: 'Hello from defaultProps',
90
91
  };
91
92
 
93
+ // The same, wrapped in `memo()`. React 18 resolved the inner component's
94
+ // `defaultProps` in `updateMemoComponent`, not at element creation, so no
95
+ // runtime ever wrote them into the element. All three ways of creating the
96
+ // element are exercised — the classic `createElement`, the externalized JSX
97
+ // runtime, and the bundled React 18 one — because the fix has to live in
98
+ // the reconciler to cover the last of them.
99
+ function MemoGreetingBase( props ) {
100
+ return jsx( 'span', {
101
+ className: props.className,
102
+ children: props.label,
103
+ } );
104
+ }
105
+ MemoGreetingBase.defaultProps = {
106
+ label: 'Hello from memo defaultProps',
107
+ };
108
+ const MemoGreeting = memo( MemoGreetingBase );
109
+
92
110
  registerBlockType( 'test/react-18-compat-block', {
93
111
  apiVersion: 3,
94
112
  edit: function Edit() {
@@ -122,6 +140,26 @@
122
140
  // The `defaultProps` component, created with the
123
141
  // externalized React runtime.
124
142
  jsx( Greeting, {}, 'greeting' ),
143
+ createElement( MemoGreeting, {
144
+ key: 'memo-greeting',
145
+ className: 'react-18-compat-block__memo-greeting',
146
+ } ),
147
+ jsx(
148
+ MemoGreeting,
149
+ {
150
+ className:
151
+ 'react-18-compat-block__memo-greeting-jsx',
152
+ },
153
+ 'memo-greeting-jsx'
154
+ ),
155
+ jsxDEV(
156
+ MemoGreeting,
157
+ {
158
+ className:
159
+ 'react-18-compat-block__memo-greeting-legacy',
160
+ },
161
+ 'memo-greeting-legacy'
162
+ ),
125
163
  ],
126
164
  } );
127
165
  },
@@ -2,7 +2,7 @@
2
2
  * Test-only WebSocket sync provider for Gutenberg RTC e2e tests.
3
3
  *
4
4
  * Speaks the y-websocket binary protocol against test/e2e/bin/rtc-test-ws-sync-server.mjs
5
- * (which is built on @y/websocket-server) so the test harness exercises the same
5
+ * (which is built on `@y/websocket-server`) so the test harness exercises the same
6
6
  * wire format as production deployments. Exposes a small debug surface on
7
7
  * window.__gutenbergTestWebSocketSync.rooms that the Playwright fixtures poll.
8
8
  */