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

431 lines
9.1 KiB
PHP
Raw Permalink Normal View History

2019-03-13 12:31:37 -04:00
<?php
use GraphQLRelay\Relay;
2019-03-21 21:43:24 -04:00
class OrderQueriesTest extends \Codeception\TestCase\WPTestCase {
private $shop_manager;
2019-04-05 14:46:59 -04:00
private $customer;
private $order;
private $order_helper;
private $product_helper;
private $customer_helper;
2019-03-13 12:31:37 -04:00
2019-03-21 21:43:24 -04:00
public function setUp() {
parent::setUp();
2019-03-13 12:31:37 -04:00
$this->shop_manager = $this->factory->user->create( array( 'role' => 'shop_manager' ) );
$this->customer = $this->factory->user->create( array( 'role' => 'customer' ) );
$this->order_helper = $this->getModule('\Helper\Wpunit')->order();
$this->product_helper = $this->getModule('\Helper\Wpunit')->product();
$this->customer_helper = $this->getModule('\Helper\Wpunit')->customer();
$this->order = $this->order_helper->create();
2019-03-21 21:43:24 -04:00
}
2019-03-13 12:31:37 -04:00
2019-03-21 21:43:24 -04:00
public function tearDown() {
// your tear down methods here
// then
parent::tearDown();
}
2019-03-13 12:31:37 -04:00
2019-03-21 21:43:24 -04:00
// tests
public function testOrderQuery() {
2019-10-17 21:39:38 -04:00
$id = Relay::toGlobalId( 'shop_order', $this->order );
2019-03-29 21:54:09 -04:00
$query = '
2019-10-17 21:39:38 -04:00
query ($id: ID!) {
order(id: $id) {
id
2019-03-21 21:43:24 -04:00
orderId
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 {
productId
}
download {
downloadId
}
}
2019-03-29 21:54:09 -04:00
}
needsPayment
needsProcessing
}
}
';
/**
* Assertion One
*
* tests query as customer, should return "null" because the customer isn't authorized.
*/
wp_set_current_user( $this->customer );
$variables = array( 'id' => $id );
2019-10-17 21:39:38 -04:00
$actual = graphql( array( 'query' => $query, 'variables' => $variables ) );
$expected = array( 'data' => array( 'order' => null ) );
// use --debug flag to view.
codecept_debug( $actual );
2019-11-13 18:53:31 -05:00
$this->assertEquals( $expected, $actual );
// Clear loader cache.
$this->getModule('\Helper\Wpunit')->clear_loader_cache( 'wc_post_crud' );
/**
* Assertion Two
*
* tests query as shop manager
*/
wp_set_current_user( $this->shop_manager );
$variables = array( 'id' => $id );
2019-10-17 21:39:38 -04:00
$actual = graphql( array( 'query' => $query, 'variables' => $variables ) );
$expected = array( 'data' => array( 'order' => $this->order_helper->print_query( $this->order ) ) );
// use --debug flag to view.
codecept_debug( $actual );
2019-11-13 18:53:31 -05:00
$this->assertEquals( $expected, $actual );
}
2020-01-24 23:40:50 -05:00
public function testOrderQueryAndIds() {
2019-10-17 21:39:38 -04:00
$id = Relay::toGlobalId( 'shop_order', $this->order );
$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.
wp_set_current_user( $this->shop_manager );
/**
* Assertion One
*
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',
);
$actual = graphql(
array(
'query' => $query,
'variables' => $variables
)
);
2020-01-12 00:24:56 -05:00
$expected = array( 'data' => array( 'order' => array( 'id' => $id ) ) );
// use --debug flag to view.
codecept_debug( $actual );
2019-11-13 18:53:31 -05:00
$this->assertEquals( $expected, $actual );
/**
* Assertion Two
*
2020-01-24 23:40:50 -05:00
* tests "DATABASE_ID" ID type.
*/
2020-01-24 23:40:50 -05:00
$variables = array(
'id' => $this->order,
'idType' => 'DATABASE_ID',
);
$actual = graphql(
array(
'query' => $query,
'variables' => $variables
)
);
2020-01-12 00:24:56 -05:00
$expected = array( 'data' => array( 'order' => array( 'id' => $id ) ) );
// use --debug flag to view.
codecept_debug( $actual );
2019-11-13 18:53:31 -05:00
$this->assertEquals( $expected, $actual );
/**
* Assertion Three
*
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->order_helper->get_order_key( $this->order ),
'idType' => 'ORDER_NUMBER',
);
$actual = graphql(
array(
'query' => $query,
'variables' => $variables
)
);
2020-01-12 00:24:56 -05:00
$expected = array( 'data' => array( 'order' => array( 'id' => $id ) ) );
// use --debug flag to view.
codecept_debug( $actual );
2019-11-13 18:53:31 -05:00
$this->assertEquals( $expected, $actual );
}
public function testOrdersQueryAndWhereArgs() {
$customer = $this->customer_helper->create();
$product = $this->product_helper->create_simple();
$orders = array(
$this->order,
$this->order_helper->create(
array(),
array(
'line_items' => array(
array(
'product' => $product,
'qty' => 4,
),
),
)
),
$this->order_helper->create(
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
*
* tests query with no without required capabilities
2019-03-21 21:43:24 -04:00
*/
wp_set_current_user( $this->customer );
2019-10-17 21:39:38 -04:00
$actual = graphql( array( 'query' => $query ) );
$expected = array( 'data' => array( 'orders' => array( 'nodes' => array() ) ) );
2019-03-14 23:55:34 -04:00
// use --debug flag to view.
codecept_debug( $actual );
$this->assertEquals( $expected, $actual );
/**
* Assertion Two
*
* tests query with required capabilities
*/
wp_set_current_user( $this->shop_manager );
2019-10-17 21:39:38 -04:00
$actual = graphql( array( 'query' => $query ) );
$expected = array(
'data' => array(
'orders' => array(
2019-04-24 01:21:29 -04:00
'nodes' => $this->order_helper->print_nodes( $orders ),
),
),
);
// use --debug flag to view.
codecept_debug( $actual );
$this->assertEquals( $expected, $actual );
/**
* Assertion Three
*
* tests "statuses" where argument
*/
2019-04-24 01:21:29 -04:00
$variables = array( 'statuses' => array( 'COMPLETED' ) );
2019-10-17 21:39:38 -04:00
$actual = graphql( array( 'query' => $query, 'variables' => $variables ) );
$expected = array(
'data' => array(
'orders' => array(
2019-04-24 01:21:29 -04:00
'nodes' => $this->order_helper->print_nodes(
$orders,
array(
'filter' => function( $id ) {
$order = new WC_Order( $id );
return $order->get_status() === 'completed';
},
)
2019-04-24 01:21:29 -04:00
),
),
),
);
// use --debug flag to view.
codecept_debug( $actual );
$this->assertEquals( $expected, $actual );
/**
* Assertion Four
*
* tests "customerId" where argument
*/
$variables = array( 'customerId' => $customer );
2019-10-17 21:39:38 -04:00
$actual = graphql( array( 'query' => $query, 'variables' => $variables ) );
$expected = array(
'data' => array(
'orders' => array(
2019-04-24 01:21:29 -04:00
'nodes' => $this->order_helper->print_nodes(
$orders,
array(
'filter' => function( $id ) use ( $customer ) {
$order = new WC_Order( $id );
return $order->get_customer_id() === $customer;
},
)
2019-04-24 01:21:29 -04:00
),
),
),
);
// use --debug flag to view.
codecept_debug( $actual );
$this->assertEquals( $expected, $actual );
/**
* Assertion Five
*
2019-04-24 01:21:29 -04:00
* tests "customerIn" where argument
*/
$variables = array( 'customersIn' => array( $customer ) );
2019-10-17 21:39:38 -04:00
$actual = graphql( array( 'query' => $query, 'variables' => $variables ) );
2019-04-24 01:21:29 -04:00
$expected = array(
'data' => array(
'orders' => array(
'nodes' => $this->order_helper->print_nodes(
$orders,
array(
'filter' => function( $id ) use ( $customer ) {
$order = new WC_Order( $id );
return $order->get_customer_id() === $customer;
},
)
),
),
),
);
// use --debug flag to view.
codecept_debug( $actual );
$this->assertEquals( $expected, $actual );
/**
* Assertion Six
*
* tests "productId" where argument
*/
$variables = array( 'productId' => $product );
2019-10-17 21:39:38 -04:00
$actual = graphql( array( 'query' => $query, 'variables' => $variables ) );
$expected = array(
'data' => array(
'orders' => array(
2019-04-24 01:21:29 -04:00
'nodes' => $this->order_helper->print_nodes(
$orders,
array(
'filter' => function( $id ) use ( $product ) {
return $this->order_helper->has_product( $id, $product );
},
)
2019-04-24 01:21:29 -04:00
),
),
),
);
// use --debug flag to view.
codecept_debug( $actual );
2019-03-14 23:55:34 -04:00
2019-03-21 21:43:24 -04:00
$this->assertEquals( $expected, $actual );
}
}