REST API: wc-blocks/v1/products/attributes/%d/terms

This commit is contained in:
Timmy Crawford 2019-02-21 12:30:00 -08:00 committed by Kelly Dwan
parent 6f983fb4eb
commit 4311670956
3 changed files with 277 additions and 0 deletions

View File

@ -0,0 +1,169 @@
<?php
/**
* REST API Product Attribute Terms controller customized for Products Block.
*
* Handles requests to the /products/categories endpoint.
*
* @package WooCommerce\Blocks\Products\Rest\Controller
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* REST API Product Attribute Terms controller class.
*
* @package WooCommerce/API
*/
class WC_REST_Blocks_Product_Attribute_Terms_Controller extends WC_REST_Product_Attribute_Terms_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-blocks/v1';
/**
* Register the routes for products.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\d]+)',
array(
'args' => array(
'id' => array(
'description' => __( 'Unique identifier for the resource.', 'woo-gutenberg-products-block' ),
'type' => 'integer',
),
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => array(
'context' => $this->get_context_param(
array(
'default' => 'view',
)
),
),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Check permissions.
*
* @param WP_REST_Request $request Full details about the request.
* @param string $context Request context.
* @return bool|WP_Error
*/
protected function check_permissions( $request, $context = 'read' ) {
// Get taxonomy.
$taxonomy = $this->get_taxonomy( $request );
if ( ! $taxonomy || ! taxonomy_exists( $taxonomy ) ) {
return new WP_Error( 'woocommerce_rest_taxonomy_invalid', __( 'Taxonomy does not exist.', 'woo-gutenberg-products-block' ), array( 'status' => 404 ) );
}
// Check permissions for a single term.
$id = intval( $request['id'] );
if ( $id ) {
$term = get_term( $id, $taxonomy );
if ( is_wp_error( $term ) || ! $term || $term->taxonomy !== $taxonomy ) {
return new WP_Error( 'woocommerce_rest_term_invalid', __( 'Resource does not exist.', 'woo-gutenberg-products-block' ), array( 'status' => 404 ) );
}
}
return current_user_can( 'edit_posts' );
}
/**
* Prepare a single product category output for response.
*
* @param WP_Term $item Term object.
* @param WP_REST_Request $request Request instance.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $item, $request ) {
// Get the attribute slug.
$attribute_id = absint( $request->get_param( 'attribute_id' ) );
$attribute = wc_get_attribute( $attribute_id );
$data = array(
'id' => (int) $item->term_id,
'name' => $item->name,
'slug' => $item->slug,
'count' => (int) $item->count,
'attr_name' => $attribute->name,
'attr_slug' => $attribute->slug,
);
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
$response = rest_ensure_response( $data );
$response->add_links( $this->prepare_links( $item, $request ) );
return $response;
}
/**
* Get the Product's schema, conforming to JSON Schema.
*
* @return array
*/
public function get_item_schema() {
$raw_schema = parent::get_item_schema();
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'product_block_category',
'type' => 'object',
'properties' => array(),
);
$schema['properties']['id'] = $raw_schema['properties']['id'];
$schema['properties']['name'] = $raw_schema['properties']['name'];
$schema['properties']['slug'] = $raw_schema['properties']['slug'];
$schema['properties']['count'] = $raw_schema['properties']['count'];
$schema['properties']['attr_name'] = array(
'description' => __( 'Attribute group name.', 'woo-gutenberg-products-block' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'arg_options' => array(
'sanitize_callback' => 'sanitize_text_field',
),
);
$schema['properties']['attr_slug'] = array(
'description' => __( 'An alphanumeric identifier for the resource unique to its type.', 'woo-gutenberg-products-block' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'arg_options' => array(
'sanitize_callback' => 'sanitize_title',
),
);
return $this->add_additional_fields_schema( $schema );
}
}

View File

@ -235,6 +235,9 @@ class WC_API extends WC_Legacy_API {
include_once dirname( __FILE__ ) . '/api/class-wc-rest-data-continents-controller.php';
include_once dirname( __FILE__ ) . '/api/class-wc-rest-data-countries-controller.php';
include_once dirname( __FILE__ ) . '/api/class-wc-rest-data-currencies-controller.php';
// Blocks REST API V1 controllers.
include_once dirname( __FILE__ ) . '/api/wc-blocks/class-wc-blocks-rest-product-attribute-terms-controller.php';
}
/**
@ -342,6 +345,9 @@ class WC_API extends WC_Legacy_API {
'WC_REST_Data_Continents_Controller',
'WC_REST_Data_Countries_Controller',
'WC_REST_Data_Currencies_Controller',
// Blocks REST API v1 Controllers.
'WC_REST_Blocks_Product_Attribute_Terms_Controller',
);
foreach ( $controllers as $controller ) {

View File

@ -0,0 +1,102 @@
<?php
/**
* @package WooCommerce\Tests\API
*/
/**
* Product Controller "products attributes terms" REST API Test
*
* @since 3.6.0
*/
class WC_Tests_API_Products_Attributes_Terms_Controller extends WC_REST_Unit_Test_Case {
/**
* Endpoints.
*
* @var string
*/
protected $endpoint = '/wc-blocks/v1';
/**
* Setup test products data. Called before every test.
*
* @since 3.6.0
*/
public function setUp() {
parent::setUp();
$this->user = $this->factory->user->create(
array(
'role' => 'administrator',
)
);
$this->editor = $this->factory->user->create(
array(
'role' => 'editor',
)
);
// Create 2 product attributes with terms.
$this->attr_color = WC_Helper_Product::create_attribute( 'color', array( 'red', 'yellow', 'blue' ) );
$this->attr_size = WC_Helper_Product::create_attribute( 'size', array( 'small', 'medium', 'large', 'xlarge' ) );
}
/**
* Test getting attribute terms.
*
* @since 3.6.0
*/
public function test_get_terms() {
wp_set_current_user( $this->user );
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/' . $this->attr_color['attribute_id'] . '/terms' );
$response = $this->server->dispatch( $request );
$response_terms = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 3, count( $response_terms ) );
$term = $response_terms[0];
$this->assertArrayHasKey( 'attr_name', $term );
$this->assertArrayHasKey( 'attr_slug', $term );
}
/**
* Test getting invalid attribute terms.
*
* @since 3.6.0
*/
public function test_get_invalid_attribute_terms() {
wp_set_current_user( $this->user );
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/99999/terms' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 404, $response->get_status() );
}
/**
* Test un-authorized getting attribute terms.
*
* @since 3.6.0
*/
public function test_get_unauthed_attribute_terms() {
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/'. $this->attr_size['attribute_id'] . '/terms' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 401, $response->get_status() );
}
/**
* Test getting attribute terms as editor.
*
* @since 3.6.0
*/
public function test_get_attribute_terms_editor() {
wp_set_current_user( $this->editor );
$request = new WP_REST_Request( 'GET', $this->endpoint . '/products/attributes/'. $this->attr_size['attribute_id'] . '/terms' );
$response = $this->server->dispatch( $request );
$response_terms = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 4, count( $response_terms ) );
}
}