Skip to content

Commit dc3d741

Browse files
committed
tests updated.
1 parent d68a615 commit dc3d741

File tree

4 files changed

+272
-8
lines changed

4 files changed

+272
-8
lines changed

bin/testing-entrypoint.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,8 @@ chmod 777 ${TESTS_OUTPUT}
6868
# Run tests
6969
run_tests ${SUITES}
7070

71-
# Fix codecoverage permissions and clean coverage.xml
71+
# Clean coverage.xml and clean up PCOV configurations.
7272
if [ -f "${TESTS_OUTPUT}/coverage.xml" ] && [[ "$COVERAGE" == "1" ]]; then
73-
echo 'Setting "coverage.xml" permissions'.
74-
chmod 777 -R "$TESTS_OUTPUT"/coverage.xml
75-
7673
echo 'Cleaning coverage.xml for deployment'.
7774
pattern="$PROJECT_DIR/"
7875
sed -i "s~$pattern~~g" "$TESTS_OUTPUT"/coverage.xml
@@ -85,6 +82,11 @@ if [ -f "${TESTS_OUTPUT}/coverage.xml" ] && [[ "$COVERAGE" == "1" ]]; then
8582
fi
8683
fi
8784

85+
# Set public test result files permissions.
86+
echo 'Setting output permissions'.
87+
chmod 777 -R "$TESTS_OUTPUT"/*
88+
89+
# Check results and exit accordingly.
8890
if [ -f "${TESTS_OUTPUT}/failed" ]; then
8991
echo "Uh oh, some went wrong."
9092
exit 1

tests/_support/Helper/GraphQLE2E.php

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,46 @@ public function removeCoupons( $input, $request_headers = array() ) {
365365
return $response;
366366
}
367367

368+
/**
369+
* Updates customers chosen shipping method.
370+
*
371+
* @param array $input
372+
* @param string $session_header
373+
* @return array
374+
*/
375+
public function updateShippingMethod( $input, $request_headers = array() ) {
376+
// updateShippingMethod mutation.
377+
$mutation = '
378+
mutation ($input: UpdateShippingMethodInput!){
379+
updateShippingMethod(input: $input) {
380+
cart {
381+
availableShippingMethods {
382+
packageDetails
383+
supportsShippingCalculator
384+
rates {
385+
id
386+
cost
387+
label
388+
}
389+
}
390+
chosenShippingMethod
391+
shippingTotal
392+
shippingTax
393+
subtotal
394+
subtotalTax
395+
total
396+
}
397+
}
398+
}
399+
';
400+
401+
// Send GraphQL request and get response.
402+
$response = $this->sendGraphQLRequest( $mutation, $input, $request_headers );
403+
404+
// Return response.
405+
return $response;
406+
}
407+
368408
/**
369409
* Place customer order.
370410
*
@@ -672,10 +712,66 @@ public function _setupStore() {
672712
'woocommerce_cart_calculate_fees',
673713
function() {
674714
$percentage = 0.01;
675-
$surcharge = ( WC()->cart->cart_contents_total + WC()->cart->shipping_total ) * $percentage;
676-
WC()->cart->add_fee( 'Surcharge', $surcharge, true, '' );
715+
$surcharge = ( \WC()->cart->cart_contents_total + \WC()->cart->shipping_total ) * $percentage;
716+
\WC()->cart->add_fee( 'Surcharge', $surcharge, true, '' );
677717
}
678718
);
719+
720+
// Create legacy flat rate shipping method.
721+
update_option(
722+
'woocommerce_flat_rate_settings',
723+
array(
724+
'enabled' => 'yes',
725+
'title' => 'Flat rate',
726+
'availability' => 'all',
727+
'countries' => '',
728+
'tax_status' => 'taxable',
729+
'cost' => '10',
730+
)
731+
);
732+
update_option( 'woocommerce_flat_rate', array() );
733+
734+
// Create legacy free shipping method.
735+
update_option(
736+
'woocommerce_free_shipping_settings',
737+
array(
738+
'enabled' => 'yes',
739+
'title' => 'Free shipping',
740+
'availability' => 'all',
741+
'countries' => '',
742+
)
743+
);
744+
update_option( 'woocommerce_free_shipping', array() );
745+
746+
// Load shipping methods.
747+
\WC_Cache_Helper::get_transient_version( 'shipping', true );
748+
\WC()->shipping()->load_shipping_methods();
749+
750+
// Create Shipping Zones.
751+
$zone = new \WC_Shipping_Zone();
752+
$zone->set_zone_name( 'Local' );
753+
$zone->set_zone_order( 1 );
754+
$zone->add_location( 'GB', 'country' );
755+
$zone->add_location( 'CB*', 'postcode' );
756+
$zone->save();
757+
758+
$zone = new \WC_Shipping_Zone();
759+
$zone->set_zone_name( 'Europe' );
760+
$zone->set_zone_order( 2 );
761+
$zone->add_location( 'EU', 'continent' );
762+
$zone->save();
763+
764+
$zone = new \WC_Shipping_Zone();
765+
$zone->set_zone_name( 'California' );
766+
$zone->set_zone_order( 3 );
767+
$zone->add_location( 'US:CA', 'state' );
768+
$zone->save();
769+
770+
$zone = new \WC_Shipping_Zone();
771+
$zone->set_zone_name( 'US' );
772+
$zone->set_zone_order( 4 );
773+
$zone->add_location( 'US', 'country' );
774+
$zone->save();
679775
}
680776

681777
/**

tests/functional/QLSessionHandlerCest.php

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,23 @@ public function testCartMutationsWithInvalidCartSessionToken( FunctionalTester $
341341

342342
$I->assertArrayHasKey( 'errors', $failed );
343343

344+
/**
345+
* Attempt to restore item to the cart with invalid session token.
346+
* GraphQL should throw an error and mutation will fail.
347+
*
348+
* @Note: No items have been removed from the cart in this session,
349+
* but mutation should failed before that becomes a factor.
350+
*/
351+
$failed = $I->updateShippingMethod(
352+
array(
353+
'clientMutationId' => 'someId',
354+
'shippingMethods' => array( 'legacy_flat_rate' ),
355+
),
356+
array( 'woocommerce-session' => "Session invalid-jwt-token-string" )
357+
);
358+
359+
$I->assertArrayHasKey( 'errors', $failed );
360+
344361
/**
345362
* Attempt to query cart with invalid session token.
346363
* GraphQL should throw an error and query will fail.
@@ -365,4 +382,153 @@ public function testCartMutationsWithInvalidCartSessionToken( FunctionalTester $
365382

366383
$I->assertArrayHasKey( 'errors', $failed );
367384
}
385+
386+
public function testCartSessionDataMutations( FunctionalTester $I ) {
387+
/**
388+
* Add item to the cart
389+
*/
390+
$success = $I->addToCart(
391+
array(
392+
'clientMutationId' => 'someId',
393+
'productId' => $this->product_catalog['socks'],
394+
'quantity' => 2,
395+
)
396+
);
397+
398+
$I->assertArrayNotHasKey( 'errors', $success );
399+
$I->assertArrayHasKey('data', $success );
400+
$I->assertArrayHasKey('addToCart', $success['data'] );
401+
$I->assertArrayHasKey('cartItem', $success['data']['addToCart'] );
402+
$I->assertArrayHasKey('key', $success['data']['addToCart']['cartItem'] );
403+
$cart_item_key = $success['data']['addToCart']['cartItem']['key'];
404+
405+
/**
406+
* Assert existence and validity of "woocommerce-session" HTTP header.
407+
*/
408+
$I->seeHttpHeaderOnce( 'woocommerce-session' );
409+
$session_token = $I->grabHttpHeader( 'woocommerce-session' );
410+
411+
// Decode token
412+
JWT::$leeway = 60;
413+
$token_data = ! empty( $session_token )
414+
? JWT::decode( $session_token, 'graphql-woo-cart-session', array( 'HS256' ) )
415+
: null;
416+
417+
$I->assertNotEmpty( $token_data );
418+
$I->assertNotEmpty( $token_data->iss );
419+
$I->assertNotEmpty( $token_data->iat );
420+
$I->assertNotEmpty( $token_data->nbf );
421+
$I->assertNotEmpty( $token_data->exp );
422+
$I->assertNotEmpty( $token_data->data );
423+
$I->assertNotEmpty( $token_data->data->customer_id );
424+
425+
$wp_url = getenv( 'WP_URL' );
426+
$I->assertEquals( $token_data->iss, $wp_url );
427+
428+
/**
429+
* Make a cart query request with "woocommerce-session" HTTP Header and confirm
430+
* correct cart contents and chosen and available shipping methods.
431+
*/
432+
$query = '
433+
query {
434+
cart {
435+
contents {
436+
nodes {
437+
key
438+
}
439+
}
440+
availableShippingMethods {
441+
packageDetails
442+
supportsShippingCalculator
443+
rates {
444+
id
445+
cost
446+
label
447+
}
448+
}
449+
chosenShippingMethod
450+
}
451+
}
452+
';
453+
454+
$actual = $I->sendGraphQLRequest( $query, null, array( 'woocommerce-session' => "Session {$session_token}" ) );
455+
$expected = array(
456+
'data' => array(
457+
'cart' => array(
458+
'contents' => array(
459+
'nodes' => array(
460+
array(
461+
'key' => $cart_item_key,
462+
),
463+
),
464+
),
465+
'availableShippingMethods' => array(
466+
array(
467+
'packageDetails' => 'socks ×2',
468+
'supportsShippingCalculator' => true,
469+
'rates' => array(
470+
array(
471+
'id' => 'legacy_flat_rate',
472+
'cost' => 10.00,
473+
'label' => 'Flat rate'
474+
),
475+
array(
476+
'id' => 'legacy_free_shipping',
477+
'cost' => 0,
478+
'label' => 'Free shipping'
479+
),
480+
)
481+
)
482+
),
483+
'chosenShippingMethod' => 'legacy_flat_rate'
484+
),
485+
),
486+
);
487+
488+
$I->assertEquals( $expected, $actual );
489+
490+
/**
491+
* Update shipping method to 'legacy_flat_rate' shipping.
492+
*/
493+
$mutation = '
494+
mutation ($input: UpdateShippingMethodInput!){
495+
updateShippingMethod(input: $input) {
496+
cart {
497+
availableShippingMethods {
498+
packageDetails
499+
supportsShippingCalculator
500+
rates {
501+
id
502+
cost
503+
label
504+
}
505+
}
506+
chosenShippingMethod
507+
shippingTotal
508+
shippingTax
509+
subtotal
510+
subtotalTax
511+
total
512+
}
513+
}
514+
}
515+
';
516+
517+
$success = $I->sendGraphQLRequest(
518+
$mutation,
519+
array(
520+
'clientMutationId' => 'someId',
521+
'shippingMethods' => array( 'legacy_free_shipping' ),
522+
),
523+
array( 'woocommerce-session' => "Session {$session_token}" )
524+
);
525+
526+
$I->assertArrayNotHasKey( 'errors', $success );
527+
$I->assertNotEmpty( $success['data'] );
528+
$I->assertNotEmpty( $success['data']['updateShippingMethod'] );
529+
$I->assertNotEmpty( $success['data']['updateShippingMethod']['cart'] );
530+
$cart = $success['data']['updateShippingMethod']['cart'];
531+
$I->assertNotEmpty( $cart['availableShippingMethods'] );
532+
$I->assertEquals( 'legacy_free_shipping', $cart['chosenShippingMethod'] );
533+
}
368534
}

tests/wpunit/ProductQueriesTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,11 +798,11 @@ public function testProductsQueryAndWhereArgs() {
798798
array(
799799
'and' => array(
800800
array(
801-
'taxonomy' => 'CATEGORY',
801+
'taxonomy' => 'PRODUCTCATEGORY',
802802
'terms' => array( 'category-three' ),
803803
),
804804
array(
805-
'taxonomy' => 'CATEGORY',
805+
'taxonomy' => 'PRODUCTCATEGORY',
806806
'terms' => array( 'category-four' ),
807807
'operator' => 'NOT_IN'
808808
),

0 commit comments

Comments
 (0)