| | 4844 | /** |
| | 4845 | * @ticket 47779 |
| | 4846 | */ |
| | 4847 | public function test_rest_post_type_item_schema_filter_change_property() { |
| | 4848 | add_filter( 'rest_post_item_schema', array( $this, 'filter_post_item_schema' ) ); |
| | 4849 | |
| | 4850 | // Re-initialize the controller to cache-bust schemas from prior test runs. |
| | 4851 | $GLOBALS['wp_rest_server']->override_by_default = true; |
| | 4852 | $controller = new WP_REST_Posts_Controller( 'post' ); |
| | 4853 | $controller->register_routes(); |
| | 4854 | $GLOBALS['wp_rest_server']->override_by_default = false; |
| | 4855 | |
| | 4856 | $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ); |
| | 4857 | $response = rest_get_server()->dispatch( $request ); |
| | 4858 | $data = $response->get_data(); |
| | 4859 | $properties = $data['schema']['properties']['content']['properties']; |
| | 4860 | |
| | 4861 | $this->assertArrayHasKey( 'new_prop', $properties ); |
| | 4862 | $this->assertEquals( array( 'new_context' ), $properties['new_prop']['context'] ); |
| | 4863 | } |
| | 4864 | |
| | 4865 | /** |
| | 4866 | * @ticket 47779 |
| | 4867 | */ |
| | 4868 | public function test_rest_post_type_item_schema_filter_add_property_triggers_doing_it_wrong() { |
| | 4869 | add_filter( 'rest_post_item_schema', array( $this, 'filter_post_item_schema_add_property' ) ); |
| | 4870 | $this->setExpectedIncorrectUsage( 'WP_REST_Posts_Controller::get_item_schema' ); |
| | 4871 | |
| | 4872 | // Re-initialize the controller to cache-bust schemas from prior test runs. |
| | 4873 | $GLOBALS['wp_rest_server']->override_by_default = true; |
| | 4874 | $controller = new WP_REST_Posts_Controller( 'post' ); |
| | 4875 | $controller->register_routes(); |
| | 4876 | $GLOBALS['wp_rest_server']->override_by_default = false; |
| | 4877 | } |
| | 4878 | |
| | 4906 | |
| | 4907 | public function filter_post_item_schema( $schema ) { |
| | 4908 | $schema['properties']['content']['properties']['new_prop'] = array( |
| | 4909 | 'description' => __( 'A new prop added with a the rest_post_item_schema filter.' ), |
| | 4910 | 'type' => 'string', |
| | 4911 | 'context' => array( 'new_context' ), |
| | 4912 | ); |
| | 4913 | return $schema; |
| | 4914 | } |
| | 4915 | |
| | 4916 | public function filter_post_item_schema_add_property( $schema ) { |
| | 4917 | $schema['properties']['something_entirely_new'] = array( |
| | 4918 | 'description' => __( 'A new prop added with a the rest_post_item_schema filter.' ), |
| | 4919 | 'type' => 'string', |
| | 4920 | 'context' => array( 'new_context' ), |
| | 4921 | ); |
| | 4922 | return $schema; |
| | 4923 | } |