Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit e89fbc8

Browse files
committed
Add unit tests for checking that updating complex meta is possible
Add test for null meta value as well Add test for variation v2 controller for complex meta object Add test for update in date-time field Add a explicit test for null as well
1 parent 064a9e3 commit e89fbc8

File tree

5 files changed

+178
-3
lines changed

5 files changed

+178
-3
lines changed

unit-tests/Bootstrap.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ protected function load_framework() {
128128
require_once $this->tests_dir . '/Helpers/SettingsHelper.php';
129129
require_once $this->tests_dir . '/Helpers/QueueHelper.php';
130130
require_once $this->tests_dir . '/AbstractRestApiTest.php';
131+
132+
// Traits.
133+
require_once $this->tests_dir . '/traits/trait-wc-rest-api-complex-meta.php';
131134
}
132135
}
133136

unit-tests/Tests/Version2/product-variations.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
class Product_Variations_API_V2 extends WC_REST_Unit_Test_Case {
10+
use WC_REST_API_Complex_Meta;
1011

1112
/**
1213
* Setup our test server, endpoints, and user info.
@@ -299,6 +300,19 @@ public function test_create_variation() {
299300
$this->assertEquals( 3, count( $variations ) );
300301
}
301302

303+
/**
304+
* Test updating complex meta object.
305+
*/
306+
public function test_update_complex_meta_27282() {
307+
wp_set_current_user( $this->user );
308+
$product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_variation_product();
309+
$product->save();
310+
$variations = $product->get_available_variations( 'objects' );
311+
$first_variation_id = $variations[0]->get_id();
312+
$url = '/wc/v2/products/' . $product->get_id() . '/variations/' . $first_variation_id;
313+
$this->assert_update_complex_meta( $url );
314+
}
315+
302316
/**
303317
* Test creating a single variation without permission.
304318
*

unit-tests/Tests/Version3/orders.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Class WC_Tests_API_Orders
1111
*/
1212
class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
13+
use WC_REST_API_Complex_Meta;
1314

1415
/**
1516
* Array of order to track
@@ -76,12 +77,22 @@ public function test_get_items_ordered_by_modified() {
7677
$order2->save();
7778

7879
$request = new WP_REST_Request( 'GET', '/wc/v3/orders' );
79-
$request->set_query_params( array( 'orderby' => 'modified', 'order' => 'asc' ) );
80+
$request->set_query_params(
81+
array(
82+
'orderby' => 'modified',
83+
'order' => 'asc',
84+
)
85+
);
8086
$response = $this->server->dispatch( $request );
8187
$orders = $response->get_data();
8288
$this->assertEquals( $order1->get_id(), $orders[0]['id'] );
8389

84-
$request->set_query_params( array( 'orderby' => 'modified', 'order' => 'desc' ) );
90+
$request->set_query_params(
91+
array(
92+
'orderby' => 'modified',
93+
'order' => 'desc',
94+
)
95+
);
8596
$response = $this->server->dispatch( $request );
8697
$orders = $response->get_data();
8798
$this->assertEquals( $order2->get_id(), $orders[0]['id'] );
@@ -208,6 +219,20 @@ public function test_create_order() {
208219
'method_title' => 'Flat rate',
209220
'total' => '10.00',
210221
'instance_id' => '1',
222+
'meta_data' => array(
223+
array(
224+
'key' => 'string',
225+
'value' => 'string_val',
226+
),
227+
array(
228+
'key' => 'integer',
229+
'value' => 1,
230+
),
231+
array(
232+
'key' => 'array',
233+
'value' => array( 1, 2 ),
234+
),
235+
),
211236
),
212237
),
213238
)

unit-tests/Tests/Version3/products.php

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* WC_Tests_API_Product class.
1111
*/
1212
class WC_Tests_API_Product extends WC_REST_Unit_Test_Case {
13+
use WC_REST_API_Complex_Meta;
1314

1415
/**
1516
* Setup our test server, endpoints, and user info.
@@ -218,7 +219,7 @@ public function test_update_product() {
218219
$product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
219220
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() ) );
220221
$data = $response->get_data();
221-
$date_created = date( 'Y-m-d\TH:i:s', current_time( 'timestamp' ) );
222+
$date_created = gmdate( 'Y-m-d\TH:i:s', current_time( 'timestamp' ) );
222223

223224
$this->assertEquals( 'DUMMY SKU', $data['sku'] );
224225
$this->assertEquals( 10, $data['regular_price'] );
@@ -455,6 +456,55 @@ public function test_create_product() {
455456
$this->assertEquals( 3, count( $products ) );
456457
}
457458

459+
/**
460+
* Test to update complex metadata.
461+
*/
462+
public function test_update_complex_meta_27282() {
463+
wp_set_current_user( $this->user );
464+
$product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
465+
$product->save();
466+
$url = '/wc/v3/products/' . $product->get_id();
467+
$this->assert_update_complex_meta( $url );
468+
}
469+
470+
/**
471+
* Test to update datetime property.
472+
*/
473+
public function test_update_date_time() {
474+
wp_set_current_user( $this->user );
475+
$product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
476+
$product->save();
477+
$date_from_sale = '2020-01-01T01:01:01';
478+
479+
$request = new WP_REST_Request( 'PUT', '/wc/v3/products/' . $product->get_id() );
480+
481+
$request->set_body_params( array( 'date_on_sale_from' => $date_from_sale ) );
482+
$response = $this->server->dispatch( $request );
483+
$this->assertEquals( 200, $response->get_status() );
484+
$data = $response->get_data();
485+
$this->assertEquals( $date_from_sale, $data['date_on_sale_from'] );
486+
487+
// Empty string should delete.
488+
$request->set_body_params( array( 'date_on_sale_from' => '' ) );
489+
$response = $this->server->dispatch( $request );
490+
$this->assertEquals( 200, $response->get_status() );
491+
$data = $response->get_data();
492+
$this->assertEquals( null, $data['date_on_sale_from'] );
493+
494+
$request->set_body_params( array( 'date_on_sale_from' => $date_from_sale ) );
495+
$response = $this->server->dispatch( $request );
496+
$this->assertEquals( 200, $response->get_status() );
497+
$data = $response->get_data();
498+
$this->assertEquals( $date_from_sale, $data['date_on_sale_from'] );
499+
500+
// Null does not delete.
501+
$request->set_body_params( array( 'date_on_sale_from' => null ) );
502+
$response = $this->server->dispatch( $request );
503+
$this->assertEquals( 200, $response->get_status() );
504+
$data = $response->get_data();
505+
$this->assertEquals( $date_from_sale, $data['date_on_sale_from'] );
506+
}
507+
458508
/**
459509
* Test creating a single product without permission.
460510
*
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* Trait for easier testing of objects that have `mixed` data type somewhere.
4+
*
5+
* @package Automattic/WooCommerce/Tests/WC_REST_API_Complex_Meta.
6+
*/
7+
8+
/**
9+
* Trait WC_REST_API_Complex_Meta
10+
*/
11+
trait WC_REST_API_Complex_Meta {
12+
13+
/**
14+
* Sample data of different built in data types.
15+
*
16+
* @var array
17+
*/
18+
public static $sample_meta = array(
19+
array(
20+
'key' => 'string_meta',
21+
'value' => 'string_value',
22+
),
23+
array(
24+
'key' => 'int_meta',
25+
'value' => 1,
26+
),
27+
array(
28+
'key' => 'bool_meta',
29+
'value' => true,
30+
),
31+
array(
32+
'key' => 'array_meta',
33+
'value' => array( 1, 2, 'string' ),
34+
),
35+
array(
36+
'key' => 'null_meta',
37+
'value' => 'null',
38+
),
39+
array(
40+
'key' => 'object_meta',
41+
'value' => array(
42+
'nested_key1' => 'nested_value1',
43+
'nested_key2' => 0,
44+
'nested_key3' => true,
45+
'nested_key4' => array( 2, 3, 4 ),
46+
'nested_key5' => array( 2, 3, array( 'deep' => 'nesting' ) ),
47+
),
48+
),
49+
);
50+
51+
/**
52+
* Test to update `meta_data` field with a complex data type.
53+
*
54+
* @param string $url URL to send request against.
55+
* @param array $options Options for customizations.
56+
*/
57+
public function assert_update_complex_meta( $url, $options = array() ) {
58+
$meta = $options['meta'] ?? self::$sample_meta;
59+
$request = new WP_REST_Request( 'PUT', $url );
60+
$request->set_body_params( array( 'meta_data' => $meta ) );
61+
62+
$response = $this->server->dispatch( $request );
63+
64+
$data = $response->get_data();
65+
66+
$this->assertEquals( 200, $response->get_status() );
67+
68+
$response_meta = $data['meta_data'];
69+
70+
foreach ( $meta as $meta_object ) {
71+
$found = false;
72+
foreach ( $response_meta as $response_meta_object ) {
73+
if ( $response_meta_object->key === $meta_object['key'] ) {
74+
$response_value = $response_meta_object->value;
75+
$this->assertEquals( $meta_object['value'], $response_value );
76+
$found = true;
77+
break;
78+
}
79+
}
80+
$this->assertEquals( true, $found, sprintf( 'Meta key %s was not found in response.', $meta_object['key'] ) );
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)