Files

320 lines
9.9 KiB
PHP
Raw Permalink Normal View History

<?php
2019-04-16 17:48:39 -04:00
use WPGraphQL\Type\WPEnumType;
class OrderItemQueriesTest extends \Tests\WPGraphQL\WooCommerce\TestCase\WooGraphQLTestCase {
// tests
public function testCouponLinesQuery() {
$order_id = $this->factory->order->createNew();
$this->factory->order->add_coupon_line( $order_id );
$order = new WC_Order( $order_id );
$coupon_lines = $order->get_items( 'coupon' );
2023-06-21 13:21:29 -04:00
$id = $this->toRelayId( 'order', $order_id );
2020-09-16 16:32:14 -04:00
$query = '
2019-10-17 21:39:38 -04:00
query ($id: ID!) {
order(id: $id) {
2019-04-16 17:48:39 -04:00
couponLines {
nodes {
2020-09-16 16:32:14 -04:00
databaseId
2019-04-16 17:48:39 -04:00
orderId
code
discount
discountTax
coupon {
id
}
}
}
}
}
';
/**
2019-04-16 17:48:39 -04:00
* Assertion One
2020-09-16 16:32:14 -04:00
*
* Tests query and results
2019-04-16 17:48:39 -04:00
*/
$this->loginAsShopManager();
$variables = [ 'id' => $id ];
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = array_map(
function ( $item ) {
return $this->expectedNode(
'order.couponLines.nodes',
[
$this->expectedField( 'databaseId', $item->get_id() ),
$this->expectedField( 'orderId', $item->get_order_id() ),
$this->expectedField( 'code', $item->get_code() ),
$this->expectedField( 'discount', $this->maybe( $item->get_discount(), static::IS_NULL ) ),
$this->expectedField( 'discountTax', $this->maybe( $item->get_discount_tax(), static::IS_NULL ) ),
$this->expectedField( 'coupon.id', $this->toRelayId( 'shop_coupon', \wc_get_coupon_id_by_code( $item->get_code() ) ) ),
]
);
},
$coupon_lines
);
2019-04-16 17:48:39 -04:00
$this->assertQuerySuccessful( $response, $expected );
}
public function testFeeLinesQuery() {
$order_id = $this->factory->order->createNew();
$this->factory->order->add_fee( $order_id );
$order = new WC_Order( $order_id );
$fee_lines = $order->get_items( 'fee' );
2023-06-21 13:21:29 -04:00
$id = $this->toRelayId( 'order', $order_id );
2020-09-16 16:32:14 -04:00
$query = '
2019-10-17 21:39:38 -04:00
query ($id: ID!) {
order(id: $id) {
2019-04-16 17:48:39 -04:00
feeLines {
nodes {
2020-09-16 16:32:14 -04:00
databaseId
2019-04-16 17:48:39 -04:00
orderId
amount
name
taxStatus
total
totalTax
taxClass
}
}
}
}
';
/**
2019-04-16 17:48:39 -04:00
* Assertion One
2020-09-16 16:32:14 -04:00
*
* Tests query and results
2019-04-16 17:48:39 -04:00
*/
$this->loginAsShopManager();
$variables = [ 'id' => $id ];
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = array_map(
function ( $item ) {
return $this->expectedNode(
'order.feeLines.nodes',
[
$this->expectedField( 'databaseId', $item->get_id() ),
$this->expectedField( 'orderId', $item->get_order_id() ),
$this->expectedField( 'amount', $item->get_amount() ),
$this->expectedField( 'name', $item->get_name() ),
$this->expectedField( 'taxStatus', strtoupper( $item->get_tax_status() ) ),
$this->expectedField( 'total', $item->get_total() ),
$this->expectedField( 'totalTax', $this->maybe( $item->get_total_tax(), static::IS_NULL ) ),
$this->expectedField(
'taxClass',
! empty( $item->get_tax_class() )
? WPEnumType::get_safe_name( $item->get_tax_class() )
: 'STANDARD'
),
]
);
},
$fee_lines
);
2019-04-16 17:48:39 -04:00
$this->assertQuerySuccessful( $response, $expected );
}
2019-04-16 17:48:39 -04:00
public function testShippingLinesQuery() {
$order_id = $this->factory->order->createNew();
$order = new WC_Order( $order_id );
$shipping_lines = $order->get_items( 'shipping' );
2023-06-21 13:21:29 -04:00
$id = $this->toRelayId( 'order', $order_id );
2020-09-16 16:32:14 -04:00
$query = '
2019-10-17 21:39:38 -04:00
query ($id: ID!) {
order(id: $id) {
2019-04-16 17:48:39 -04:00
shippingLines {
nodes {
2020-09-16 16:32:14 -04:00
databaseId
2019-04-16 17:48:39 -04:00
orderId
methodTitle
total
totalTax
taxClass
}
}
}
}
';
/**
2019-04-16 17:48:39 -04:00
* Assertion One
2020-09-16 16:32:14 -04:00
*
* Tests query and results
2019-04-16 17:48:39 -04:00
*/
$this->loginAsShopManager();
$variables = [ 'id' => $id ];
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = array_map(
function ( $item ) {
return $this->expectedNode(
'order.shippingLines.nodes',
[
$this->expectedField( 'databaseId', $item->get_id() ),
$this->expectedField( 'orderId', $item->get_order_id() ),
$this->expectedField( 'methodTitle', $item->get_method_title() ),
$this->expectedField( 'total', $item->get_total() ),
$this->expectedField( 'totalTax', $this->maybe( $item->get_total_tax(), static::IS_NULL ) ),
$this->expectedField(
'taxClass',
! empty( $item->get_tax_class() )
? $item->get_tax_class() === 'inherit'
? WPEnumType::get_safe_name( 'inherit cart' )
: WPEnumType::get_safe_name( $item->get_tax_class() )
: 'STANDARD'
),
]
);
},
$shipping_lines
);
2019-04-16 17:48:39 -04:00
$this->assertQuerySuccessful( $response, $expected );
}
2019-04-16 17:48:39 -04:00
public function testTaxLinesQuery() {
$order_id = $this->factory->order->createNew();
$this->factory->order->add_tax( $order_id );
$order = new WC_Order( $order_id );
$tax_lines = $order->get_items( 'tax' );
2023-06-21 13:21:29 -04:00
$id = $this->toRelayId( 'order', $order_id );
2020-09-16 16:32:14 -04:00
$query = '
2019-10-17 21:39:38 -04:00
query ($id: ID!) {
order(id: $id) {
2019-04-16 17:48:39 -04:00
taxLines {
nodes {
rateCode
label
taxTotal
shippingTaxTotal
isCompound
taxRate {
2020-09-16 16:32:14 -04:00
databaseId
2019-04-16 17:48:39 -04:00
}
}
}
}
}
';
/**
2019-04-16 17:48:39 -04:00
* Assertion One
2020-09-16 16:32:14 -04:00
*
* Tests query and results
2019-04-16 17:48:39 -04:00
*/
$this->loginAsShopManager();
$variables = [ 'id' => $id ];
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = array_map(
function ( $item ) {
return $this->expectedNode(
'order.taxLines.nodes',
[
$this->expectedField( 'rateCode', $item->get_rate_code() ),
$this->expectedField( 'label', $item->get_label() ),
$this->expectedField( 'taxTotal', $item->get_tax_total() ),
$this->expectedField( 'shippingTaxTotal', $item->get_shipping_tax_total() ),
$this->expectedField( 'isCompound', $item->is_compound() ),
$this->expectedField( 'taxRate.databaseId', $item->get_rate_id() ),
]
);
},
$tax_lines
);
2019-04-16 17:48:39 -04:00
$this->assertQuerySuccessful( $response, $expected );
}
2019-04-16 17:48:39 -04:00
public function testLineItemsQuery() {
$order_id = $this->factory->order->createNew();
$order = new WC_Order( $order_id );
$line_items = $order->get_items();
2023-06-21 13:21:29 -04:00
$id = $this->toRelayId( 'order', $order_id );
2020-09-16 16:32:14 -04:00
$query = '
2019-10-17 21:39:38 -04:00
query ($id: ID!) {
order(id: $id) {
2019-04-16 17:48:39 -04:00
lineItems {
nodes {
productId
variationId
quantity
taxClass
subtotal(format: RAW)
subtotalTax(format: RAW)
total(format: RAW)
totalTax(format: RAW)
2019-04-16 17:48:39 -04:00
itemDownloads {
downloadId
}
taxStatus
product {
node {
... on SimpleProduct {
id
}
... on VariableProduct {
id
}
}
2019-04-16 17:48:39 -04:00
}
variation {
node { id }
2019-04-16 17:48:39 -04:00
}
}
}
}
}
';
/**
2019-04-16 17:48:39 -04:00
* Assertion One
2020-09-16 16:32:14 -04:00
*
* Tests query and results
2019-04-16 17:48:39 -04:00
*/
$this->loginAsShopManager();
$variables = [ 'id' => $id ];
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = array_map(
function ( $item ) {
return $this->expectedNode(
'order.lineItems.nodes',
[
$this->expectedField( 'productId', $item->get_product_id() ),
$this->expectedField( 'variationId', $this->maybe( $item->get_variation_id(), static::IS_NULL ) ),
$this->expectedField( 'quantity', $item->get_quantity() ),
$this->expectedField(
'taxClass',
! empty( $item->get_tax_class() )
? strtoupper( $item->get_tax_class() )
: 'STANDARD'
),
$this->expectedField( 'subtotal', $this->maybe( $item->get_subtotal(), static::IS_NULL ) ),
$this->expectedField( 'subtotalTax', $this->maybe( $item->get_subtotal_tax(), static::IS_NULL ) ),
$this->expectedField( 'total', $this->maybe( $item->get_total(), static::IS_NULL ) ),
$this->expectedField( 'totalTax', $this->maybe( $item->get_total_tax(), static::IS_NULL ) ),
$this->expectedField( 'itemDownloads', null ),
$this->expectedField( 'taxStatus', strtoupper( $item->get_tax_status() ) ),
$this->expectedField( 'product.node.id', $this->toRelayId( 'post', $item->get_product_id() ) ),
$this->expectedField(
'variation.node.id',
! empty( $item->get_variation_id() )
? $this->toRelayId( 'post', $item->get_variation_id() )
: static::IS_NULL
),
]
);
},
$line_items
);
2019-04-16 17:48:39 -04:00
$this->assertQuerySuccessful( $response, $expected );
}
2020-09-16 16:32:14 -04:00
}