Files

383 lines
9.8 KiB
PHP
Raw Permalink Normal View History

2019-03-14 23:55:34 -04:00
<?php
2021-06-04 00:05:24 -04:00
class RefundQueriesTest extends \Tests\WPGraphQL\WooCommerce\TestCase\WooGraphQLTestCase {
public function expectedRefundData( $refund_id ) {
$refund = \wc_get_order( $refund_id );
2019-03-14 23:55:34 -04:00
return [
2023-06-21 13:21:29 -04:00
$this->expectedField( 'refund.id', $this->toRelayId( 'order', $refund_id ) ),
2021-07-05 13:51:52 -04:00
$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.date', (string) $refund->get_date_modified() ),
];
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() {
2023-06-21 13:21:29 -04:00
$customer_id = $this->factory->customer->create();
$invalid_customer_id = $this->factory->customer->create();
$order_id = $this->factory->order->createNew( [ 'customer_id' => $customer_id ] );
$this->loginAsShopManager();
$refund_id = $this->factory->refund->createNew( $order_id );
$refund = \wc_get_order( $refund_id );
$relay_id = $this->toRelayId( 'order', $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
*/
2023-06-21 13:21:29 -04:00
$this->loginAs( $invalid_customer_id );
$variables = [ 'id' => $relay_id ];
2021-06-04 00:05:24 -04:00
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
$this->expectedField( 'refund', static::IS_NULL ),
];
2019-04-17 14:18:35 -04:00
2023-06-21 13:21:29 -04:00
$this->assertQueryError( $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
*/
2023-06-21 13:21:29 -04:00
$this->loginAs( $customer_id );
$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 );
2023-06-21 13:21:29 -04:00
// Clear wc_post loader cache.
$this->clearLoaderCache( 'wc_post' );
/**
* Assertion Three
*
* Test query and results for shop managers
*/
$this->loginAs( 1 );
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = $this->expectedRefundData( $refund_id );
// $expected[] = $this->expectedField( 'refund.refundedBy.databaseId', $refund->get_refunded_by() );
2023-06-21 13:21:29 -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( [ 'customer_id' => $customer_id ] );
2021-06-04 00:05:24 -04:00
$refund_id = $this->factory->refund->createNew( $order_id );
2023-06-21 13:21:29 -04:00
$relay_id = $this->toRelayId( 'order', $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 );
$variables = [
2021-06-04 00:05:24 -04:00
'id' => $relay_id,
2020-01-24 23:40:50 -05:00
'idType' => 'ID',
];
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [ $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
*/
$variables = [
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();
$refunds = [
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(), [ 'status' => 'pending' ] ),
2021-06-04 00:05:24 -04:00
$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] ) {
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 = [
$this->expectedField( 'refunds.nodes', [] ),
];
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' ) );
$expected = [
$this->expectedNode( 'refunds.nodes', [ 'databaseId' => $refunds[0] ] ),
$this->expectedNode( 'refunds.nodes', [ 'databaseId' => $refunds[1] ] ),
$this->expectedNode( 'refunds.nodes', [ 'databaseId' => $refunds[2] ] ),
$this->expectedNode( 'refunds.nodes', [ 'databaseId' => $refunds[3] ] ),
];
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
*/
$variables = [ 'statuses' => [ 'completed' ] ];
$response = $this->graphql( compact( 'query', 'variables' ) );
2021-06-04 00:05:24 -04:00
$expected = [
$this->expectedNode( 'refunds.nodes', [ 'databaseId' => $refunds[0] ] ),
$this->expectedNode( 'refunds.nodes', [ 'databaseId' => $refunds[1] ] ),
$this->expectedNode( 'refunds.nodes', [ '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
*/
$variables = [ 'orderIn' => [ $order_id ] ];
$response = $this->graphql( compact( 'query', 'variables' ) );
2021-06-04 00:05:24 -04:00
$expected = [
$this->expectedNode( 'refunds.nodes', [ '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 = [
$this->factory->refund->createNew( $order_id, [ 'amount' => 0.5 ] ),
$this->factory->refund->createNew(
$order_id,
[
'status' => 'pending',
'amount' => 0.5,
]
),
2021-06-04 00:05:24 -04:00
$this->factory->refund->createNew( $this->factory->order->createNew() ),
];
2020-01-12 00:24:56 -05:00
$query = '
2020-01-12 00:24:56 -05:00
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();
2023-06-21 13:21:29 -04:00
$variables = [ 'id' => $this->toRelayId( 'order', $order_id ) ];
$response = $this->graphql( compact( 'query', 'variables' ) );
2021-06-04 00:05:24 -04:00
$expected = [
$this->expectedNode( 'order.refunds.nodes', [ 'databaseId' => $refunds[0] ] ),
$this->expectedNode( 'order.refunds.nodes', [ '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() {
$order_id = $this->factory->order->createNew( [ 'customer_id' => $this->customer ] );
$refunds = [
2021-06-04 00:05:24 -04:00
$this->factory->refund->createNew( $this->factory->order->createNew() ),
$this->factory->refund->createNew( $order_id, [ 'amount' => 0.5 ] ),
$this->factory->refund->createNew(
$order_id,
[
'status' => 'pending',
'amount' => 0.5,
]
),
];
2020-01-12 00:24:56 -05:00
$query = '
2020-01-12 00:24:56 -05:00
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' ) );
$expected = [
$this->not()->expectedNode( 'customer.refunds.nodes', [ 'databaseId' => $refunds[0] ] ),
$this->expectedNode( 'customer.refunds.nodes', [ 'databaseId' => $refunds[1] ] ),
$this->expectedNode( 'customer.refunds.nodes', [ '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
}
public function testCustomerRefundFieldsNotNull() {
$order_id = $this->factory->order->createNew( [ 'customer_id' => $this->customer ] );
$this->loginAsShopManager();
$refund_id = $this->factory->refund->createNew( $order_id, [ 'amount' => 0.5 ] );
$refund = \wc_get_order( $refund_id );
$relay_id = $this->toRelayId( 'order', $order_id );
/**
* Assertion One
*
* Customer can see refund fields via customer.refunds connection.
*/
$query = '
query {
customer {
refunds {
nodes {
databaseId
title
amount
reason
}
}
}
}
';
$this->loginAsCustomer();
$response = $this->graphql( compact( 'query' ) );
$expected = [
$this->expectedNode(
'customer.refunds.nodes',
[
'databaseId' => $refund->get_id(),
'title' => $refund->get_post_title(),
'amount' => floatval( $refund->get_amount() ),
'reason' => $refund->get_reason(),
]
),
];
$this->assertQuerySuccessful( $response, $expected );
// Clear loader cache.
$this->clearLoaderCache( 'wc_post' );
/**
* Assertion Two
*
* Customer can see refund fields via order.refunds connection.
*/
$query = '
query ( $id: ID! ) {
order( id: $id ) {
refunds {
nodes {
databaseId
title
amount
reason
}
}
}
}
';
$variables = [ 'id' => $relay_id ];
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
$this->expectedNode(
'order.refunds.nodes',
[
'databaseId' => $refund->get_id(),
'title' => $refund->get_post_title(),
'amount' => floatval( $refund->get_amount() ),
'reason' => $refund->get_reason(),
]
),
];
$this->assertQuerySuccessful( $response, $expected );
}
2019-03-21 21:43:24 -04:00
}