Skip to content

Commit cb25be4

Browse files
committed
add test for tax_based_on meta
1 parent 2a5498e commit cb25be4

File tree

4 files changed

+286
-63
lines changed

4 files changed

+286
-63
lines changed

includes/API/Orders_Controller.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ public function wcpos_tax_based_on( $value, $option ) {
536536

537537
// try to get POS tax settings from order meta
538538
$raw_data = $this->wcpos_request->get_json_params();
539+
539540
if ( isset( $raw_data['meta_data'] ) ) {
540541
foreach ( $raw_data['meta_data'] as $meta ) {
541542
if ( '_woocommerce_pos_tax_based_on' == $meta['key'] ) {
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
<?php
2+
3+
namespace WCPOS\WooCommercePOS\Tests\API;
4+
5+
use Automattic\WooCommerce\RestApi\UnitTests\Helpers\CustomerHelper;
6+
use Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper;
7+
use Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper;
8+
use WC_Order_Item_Fee;
9+
use WCPOS\WooCommercePOS\API\Orders_Controller;
10+
use WCPOS\WooCommercePOS\Tests\Helpers\TaxHelper;
11+
12+
/**
13+
* @internal
14+
*
15+
* @coversNothing
16+
*/
17+
class Test_Order_Taxes extends WCPOS_REST_Unit_Test_Case {
18+
public function setup(): void {
19+
parent::setUp();
20+
$this->endpoint = new Orders_Controller();
21+
update_option( 'woocommerce_calc_taxes', 'yes' );
22+
23+
// Set default address
24+
// update_option( 'woocommerce_default_country', 'GB' );
25+
26+
/**
27+
* Init Taxes
28+
*
29+
* use WooCommerce Tax Dummy Data
30+
*/
31+
TaxHelper::create_tax_rate(
32+
array(
33+
'country' => 'GB',
34+
'rate' => '20.000',
35+
'name' => 'VAT',
36+
'priority' => 1,
37+
'compound' => true,
38+
'shipping' => true,
39+
)
40+
);
41+
TaxHelper::create_tax_rate(
42+
array(
43+
'country' => 'GB',
44+
'rate' => '5.000',
45+
'name' => 'VAT',
46+
'priority' => 1,
47+
'compound' => true,
48+
'shipping' => true,
49+
'class' => 'reduced-rate',
50+
)
51+
);
52+
TaxHelper::create_tax_rate(
53+
array(
54+
'country' => 'GB',
55+
'rate' => '0.000',
56+
'name' => 'VAT',
57+
'priority' => 1,
58+
'compound' => true,
59+
'shipping' => true,
60+
'class' => 'zero-rate',
61+
)
62+
);
63+
TaxHelper::create_tax_rate(
64+
array(
65+
'country' => 'US',
66+
'rate' => '10.000',
67+
'name' => 'US',
68+
'priority' => 1,
69+
'compound' => true,
70+
'shipping' => true,
71+
)
72+
);
73+
TaxHelper::create_tax_rate(
74+
array(
75+
'country' => 'US',
76+
'state' => 'AL',
77+
'postcode' => '12345; 123456',
78+
'rate' => '2.000',
79+
'name' => 'US AL',
80+
'priority' => 2,
81+
'compound' => true,
82+
'shipping' => true,
83+
)
84+
);
85+
}
86+
87+
public function tearDown(): void {
88+
parent::tearDown();
89+
}
90+
91+
/**
92+
* Create a new order.
93+
*/
94+
public function test_create_order_with_tax(): void {
95+
$request = $this->wp_rest_post_request( '/wcpos/v1/orders' );
96+
$request->set_body_params(
97+
array(
98+
'payment_method' => 'pos_cash',
99+
'line_items' => array(
100+
array(
101+
'product_id' => 1,
102+
'quantity' => 1,
103+
'price' => 10,
104+
'total' => '10.00',
105+
),
106+
),
107+
'billing' => array(
108+
'email' => '',
109+
'first_name' => '',
110+
'last_name' => '',
111+
'address_1' => '',
112+
'address_2' => '',
113+
'city' => '',
114+
'state' => '',
115+
'postcode' => '',
116+
'country' => '',
117+
'phone' => '',
118+
),
119+
)
120+
);
121+
122+
$response = $this->server->dispatch( $request );
123+
$data = $response->get_data();
124+
$this->assertEquals( 201, $response->get_status() );
125+
126+
// line item taxes
127+
$this->assertEquals( 1, \count( $data['line_items'] ) );
128+
$this->assertEquals( 1, \count( $data['line_items'][0]['taxes'] ) );
129+
$this->assertEquals( '1', $data['line_items'][0]['taxes'][0]['total'] );
130+
131+
// order taxes
132+
$this->assertEquals( 1, \count( $data['tax_lines'] ) );
133+
$this->assertEquals( '1.00', $data['tax_lines'][0]['tax_total'] );
134+
$this->assertEquals( 'US', $data['tax_lines'][0]['label'] );
135+
$this->assertEquals( '10', $data['tax_lines'][0]['rate_percent'] );
136+
}
137+
138+
/**
139+
* Create a new order with customer billing address as tax location.
140+
*/
141+
public function test_create_order_with_customer_billing_address_as_tax_location(): void {
142+
$request = $this->wp_rest_post_request( '/wcpos/v1/orders' );
143+
// Prepare your data as an array and then JSON-encode it
144+
$data = array(
145+
'payment_method' => 'pos_cash',
146+
'line_items' => array(
147+
array(
148+
'product_id' => 1,
149+
'quantity' => 1,
150+
'price' => 10,
151+
'total' => '10.00',
152+
),
153+
),
154+
'billing' => array(
155+
'email' => '',
156+
'first_name' => '',
157+
'last_name' => '',
158+
'address_1' => '',
159+
'address_2' => '',
160+
'city' => '',
161+
'state' => '',
162+
'postcode' => '',
163+
'country' => 'GB',
164+
'phone' => '',
165+
),
166+
'meta_data' => array(
167+
array(
168+
'key' => '_woocommerce_pos_tax_based_on',
169+
'value' => 'billing',
170+
),
171+
),
172+
);
173+
174+
// Set the body to a JSON string
175+
$request->set_header( 'Content-Type', 'application/json' );
176+
$request->set_body( wp_json_encode( $data ) );
177+
178+
$response = $this->server->dispatch( $request );
179+
$data = $response->get_data();
180+
$this->assertEquals( 201, $response->get_status() );
181+
182+
// check meta data
183+
$count = 0;
184+
$tax_based_on = '';
185+
186+
// Look for the _woocommerce_pos_uuid key in meta_data
187+
foreach ( $data['meta_data'] as $meta ) {
188+
if ( '_woocommerce_pos_tax_based_on' === $meta['key'] ) {
189+
$count++;
190+
$tax_based_on = $meta['value'];
191+
}
192+
}
193+
194+
$this->assertEquals( 1, $count, 'There should only be one _woocommerce_pos_tax_based_on.' );
195+
$this->assertEquals( 'billing', $tax_based_on, 'The value of _woocommerce_pos_tax_based_on should be billing.' );
196+
197+
// line item taxes
198+
$this->assertEquals( 1, \count( $data['line_items'] ) );
199+
$this->assertEquals( 1, \count( $data['line_items'][0]['taxes'] ) );
200+
$this->assertEquals( '2', $data['line_items'][0]['taxes'][0]['total'] );
201+
202+
// order taxes
203+
$this->assertEquals( 1, \count( $data['tax_lines'] ) );
204+
$this->assertEquals( '2.00', $data['tax_lines'][0]['tax_total'] );
205+
$this->assertEquals( 'VAT', $data['tax_lines'][0]['label'] );
206+
$this->assertEquals( '20', $data['tax_lines'][0]['rate_percent'] );
207+
}
208+
}

tests/includes/API/Test_Products_Controller.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,11 +464,15 @@ public function test_product_update_decimal_quantities(): void {
464464
$product->save();
465465

466466
$request = $this->wp_rest_patch_request( '/wcpos/v1/products/' . $product->get_id() );
467-
$request->set_body_params(
468-
array(
469-
'stock_quantity' => '3.85',
467+
$request->set_header( 'Content-Type', 'application/json' );
468+
$request->set_body(
469+
wp_json_encode(
470+
array(
471+
'stock_quantity' => '3.85',
472+
)
470473
)
471474
);
475+
472476
$response = $this->server->dispatch( $request );
473477
$data = $response->get_data();
474478

0 commit comments

Comments
 (0)