Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#53621 Correct the schema for the wp/v2/block-directory/search endpoint #1487

Closed
wants to merge 5 commits into from

Conversation

johnbillion
Copy link
Member

@adamziel
Copy link
Contributor

adamziel commented Nov 9, 2021

Here's a unit test that could be added to tests/phpunit/tests/rest-api/rest-block-directory-controller.php. I started off with some actual API data, but turns out there is a get_mock_plugin() that does essentially the same thing so I used that instead.

	/**
	 * @ticket 53621
	 */
	public function test_get_items_response_conforms_to_schema() {
		wp_set_current_user( self::$admin_id );
		$plugin = $this->get_mock_plugin();
		$cb     = function () use ( $plugin ) {
			return (object) array(
				'info'    =>
					array(
						'page'    => 1,
						'pages'   => 1,
						'results' => 1,
					),
				'plugins' => array(
					$plugin,
				),
			);
		};
		add_filter( 'plugins_api', $cb );

		$request = new WP_REST_Request( 'GET', '/wp/v2/block-directory/search' );
		$request->set_query_params( array( 'term' => 'cache' ) );

		$expected = array(
			array(
				'name'                => 'sortabrilliant/guidepost',
				'title'               => 'Guidepost',
				'description'         => 'A guidepost gives you directions. It lets you know where you’re going. It gives you a preview of what’s to come.',
				'id'                  => 'guidepost',
				'rating'              => 4.3,
				'rating_count'        => 90,
				'active_installs'     => 100,
				'author_block_rating' => 0,
				'author_block_count'  => 1,
				'author'              => 'sorta brilliant',
				'icon'                => 'https://ps.w.org/guidepost/assets/icon-128x128.jpg?rev=2235512',
				'last_updated'        => gmdate( 'Y-m-d\TH:i:s', strtotime( $plugin['last_updated'] ) ),
				'humanized_updated'   => sprintf( '%s ago', human_time_diff( strtotime( $plugin['last_updated'] ) ) ),
				'_links'              => array(
					'wp:install-plugin' => array(
						array(
							'href' => 'http://example.org/index.php?rest_route=%2Fwp%2Fv2%2Fplugins&slug=guidepost',
						),
					),
					'curies'            => array(
						array(
							'name'      => 'wp',
							'href'      => 'https://api.w.org/{rel}',
							'templated' => true,
						),
					),
				),
			),
		);

		$result = rest_do_request( $request );
		remove_filter( 'plugins_api', $cb );

		$this->assertNotWPError( $result->as_error() );
		$this->assertSame( 200, $result->status );
		$this->assertSame( $expected, $result->get_data() );
	}

@TimothyBJacobs
Copy link
Member

This test is somewhat of a duplicate of our existing test_prepare_item. What we probably actually want here is to test that the REST API response validates against the JSON Schema for the endpoint.

We can do this by applying rest_validate_value_from_schema on the response data. We'll need to grab the schema for the endpoint which could be done by making an OPTIONS request like we do in test_context_param (preferred) or instantiating a WP_REST_Block_Directory_Controller instance and just calling get_public_item_schema.

@johnbillion johnbillion changed the base branch from master to trunk April 1, 2022 17:37
@johnbillion
Copy link
Member Author

I adjusted then test as per Timothy's recommendation. It now validates the result against the schema. It successfully fails with the old schema and passes with the corrected one.

@spacedmonkey
Copy link
Member

Looks good to me.

@johnbillion
Copy link
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants