2019-03-14 23:55:34 -04:00
|
|
|
<?php
|
|
|
|
|
|
2019-04-13 00:46:16 -04:00
|
|
|
use GraphQLRelay\Relay;
|
2021-06-04 00:05:24 -04:00
|
|
|
class RefundQueriesTest extends \Tests\WPGraphQL\WooCommerce\TestCase\WooGraphQLTestCase {
|
2019-03-14 23:55:34 -04:00
|
|
|
|
2021-06-04 00:05:24 -04:00
|
|
|
public function expectedRefundData( $refund_id ) {
|
|
|
|
|
$refund = \wc_get_order( $refund_id );
|
2019-03-14 23:55:34 -04:00
|
|
|
|
2021-06-04 00:05:24 -04:00
|
|
|
return array(
|
2021-07-05 13:51:52 -04:00
|
|
|
$this->expectedField( 'refund.id', $this->toRelayId( 'shop_order_refund', $refund_id ) ),
|
|
|
|
|
$this->expectedField( 'refund.databaseId', $refund->get_id() ),
|
|
|
|
|
$this->expectedField( 'refund.title', $refund->get_post_title() ),
|
|
|
|
|
$this->expectedField( 'refund.reason', $refund->get_reason() ),
|
|
|
|
|
$this->expectedField( 'refund.amount', floatval( $refund->get_amount() ) ),
|
|
|
|
|
$this->expectedField( 'refund.refundedBy.databaseId', $refund->get_refunded_by() ),
|
|
|
|
|
$this->expectedField( 'refund.date', (string) $refund->get_date_modified() ),
|
2020-01-24 23:40:50 -05:00
|
|
|
);
|
2019-03-21 21:43:24 -04:00
|
|
|
}
|
2019-03-14 23:55:34 -04:00
|
|
|
|
2019-03-21 21:43:24 -04:00
|
|
|
// tests
|
|
|
|
|
public function testRefundQuery() {
|
2021-06-04 00:05:24 -04:00
|
|
|
$customer_id = $this->factory->customer->create();
|
|
|
|
|
$order_id = $this->factory->order->createNew( array( 'customer_id' => $customer_id ) );
|
|
|
|
|
$refund_id = $this->factory->refund->createNew( $order_id );
|
|
|
|
|
$relay_id = $this->toRelayId( 'shop_order_refund', $refund_id );
|
2019-04-17 14:18:35 -04:00
|
|
|
|
|
|
|
|
$query = '
|
2020-01-12 00:24:56 -05:00
|
|
|
query ( $id: ID! ) {
|
2019-04-17 14:18:35 -04:00
|
|
|
refund( id: $id ) {
|
2019-04-05 14:46:59 -04:00
|
|
|
id
|
2020-09-16 16:32:14 -04:00
|
|
|
databaseId
|
2019-04-05 14:46:59 -04:00
|
|
|
title
|
2019-03-21 21:43:24 -04:00
|
|
|
reason
|
2019-04-05 14:46:59 -04:00
|
|
|
amount
|
2019-03-21 21:43:24 -04:00
|
|
|
refundedBy {
|
2021-06-14 11:40:42 -04:00
|
|
|
databaseId
|
2019-03-21 21:43:24 -04:00
|
|
|
}
|
2021-03-06 04:31:26 +01:00
|
|
|
date
|
2019-03-21 21:43:24 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
';
|
2019-03-14 23:55:34 -04:00
|
|
|
|
2019-04-17 14:18:35 -04:00
|
|
|
/**
|
|
|
|
|
* Assertion One
|
2020-09-16 16:32:14 -04:00
|
|
|
*
|
2019-04-17 14:18:35 -04:00
|
|
|
* Test query and failed results for users lacking required caps
|
|
|
|
|
*/
|
2021-06-04 00:05:24 -04:00
|
|
|
$this->loginAs( $customer_id );
|
|
|
|
|
$variables = array( 'id' => $relay_id );
|
|
|
|
|
$response = $this->graphql( compact( 'query', 'variables' ) );
|
2019-04-17 14:18:35 -04:00
|
|
|
$expected = array(
|
2021-07-05 13:51:52 -04:00
|
|
|
$this->expectedField( 'refund.id', $relay_id ),
|
|
|
|
|
$this->expectedField( 'refund.databaseId', $refund_id ),
|
|
|
|
|
$this->expectedField( 'refund.title', self::IS_NULL ),
|
|
|
|
|
$this->expectedField( 'refund.reason', self::IS_NULL ),
|
|
|
|
|
$this->expectedField( 'refund.amount', self::IS_NULL ),
|
|
|
|
|
$this->expectedField( 'refund.refundedBy', self::IS_NULL ),
|
|
|
|
|
$this->expectedField( 'refund.date', self::IS_NULL ),
|
2019-04-17 14:18:35 -04:00
|
|
|
);
|
|
|
|
|
|
2021-06-04 00:05:24 -04:00
|
|
|
$this->assertQuerySuccessful( $response, $expected );
|
2019-04-17 14:18:35 -04:00
|
|
|
|
2021-06-04 00:05:24 -04:00
|
|
|
// Clear wc_post loader cache.
|
|
|
|
|
$this->clearLoaderCache( 'wc_post' );
|
2019-03-14 23:55:34 -04:00
|
|
|
|
2019-03-21 21:43:24 -04:00
|
|
|
/**
|
2019-04-17 14:18:35 -04:00
|
|
|
* Assertion Two
|
2020-09-16 16:32:14 -04:00
|
|
|
*
|
2019-04-17 14:18:35 -04:00
|
|
|
* Test query and results for users with required caps
|
2019-03-21 21:43:24 -04:00
|
|
|
*/
|
2021-06-04 00:05:24 -04:00
|
|
|
$this->loginAsShopManager();
|
|
|
|
|
$response = $this->graphql( compact( 'query', 'variables' ) );
|
|
|
|
|
$expected = $this->expectedRefundData( $refund_id );
|
2019-03-14 23:55:34 -04:00
|
|
|
|
2021-06-04 00:05:24 -04:00
|
|
|
$this->assertQuerySuccessful( $response, $expected );
|
2019-03-21 21:43:24 -04:00
|
|
|
}
|
2019-03-14 23:55:34 -04:00
|
|
|
|
2020-01-24 23:40:50 -05:00
|
|
|
public function testRefundQueryAndIds() {
|
2021-06-04 00:05:24 -04:00
|
|
|
$customer_id = $this->factory->customer->create();
|
|
|
|
|
$order_id = $this->factory->order->createNew( array( 'customer_id' => $customer_id ) );
|
|
|
|
|
$refund_id = $this->factory->refund->createNew( $order_id );
|
|
|
|
|
$relay_id = $this->toRelayId( 'shop_order_refund', $refund_id );
|
2019-04-17 14:18:35 -04:00
|
|
|
|
2019-03-21 21:43:24 -04:00
|
|
|
$query = '
|
2020-01-24 23:40:50 -05:00
|
|
|
query ( $id: ID!, $idType: RefundIdTypeEnum ) {
|
|
|
|
|
refund( id: $id, idType: $idType ) {
|
2019-04-17 14:18:35 -04:00
|
|
|
id
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Assertion One
|
2020-09-16 16:32:14 -04:00
|
|
|
*
|
2019-04-17 14:18:35 -04:00
|
|
|
* Test query and "id" argument
|
|
|
|
|
*/
|
2021-06-04 00:05:24 -04:00
|
|
|
$this->loginAs( $customer_id );
|
2020-01-24 23:40:50 -05:00
|
|
|
$variables = array(
|
2021-06-04 00:05:24 -04:00
|
|
|
'id' => $relay_id,
|
2020-01-24 23:40:50 -05:00
|
|
|
'idType' => 'ID',
|
|
|
|
|
);
|
2021-06-04 00:05:24 -04:00
|
|
|
$response = $this->graphql( compact( 'query', 'variables' ) );
|
2021-07-05 13:51:52 -04:00
|
|
|
$expected = array( $this->expectedField( 'refund.id', $relay_id ) );
|
2019-04-17 14:18:35 -04:00
|
|
|
|
2021-06-04 00:05:24 -04:00
|
|
|
$this->assertQuerySuccessful( $response, $expected );
|
2019-04-17 14:18:35 -04:00
|
|
|
|
2021-06-04 00:05:24 -04:00
|
|
|
$this->clearLoaderCache( 'wc_post' );
|
2019-04-17 14:18:35 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Assertion Two
|
2020-09-16 16:32:14 -04:00
|
|
|
*
|
2019-04-17 14:18:35 -04:00
|
|
|
* Test query and "refundId" argument
|
|
|
|
|
*/
|
2020-01-24 23:40:50 -05:00
|
|
|
$variables = array(
|
2021-06-04 00:05:24 -04:00
|
|
|
'id' => $refund_id,
|
2020-01-24 23:40:50 -05:00
|
|
|
'idType' => 'DATABASE_ID',
|
|
|
|
|
);
|
2021-06-04 00:05:24 -04:00
|
|
|
$response = $this->graphql( compact( 'query', 'variables' ) );
|
|
|
|
|
// Same $expected as last assertion.
|
2019-04-17 14:18:35 -04:00
|
|
|
|
2021-06-04 00:05:24 -04:00
|
|
|
$this->assertQuerySuccessful( $response, $expected );
|
2019-04-17 14:18:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRefundsQueryAndWhereArgs() {
|
2021-06-04 00:05:24 -04:00
|
|
|
$order_id = $this->factory->order->createNew();
|
2019-04-17 14:18:35 -04:00
|
|
|
$refunds = array(
|
2021-06-04 00:05:24 -04:00
|
|
|
$this->factory->refund->createNew( $order_id ),
|
|
|
|
|
$this->factory->refund->createNew( $this->factory->order->createNew() ),
|
|
|
|
|
$this->factory->refund->createNew( $this->factory->order->createNew(), array( 'status' => 'pending' ) ),
|
|
|
|
|
$this->factory->refund->createNew( $this->factory->order->createNew() ),
|
2019-04-17 14:18:35 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$query = '
|
2020-01-12 00:24:56 -05:00
|
|
|
query ( $statuses: [String], $orderIn: [Int] ) {
|
2019-04-17 17:13:39 -04:00
|
|
|
refunds( where: {
|
|
|
|
|
statuses: $statuses,
|
|
|
|
|
orderIn: $orderIn,
|
|
|
|
|
} ) {
|
2019-03-21 21:43:24 -04:00
|
|
|
nodes {
|
2021-06-04 00:05:24 -04:00
|
|
|
databaseId
|
2019-03-21 21:43:24 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
';
|
2019-03-14 23:55:34 -04:00
|
|
|
|
2019-04-17 14:18:35 -04:00
|
|
|
/**
|
|
|
|
|
* Assertion One
|
2020-09-16 16:32:14 -04:00
|
|
|
*
|
2019-04-17 14:18:35 -04:00
|
|
|
* Test query and failed results for users lacking required caps
|
|
|
|
|
*/
|
2021-06-04 00:05:24 -04:00
|
|
|
$this->loginAsCustomer();
|
|
|
|
|
$response = $this->graphql( compact( 'query' ) );
|
|
|
|
|
$expected = array(
|
2021-07-05 13:51:52 -04:00
|
|
|
$this->expectedField( 'refunds.nodes', array() ),
|
2021-06-04 00:05:24 -04:00
|
|
|
);
|
2019-04-17 14:18:35 -04:00
|
|
|
|
2021-06-04 00:05:24 -04:00
|
|
|
$this->assertQuerySuccessful( $response, $expected );
|
2019-04-17 14:18:35 -04:00
|
|
|
|
2021-06-04 00:05:24 -04:00
|
|
|
$this->clearLoaderCache( 'wc_post' );
|
2019-03-14 23:55:34 -04:00
|
|
|
|
2019-03-21 21:43:24 -04:00
|
|
|
/**
|
2019-04-17 14:18:35 -04:00
|
|
|
* Assertion Two
|
2020-09-16 16:32:14 -04:00
|
|
|
*
|
2019-04-17 14:18:35 -04:00
|
|
|
* Test query and results for users with required caps
|
2019-03-21 21:43:24 -04:00
|
|
|
*/
|
2021-06-04 00:05:24 -04:00
|
|
|
$this->loginAsShopManager();
|
|
|
|
|
$response = $this->graphql( compact( 'query' ) );
|
2020-01-12 00:24:56 -05:00
|
|
|
$expected = array(
|
2021-06-04 00:05:24 -04:00
|
|
|
$this->expectedNode( 'refunds.nodes', array( 'databaseId' => $refunds[0] ) ),
|
|
|
|
|
$this->expectedNode( 'refunds.nodes', array( 'databaseId' => $refunds[1] ) ),
|
|
|
|
|
$this->expectedNode( 'refunds.nodes', array( 'databaseId' => $refunds[2] ) ),
|
|
|
|
|
$this->expectedNode( 'refunds.nodes', array( 'databaseId' => $refunds[3] ) ),
|
2019-04-17 14:18:35 -04:00
|
|
|
);
|
2019-03-14 23:55:34 -04:00
|
|
|
|
2021-06-04 00:05:24 -04:00
|
|
|
$this->assertQuerySuccessful( $response, $expected );
|
2019-04-17 14:18:35 -04:00
|
|
|
|
2021-06-04 00:05:24 -04:00
|
|
|
$this->clearLoaderCache( 'wc_post' );
|
2019-04-17 14:18:35 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Assertion Three
|
2020-09-16 16:32:14 -04:00
|
|
|
*
|
2019-04-17 14:18:35 -04:00
|
|
|
* Test "statuses" where argument results should be empty
|
2019-04-18 00:06:05 -04:00
|
|
|
* Note: This argument is functionally useless Refunds' "post_status" is always set to "completed".
|
2019-04-17 14:18:35 -04:00
|
|
|
*/
|
2019-04-18 00:06:05 -04:00
|
|
|
$variables = array( 'statuses' => array( 'completed' ) );
|
2021-06-04 00:05:24 -04:00
|
|
|
$response = $this->graphql( compact( 'query', 'variables' ) );
|
|
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
|
$this->expectedNode( 'refunds.nodes', array( 'databaseId' => $refunds[0] ) ),
|
|
|
|
|
$this->expectedNode( 'refunds.nodes', array( 'databaseId' => $refunds[1] ) ),
|
|
|
|
|
$this->expectedNode( 'refunds.nodes', array( 'databaseId' => $refunds[3] ) ),
|
2019-04-17 14:18:35 -04:00
|
|
|
);
|
|
|
|
|
|
2021-06-04 00:05:24 -04:00
|
|
|
$this->assertQuerySuccessful( $response, $expected );
|
2019-04-17 14:18:35 -04:00
|
|
|
|
2021-06-04 00:05:24 -04:00
|
|
|
$this->clearLoaderCache( 'wc_post' );
|
2019-04-17 14:18:35 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Assertion Four
|
2020-09-16 16:32:14 -04:00
|
|
|
*
|
2019-04-17 14:18:35 -04:00
|
|
|
* Test "orderIn" where argument
|
|
|
|
|
*/
|
2021-06-04 00:05:24 -04:00
|
|
|
$variables = array( 'orderIn' => array( $order_id ) );
|
|
|
|
|
$response = $this->graphql( compact( 'query', 'variables' ) );
|
|
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
|
$this->expectedNode( 'refunds.nodes', array( 'databaseId' => $refunds[0] ) ),
|
2019-04-17 14:18:35 -04:00
|
|
|
);
|
|
|
|
|
|
2021-06-04 00:05:24 -04:00
|
|
|
$this->assertQuerySuccessful( $response, $expected );
|
2019-03-21 21:43:24 -04:00
|
|
|
}
|
2020-01-12 00:24:56 -05:00
|
|
|
|
|
|
|
|
public function testOrderToRefundsConnection() {
|
2021-06-04 00:05:24 -04:00
|
|
|
$order_id = $this->factory->order->createNew();
|
|
|
|
|
$refunds = array(
|
|
|
|
|
$this->factory->refund->createNew( $order_id, array( 'amount' => 0.5 ) ),
|
|
|
|
|
$this->factory->refund->createNew( $order_id, array( 'status' => 'pending', 'amount' => 0.5 ) ),
|
|
|
|
|
$this->factory->refund->createNew( $this->factory->order->createNew() ),
|
2020-01-12 00:24:56 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$query = '
|
|
|
|
|
query ( $id: ID! ) {
|
|
|
|
|
order(id: $id) {
|
|
|
|
|
refunds {
|
|
|
|
|
nodes {
|
2021-06-04 00:05:24 -04:00
|
|
|
databaseId
|
2020-01-12 00:24:56 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
';
|
|
|
|
|
|
2021-06-04 00:05:24 -04:00
|
|
|
$this->loginAsShopManager();
|
|
|
|
|
$variables = array( 'id' => $this->toRelayId( 'shop_order', $order_id ) );
|
|
|
|
|
$response = $this->graphql( compact( 'query', 'variables' ) );
|
|
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
|
$this->expectedNode( 'order.refunds.nodes', array( 'databaseId' => $refunds[0] ) ),
|
|
|
|
|
$this->expectedNode( 'order.refunds.nodes', array( 'databaseId' => $refunds[1] ) ),
|
2020-01-12 00:24:56 -05:00
|
|
|
);
|
|
|
|
|
|
2021-06-04 00:05:24 -04:00
|
|
|
$this->assertQuerySuccessful( $response, $expected );
|
2020-01-12 00:24:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCustomerToRefundsConnection() {
|
2021-06-04 00:05:24 -04:00
|
|
|
$order_id = $this->factory->order->createNew( array( 'customer_id' => $this->customer ) );
|
|
|
|
|
$refunds = array(
|
|
|
|
|
$this->factory->refund->createNew( $this->factory->order->createNew() ),
|
|
|
|
|
$this->factory->refund->createNew( $order_id, array( 'amount' => 0.5 ) ),
|
|
|
|
|
$this->factory->refund->createNew( $order_id, array( 'status' => 'pending', 'amount' => 0.5 ) ),
|
2020-01-12 00:24:56 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$query = '
|
|
|
|
|
query {
|
|
|
|
|
customer {
|
|
|
|
|
refunds {
|
|
|
|
|
nodes {
|
2021-06-04 00:05:24 -04:00
|
|
|
databaseId
|
2020-01-12 00:24:56 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
';
|
|
|
|
|
|
2021-06-04 00:05:24 -04:00
|
|
|
$this->loginAsCustomer();
|
|
|
|
|
$response = $this->graphql( compact( 'query' ) );
|
|
|
|
|
|
2020-01-12 00:24:56 -05:00
|
|
|
$expected = array(
|
2021-06-04 00:05:24 -04:00
|
|
|
$this->not()->expectedNode( 'customer.refunds.nodes', array( 'databaseId' => $refunds[0] ) ),
|
|
|
|
|
$this->expectedNode( 'customer.refunds.nodes', array( 'databaseId' => $refunds[1] ) ),
|
|
|
|
|
$this->expectedNode( 'customer.refunds.nodes', array( 'databaseId' => $refunds[2] ) ),
|
2020-01-12 00:24:56 -05:00
|
|
|
);
|
|
|
|
|
|
2021-06-04 00:05:24 -04:00
|
|
|
$this->assertQuerySuccessful( $response, $expected );
|
2020-01-12 00:24:56 -05:00
|
|
|
}
|
2019-03-21 21:43:24 -04:00
|
|
|
}
|