mirror of
https://github.com/wp-graphql/wp-graphql-woocommerce.git
synced 2026-08-14 12:53:44 +02:00
tests updated.
This commit is contained in:
@@ -68,11 +68,8 @@ chmod 777 ${TESTS_OUTPUT}
|
||||
# Run tests
|
||||
run_tests ${SUITES}
|
||||
|
||||
# Fix codecoverage permissions and clean coverage.xml
|
||||
# Clean coverage.xml and clean up PCOV configurations.
|
||||
if [ -f "${TESTS_OUTPUT}/coverage.xml" ] && [[ "$COVERAGE" == "1" ]]; then
|
||||
echo 'Setting "coverage.xml" permissions'.
|
||||
chmod 777 -R "$TESTS_OUTPUT"/coverage.xml
|
||||
|
||||
echo 'Cleaning coverage.xml for deployment'.
|
||||
pattern="$PROJECT_DIR/"
|
||||
sed -i "s~$pattern~~g" "$TESTS_OUTPUT"/coverage.xml
|
||||
@@ -85,6 +82,11 @@ if [ -f "${TESTS_OUTPUT}/coverage.xml" ] && [[ "$COVERAGE" == "1" ]]; then
|
||||
fi
|
||||
fi
|
||||
|
||||
# Set public test result files permissions.
|
||||
echo 'Setting output permissions'.
|
||||
chmod 777 -R "$TESTS_OUTPUT"/*
|
||||
|
||||
# Check results and exit accordingly.
|
||||
if [ -f "${TESTS_OUTPUT}/failed" ]; then
|
||||
echo "Uh oh, some went wrong."
|
||||
exit 1
|
||||
|
||||
@@ -365,6 +365,46 @@ class GraphQLE2E extends \Codeception\Module {
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates customers chosen shipping method.
|
||||
*
|
||||
* @param array $input
|
||||
* @param string $session_header
|
||||
* @return array
|
||||
*/
|
||||
public function updateShippingMethod( $input, $request_headers = array() ) {
|
||||
// updateShippingMethod mutation.
|
||||
$mutation = '
|
||||
mutation ($input: UpdateShippingMethodInput!){
|
||||
updateShippingMethod(input: $input) {
|
||||
cart {
|
||||
availableShippingMethods {
|
||||
packageDetails
|
||||
supportsShippingCalculator
|
||||
rates {
|
||||
id
|
||||
cost
|
||||
label
|
||||
}
|
||||
}
|
||||
chosenShippingMethod
|
||||
shippingTotal
|
||||
shippingTax
|
||||
subtotal
|
||||
subtotalTax
|
||||
total
|
||||
}
|
||||
}
|
||||
}
|
||||
';
|
||||
|
||||
// Send GraphQL request and get response.
|
||||
$response = $this->sendGraphQLRequest( $mutation, $input, $request_headers );
|
||||
|
||||
// Return response.
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Place customer order.
|
||||
*
|
||||
@@ -672,10 +712,66 @@ class GraphQLE2E extends \Codeception\Module {
|
||||
'woocommerce_cart_calculate_fees',
|
||||
function() {
|
||||
$percentage = 0.01;
|
||||
$surcharge = ( WC()->cart->cart_contents_total + WC()->cart->shipping_total ) * $percentage;
|
||||
WC()->cart->add_fee( 'Surcharge', $surcharge, true, '' );
|
||||
$surcharge = ( \WC()->cart->cart_contents_total + \WC()->cart->shipping_total ) * $percentage;
|
||||
\WC()->cart->add_fee( 'Surcharge', $surcharge, true, '' );
|
||||
}
|
||||
);
|
||||
|
||||
// Create legacy flat rate shipping method.
|
||||
update_option(
|
||||
'woocommerce_flat_rate_settings',
|
||||
array(
|
||||
'enabled' => 'yes',
|
||||
'title' => 'Flat rate',
|
||||
'availability' => 'all',
|
||||
'countries' => '',
|
||||
'tax_status' => 'taxable',
|
||||
'cost' => '10',
|
||||
)
|
||||
);
|
||||
update_option( 'woocommerce_flat_rate', array() );
|
||||
|
||||
// Create legacy free shipping method.
|
||||
update_option(
|
||||
'woocommerce_free_shipping_settings',
|
||||
array(
|
||||
'enabled' => 'yes',
|
||||
'title' => 'Free shipping',
|
||||
'availability' => 'all',
|
||||
'countries' => '',
|
||||
)
|
||||
);
|
||||
update_option( 'woocommerce_free_shipping', array() );
|
||||
|
||||
// Load shipping methods.
|
||||
\WC_Cache_Helper::get_transient_version( 'shipping', true );
|
||||
\WC()->shipping()->load_shipping_methods();
|
||||
|
||||
// Create Shipping Zones.
|
||||
$zone = new \WC_Shipping_Zone();
|
||||
$zone->set_zone_name( 'Local' );
|
||||
$zone->set_zone_order( 1 );
|
||||
$zone->add_location( 'GB', 'country' );
|
||||
$zone->add_location( 'CB*', 'postcode' );
|
||||
$zone->save();
|
||||
|
||||
$zone = new \WC_Shipping_Zone();
|
||||
$zone->set_zone_name( 'Europe' );
|
||||
$zone->set_zone_order( 2 );
|
||||
$zone->add_location( 'EU', 'continent' );
|
||||
$zone->save();
|
||||
|
||||
$zone = new \WC_Shipping_Zone();
|
||||
$zone->set_zone_name( 'California' );
|
||||
$zone->set_zone_order( 3 );
|
||||
$zone->add_location( 'US:CA', 'state' );
|
||||
$zone->save();
|
||||
|
||||
$zone = new \WC_Shipping_Zone();
|
||||
$zone->set_zone_name( 'US' );
|
||||
$zone->set_zone_order( 4 );
|
||||
$zone->add_location( 'US', 'country' );
|
||||
$zone->save();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -341,6 +341,23 @@ class QLSessionHandlerCest {
|
||||
|
||||
$I->assertArrayHasKey( 'errors', $failed );
|
||||
|
||||
/**
|
||||
* Attempt to restore item to the cart with invalid session token.
|
||||
* GraphQL should throw an error and mutation will fail.
|
||||
*
|
||||
* @Note: No items have been removed from the cart in this session,
|
||||
* but mutation should failed before that becomes a factor.
|
||||
*/
|
||||
$failed = $I->updateShippingMethod(
|
||||
array(
|
||||
'clientMutationId' => 'someId',
|
||||
'shippingMethods' => array( 'legacy_flat_rate' ),
|
||||
),
|
||||
array( 'woocommerce-session' => "Session invalid-jwt-token-string" )
|
||||
);
|
||||
|
||||
$I->assertArrayHasKey( 'errors', $failed );
|
||||
|
||||
/**
|
||||
* Attempt to query cart with invalid session token.
|
||||
* GraphQL should throw an error and query will fail.
|
||||
@@ -365,4 +382,153 @@ class QLSessionHandlerCest {
|
||||
|
||||
$I->assertArrayHasKey( 'errors', $failed );
|
||||
}
|
||||
|
||||
public function testCartSessionDataMutations( FunctionalTester $I ) {
|
||||
/**
|
||||
* Add item to the cart
|
||||
*/
|
||||
$success = $I->addToCart(
|
||||
array(
|
||||
'clientMutationId' => 'someId',
|
||||
'productId' => $this->product_catalog['socks'],
|
||||
'quantity' => 2,
|
||||
)
|
||||
);
|
||||
|
||||
$I->assertArrayNotHasKey( 'errors', $success );
|
||||
$I->assertArrayHasKey('data', $success );
|
||||
$I->assertArrayHasKey('addToCart', $success['data'] );
|
||||
$I->assertArrayHasKey('cartItem', $success['data']['addToCart'] );
|
||||
$I->assertArrayHasKey('key', $success['data']['addToCart']['cartItem'] );
|
||||
$cart_item_key = $success['data']['addToCart']['cartItem']['key'];
|
||||
|
||||
/**
|
||||
* Assert existence and validity of "woocommerce-session" HTTP header.
|
||||
*/
|
||||
$I->seeHttpHeaderOnce( 'woocommerce-session' );
|
||||
$session_token = $I->grabHttpHeader( 'woocommerce-session' );
|
||||
|
||||
// Decode token
|
||||
JWT::$leeway = 60;
|
||||
$token_data = ! empty( $session_token )
|
||||
? JWT::decode( $session_token, 'graphql-woo-cart-session', array( 'HS256' ) )
|
||||
: null;
|
||||
|
||||
$I->assertNotEmpty( $token_data );
|
||||
$I->assertNotEmpty( $token_data->iss );
|
||||
$I->assertNotEmpty( $token_data->iat );
|
||||
$I->assertNotEmpty( $token_data->nbf );
|
||||
$I->assertNotEmpty( $token_data->exp );
|
||||
$I->assertNotEmpty( $token_data->data );
|
||||
$I->assertNotEmpty( $token_data->data->customer_id );
|
||||
|
||||
$wp_url = getenv( 'WP_URL' );
|
||||
$I->assertEquals( $token_data->iss, $wp_url );
|
||||
|
||||
/**
|
||||
* Make a cart query request with "woocommerce-session" HTTP Header and confirm
|
||||
* correct cart contents and chosen and available shipping methods.
|
||||
*/
|
||||
$query = '
|
||||
query {
|
||||
cart {
|
||||
contents {
|
||||
nodes {
|
||||
key
|
||||
}
|
||||
}
|
||||
availableShippingMethods {
|
||||
packageDetails
|
||||
supportsShippingCalculator
|
||||
rates {
|
||||
id
|
||||
cost
|
||||
label
|
||||
}
|
||||
}
|
||||
chosenShippingMethod
|
||||
}
|
||||
}
|
||||
';
|
||||
|
||||
$actual = $I->sendGraphQLRequest( $query, null, array( 'woocommerce-session' => "Session {$session_token}" ) );
|
||||
$expected = array(
|
||||
'data' => array(
|
||||
'cart' => array(
|
||||
'contents' => array(
|
||||
'nodes' => array(
|
||||
array(
|
||||
'key' => $cart_item_key,
|
||||
),
|
||||
),
|
||||
),
|
||||
'availableShippingMethods' => array(
|
||||
array(
|
||||
'packageDetails' => 'socks ×2',
|
||||
'supportsShippingCalculator' => true,
|
||||
'rates' => array(
|
||||
array(
|
||||
'id' => 'legacy_flat_rate',
|
||||
'cost' => 10.00,
|
||||
'label' => 'Flat rate'
|
||||
),
|
||||
array(
|
||||
'id' => 'legacy_free_shipping',
|
||||
'cost' => 0,
|
||||
'label' => 'Free shipping'
|
||||
),
|
||||
)
|
||||
)
|
||||
),
|
||||
'chosenShippingMethod' => 'legacy_flat_rate'
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$I->assertEquals( $expected, $actual );
|
||||
|
||||
/**
|
||||
* Update shipping method to 'legacy_flat_rate' shipping.
|
||||
*/
|
||||
$mutation = '
|
||||
mutation ($input: UpdateShippingMethodInput!){
|
||||
updateShippingMethod(input: $input) {
|
||||
cart {
|
||||
availableShippingMethods {
|
||||
packageDetails
|
||||
supportsShippingCalculator
|
||||
rates {
|
||||
id
|
||||
cost
|
||||
label
|
||||
}
|
||||
}
|
||||
chosenShippingMethod
|
||||
shippingTotal
|
||||
shippingTax
|
||||
subtotal
|
||||
subtotalTax
|
||||
total
|
||||
}
|
||||
}
|
||||
}
|
||||
';
|
||||
|
||||
$success = $I->sendGraphQLRequest(
|
||||
$mutation,
|
||||
array(
|
||||
'clientMutationId' => 'someId',
|
||||
'shippingMethods' => array( 'legacy_free_shipping' ),
|
||||
),
|
||||
array( 'woocommerce-session' => "Session {$session_token}" )
|
||||
);
|
||||
|
||||
$I->assertArrayNotHasKey( 'errors', $success );
|
||||
$I->assertNotEmpty( $success['data'] );
|
||||
$I->assertNotEmpty( $success['data']['updateShippingMethod'] );
|
||||
$I->assertNotEmpty( $success['data']['updateShippingMethod']['cart'] );
|
||||
$cart = $success['data']['updateShippingMethod']['cart'];
|
||||
$I->assertNotEmpty( $cart['availableShippingMethods'] );
|
||||
$I->assertEquals( 'legacy_free_shipping', $cart['chosenShippingMethod'] );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -798,11 +798,11 @@ class ProductQueriesTest extends \Codeception\TestCase\WPTestCase {
|
||||
array(
|
||||
'and' => array(
|
||||
array(
|
||||
'taxonomy' => 'CATEGORY',
|
||||
'taxonomy' => 'PRODUCTCATEGORY',
|
||||
'terms' => array( 'category-three' ),
|
||||
),
|
||||
array(
|
||||
'taxonomy' => 'CATEGORY',
|
||||
'taxonomy' => 'PRODUCTCATEGORY',
|
||||
'terms' => array( 'category-four' ),
|
||||
'operator' => 'NOT_IN'
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user