Files
wp-graphql-woocommerce/tests/wpunit/DownloadableItemQueriesTest.php
T
a7714d0cd2 Updates to be compatible with WPGraphQL v1.6.1 (#537)
* fix: Huge refactor across schema and unit tests

* - add shipping_method loader in class-core-schema-filters.php

* - Update class-wc-db-loader.php to respect the new SHIPPING_METHOD loader with a `load_shipping_method_from_id()` function

* - Convert Shipping_Method_Connection_Resolver to extend AbstractConnectionResolver

* - Replace resolver for Shipping Methods connection. Remove use of `resolveNode` and replace the resolver with the new connection class `get_connection()` call

* chore: Factory class refactored.

* - update the function that registers Product Types to the Schema to use the value from the `get_enabled_product_types()` as the Type name and the key as the fieldName

* chore: PaymentGatewayQueriesTest refactored.

* chore: WPCS compliance met

* chore: Syntax error fixed.

Co-authored-by: Jason Bahl <jasonbahl@mac.com>
2021-08-11 14:26:09 -04:00

379 lines
13 KiB
PHP

<?php
class DownloadableItemQueriesTest extends \Tests\WPGraphQL\WooCommerce\TestCase\WooGraphQLTestCase{
public function setUp(): void {
// before
parent::setUp();
update_option( 'woocommerce_downloads_grant_access_after_payment', 'yes' );
}
// tests
public function testOrderToDownloadableItemsQuery() {
$downloadable_product = $this->factory->product->createSimple(
array(
'downloadable' => true,
'downloads' => array( $this->factory->product->createDownload() )
)
);
$order_id = $this->factory->order->createNew(
array(
'status' => 'completed',
'customer_id' => $this->customer,
),
array(
'line_items' => array(
array(
'product' => $downloadable_product,
'qty' => 1,
),
),
)
);
// Force download permission updated.
wc_downloadable_product_permissions( $order_id, true );
$order = wc_get_order( $order_id );
$downloadable_items = $order->get_downloadable_items();
$query = '
query {
customer {
orders {
nodes {
downloadableItems(first: 1) {
nodes {
url
accessExpires
downloadId
downloadsRemaining
name
product {
databaseId
}
download {
downloadId
}
}
}
}
}
}
}
';
/**
* Assertion One
*
* tests query results
*/
$this->loginAsCustomer();
$response = $this->graphql( compact( 'query' ) );
$expected = array_map(
function( $item ) {
return $this->expectedNode(
'customer.orders.nodes',
array(
$this->expectedNode(
'downloadableItems.nodes',
array(
$this->expectedField( 'url', $item['download_url'] ),
$this->expectedField( 'accessExpires', $item['access_expires'] ),
$this->expectedField( 'downloadId', $item['download_id'] ),
$this->expectedField(
'downloadsRemaining',
isset( $item['downloads_remaining'] ) && 'integer' === gettype( $item['downloads_remaining'] )
? $item['downloads_remaining']
: self::IS_NULL
),
$this->expectedField( 'name', $item['download_name'] ),
$this->expectedField( 'product.databaseId', $item['product_id'] ),
$this->expectedField( 'download.downloadId', $item['download_id'] ),
)
),
),
0
);
},
$downloadable_items
);
$this->assertQuerySuccessful( $response, $expected );
}
public function testOrderToDownloadableItemsQueryArgs() {
$valid_product = $this->factory->product->createSimple(
array(
'downloadable' => true,
'downloads' => array( $this->factory->product->createDownload() )
)
);
$downloadable_product = $this->factory->product->createSimple(
array(
'download_expiry' => 5,
'download_limit' => 3,
'downloadable' => true,
'downloads' => array( $this->factory->product->createDownload() )
)
);
$downloaded_product = $this->factory->product->createSimple(
array(
'download_limit' => 0,
'downloadable' => true,
'downloads' => array( $this->factory->product->createDownload() )
)
);
$order_id = $this->factory->order->createNew(
array(
'status' => 'completed',
'customer_id' => $this->customer,
),
array(
'line_items' => array(
array(
'product' => $valid_product,
'qty' => 1,
),
array(
'product' => $downloadable_product,
'qty' => 1,
),
array(
'product' => $downloaded_product,
'qty' => 1,
),
),
)
);
// Force download permission updated.
wc_downloadable_product_permissions( $order_id, true );
$query = '
query($active: Boolean, $hasDownloadsRemaining: Boolean) {
customer {
orders {
nodes {
downloadableItems(where: { active: $active, hasDownloadsRemaining: $hasDownloadsRemaining }) {
nodes {
product {
databaseId
}
}
}
}
}
}
}
';
/**
* Assertion One
*
* tests "active" whereArg
*/
$this->loginAsCustomer();
$variables = array( 'active' => true );
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = array(
$this->expectedNode(
'customer.orders.nodes',
array(
$this->expectedNode(
'downloadableItems.nodes',
array( $this->expectedField( 'product.databaseId', $valid_product ) )
),
$this->expectedNode(
'downloadableItems.nodes',
array( $this->expectedField( 'product.databaseId', $downloadable_product ) )
),
$this->not()->expectedNode(
'downloadableItems.nodes',
array( $this->expectedField( 'product.databaseId', $downloaded_product ) )
),
),
0
),
);
$this->assertQuerySuccessful( $response, $expected );
/**
* Assertion Two
*
* tests "active" whereArg reversed
*/
$variables = array( 'active' => false );
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = array(
$this->expectedNode(
'customer.orders.nodes',
array(
$this->expectedNode(
'downloadableItems.nodes',
array( $this->expectedField( 'product.databaseId', $downloaded_product ) )
),
$this->not()->expectedNode(
'downloadableItems.nodes',
array( $this->expectedField( 'product.databaseId', $valid_product ) )
),
$this->not()->expectedNode(
'downloadableItems.nodes',
array( $this->expectedField( 'product.databaseId', $downloadable_product ) )
),
),
0
),
);
$this->assertQuerySuccessful( $response, $expected );
/**
* Assertion Three
*
* tests "hasDownloadsRemaining" whereArg
*/
$variables = array( 'hasDownloadsRemaining' => true );
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = array(
$this->expectedNode(
'customer.orders.nodes',
array(
$this->not()->expectedNode(
'downloadableItems.nodes',
array( $this->expectedField( 'product.databaseId', $downloaded_product ) )
),
$this->expectedNode(
'downloadableItems.nodes',
array( $this->expectedField( 'product.databaseId', $valid_product ) )
),
$this->expectedNode(
'downloadableItems.nodes',
array( $this->expectedField( 'product.databaseId', $downloadable_product ) )
),
),
0
),
);
$this->assertQuerySuccessful( $response, $expected );
/**
* Assertion Four
*
* tests "hasDownloadsRemaining" whereArg reversed
*/
$variables = array( 'hasDownloadsRemaining' => false );
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = array(
$this->expectedNode(
'customer.orders.nodes',
array(
$this->expectedNode(
'downloadableItems.nodes',
array( $this->expectedField( 'product.databaseId', $downloaded_product ) )
),
$this->not()->expectedNode(
'downloadableItems.nodes',
array( $this->expectedField( 'product.databaseId', $valid_product ) )
),
$this->not()->expectedNode(
'downloadableItems.nodes',
array( $this->expectedField( 'product.databaseId', $downloadable_product ) )
),
),
0
),
);
$this->assertQuerySuccessful( $response, $expected );
}
public function testCustomerToDownloadableItemsQuery() {
$downloadable_product = $this->factory->product->createSimple(
array(
'downloadable' => true,
'downloads' => array( $this->factory->product->createDownload() )
)
);
$order_id = $this->factory->order->createNew(
array(
'status' => 'completed',
'customer_id' => $this->customer,
),
array(
'line_items' => array(
array(
'product' => $downloadable_product,
'qty' => 1,
),
),
)
);
// Force download permission updated.
wc_downloadable_product_permissions( $order_id, true );
$downloadable_items = \wc_get_customer_available_downloads( $this->customer );
$query = '
query {
customer {
downloadableItems(first: 1) {
nodes {
url
accessExpires
downloadId
downloadsRemaining
name
product {
databaseId
}
download {
downloadId
}
}
}
}
}
';
/**
* Assertion One
*
* tests query results
*/
$this->loginAsCustomer();
$response = $this->graphql( compact( 'query' ) );
$expected = array_map(
function( $item ) {
return $this->expectedNode(
'customer.downloadableItems.nodes',
array(
$this->expectedField( 'url', $item['download_url'] ),
$this->expectedField( 'accessExpires', $item['access_expires'] ),
$this->expectedField( 'downloadId', $item['download_id'] ),
$this->expectedField(
'downloadsRemaining',
isset( $item['downloads_remaining'] ) && 'integer' === gettype( $item['downloads_remaining'] )
? $item['downloads_remaining']
: self::IS_NULL
),
$this->expectedField( 'name', $item['download_name'] ),
$this->expectedField( 'product.databaseId', $item['product_id'] ),
$this->expectedField( 'download.downloadId', $item['download_id'] ),
),
0
);
},
$downloadable_items
);
$this->assertQuerySuccessful( $response, $expected );
}
}