|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once __DIR__ . '/base.php'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Tests for the preloading of the entity fields. |
| 7 | + * |
| 8 | + * @package WordPress |
| 9 | + * @subpackage Entity Fields API |
| 10 | + * @since 7.2.0 |
| 11 | + * |
| 12 | + * @group entity-fields |
| 13 | + * |
| 14 | + * @covers ::_wp_preload_entity_fields |
| 15 | + * @covers ::_wp_get_post_type_fields_preload_paths |
| 16 | + * @covers ::_wp_get_site_editor_screen_post_types |
| 17 | + */ |
| 18 | +class Tests_Entity_Fields_WpPreloadEntityFields extends WP_Entity_Fields_UnitTestCase { |
| 19 | + |
| 20 | + public function tear_down() { |
| 21 | + unset( $_GET['p'] ); |
| 22 | + parent::tear_down(); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * The preloading is hooked to the preload paths of the block editor. |
| 27 | + * |
| 28 | + * @ticket 66229 |
| 29 | + */ |
| 30 | + public function test_the_fields_are_preloaded_on_the_block_editor_preload_paths() { |
| 31 | + $this->assertSame( 10, has_filter( 'block_editor_rest_api_preload_paths', '_wp_preload_entity_fields' ) ); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * The post editor does not preload the fields, whatever its `p` query arg. |
| 36 | + * |
| 37 | + * @ticket 66229 |
| 38 | + */ |
| 39 | + public function test_the_post_editor_does_not_preload_the_fields() { |
| 40 | + $_GET['p'] = '/page'; |
| 41 | + $post = self::factory()->post->create_and_get( array( 'post_type' => 'page' ) ); |
| 42 | + $context = new WP_Block_Editor_Context( |
| 43 | + array( |
| 44 | + 'name' => 'core/edit-post', |
| 45 | + 'post' => $post, |
| 46 | + ) |
| 47 | + ); |
| 48 | + |
| 49 | + $this->assertSame( array(), _wp_preload_entity_fields( array(), $context ) ); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * The preloaded path serves the fields of the post type. |
| 54 | + * |
| 55 | + * @ticket 66229 |
| 56 | + */ |
| 57 | + public function test_the_preloaded_fields_path_serves_the_fields() { |
| 58 | + wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) ); |
| 59 | + $_GET['p'] = '/page'; |
| 60 | + $context = new WP_Block_Editor_Context( array( 'name' => 'core/edit-site' ) ); |
| 61 | + $paths = _wp_preload_entity_fields( array(), $context ); |
| 62 | + |
| 63 | + $preloaded = array_reduce( $paths, 'rest_preload_api_request', array() ); |
| 64 | + |
| 65 | + $this->assertSame( 'page', $preloaded[ $paths[0] ]['body']['name'] ); |
| 66 | + $this->assertContains( 'author', wp_list_pluck( $preloaded[ $paths[0] ]['body']['fields'], 'id' ) ); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * The site editor preloads the fields of the post types its screen lists, |
| 71 | + * from the `p` query arg. |
| 72 | + * |
| 73 | + * @ticket 66229 |
| 74 | + * |
| 75 | + * @dataProvider data_site_editor_screens |
| 76 | + * |
| 77 | + * @param string $path The `p` query arg. |
| 78 | + * @param string[] $post_types The post types whose fields are preloaded. |
| 79 | + */ |
| 80 | + public function test_the_site_editor_preloads_the_fields_of_its_screen( $path, $post_types ) { |
| 81 | + $_GET['p'] = $path; |
| 82 | + $context = new WP_Block_Editor_Context( array( 'name' => 'core/edit-site' ) ); |
| 83 | + |
| 84 | + $paths = _wp_preload_entity_fields( array(), $context ); |
| 85 | + unset( $_GET['p'] ); |
| 86 | + |
| 87 | + $expected = array(); |
| 88 | + foreach ( $post_types as $post_type ) { |
| 89 | + $expected[] = '/wp/v2/entity-fields?kind=postType&name=' . $post_type; |
| 90 | + } |
| 91 | + $this->assertSame( $expected, $paths ); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Data provider for test_the_site_editor_preloads_the_fields_of_its_screen(). |
| 96 | + * |
| 97 | + * @return array[] |
| 98 | + */ |
| 99 | + public function data_site_editor_screens() { |
| 100 | + return array( |
| 101 | + 'pages' => array( '/page', array( 'page' ) ), |
| 102 | + 'page' => array( '/page/12', array( 'page' ) ), |
| 103 | + 'templates' => array( '/template', array( 'wp_template' ) ), |
| 104 | + 'template' => array( '/wp_template/emptytheme//index', array( 'wp_template' ) ), |
| 105 | + 'patterns' => array( '/pattern', array( 'wp_block', 'wp_template_part' ) ), |
| 106 | + 'pattern' => array( '/wp_block/12', array( 'wp_block' ) ), |
| 107 | + 'template part' => array( '/wp_template_part/emptytheme//header', array( 'wp_template_part' ) ), |
| 108 | + 'root' => array( '/', array() ), |
| 109 | + 'styles' => array( '/styles', array() ), |
| 110 | + 'navigation' => array( '/navigation', array() ), |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * The site editor preloads the fields of the edited post, once, next to |
| 116 | + * those of its screen. |
| 117 | + * |
| 118 | + * @ticket 66229 |
| 119 | + */ |
| 120 | + public function test_the_site_editor_preloads_the_fields_of_the_edited_post() { |
| 121 | + $post = self::factory()->post->create_and_get( array( 'post_type' => 'page' ) ); |
| 122 | + $_GET['p'] = '/page'; |
| 123 | + $context = new WP_Block_Editor_Context( |
| 124 | + array( |
| 125 | + 'name' => 'core/edit-site', |
| 126 | + 'post' => $post, |
| 127 | + ) |
| 128 | + ); |
| 129 | + |
| 130 | + $paths = _wp_preload_entity_fields( array(), $context ); |
| 131 | + unset( $_GET['p'] ); |
| 132 | + |
| 133 | + $this->assertSame( array( '/wp/v2/entity-fields?kind=postType&name=page' ), $paths ); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Post types that are not registered are skipped. |
| 138 | + * |
| 139 | + * @ticket 66229 |
| 140 | + */ |
| 141 | + public function test_unregistered_post_types_are_not_preloaded() { |
| 142 | + $this->assertSame( |
| 143 | + array( '/wp/v2/entity-fields?kind=postType&name=page' ), |
| 144 | + _wp_get_post_type_fields_preload_paths( array( 'page', 'not_a_post_type', 'page' ) ) |
| 145 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Other editors, a post editor context without a post, and a site editor |
| 150 | + * screen that lists no post fields are left untouched. |
| 151 | + * |
| 152 | + * @ticket 66229 |
| 153 | + */ |
| 154 | + public function test_other_editors_do_not_preload_the_fields() { |
| 155 | + $site_editor = new WP_Block_Editor_Context( array( 'name' => 'core/edit-site' ) ); |
| 156 | + $no_post = new WP_Block_Editor_Context( array( 'name' => 'core/edit-post' ) ); |
| 157 | + $widgets = new WP_Block_Editor_Context( array( 'name' => 'core/edit-widgets' ) ); |
| 158 | + |
| 159 | + $this->assertSame( array(), _wp_preload_entity_fields( array(), $site_editor ) ); |
| 160 | + $this->assertSame( array(), _wp_preload_entity_fields( array(), $no_post ) ); |
| 161 | + $this->assertSame( array(), _wp_preload_entity_fields( array(), $widgets ) ); |
| 162 | + } |
| 163 | +} |
0 commit comments