Files

209 lines
4.9 KiB
PHP
Raw Permalink Normal View History

2021-07-05 13:51:52 -04:00
<?php
class CouponMutationsTest extends \Tests\WPGraphQL\WooCommerce\TestCase\WooGraphQLTestCase {
// Tests
public function testCreateCoupon() {
$query = '
2021-07-05 13:51:52 -04:00
mutation($input: CreateCouponInput!) {
createCoupon(input: $input) {
coupon {
id
databaseId
code
amount
discountType
}
}
}
';
$variables = [
'input' => [
2021-07-05 13:51:52 -04:00
'clientMutationId' => 'some_id',
'code' => 'testcode',
'amount' => 0.25,
'discountType' => 'PERCENT',
],
];
2021-07-05 13:51:52 -04:00
/**
* Assertion One
*
* Expect mutation to failed due to lack of capabilities
*/
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
2021-07-05 13:51:52 -04:00
$this->expectedErrorPath( 'createCoupon' ),
$this->expectedField( 'createCoupon', static::IS_NULL ),
];
2021-07-05 13:51:52 -04:00
$this->assertQueryError( $response, $expected );
2021-07-05 13:51:52 -04:00
/**
* Assertion Two
*
* Try again as an authenticated customer and expect continued failure.
*/
$this->loginAsCustomer();
$response = $this->graphql( compact( 'query', 'variables' ) );
$this->assertQueryError( $response, $expected );
/**
* Assertion Three
*
* Try as shop manager and expect mutation to succeed
*/
$this->loginAsShopManager();
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
2021-07-05 13:51:52 -04:00
$this->expectedObject(
'createCoupon.coupon',
[
$this->expectedField( 'id', static::NOT_FALSY ),
$this->expectedField( 'databaseId', static::NOT_FALSY ),
2021-07-05 13:51:52 -04:00
$this->expectedField( 'code', 'testcode' ),
$this->expectedField( 'amount', 0.25 ),
$this->expectedField( 'discountType', 'PERCENT' ),
]
2021-07-05 13:51:52 -04:00
),
];
2021-07-05 13:51:52 -04:00
$this->assertQuerySuccessful( $response, $expected );
}
2021-07-05 13:51:52 -04:00
public function testUpdateCoupon() {
$coupon_id = $this->factory->coupon->create();
2021-07-05 13:51:52 -04:00
$query = '
2021-07-05 13:51:52 -04:00
mutation($input: UpdateCouponInput!) {
updateCoupon(input: $input) {
coupon {
id
databaseId
code
2022-06-24 17:42:05 -04:00
amount
discountType
2021-07-05 13:51:52 -04:00
}
}
}
';
$variables = [
'input' => [
2021-07-05 13:51:52 -04:00
'clientMutationId' => 'some_id',
'id' => $this->toRelayId( 'shop_coupon', $coupon_id ),
'code' => 'blahblah',
'amount' => 0.25,
'discountType' => 'PERCENT',
],
];
2021-07-05 13:51:52 -04:00
/**
* Assertion One
*
* Expect mutation to failed due to lack of capabilities
*/
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
2021-07-05 13:51:52 -04:00
$this->expectedErrorPath( 'updateCoupon' ),
$this->expectedField( 'updateCoupon', static::IS_NULL ),
];
2021-07-05 13:51:52 -04:00
$this->assertQueryError( $response, $expected );
2021-07-05 13:51:52 -04:00
/**
* Assertion Two
*
* Try again as an authenticated customer and expect continued failure.
*/
$this->loginAsCustomer();
$response = $this->graphql( compact( 'query', 'variables' ) );
$this->assertQueryError( $response, $expected );
/**
* Assertion Three
*
* Try as shop manager and expect mutation to succeed
*/
$this->loginAsShopManager();
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
2021-07-05 13:51:52 -04:00
$this->expectedObject(
'updateCoupon.coupon',
[
2021-07-05 13:51:52 -04:00
$this->expectedField( 'id', $this->toRelayId( 'shop_coupon', $coupon_id ) ),
$this->expectedField( 'databaseId', $coupon_id ),
$this->expectedField( 'code', 'blahblah' ),
$this->expectedField( 'amount', 0.25 ),
$this->expectedField( 'discountType', 'PERCENT' ),
]
2021-07-05 13:51:52 -04:00
),
];
2021-07-05 13:51:52 -04:00
$this->assertQuerySuccessful( $response, $expected );
}
2021-07-05 13:51:52 -04:00
public function testDeleteCoupon() {
$coupon_id = $this->factory->coupon->create();
2021-07-05 13:51:52 -04:00
$query = '
mutation($input: DeleteCouponInput!) {
deleteCoupon(input: $input) {
coupon {
id
databaseId
}
}
}
';
$variables = [
'input' => [
2021-07-05 13:51:52 -04:00
'clientMutationId' => 'some_id',
'id' => $this->toRelayId( 'shop_coupon', $coupon_id ),
],
];
2021-07-05 13:51:52 -04:00
/**
* Assertion One
*
* Expect mutation to failed due to lack of capabilities
*/
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
2021-07-05 13:51:52 -04:00
$this->expectedErrorPath( 'deleteCoupon' ),
$this->expectedField( 'deleteCoupon', static::IS_NULL ),
];
2021-07-05 13:51:52 -04:00
$this->assertQueryError( $response, $expected );
/**
* Assertion Two
*
* Try again as an authenticated customer and expect continued failure.
*/
$this->loginAsCustomer();
$response = $this->graphql( compact( 'query', 'variables' ) );
$this->assertQueryError( $response, $expected );
/**
* Assertion Three
*
* Try as shop manager and expect mutation to succeed
*/
$this->loginAsShopManager();
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
2021-07-05 13:51:52 -04:00
$this->expectedObject(
'deleteCoupon.coupon',
[
2021-07-05 13:51:52 -04:00
$this->expectedField( 'id', $this->toRelayId( 'shop_coupon', $coupon_id ) ),
$this->expectedField( 'databaseId', $coupon_id ),
]
2021-07-05 13:51:52 -04:00
),
];
2021-07-05 13:51:52 -04:00
$this->assertQuerySuccessful( $response, $expected );
}
2021-07-05 13:51:52 -04:00
}