@wordpress/e2e-tests 10.1.0 → 10.3.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,10 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 10.3.0 (2026-12-02)
6
+
7
+ ## 10.2.0 (2026-11-18)
8
+
5
9
  ## 10.1.0 (2026-11-04)
6
10
 
7
11
  ## 10.0.0 (2026-10-20)
@@ -5,6 +5,8 @@
5
5
  * The sync module reads its polling intervals via applyFilters at load time.
6
6
  * The sync module is bundled into the wp-core-data script, so the filters
7
7
  * must be registered before that script executes.
8
+ *
9
+ * @package gutenberg
8
10
  */
9
11
  add_action(
10
12
  'enqueue_block_editor_assets',
@@ -1,4 +1,9 @@
1
1
  <?php
2
+ /**
3
+ * Server timing MU-plugin for e2e tests.
4
+ *
5
+ * @package gutenberg
6
+ */
2
7
 
3
8
  /*
4
9
  * Note: As of WordPress 6.9, there is a template enhancement output buffer which can be used instead of the following
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/e2e-tests",
3
- "version": "10.1.0",
3
+ "version": "10.3.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",
@@ -23,8 +23,8 @@
23
23
  "node": ">=22.13.0"
24
24
  },
25
25
  "dependencies": {
26
- "@wordpress/interactivity": "^8.1.0",
27
- "@wordpress/interactivity-router": "^3.1.0"
26
+ "@wordpress/interactivity": "^8.3.0",
27
+ "@wordpress/interactivity-router": "^3.3.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": "9b70363b9f93cff0583a1f3153738d383cf5a49e"
39
+ "gitHead": "7f19202f61aeba8f4bdce892a1a0df485ad76e2b"
40
40
  }
@@ -134,6 +134,41 @@ function hierarchical_without_title_cpt() {
134
134
  }
135
135
  add_action( 'init', 'hierarchical_without_title_cpt' );
136
136
 
137
+ /**
138
+ * Registers a non-hierarchical post type that supports page attributes.
139
+ */
140
+ function non_hierarchical_with_page_attributes_cpt() {
141
+ register_post_type(
142
+ 'non-hierarchical',
143
+ array(
144
+ 'public' => true,
145
+ 'label' => 'Non-Hierarchical',
146
+ 'show_in_rest' => true,
147
+ 'hierarchical' => false,
148
+ 'supports' => array( 'title', 'editor', 'page-attributes' ),
149
+ )
150
+ );
151
+ }
152
+ add_action( 'init', 'non_hierarchical_with_page_attributes_cpt' );
153
+
154
+ /**
155
+ * Registers a hierarchical post type that is not publicly viewable.
156
+ */
157
+ function hierarchical_not_viewable_cpt() {
158
+ register_post_type(
159
+ 'hierar-private',
160
+ array(
161
+ 'public' => false,
162
+ 'show_ui' => true,
163
+ 'label' => 'Hierarchical Private',
164
+ 'show_in_rest' => true,
165
+ 'hierarchical' => true,
166
+ 'supports' => array( 'title', 'editor', 'page-attributes' ),
167
+ )
168
+ );
169
+ }
170
+ add_action( 'init', 'hierarchical_not_viewable_cpt' );
171
+
137
172
  /**
138
173
  * Registers a custom post type that includes a legacy block in `template`.
139
174
  */
@@ -0,0 +1,47 @@
1
+ <?php
2
+ /**
3
+ * Plugin Name: Gutenberg Test Footnotes Locked Template
4
+ * Plugin URI: https://github.com/WordPress/gutenberg
5
+ * Author: Gutenberg Team
6
+ *
7
+ * @package gutenberg-test-footnotes-locked-template
8
+ */
9
+
10
+ /**
11
+ * Registers two post types with a locked template, one without and one with
12
+ * a Footnotes block. Both support custom fields and revisions, which the
13
+ * footnotes post meta needs.
14
+ */
15
+ function gutenberg_test_footnotes_locked_template() {
16
+ $supports = array( 'title', 'editor', 'custom-fields', 'revisions' );
17
+
18
+ register_post_type(
19
+ 'locked-paragraph',
20
+ array(
21
+ 'public' => true,
22
+ 'label' => 'Locked Paragraph',
23
+ 'show_in_rest' => true,
24
+ 'supports' => $supports,
25
+ 'template' => array(
26
+ array( 'core/paragraph' ),
27
+ ),
28
+ 'template_lock' => 'all',
29
+ )
30
+ );
31
+
32
+ register_post_type(
33
+ 'locked-footnotes',
34
+ array(
35
+ 'public' => true,
36
+ 'label' => 'Locked Footnotes',
37
+ 'show_in_rest' => true,
38
+ 'supports' => $supports,
39
+ 'template' => array(
40
+ array( 'core/paragraph' ),
41
+ array( 'core/footnotes' ),
42
+ ),
43
+ 'template_lock' => 'all',
44
+ )
45
+ );
46
+ }
47
+ add_action( 'init', 'gutenberg_test_footnotes_locked_template' );
@@ -8,9 +8,38 @@
8
8
  wp_interactivity_state(
9
9
  'directive-html',
10
10
  array(
11
- 'serverHtml' => array( 'wpServerHtml' => true ),
11
+ 'serverHtml' => array( 'wpServerHtml' => true ),
12
+ 'lazy' => static function () {
13
+ return null;
14
+ },
15
+ /**
16
+ * Leaves the list pending until its client fixture getter is registered.
17
+ *
18
+ * @return null Pending list value.
19
+ */
20
+ 'pendingHydrationList' => static function () {
21
+ return null;
22
+ },
12
23
  )
13
24
  );
25
+
26
+ add_filter(
27
+ 'script_module_data_@wordpress/interactivity',
28
+ /**
29
+ * Marks client-only values as pending until their fixture actions register.
30
+ *
31
+ * @param array|null $data Serialized Interactivity API server data.
32
+ * @return array Server data with the pending fixture value declarations.
33
+ */
34
+ static function ( $data ) {
35
+ $data = $data ?? array();
36
+ $data['derivedStateClosures']['directive-html'] = array_merge(
37
+ $data['derivedStateClosures']['directive-html'] ?? array(),
38
+ array( 'state.lazy', 'state.pendingHydrationList' )
39
+ );
40
+ return $data;
41
+ }
42
+ );
14
43
  ?>
15
44
 
16
45
  <div data-wp-interactive="directive-html">
@@ -28,7 +57,43 @@ wp_interactivity_state(
28
57
  <ul data-testid="created items">
29
58
  <template data-wp-each="state.list"><li><div data-wp-html="state.empty"></div></li></template>
30
59
  </ul>
60
+ <button data-wp-on--click="actions.addFallback" data-testid="add fallback">Add fallback</button>
61
+ <button data-wp-on--click="actions.addTwoFallbacks" data-testid="add two fallbacks">Add two fallbacks</button>
62
+ <button data-wp-on--click="actions.addLazyItem" data-testid="add lazy item">Add lazy item</button>
63
+ <button data-wp-on--click="actions.registerLazy" data-testid="register lazy">Register lazy value</button>
64
+ <button data-wp-on--click="actions.addScriptItems" data-testid="add script items">Add script items</button>
65
+ <button data-wp-on--click="actions.addValueItems" data-testid="add value items">Add value items</button>
66
+ <button data-wp-on--click="actions.addEmptyItem" data-testid="add empty item">Add empty item</button>
67
+ <button data-wp-on--click="actions.rejectFirstItem" data-testid="reject first item">Reject first item</button>
68
+ <button data-wp-on--click="actions.addRejectedItem" data-testid="add rejected item">Add rejected item</button>
69
+ <button data-wp-on--click="actions.resolvePendingList" data-testid="resolve pending list">Resolve pending list</button>
70
+ <ul data-testid="fallback items">
71
+ <template data-wp-each="state.fallbackItems"><li><div data-wp-html="context.item.html"><p>Loading</p></div></li></template>
72
+ </ul>
73
+ <ul data-testid="lazy items">
74
+ <template data-wp-each="state.lazyItems"><li><div data-wp-html="state.lazy"><p>Loading</p></div></li></template>
75
+ </ul>
76
+ <ul data-testid="script items">
77
+ <template data-wp-each="state.scriptItems"><li><div data-wp-html="context.item.html"><p>Loading</p><script>window.fallbackRuns = ( window.fallbackRuns || 0 ) + 1;</script></div></li></template>
78
+ </ul>
79
+ <ul data-testid="value items">
80
+ <template data-wp-each="state.valueItems"><li><div data-wp-html="context.item.html"><p>Loading</p></div></li></template>
81
+ </ul>
82
+ <ul data-testid="empty fallback items">
83
+ <template data-wp-each="state.emptyItems"><li><div data-wp-html="context.item.html"></div></li></template>
84
+ </ul>
85
+ <ul data-testid="hydration items">
86
+ <template data-wp-each="state.hydrationList"><li><span data-wp-html="context.item.html"><i>Fallback</i></span></li></template>
87
+ <li data-wp-each-child><span data-wp-html="context.item.html"><b>one</b></span></li>
88
+ <li data-wp-each-child><span data-wp-html="context.item.html"><b>two</b></span></li>
89
+ </ul>
90
+ <ul data-testid="pending hydration items">
91
+ <template data-wp-each="state.pendingHydrationList"><li><span data-wp-html="context.item.html"><i>Fallback</i></span></li></template>
92
+ <li data-wp-each-child><span data-wp-html="context.item.html"><b>pending one</b></span></li>
93
+ <li data-wp-each-child><span data-wp-html="context.item.html"><b>pending two</b></span></li>
94
+ </ul>
31
95
  <button data-wp-on--click="actions.setRejected" data-testid="set rejected html">Set Rejected HTML</button>
96
+ <button data-wp-on--click="actions.setRejectedTrusted" data-testid="set rejected trusted html">Set Rejected Trusted HTML</button>
32
97
 
33
98
  <button data-wp-on--click="actions.setHtml" data-testid="set html">
34
99
  Set HTML
@@ -4,6 +4,11 @@ const policy = window.trustedTypes?.createPolicy( 'directive-html', {
4
4
  createHTML: ( html ) => html,
5
5
  } );
6
6
 
7
+ // Matches the server's token representation for fallback creation cases.
8
+ const serverHTML = { wpServerHtml: true };
9
+ // Supplies unique item IDs for the created list items.
10
+ let itemId = 0;
11
+
7
12
  const { state } = store( 'directive-html', {
8
13
  state: {
9
14
  html: null,
@@ -16,8 +21,98 @@ const { state } = store( 'directive-html', {
16
21
  empty: null,
17
22
  list: [ 'a', 'b' ],
18
23
  hydration: 'hydrated',
24
+ /** Items whose individual values are server HTML representations. */
25
+ fallbackItems: [],
26
+ /** Items bound to the client getter registered by the fixture. */
27
+ lazyItems: [],
28
+ /** Items whose fallback contains an inert script fixture. */
29
+ scriptItems: [],
30
+ /** Items covering null, invalid, and explicitly trusted values. */
31
+ valueItems: [],
32
+ /** Items bound to an element with no fallback content. */
33
+ emptyItems: [],
34
+ /** Values matching the server-rendered items and one new item. */
35
+ hydrationList: [
36
+ { html: serverHTML },
37
+ { html: serverHTML },
38
+ { html: serverHTML },
39
+ ],
19
40
  },
20
41
  actions: {
42
+ /** Adds one item whose pending representation should show its fallback. */
43
+ addFallback() {
44
+ state.fallbackItems.push( {
45
+ id: ++itemId,
46
+ html: serverHTML,
47
+ } );
48
+ },
49
+ /** Adds two fallback items in the same reactive update. */
50
+ addTwoFallbacks() {
51
+ state.fallbackItems.push(
52
+ { id: ++itemId, html: serverHTML },
53
+ { id: ++itemId, html: serverHTML }
54
+ );
55
+ },
56
+ /** Adds an item whose value remains pending until its getter is registered. */
57
+ addLazyItem() {
58
+ state.lazyItems.push( { id: ++itemId } );
59
+ },
60
+ /** Registers the late trusted HTML getter for the pending item. */
61
+ registerLazy() {
62
+ store( 'directive-html', {
63
+ state: {
64
+ /** Resolves the pending value to trusted markup. */
65
+ get lazy() {
66
+ return asDangerousHTML(
67
+ policy.createHTML( '<b>Late</b>' )
68
+ );
69
+ },
70
+ },
71
+ } );
72
+ },
73
+ /** Adds two items whose fallback includes a script that must stay inert. */
74
+ addScriptItems() {
75
+ state.scriptItems.push(
76
+ { id: ++itemId, html: serverHTML },
77
+ { id: ++itemId, html: serverHTML }
78
+ );
79
+ },
80
+ /** Adds null, invalid-string, and trusted-token values for enforcement. */
81
+ addValueItems() {
82
+ state.valueItems = [
83
+ { html: null },
84
+ { html: '<b>x</b>' },
85
+ {
86
+ html: asDangerousHTML( policy.createHTML( '<b>t</b>' ) ),
87
+ },
88
+ ];
89
+ },
90
+ /** Adds a representation to the list with an empty template fallback. */
91
+ addEmptyItem() {
92
+ state.emptyItems.push( { html: serverHTML } );
93
+ },
94
+ /** Replaces the first displayed fallback with a rejected plain-string token. */
95
+ rejectFirstItem() {
96
+ state.fallbackItems[ 0 ].html = asDangerousHTML( '<b>x</b>' );
97
+ },
98
+ /** Adds a token that enforcement rejects at element creation. */
99
+ addRejectedItem() {
100
+ state.fallbackItems.push( {
101
+ id: ++itemId,
102
+ html: asDangerousHTML( '<b>y</b>' ),
103
+ } );
104
+ },
105
+ /** Resolves the list whose server value stayed pending through hydration. */
106
+ resolvePendingList() {
107
+ store( 'directive-html', {
108
+ state: {
109
+ /** Supplies two server-representation items after hydration. */
110
+ get pendingHydrationList() {
111
+ return [ { html: serverHTML }, { html: serverHTML } ];
112
+ },
113
+ },
114
+ } );
115
+ },
21
116
  setHtml() {
22
117
  state.html = asDangerousHTML(
23
118
  '<strong data-testid="rendered-strong">Rendered HTML</strong>'
@@ -32,6 +127,10 @@ const { state } = store( 'directive-html', {
32
127
  setRejected() {
33
128
  state.rejected = asDangerousHTML( '<b>x</b>' );
34
129
  },
130
+ /** Replaces the rejected string with a policy value of the same markup. */
131
+ setRejectedTrusted() {
132
+ state.rejected = asDangerousHTML( policy.createHTML( '<b>x</b>' ) );
133
+ },
35
134
  setServerHtml() {
36
135
  state.serverHtml = asDangerousHTML( '<p>Client HTML</p>' );
37
136
  },
@@ -8,7 +8,9 @@
8
8
  "attributes": {
9
9
  "page": { "type": "string" },
10
10
  "value": { "type": "string", "default": "null" },
11
- "next": { "type": "string" }
11
+ "next": { "type": "string" },
12
+ "resource": { "type": "string" },
13
+ "prefetch": { "type": "boolean", "default": false }
12
14
  },
13
15
  "supports": {
14
16
  "interactivity": true
@@ -7,6 +7,9 @@
7
7
 
8
8
  $attributes = get_defined_vars()['attributes'];
9
9
  $page = $attributes['page'];
10
+ $resource = is_string( $attributes['resource'] ?? null )
11
+ ? sanitize_key( $attributes['resource'] )
12
+ : '';
10
13
 
11
14
  if ( 'pending' === $attributes['value'] ) {
12
15
  wp_interactivity_state(
@@ -55,10 +58,23 @@ if ( 'pending' === $attributes['value'] ) {
55
58
  <p data-testid="page-content">content from <?php echo esc_html( $page ); ?></p>
56
59
  <div data-testid="kept-html" data-wp-html="state.html"><i><?php echo 'one' === $page ? 'one' : 'two'; ?></i></div>
57
60
  <div data-testid="never-html" data-wp-html="state.never"><i><?php echo 'one' === $page ? 'never one' : 'never two'; ?></i></div>
61
+ <?php if ( $resource ) : ?>
62
+ <div data-testid="fallback-media" data-wp-html="state.media">
63
+ <p>Media fallback</p>
64
+ <video preload="metadata" src="<?php echo esc_url( home_url( '/wp-content/uploads/router-html-' . $resource . '.mp4' ) ); ?>"></video>
65
+ </div>
66
+ <?php endif; ?>
67
+ <?php if ( 'two' === $page && 'pending' === $attributes['value'] ) : ?>
68
+ <div data-testid="added-html" data-wp-html="state.late"><p>B fallback</p><script>window.fallbackRuns = ( window.fallbackRuns || 0 ) + 1;</script></div>
69
+ <?php endif; ?>
58
70
  <p data-testid="value" data-wp-text="state.value">not hydrated</p>
59
71
  <button data-wp-on--click="actions.resolve">Resolve HTML</button>
60
72
  <?php if ( ! empty( $attributes['next'] ) ) : ?>
61
73
  <a href="<?php echo esc_url( $attributes['next'] ); ?>" data-wp-on--click="actions.navigate">Next</a>
62
74
  <?php endif; ?>
75
+ <?php if ( ! empty( $attributes['prefetch'] ) && ! empty( $attributes['next'] ) ) : ?>
76
+ <button data-prefetch-url="<?php echo esc_attr( $attributes['next'] ); ?>" data-testid="prefetch-next" data-wp-on--click="actions.prefetch">Prefetch next page</button>
77
+ <span data-testid="prefetching" data-wp-text="state.prefetching">false</span>
78
+ <?php endif; ?>
63
79
  </div>
64
80
  </div>
@@ -16,16 +16,24 @@ const tokens = {
16
16
 
17
17
  const { state } = store( 'router-html', {
18
18
  state: {
19
+ /** The server's token representation used by the fallback-media fixture. */
20
+ media: { wpServerHtml: true },
21
+ /** Indicates that the fixture's router prefetch action is in progress. */
22
+ prefetching: false,
19
23
  /** Always empty, including on a kept node. */
20
24
  never: null,
21
- /** Returns the destination token, null, or a pending derived value. */
25
+ /** Returns the destination token, representation, null, or pending value. */
22
26
  get html() {
23
- if ( getServerContext().value === 'pending' ) {
27
+ const value = getServerContext().value;
28
+ if ( value === 'pending' ) {
24
29
  return state.late;
25
30
  }
31
+ if ( value === 'representation' ) {
32
+ return { wpServerHtml: true };
33
+ }
26
34
  return getContext().resolved
27
35
  ? tokens.U
28
- : ( tokens[ getServerContext().value ] ?? null );
36
+ : ( tokens[ value ] ?? null );
29
37
  },
30
38
  /** Reports the destination's own serialized value selection. */
31
39
  get value() {
@@ -40,6 +48,20 @@ const { state } = store( 'router-html', {
40
48
  yield import( '@wordpress/interactivity-router' );
41
49
  yield actions.navigate( event.target.href );
42
50
  } ),
51
+ /** Prefetches the destination URL selected by the test fixture. */
52
+ prefetch: withSyncEvent( function* ( event ) {
53
+ event.preventDefault();
54
+ state.prefetching = true;
55
+ try {
56
+ const { actions } =
57
+ yield import( '@wordpress/interactivity-router' );
58
+ yield actions.prefetch(
59
+ event.target.getAttribute( 'data-prefetch-url' )
60
+ );
61
+ } finally {
62
+ state.prefetching = false;
63
+ }
64
+ } ),
43
65
  /** Resolves null HTML or registers the pending derived getter. */
44
66
  resolve() {
45
67
  getContext().resolved = true;
@@ -0,0 +1,20 @@
1
+ {
2
+ "$schema": "https://schemas.wp.org/trunk/block.json",
3
+ "apiVersion": 3,
4
+ "name": "test/router-html-server",
5
+ "title": "E2E Interactivity tests - router server-rendered HTML",
6
+ "category": "text",
7
+ "icon": "heart",
8
+ "attributes": {
9
+ "page": { "type": "string" },
10
+ "label": { "type": "string" },
11
+ "next": { "type": "string" },
12
+ "syncedNull": { "type": "boolean" }
13
+ },
14
+ "supports": {
15
+ "interactivity": true
16
+ },
17
+ "textdomain": "e2e-interactivity",
18
+ "viewScriptModule": "file:./view.js",
19
+ "render": "file:./render.php"
20
+ }
@@ -0,0 +1,67 @@
1
+ <?php
2
+ /**
3
+ * HTML for testing kept HTML elements whose value the server renders.
4
+ *
5
+ * @package gutenberg-test-interactive-blocks
6
+ */
7
+
8
+ $attributes = get_defined_vars()['attributes'];
9
+ $page = $attributes['page'];
10
+ $html = '<p>' . esc_html( ucfirst( $attributes['label'] ?? $page ) ) . '</p>';
11
+ // The value that data-wp-watch callbacks copy from this page's server data.
12
+ $synced = empty( $attributes['syncedNull'] ) ? wp_interactivity_as_dangerous_html( $html ) : null;
13
+
14
+ wp_interactivity_state(
15
+ 'router-html-server',
16
+ array(
17
+ 'html' => wp_interactivity_as_dangerous_html( $html ),
18
+ /**
19
+ * Supplies this page's HTML as derived state the client doesn't define.
20
+ *
21
+ * @return WP_Interactivity_Dangerous_HTML The page's HTML.
22
+ */
23
+ 'late' => function () use ( $html ) {
24
+ return wp_interactivity_as_dangerous_html( $html );
25
+ },
26
+ 'same' => wp_interactivity_as_dangerous_html( '<p>Same</p>' ),
27
+ /**
28
+ * Supplies HTML shared by every page as derived state the client
29
+ * doesn't define.
30
+ *
31
+ * @return WP_Interactivity_Dangerous_HTML The shared HTML.
32
+ */
33
+ 'sameLate' => function () {
34
+ return wp_interactivity_as_dangerous_html( '<p>Same</p>' );
35
+ },
36
+ 'synced' => $synced,
37
+ /**
38
+ * Supplies the HTML of the value the client reads from context, since
39
+ * the server never resolves a context value to a token.
40
+ *
41
+ * @return WP_Interactivity_Dangerous_HTML|null The page's HTML, if any.
42
+ */
43
+ 'syncedContext' => function () use ( $synced ) {
44
+ return $synced;
45
+ },
46
+ )
47
+ );
48
+ ?>
49
+
50
+ <div data-wp-interactive="router-html-server">
51
+ <div data-wp-interactive="router-html-server" data-wp-router-region="router-html-server" data-wp-watch="callbacks.syncState">
52
+ <p data-testid="page-content">content from <?php echo esc_html( $page ); ?></p>
53
+ <div data-testid="state-html" data-wp-html="state.html"></div>
54
+ <div data-testid="pending-html" data-wp-html="state.late"></div>
55
+ <div data-testid="same-html" data-wp-html="state.same"></div>
56
+ <div data-testid="same-pending-html" data-wp-html="state.sameLate"></div>
57
+ <div data-testid="synced-state-html" data-wp-html="state.synced"></div>
58
+ <div <?php echo wp_interactivity_data_wp_context( array( 'synced' => $synced ) ); ?> data-wp-watch="callbacks.syncContext">
59
+ <div data-testid="synced-context-html" data-wp-html="state.syncedContext"></div>
60
+ </div>
61
+ <p data-testid="hydrated" data-wp-text="state.hydrated">no</p>
62
+ <?php if ( ! empty( $attributes['next'] ) ) : ?>
63
+ <a href="<?php echo esc_url( $attributes['next'] ); ?>" data-wp-on--click="actions.navigate">Next</a>
64
+ <?php endif; ?>
65
+ <button data-wp-on--click="actions.refresh">Refresh</button>
66
+ </div>
67
+ </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,47 @@
1
+ import {
2
+ store,
3
+ getContext,
4
+ getServerContext,
5
+ getServerState,
6
+ withSyncEvent,
7
+ } from '@wordpress/interactivity';
8
+
9
+ const { state } = store( 'router-html-server', {
10
+ state: {
11
+ /** Shows that the page has hydrated. */
12
+ hydrated: 'yes',
13
+ /**
14
+ * Reads the value from the element's context.
15
+ *
16
+ * @return {unknown} The value the context holds.
17
+ */
18
+ get syncedContext() {
19
+ return getContext().synced;
20
+ },
21
+ },
22
+ actions: {
23
+ /** Navigates using the router without a document reload. */
24
+ navigate: withSyncEvent( function* ( event ) {
25
+ event.preventDefault();
26
+ const { actions } =
27
+ yield import( '@wordpress/interactivity-router' );
28
+ yield actions.navigate( event.target.href );
29
+ } ),
30
+ /** Fetches the current page again and renders it. */
31
+ *refresh() {
32
+ const { actions } =
33
+ yield import( '@wordpress/interactivity-router' );
34
+ yield actions.navigate( window.location.href, { force: true } );
35
+ },
36
+ },
37
+ callbacks: {
38
+ /** Copies the page's server state into the client state. */
39
+ syncState() {
40
+ state.synced = getServerState().synced;
41
+ },
42
+ /** Copies the page's server context into the element's context. */
43
+ syncContext() {
44
+ getContext().synced = getServerContext().synced;
45
+ },
46
+ },
47
+ } );
@@ -52,7 +52,7 @@ if ( isset( $attributes['secondRegionId'] ) ) {
52
52
  'id' => $attributes['secondRegionId'],
53
53
  // The second region's own "navigate" link targets `other`, not
54
54
  // `next` -- deliberately, so a cross-region flow has two distinct
55
- // hrefs for the router's `navigatingTo` bail to separate. "Same
55
+ // hrefs whose requests a test can hold separately. "Same
56
56
  // structure as the first" stops here: every other link below keeps
57
57
  // its usual meaning for both regions.
58
58
  'navigate_href' => $other_href,
@@ -0,0 +1,15 @@
1
+ {
2
+ "$schema": "https://schemas.wp.org/trunk/block.json",
3
+ "apiVersion": 3,
4
+ "name": "test/router-overlapping-navigations",
5
+ "title": "E2E Interactivity tests - router overlapping navigations",
6
+ "category": "text",
7
+ "icon": "heart",
8
+ "description": "",
9
+ "supports": {
10
+ "interactivity": true
11
+ },
12
+ "textdomain": "e2e-interactivity",
13
+ "viewScriptModule": "file:./view.js",
14
+ "render": "file:./render.php"
15
+ }
@@ -0,0 +1,62 @@
1
+ <?php
2
+ /**
3
+ * HTML for testing what reaches the page when client-side navigations
4
+ * overlap: the router region, the document title, the stylesheets, the
5
+ * server state and context, and the scroll to a URL fragment. Its in-page
6
+ * link and its readouts of `core/router`'s URL and lifecycle test what an
7
+ * in-page link click does to a navigation in flight.
8
+ *
9
+ * @package gutenberg-test-interactive-blocks
10
+ */
11
+
12
+ $attributes = get_defined_vars()['attributes'];
13
+ $page = $attributes['page'] ?? '';
14
+
15
+ // A stylesheet that only the pages with the `stylesheet` attribute carry.
16
+ if ( ! empty( $attributes['stylesheet'] ) ) {
17
+ add_action(
18
+ 'wp_enqueue_scripts',
19
+ function () {
20
+ wp_register_style( 'test-router-overlapping-navigations', false );
21
+ wp_enqueue_style( 'test-router-overlapping-navigations' );
22
+ wp_add_inline_style(
23
+ 'test-router-overlapping-navigations',
24
+ '.router-overlapping-navigations-styled { color: rgb(255, 0, 0); }'
25
+ );
26
+ }
27
+ );
28
+ }
29
+
30
+ wp_interactivity_state(
31
+ 'router-overlapping-navigations',
32
+ array( 'page' => $page )
33
+ );
34
+ ?>
35
+ <div data-wp-interactive="router-overlapping-navigations">
36
+ <p data-testid="server state" data-wp-text="state.serverStatePage"></p>
37
+ <p data-testid="resolved" data-wp-text="state.resolved"></p>
38
+ <p data-testid="router url" data-wp-text="state.routerUrl"></p>
39
+ <p data-testid="lifecycle" data-wp-text="state.lifecycle"></p>
40
+ <a data-testid="in-page link" href="#target">in-page link</a>
41
+ <p data-testid="styled" class="router-overlapping-navigations-styled">styled</p>
42
+
43
+ <div
44
+ data-wp-interactive="router-overlapping-navigations"
45
+ data-wp-router-region="router-overlapping-navigations"
46
+ <?php echo wp_interactivity_data_wp_context( array( 'page' => $page ) ); ?>
47
+ >
48
+ <p data-testid="page"><?php echo esc_html( $page ); ?></p>
49
+ <p data-testid="server context" data-wp-text="state.serverContextPage"></p>
50
+ <?php if ( isset( $attributes['link'] ) ) : ?>
51
+ <a
52
+ data-testid="link"
53
+ data-wp-on--click="actions.navigate"
54
+ href="<?php echo esc_url( $attributes['link'] ); ?>"
55
+ >link</a>
56
+ <?php endif; ?>
57
+ </div>
58
+
59
+ <?php // Leaves the fragment target below the fold, so scrolling to it shows. ?>
60
+ <div style="height: 200vh"></div>
61
+ <p id="target" data-testid="target">target</p>
62
+ </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,50 @@
1
+ import {
2
+ store,
3
+ getElement,
4
+ getServerContext,
5
+ getServerState,
6
+ watch,
7
+ withSyncEvent,
8
+ } from '@wordpress/interactivity';
9
+
10
+ // Read, not imported, so the readouts work before the router module loads.
11
+ const { state: routerState } = store( 'core/router' );
12
+
13
+ const { state } = store( 'router-overlapping-navigations', {
14
+ state: {
15
+ // The `href` of the last navigation whose promise resolved.
16
+ resolved: '',
17
+ // Every value `state.navigating` and `state.initiator` of `core/router`
18
+ // have taken, in order.
19
+ lifecycle: '',
20
+ get routerUrl() {
21
+ return routerState.url;
22
+ },
23
+ get serverStatePage() {
24
+ return getServerState().page;
25
+ },
26
+ get serverContextPage() {
27
+ return getServerContext().page;
28
+ },
29
+ },
30
+ actions: {
31
+ navigate: withSyncEvent( function* ( event ) {
32
+ event.preventDefault();
33
+ const { href } = getElement().ref;
34
+ const { actions } =
35
+ yield import( '@wordpress/interactivity-router' );
36
+ yield actions.navigate( href );
37
+ state.resolved = href;
38
+ } ),
39
+ },
40
+ } );
41
+
42
+ const lifecycle = [];
43
+ watch( () => {
44
+ lifecycle.push(
45
+ `${ routerState.navigating ? 'navigating' : 'idle' } ${
46
+ routerState.initiator
47
+ }`
48
+ );
49
+ state.lifecycle = lifecycle.join( ', ' );
50
+ } );
@@ -38,7 +38,14 @@ function updateDebugState( room, patch ) {
38
38
  }
39
39
 
40
40
  function createWebSocketProvider() {
41
- return async ( { awareness, objectType, objectId, ydoc, Y } ) => {
41
+ return async ( {
42
+ awareness,
43
+ flushLocalUpdates,
44
+ objectType,
45
+ objectId,
46
+ ydoc,
47
+ Y,
48
+ } ) => {
42
49
  // Point the bundled y-websocket and y-protocols modules at the Yjs
43
50
  // instance used by the editor. See yjs-external.js.
44
51
  setYjsModule( Y );
@@ -58,6 +65,15 @@ function createWebSocketProvider() {
58
65
  disableBc: true,
59
66
  } );
60
67
 
68
+ // Apply the local updates that the sync manager holds back before
69
+ // each sync message, which can carry a remote update. The sync
70
+ // message type of y-websocket is 0.
71
+ const handleSyncMessage = provider.messageHandlers[ 0 ];
72
+ provider.messageHandlers[ 0 ] = ( ...args ) => {
73
+ flushLocalUpdates?.();
74
+ return handleSyncMessage( ...args );
75
+ };
76
+
61
77
  const statusListeners = new Set();
62
78
 
63
79
  const onStatus = ( event ) => {
@@ -105,6 +121,7 @@ function createWebSocketProvider() {
105
121
  synced: false,
106
122
  } );
107
123
  },
124
+ supportsDeferredUpdates: true,
108
125
  on: ( event, callback ) => {
109
126
  if ( event === 'status' ) {
110
127
  statusListeners.add( callback );
@@ -1,7 +1,12 @@
1
1
  ( function () {
2
2
  const { createElement: el, Fragment } = wp.element;
3
3
  const { registerBlockType } = wp.blocks;
4
- const { InspectorControls, useBlockProps } = wp.blockEditor;
4
+ const {
5
+ InnerBlocks,
6
+ InspectorControls,
7
+ useBlockProps,
8
+ useInnerBlocksProps,
9
+ } = wp.blockEditor;
5
10
  const ServerSideRender = wp.serverSideRender;
6
11
  const { PanelBody, __experimentalNumberControl: NumberControl } =
7
12
  wp.components;
@@ -45,4 +50,41 @@
45
50
  );
46
51
  },
47
52
  } );
53
+
54
+ registerBlockType( 'test/post-context-provider', {
55
+ apiVersion: 3,
56
+ title: 'Test Post Context Provider',
57
+ icon: 'list-view',
58
+ category: 'text',
59
+
60
+ // Redundant with the server-side registration, but required since it
61
+ // is not picked up in `get_block_editor_server_block_settings`.
62
+ providesContext: {
63
+ postId: 'postId',
64
+ postType: 'postType',
65
+ },
66
+
67
+ edit: function Edit( { attributes, setAttributes } ) {
68
+ const blockProps = useBlockProps();
69
+ const innerBlocksProps = useInnerBlocksProps( blockProps );
70
+ return el(
71
+ 'div',
72
+ innerBlocksProps,
73
+ el( 'input', {
74
+ 'aria-label': 'Context post ID',
75
+ value: attributes.postId,
76
+ onChange( event ) {
77
+ setAttributes( {
78
+ postId: Number( event.currentTarget.value ),
79
+ } );
80
+ },
81
+ } ),
82
+ innerBlocksProps.children
83
+ );
84
+ },
85
+
86
+ save() {
87
+ return el( InnerBlocks.Content );
88
+ },
89
+ } );
48
90
  } )();
@@ -87,6 +87,30 @@ add_action(
87
87
  )
88
88
  );
89
89
 
90
+ // Context provider block that provides the `postId`/`postType` block
91
+ // context from its attributes, simulating query-like blocks whose
92
+ // items can reference posts the current user can't edit.
93
+ register_block_type(
94
+ 'test/post-context-provider',
95
+ array(
96
+ 'attributes' => array(
97
+ 'postId' => array(
98
+ 'type' => 'number',
99
+ 'default' => 0,
100
+ ),
101
+ 'postType' => array(
102
+ 'type' => 'string',
103
+ 'default' => 'post',
104
+ ),
105
+ ),
106
+ 'provides_context' => array(
107
+ 'postId' => 'postId',
108
+ 'postType' => 'postType',
109
+ ),
110
+ 'editor_script_handles' => array( 'server-side-rendered-block' ),
111
+ )
112
+ );
113
+
90
114
  // Add binding support for the auto-register-with-controls block.
91
115
  add_filter(
92
116
  'block_bindings_supported_attributes_test/auto-register-with-controls',