Files
wp-graphql-woocommerce/tests/wpunit/OrderQueriesTest.php
T

411 lines
13 KiB
PHP
Raw Normal View History

2019-03-13 12:31:37 -04:00
<?php
use WPGraphQL\Type\WPEnumType;
class OrderQueriesTest extends \Tests\WPGraphQL\WooCommerce\TestCase\WooGraphQLTestCase {
2019-03-13 12:31:37 -04:00
public function expectedOrderData( $order_id ) {
$order = \wc_get_order( $order_id );
2019-03-13 12:31:37 -04:00
$expected = array(
$this->expectedObject( 'order.id', $this->toRelayId( 'shop_order', $order_id ) ),
$this->expectedObject( 'order.databaseId', $order->get_id() ),
$this->expectedObject( 'order.currency', $this->maybe( $order->get_currency(), 'null' ) ),
$this->expectedObject( 'order.orderVersion', $this->maybe( $order->get_version(), 'null' ) ),
$this->expectedObject( 'order.date', $order->get_date_created()->__toString() ),
$this->expectedObject( 'order.modified', $order->get_date_modified()->__toString() ),
$this->expectedObject( 'order.status', WPEnumType::get_safe_name( $order->get_status() ) ),
$this->expectedObject( 'order.discountTotal', \wc_graphql_price( $order->get_discount_total(), array( 'currency' => $order->get_currency() ) ) ),
$this->expectedObject( 'order.discountTax', \wc_graphql_price( $order->get_discount_tax(), array( 'currency' => $order->get_currency() ) ) ),
$this->expectedObject( 'order.shippingTotal', \wc_graphql_price( $order->get_shipping_total(), array( 'currency' => $order->get_currency() ) ) ),
$this->expectedObject( 'order.shippingTax', \wc_graphql_price( $order->get_shipping_tax(), array( 'currency' => $order->get_currency() ) ) ),
$this->expectedObject( 'order.cartTax', \wc_graphql_price( $order->get_cart_tax(), array( 'currency' => $order->get_currency() ) ) ),
$this->expectedObject( 'order.total', \wc_graphql_price( $order->get_total(), array( 'currency' => $order->get_currency() ) ) ),
$this->expectedObject( 'order.totalTax', \wc_graphql_price( $order->get_total_tax(), array( 'currency' => $order->get_currency() ) ) ),
$this->expectedObject( 'order.subtotal', \wc_graphql_price( $order->get_subtotal(), array( 'currency' => $order->get_currency() ) ) ),
$this->expectedObject( 'order.orderNumber', $order->get_order_number() ),
$this->expectedObject( 'order.orderKey', $order->get_order_key() ),
$this->expectedObject( 'order.createdVia', $this->maybe( $order->get_created_via(), 'null' ) ),
$this->expectedObject( 'order.pricesIncludeTax', $order->get_prices_include_tax() ),
$this->expectedObject( 'order.parent', 'null' ),
$this->expectedObject(
'order.customer',
$this->maybe(
array(
$order->get_customer_id(),
array( 'id' => $this->toRelayId( 'customer', $order->get_customer_id() ) ),
),
'null'
)
),
$this->expectedObject( 'order.customerIpAddress', $this->maybe( $order->get_customer_ip_address(), 'null' ) ),
$this->expectedObject( 'order.customerUserAgent', $this->maybe( $order->get_customer_user_agent(), 'null' ) ),
$this->expectedObject( 'order.customerNote', $this->maybe( $order->get_customer_note(), 'null' ) ),
$this->expectedObject(
'order.billing',
array(
2021-06-03 21:37:09 -04:00
'firstName' => $this->maybe( $order->get_billing_first_name() ),
'lastName' => $this->maybe( $order->get_billing_last_name() ),
'company' => $this->maybe( $order->get_billing_company() ),
'address1' => $this->maybe( $order->get_billing_address_1() ),
'address2' => $this->maybe( $order->get_billing_address_2() ),
'city' => $this->maybe( $order->get_billing_city() ),
'state' => $this->maybe( $order->get_billing_state() ),
'postcode' => $this->maybe( $order->get_billing_postcode() ),
'country' => $this->maybe( $order->get_billing_country() ),
'email' => $this->maybe( $order->get_billing_email() ),
'phone' => $this->maybe( $order->get_billing_phone() ),
)
),
$this->expectedObject(
'order.shipping',
array(
2021-06-03 21:37:09 -04:00
'firstName' => $this->maybe( $order->get_shipping_first_name() ),
'lastName' => $this->maybe( $order->get_shipping_last_name() ),
'company' => $this->maybe( $order->get_shipping_company() ),
'address1' => $this->maybe( $order->get_shipping_address_1() ),
'address2' => $this->maybe( $order->get_shipping_address_2() ),
'city' => $this->maybe( $order->get_shipping_city() ),
'state' => $this->maybe( $order->get_shipping_state() ),
'postcode' => $this->maybe( $order->get_shipping_postcode() ),
'country' => $this->maybe( $order->get_shipping_country() ),
)
),
$this->expectedObject( 'order.paymentMethod', $this->maybe( $order->get_payment_method(), 'null' ) ),
$this->expectedObject( 'order.paymentMethodTitle', $this->maybe( $order->get_payment_method_title(), 'null' ) ),
$this->expectedObject( 'order.transactionId', $this->maybe( $order->get_transaction_id(), 'null' ) ),
$this->expectedObject( 'order.dateCompleted', $this->maybe( $order->get_date_completed(), 'null' ) ),
$this->expectedObject( 'order.datePaid', $this->maybe( $order->get_date_paid(), 'null' ) ),
$this->expectedObject( 'order.cartHash', $this->maybe( $order->get_cart_hash(), 'null' ) ),
$this->expectedObject( 'order.shippingAddressMapUrl', $this->maybe( $order->get_shipping_address_map_url(), 'null' ) ),
$this->expectedObject( 'order.hasBillingAddress', $order->has_billing_address() ),
$this->expectedObject( 'order.hasShippingAddress', $order->has_shipping_address() ),
$this->expectedObject( 'order.isDownloadPermitted', $order->is_download_permitted() ),
$this->expectedObject( 'order.needsShippingAddress', $order->needs_shipping_address() ),
$this->expectedObject( 'order.hasDownloadableItem', $order->has_downloadable_item() ),
$this->expectedObject( 'order.needsPayment', $order->needs_payment() ),
$this->expectedObject( 'order.needsProcessing', $order->needs_processing() ),
);
2019-03-13 12:31:37 -04:00
return $expected;
2019-03-21 21:43:24 -04:00
}
2019-03-13 12:31:37 -04:00
2019-03-21 21:43:24 -04:00
// tests
public function testOrderQuery() {
$order_id = $this->factory->order->createNew();
$id = $this->toRelayId( 'shop_order', $order_id );
2019-03-29 21:54:09 -04:00
$query = '
2019-10-17 21:39:38 -04:00
query ($id: ID!) {
order(id: $id) {
id
2020-09-16 16:32:14 -04:00
databaseId
2019-03-21 21:43:24 -04:00
currency
orderVersion
2019-03-29 21:54:09 -04:00
date
modified
status
2019-03-21 21:43:24 -04:00
discountTotal
discountTax
shippingTotal
shippingTax
cartTax
total
totalTax
2019-03-29 21:54:09 -04:00
subtotal
orderNumber
2019-03-29 21:54:09 -04:00
orderKey
createdVia
2019-03-21 21:43:24 -04:00
pricesIncludeTax
2019-03-29 21:54:09 -04:00
parent {
id
}
2019-03-21 21:43:24 -04:00
customer {
id
}
customerIpAddress
customerUserAgent
customerNote
billing {
firstName
lastName
company
address1
address2
city
state
postcode
country
email
phone
}
shipping {
firstName
lastName
company
address1
address2
city
state
postcode
country
}
paymentMethod
paymentMethodTitle
transactionId
dateCompleted
2019-03-29 21:54:09 -04:00
datePaid
2019-03-21 21:43:24 -04:00
cartHash
2019-03-29 21:54:09 -04:00
shippingAddressMapUrl
hasBillingAddress
hasShippingAddress
isDownloadPermitted
needsShippingAddress
hasDownloadableItem
downloadableItems {
nodes {
url
accessExpires
downloadId
downloadsRemaining
name
product {
2020-09-16 16:32:14 -04:00
databaseId
}
download {
downloadId
}
}
2019-03-29 21:54:09 -04:00
}
needsPayment
needsProcessing
}
}
';
2020-08-11 19:18:19 -04:00
/**
* Assertion One
2020-08-11 19:18:19 -04:00
*
* tests query as customer, should return "null" because the customer isn't authorized.
*/
$this->loginAsCustomer();
$variables = array( 'id' => $id );
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = array( $this->expectedObject( 'order', 'null' ) );
$this->assertQuerySuccessful( $response, $expected );
/**
* Assertion Two
2020-08-11 19:18:19 -04:00
*
* tests query as shop manager
*/
$this->loginAsShopManager();
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = $this->expectedOrderData( $order_id );
$this->assertQuerySuccessful( $response, $expected );
}
2020-01-24 23:40:50 -05:00
public function testOrderQueryAndIds() {
$order_id = $this->factory->order->createNew();
$id = $this->toRelayId( 'shop_order', $order_id );
$query = '
2020-01-24 23:40:50 -05:00
query ($id: ID!, $idType: OrderIdTypeEnum ) {
order(id: $id, idType: $idType) {
id
}
}
';
// Must be an "shop_manager" or "admin" to query orders not owned by the user.
$this->loginAsShopManager();
/**
* Assertion One
2020-08-11 19:18:19 -04:00
*
2020-01-24 23:40:50 -05:00
* tests "ID" ID type.
*/
2020-01-24 23:40:50 -05:00
$variables = array(
'id' => $id,
'idType' => 'ID',
);
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = array( $this->expectedObject( 'order.id', $id ) );
$this->assertQuerySuccessful( $response, $expected );
/**
* Assertion Two
2020-08-11 19:18:19 -04:00
*
2020-01-24 23:40:50 -05:00
* tests "DATABASE_ID" ID type.
*/
2020-01-24 23:40:50 -05:00
$variables = array(
'id' => $order_id,
2020-01-24 23:40:50 -05:00
'idType' => 'DATABASE_ID',
);
$response = $this->graphql( compact( 'query', 'variables' ) );
$this->assertQuerySuccessful( $response, $expected );
/**
* Assertion Three
2020-08-11 19:18:19 -04:00
*
2020-01-24 23:40:50 -05:00
* tests "ORDER_NUMBER" ID type
*/
2020-01-24 23:40:50 -05:00
$variables = array(
'id' => $this->factory->order->get_order_key( $order_id ),
2020-01-24 23:40:50 -05:00
'idType' => 'ORDER_NUMBER',
);
$response = $this->graphql( compact( 'query', 'variables' ) );
$this->assertQuerySuccessful( $response, $expected );
}
public function testOrdersQueryAndWhereArgs() {
// Create and delete scrap/old order(s).
$this->factory->order->createNew();
2020-10-14 20:33:55 -04:00
$query = new \WC_Order_Query();
$old_orders = $query->get_orders();
foreach ( $old_orders as $order ) {
2021-06-03 21:37:09 -04:00
$this->logData( 'Order ' . $order->get_id() . ' deleted.' );
$this->factory->order->delete_order( $order );
2020-10-14 20:33:55 -04:00
}
// Create order for query response.
$customer = $this->factory->customer->create();
$product = $this->factory->product->createSimple();
$orders = array(
$this->factory->order->createNew(
array(),
array(
'line_items' => array(
array(
'product' => $product,
'qty' => 4,
),
),
)
),
$this->factory->order->createNew(
array(
'status' => 'completed',
'customer_id' => $customer,
),
array(
'line_items' => array(
array(
'product' => $product,
'qty' => 2,
),
),
)
),
);
$query = '
2019-10-17 21:39:38 -04:00
query ($statuses: [OrderStatusEnum], $customerId: Int, $customersIn: [Int] $productId: Int) {
orders(where: {
statuses: $statuses,
customerId: $customerId,
2019-04-24 01:21:29 -04:00
customersIn: $customersIn,
productId: $productId,
orderby: { field: MENU_ORDER, order: ASC }
2019-10-17 21:39:38 -04:00
}) {
nodes {
id
2019-03-21 21:43:24 -04:00
}
}
}
';
2019-03-14 23:55:34 -04:00
2019-03-21 21:43:24 -04:00
/**
* Assertion One
2020-08-11 19:18:19 -04:00
*
* tests query with no without required capabilities
2019-03-21 21:43:24 -04:00
*/
$this->loginAsCustomer();
$response = $this->graphql( compact( 'query' ) );
$expected = array( $this->expectedObject( 'orders.nodes', array() ) );
2019-03-14 23:55:34 -04:00
$this->assertQuerySuccessful( $response, $expected );
2021-06-03 21:37:09 -04:00
$this->clearLoaderCache( 'wc_post' );
/**
* Assertion Two
2020-08-11 19:18:19 -04:00
*
* tests query with required capabilities
*/
$this->loginAsShopManager();
$response = $this->graphql( compact( 'query' ) );
$expected = array(
$this->expectedNode( 'orders.nodes', array( 'id' => $this->toRelayId( 'shop_order', $orders[0] ) ) ),
$this->expectedNode( 'orders.nodes', array( 'id' => $this->toRelayId( 'shop_order', $orders[1] ) ) ),
$this->not()->expectedNode( 'orders.nodes', array( 'id' => $this->toRelayId( 'shop_order', $old_orders[0] ) ) ),
);
$this->assertQuerySuccessful( $response, $expected );
2021-06-03 21:37:09 -04:00
$this->clearLoaderCache( 'wc_post' );
/**
* Assertion Three
2020-08-11 19:18:19 -04:00
*
* tests "statuses" where argument
*/
2019-04-24 01:21:29 -04:00
$variables = array( 'statuses' => array( 'COMPLETED' ) );
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = array(
2021-06-03 21:37:09 -04:00
$this->expectedNode( 'orders.nodes', array( 'id' => $this->toRelayId( 'shop_order', $orders[1] ) ) ),
);
$this->assertQuerySuccessful( $response, $expected );
2021-06-03 21:37:09 -04:00
$this->clearLoaderCache( 'wc_post' );
/**
* Assertion Four
2020-08-11 19:18:19 -04:00
*
* tests "customerId" where argument
*/
$variables = array( 'customerId' => $customer );
$response = $this->graphql( compact( 'query', 'variables' ) );
$this->assertQuerySuccessful( $response, $expected );
2021-06-03 21:37:09 -04:00
$this->clearLoaderCache( 'wc_post' );
/**
* Assertion Five
2020-08-11 19:18:19 -04:00
*
2019-04-24 01:21:29 -04:00
* tests "customerIn" where argument
*/
$variables = array( 'customersIn' => array( $customer ) );
$response = $this->graphql( compact( 'query', 'variables' ) );
2019-04-24 01:21:29 -04:00
$this->assertQuerySuccessful( $response, $expected );
2021-06-03 21:37:09 -04:00
$this->clearLoaderCache( 'wc_post' );
2019-04-24 01:21:29 -04:00
/**
* Assertion Six
2020-08-11 19:18:19 -04:00
*
* tests "productId" where argument
*/
$variables = array( 'productId' => $product );
$response = $this->graphql( compact( 'query', 'variables' ) );
$this->assertQuerySuccessful( $response, $expected );
2021-06-03 21:37:09 -04:00
$this->clearLoaderCache( 'wc_post' );
2020-08-11 19:18:19 -04:00
/**
* Assertion Seven
*
* tests `orders` query as existing customer, should return customer's
* orders only
*/
2021-06-03 21:37:09 -04:00
$this->loginAs( $customer );
$response = $this->graphql( compact( 'query' ) );
2020-08-11 19:18:19 -04:00
$this->assertQuerySuccessful( $response, $expected );
2021-06-03 21:37:09 -04:00
$this->clearLoaderCache( 'wc_post' );
2019-03-21 21:43:24 -04:00
}
}