88 * @group entity-actions
99 *
1010 * @covers ::_wp_add_entity_action_modules_to_editor_script
11+ * @covers ::_wp_preload_entity_actions
1112 * @covers ::wp_register_entity_actions
1213 * @covers ::wp_unregister_entity_actions
1314 * @covers WP_Entity_Actions_Registry::initialize
1718class Tests_Entity_Actions extends WP_UnitTestCase {
1819
1920 /**
20- * Sets up each test: the default actions of the tests are registered at
21- * priority 0, so every test runs against a registry holding actions
22- * registered before the callbacks hooked at the default priority run.
21+ * Sets up each test: the actions WordPress registers are replaced with
22+ * the default actions of the tests, hooked at priority 0 as WordPress
23+ * registers its own, so every test runs against a registry holding
24+ * actions registered before the callbacks hooked at the default priority
25+ * run.
2326 */
2427 public function set_up () {
2528 parent ::set_up ();
29+ remove_action ( 'wp_entity_actions_api_init ' , 'register_core_entity_actions ' , 0 );
2630 add_action ( 'wp_entity_actions_api_init ' , array ( __CLASS__ , 'register_default_actions ' ), 0 );
2731 }
2832
@@ -31,8 +35,9 @@ public function set_up() {
3135 *
3236 * Resetting the registry drops the actions a test registered along with
3337 * the defaults; the next read fires `wp_entity_actions_api_init` again and
34- * registers the defaults anew. The test case removes the callbacks the
35- * test hooked, the defaults included.
38+ * registers the defaults anew. The test case restores the hooks: it
39+ * removes the callbacks the test hooked, the defaults included, and hooks
40+ * the actions WordPress registers back.
3641 */
3742 public function tear_down () {
3843 self ::reset_registry ();
@@ -387,6 +392,51 @@ public function test_action_script_modules_reach_the_import_map() {
387392 $ this ->assertArrayHasKey ( 'plugin/feature ' , $ import_map ['imports ' ] );
388393 }
389394
395+ /**
396+ * The post editor preloads the actions of the post type it edits, with
397+ * the path the `getEntityActionsConfig` resolver of core data requests.
398+ *
399+ * @ticket 66343
400+ */
401+ public function test_the_post_editor_preloads_the_actions_of_the_post_type () {
402+ $ context = new WP_Block_Editor_Context ( array ( 'post ' => self ::factory ()->post ->create_and_get ( array ( 'post_type ' => 'page ' ) ) ) );
403+
404+ $ paths = apply_filters ( 'block_editor_rest_api_preload_paths ' , array (), $ context );
405+
406+ $ this ->assertContains ( '/wp/v2/entity-actions?kind=postType&name=page ' , $ paths , 'The actions of pages are preloaded. ' );
407+ }
408+
409+ /**
410+ * The preloaded path serves the actions of the post type.
411+ *
412+ * @ticket 66343
413+ */
414+ public function test_the_preloaded_actions_path_serves_the_actions () {
415+ wp_set_current_user ( self ::factory ()->user ->create ( array ( 'role ' => 'administrator ' ) ) );
416+ $ context = new WP_Block_Editor_Context ( array ( 'post ' => self ::factory ()->post ->create_and_get ( array ( 'post_type ' => 'page ' ) ) ) );
417+
418+ $ paths = _wp_preload_entity_actions ( array (), $ context );
419+ $ preloaded = array_reduce ( $ paths , 'rest_preload_api_request ' , array () );
420+
421+ $ this ->assertSame (
422+ array ( 'rename-post ' , 'move-to-trash ' ),
423+ array_column ( $ preloaded ['/wp/v2/entity-actions?kind=postType&name=page ' ]['body ' ]['actions ' ], 'id ' ),
424+ 'The preloaded response holds the actions of pages. '
425+ );
426+ }
427+
428+ /**
429+ * Only the post editor preloads the actions: the other editors have no
430+ * post type of their own.
431+ *
432+ * @ticket 66343
433+ */
434+ public function test_other_editors_do_not_preload_the_actions () {
435+ $ context = new WP_Block_Editor_Context ( array ( 'name ' => 'core/edit-site ' ) );
436+
437+ $ this ->assertSame ( array ( '/wp/v2/types ' ), _wp_preload_entity_actions ( array ( '/wp/v2/types ' ), $ context ), 'The paths are unchanged. ' );
438+ }
439+
390440 /**
391441 * Reading the registry fires `wp_entity_actions_api_init` once, whichever
392442 * getter is read first and however many times it is read.
0 commit comments