Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 6762856

Browse files
mikejolleyAljullu
andauthored
Store API - Add tests for product attributes endpoints (#1151)
* Add product attribute unit tests * Add missing description param * Allow returning empty terms * Attribute terms tests * Update tests/php/RestApi/StoreApi/Controllers/ProductAttributes.php Co-Authored-By: Albert Juhé Lluveras <[email protected]> * Update tests/php/RestApi/StoreApi/Controllers/ProductAttributeTerms.php Co-Authored-By: Albert Juhé Lluveras <[email protected]>
1 parent 6c2a9c2 commit 6762856

File tree

6 files changed

+232
-22
lines changed

6 files changed

+232
-22
lines changed

src/RestApi/StoreApi/Controllers/ProductAttributeTerms.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,15 @@ public function get_items( $request ) {
116116
return new \WP_Error( 'woocommerce_rest_taxonomy_invalid', __( 'Attribute does not exist.', 'woo-gutenberg-products-block' ), array( 'status' => 404 ) );
117117
}
118118

119-
$request['taxonomy'] = $attribute->slug;
120-
$objects = $this->term_query->get_objects( $request );
119+
$term_request = [
120+
'taxonomy' => $attribute->slug,
121+
'order' => $request['order'],
122+
'orderby' => $request['orderby'],
123+
'hide_empty' => $request['hide_empty'],
124+
];
125+
126+
$objects = $this->term_query->get_objects( $term_request );
127+
$return = [];
121128

122129
foreach ( $objects as $object ) {
123130
$data = $this->prepare_item_for_response( $object, $request );
@@ -157,6 +164,12 @@ public function get_collection_params() {
157164
'validate_callback' => 'rest_validate_request_arg',
158165
);
159166

167+
$params['hide_empty'] = array(
168+
'description' => __( 'Should empty terms be hidden?', 'woo-gutenberg-products-block' ),
169+
'type' => 'boolean',
170+
'default' => true,
171+
);
172+
160173
return $params;
161174
}
162175
}

src/RestApi/StoreApi/Controllers/ProductAttributes.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,4 @@ public function get_items( $request ) {
149149

150150
return rest_ensure_response( $return );
151151
}
152-
153-
/**
154-
* Get the query params for collections of attributes.
155-
*
156-
* @return array
157-
*/
158-
public function get_collection_params() {
159-
$params = array();
160-
$params['context'] = $this->get_context_param();
161-
$params['context']['default'] = 'view';
162-
return $params;
163-
}
164152
}

src/RestApi/StoreApi/Schemas/TermSchema.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@ protected function get_properties() {
7070
*/
7171
public function get_item_response( $term ) {
7272
return [
73-
'id' => (int) $term->term_id,
74-
'name' => $term->name,
75-
'slug' => $term->slug,
76-
'count' => (int) $term->count,
73+
'id' => (int) $term->term_id,
74+
'name' => $term->name,
75+
'description' => $term->description,
76+
'slug' => $term->slug,
77+
'count' => (int) $term->count,
7778
];
7879
}
7980
}

src/RestApi/StoreApi/Utilities/TermQuery.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ class TermQuery {
2222
* @return array
2323
*/
2424
public function prepare_objects_query( $request ) {
25-
$args = array();
26-
$args['order'] = $request['order'];
27-
$args['orderby'] = $request['orderby'];
28-
$args['taxonomy'] = $request['taxonomy'];
25+
$args = array();
26+
$args['order'] = $request['order'];
27+
$args['orderby'] = $request['orderby'];
28+
$args['taxonomy'] = $request['taxonomy'];
29+
$args['hide_empty'] = (bool) $request['hide_empty'];
2930
return $args;
3031
}
3132

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
/**
3+
* Controller Tests.
4+
*
5+
* @package WooCommerce\Blocks\Tests
6+
*/
7+
8+
namespace Automattic\WooCommerce\Blocks\Tests\RestApi\StoreApi\Controllers;
9+
10+
use \WP_REST_Request;
11+
use \WC_REST_Unit_Test_Case as TestCase;
12+
use \WC_Helper_Product as ProductHelper;
13+
14+
/**
15+
* Product Attributes Controller Tests.
16+
*/
17+
class ProductAttributeTerms extends TestCase {
18+
/**
19+
* Setup test products data. Called before every test.
20+
*/
21+
public function setUp() {
22+
parent::setUp();
23+
24+
wp_set_current_user( 0 );
25+
26+
$this->attributes = [];
27+
$this->attributes[0] = ProductHelper::create_attribute( 'color', [ 'red', 'green', 'blue' ] );
28+
$this->attributes[1] = ProductHelper::create_attribute( 'size', [ 'small', 'medium', 'large' ] );
29+
30+
wp_insert_term( 'test', 'pa_size', [
31+
'description' => 'This is a test description',
32+
'slug' => 'test-slug',
33+
] );
34+
}
35+
36+
/**
37+
* Test route registration.
38+
*/
39+
public function test_register_routes() {
40+
$routes = $this->server->get_routes();
41+
$this->assertArrayHasKey( '/wc/store/products/attributes/(?P<attribute_id>[\d]+)/terms', $routes );
42+
}
43+
44+
/**
45+
* Test getting items.
46+
*/
47+
public function test_get_items() {
48+
$request = new WP_REST_Request( 'GET', '/wc/store/products/attributes/' . $this->attributes[0]['attribute_id'] . '/terms' );
49+
$request->set_param( 'hide_empty', false );
50+
$response = $this->server->dispatch( $request );
51+
$data = $response->get_data();
52+
53+
$this->assertEquals( 200, $response->get_status() );
54+
$this->assertEquals( 3, count( $data ) );
55+
$this->assertArrayHasKey( 'id', $data[0] );
56+
$this->assertArrayHasKey( 'name', $data[0] );
57+
$this->assertArrayHasKey( 'slug', $data[0] );
58+
$this->assertArrayHasKey( 'description', $data[0] );
59+
$this->assertArrayHasKey( 'count', $data[0] );
60+
}
61+
62+
/**
63+
* Test schema retrieval.
64+
*/
65+
public function test_get_item_schema() {
66+
$controller = new \Automattic\WooCommerce\Blocks\RestApi\StoreApi\Controllers\ProductAttributeTerms();
67+
$schema = $controller->get_item_schema();
68+
69+
$this->assertArrayHasKey( 'id', $schema['properties'] );
70+
$this->assertArrayHasKey( 'name', $schema['properties'] );
71+
$this->assertArrayHasKey( 'slug', $schema['properties'] );
72+
$this->assertArrayHasKey( 'description', $schema['properties'] );
73+
$this->assertArrayHasKey( 'count', $schema['properties'] );
74+
}
75+
76+
/**
77+
* Test conversion of product to rest response.
78+
*/
79+
public function test_prepare_item_for_response() {
80+
$controller = new \Automattic\WooCommerce\Blocks\RestApi\StoreApi\Controllers\ProductAttributeTerms();
81+
$response = $controller->prepare_item_for_response( get_term_by( 'name', 'test', 'pa_size' ), [] );
82+
$data = $response->get_data();
83+
84+
$this->assertArrayHasKey( 'id', $data );
85+
$this->assertEquals( 'test', $data['name'] );
86+
$this->assertEquals( 'test-slug', $data['slug'] );
87+
$this->assertEquals( 'This is a test description', $data['description'] );
88+
$this->assertEquals( 0, $data['count'] );
89+
}
90+
91+
/**
92+
* Test collection params getter.
93+
*/
94+
public function test_get_collection_params() {
95+
$controller = new \Automattic\WooCommerce\Blocks\RestApi\StoreApi\Controllers\ProductAttributeTerms();
96+
$params = $controller->get_collection_params();
97+
98+
$this->assertArrayHasKey( 'order', $params );
99+
$this->assertArrayHasKey( 'orderby', $params );
100+
$this->assertArrayHasKey( 'hide_empty', $params );
101+
}
102+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/**
3+
* Controller Tests.
4+
*
5+
* @package WooCommerce\Blocks\Tests
6+
*/
7+
8+
namespace Automattic\WooCommerce\Blocks\Tests\RestApi\StoreApi\Controllers;
9+
10+
use \WP_REST_Request;
11+
use \WC_REST_Unit_Test_Case as TestCase;
12+
use \WC_Helper_Product as ProductHelper;
13+
14+
/**
15+
* Product Attributes Controller Tests.
16+
*/
17+
class ProductAttributes extends TestCase {
18+
/**
19+
* Setup test products data. Called before every test.
20+
*/
21+
public function setUp() {
22+
parent::setUp();
23+
24+
wp_set_current_user( 0 );
25+
26+
$color_attribute = ProductHelper::create_attribute( 'color', [ 'red', 'green', 'blue' ] );
27+
$size_attribute = ProductHelper::create_attribute( 'size', [ 'small', 'medium', 'large' ] );
28+
29+
$this->attributes = [];
30+
$this->attributes[] = wc_get_attribute( $color_attribute['attribute_id'] );
31+
$this->attributes[] = wc_get_attribute( $size_attribute['attribute_id'] );
32+
}
33+
34+
/**
35+
* Test route registration.
36+
*/
37+
public function test_register_routes() {
38+
$routes = $this->server->get_routes();
39+
$this->assertArrayHasKey( '/wc/store/products/attributes', $routes );
40+
$this->assertArrayHasKey( '/wc/store/products/attributes/(?P<id>[\d]+)', $routes );
41+
}
42+
43+
/**
44+
* Test getting item.
45+
*/
46+
public function test_get_item() {
47+
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/store/products/attributes/' . $this->attributes[0]->id ) );
48+
$data = $response->get_data();
49+
50+
$this->assertEquals( 200, $response->get_status() );
51+
$this->assertEquals( $this->attributes[0]->id, $data['id'] );
52+
$this->assertEquals( $this->attributes[0]->name, $data['name'] );
53+
$this->assertEquals( $this->attributes[0]->slug, $data['slug'] );
54+
$this->assertEquals( $this->attributes[0]->type, $data['type'] );
55+
$this->assertEquals( $this->attributes[0]->order_by, $data['order'] );
56+
$this->assertEquals( $this->attributes[0]->has_archives, $data['has_archives'] );
57+
}
58+
59+
/**
60+
* Test getting items.
61+
*/
62+
public function test_get_items() {
63+
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/store/products/attributes' ) );
64+
$data = $response->get_data();
65+
66+
$this->assertEquals( 200, $response->get_status() );
67+
$this->assertEquals( 2, count( $data ) );
68+
$this->assertArrayHasKey( 'id', $data[0] );
69+
$this->assertArrayHasKey( 'name', $data[0] );
70+
$this->assertArrayHasKey( 'slug', $data[0] );
71+
$this->assertArrayHasKey( 'type', $data[0] );
72+
$this->assertArrayHasKey( 'order', $data[0] );
73+
$this->assertArrayHasKey( 'has_archives', $data[0] );
74+
}
75+
76+
/**
77+
* Test schema retrieval.
78+
*/
79+
public function test_get_item_schema() {
80+
$controller = new \Automattic\WooCommerce\Blocks\RestApi\StoreApi\Controllers\ProductAttributes();
81+
$schema = $controller->get_item_schema();
82+
83+
$this->assertArrayHasKey( 'id', $schema['properties'] );
84+
$this->assertArrayHasKey( 'name', $schema['properties'] );
85+
$this->assertArrayHasKey( 'slug', $schema['properties'] );
86+
$this->assertArrayHasKey( 'type', $schema['properties'] );
87+
$this->assertArrayHasKey( 'order', $schema['properties'] );
88+
$this->assertArrayHasKey( 'has_archives', $schema['properties'] );
89+
}
90+
91+
/**
92+
* Test conversion of product to rest response.
93+
*/
94+
public function test_prepare_item_for_response() {
95+
$controller = new \Automattic\WooCommerce\Blocks\RestApi\StoreApi\Controllers\ProductAttributes();
96+
$response = $controller->prepare_item_for_response( $this->attributes[0], [] );
97+
98+
$this->assertArrayHasKey( 'id', $response->get_data() );
99+
$this->assertArrayHasKey( 'name', $response->get_data() );
100+
$this->assertArrayHasKey( 'slug', $response->get_data() );
101+
$this->assertArrayHasKey( 'type', $response->get_data() );
102+
$this->assertArrayHasKey( 'order', $response->get_data() );
103+
$this->assertArrayHasKey( 'has_archives', $response->get_data() );
104+
}
105+
}

0 commit comments

Comments
 (0)