Files

682 lines
16 KiB
PHP
Raw Permalink Normal View History

2019-08-19 09:52:16 -04:00
<?php
2023-06-21 13:21:29 -04:00
class MetaDataQueriesTest extends \Tests\WPGraphQL\WooCommerce\TestCase\WooGraphQLTestCase {
// tests
public function testCartMetaDataQueries() {
// Create Variation Product.
$product_ids = $this->factory->product_variation->createSome();
// Create Cart Item with meta data.
$meta_data = [
'meta_1' => 'test_meta_1',
'meta_2' => 'test_meta_2',
];
2020-06-12 16:50:21 -04:00
// Add item to cart.
2023-06-21 13:21:29 -04:00
$cart_item_key = $this->factory->cart->add(
[
2023-06-21 13:21:29 -04:00
'product_id' => $product_ids['product'],
'quantity' => 2,
2023-06-21 13:21:29 -04:00
'variation_id' => $product_ids['variations'][0],
'variation' => [ 'attribute_pa_color' => 'red' ],
2023-06-21 13:21:29 -04:00
'cart_item_data' => $meta_data,
]
2020-06-12 16:50:21 -04:00
)[0];
2019-08-19 09:52:16 -04:00
$query = '
query($key: String, $keysIn: [String]) {
cart {
contents {
nodes {
2020-06-12 16:50:21 -04:00
key
extraData(key: $key, keysIn: $keysIn) {
key
value
}
}
}
}
}
';
/**
* Assertion One
*
* Query w/o filter
*/
2023-06-21 13:21:29 -04:00
$response = $this->graphql( compact( 'query' ) );
$expected = [
2023-06-21 13:21:29 -04:00
$this->expectedObject(
'cart.contents.nodes.0',
[
$this->expectedField( 'key', $cart_item_key ),
$this->expectedObject(
'extraData.#',
[
$this->expectedField( 'key', 'meta_1' ),
$this->expectedField( 'value', 'test_meta_1' ),
]
),
$this->expectedObject(
'extraData.#',
[
$this->expectedField( 'key', 'meta_2' ),
$this->expectedField( 'value', 'test_meta_2' ),
]
),
]
),
];
2023-06-21 13:21:29 -04:00
$this->assertQuerySuccessful( $response, $expected );
/**
* Assertion Two
*
* Query w/ "key" filter
*/
$variables = [ 'key' => 'meta_2' ];
2023-06-21 13:21:29 -04:00
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
2023-06-21 13:21:29 -04:00
$this->expectedObject(
'cart.contents.nodes.0',
[
$this->expectedField( 'key', $cart_item_key ),
$this->expectedObject(
'extraData.0',
[
$this->expectedField( 'key', 'meta_2' ),
$this->expectedField( 'value', 'test_meta_2' ),
]
),
$this->expectedObject(
'extraData.#',
[
$this->not()->expectedField( 'key', 'meta_1' ),
$this->not()->expectedField( 'value', 'test_meta_1' ),
]
),
]
),
];
2023-06-21 13:21:29 -04:00
$this->assertQuerySuccessful( $response, $expected );
/**
* Assertion Three
*
* Query w/ "keysIn" filter
*/
$variables = [ 'keysIn' => [ 'meta_2' ] ];
2023-06-21 13:21:29 -04:00
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
2023-06-21 13:21:29 -04:00
$this->expectedObject(
'cart.contents.nodes.0',
[
$this->expectedField( 'key', $cart_item_key ),
$this->expectedObject(
'extraData.0',
[
$this->expectedField( 'key', 'meta_2' ),
$this->expectedField( 'value', 'test_meta_2' ),
]
),
$this->expectedObject(
'extraData.#',
[
$this->not()->expectedField( 'key', 'meta_1' ),
$this->not()->expectedField( 'value', 'test_meta_1' ),
]
),
]
),
];
2023-06-21 13:21:29 -04:00
$this->assertQuerySuccessful( $response, $expected );
}
public function testCouponMetaDataQueries() {
2023-06-21 13:21:29 -04:00
// Create Coupon with meta data.
$coupon_id = $this->factory->coupon->create(
[
'meta_data' => [
[
'id' => 0,
'key' => 'meta_1',
'value' => 'test_meta_1',
],
[
'id' => 0,
'key' => 'meta_2',
'value' => 'test_meta_2',
],
[
'id' => 0,
'key' => 'meta_1',
'value' => 75,
],
],
]
);
$query = '
query ($id: ID!, $key: String, $keysIn: [String], $multiple: Boolean) {
2019-08-19 09:52:16 -04:00
coupon(id: $id) {
id
metaData(key: $key, keysIn: $keysIn, multiple: $multiple) {
key
value
}
2019-08-19 09:52:16 -04:00
}
}
';
2020-06-12 16:50:21 -04:00
/**
* Assertion One
*
* Query w/o filters
*/
wp_set_current_user( $this->shop_manager );
2023-06-21 13:21:29 -04:00
$variables = [ 'id' => $this->toRelayId( 'shop_coupon', $coupon_id ) ];
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
2023-06-21 13:21:29 -04:00
$this->expectedObject(
'coupon.metaData.#',
[
$this->expectedField( 'key', 'meta_1' ),
$this->expectedField( 'value', 'test_meta_1' ),
$this->not()->expectedField( 'value', 75 ),
]
),
$this->expectedObject(
'coupon.metaData.#',
[
$this->expectedField( 'key', 'meta_2' ),
$this->expectedField( 'value', 'test_meta_2' ),
]
),
];
2023-06-21 13:21:29 -04:00
$this->assertQuerySuccessful( $response, $expected );
/**
* Assertion Two
*
* Query w/ "key" filter
*/
$variables = [
2023-06-21 13:21:29 -04:00
'id' => $this->toRelayId( 'shop_coupon', $coupon_id ),
'key' => 'meta_2',
];
2023-06-21 13:21:29 -04:00
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
2023-06-21 13:21:29 -04:00
$this->expectedObject(
'coupon.metaData.0',
[
$this->expectedField( 'key', 'meta_2' ),
$this->expectedField( 'value', 'test_meta_2' ),
]
),
$this->expectedObject(
'coupon.metaData.#',
[
$this->not()->expectedField( 'key', 'meta_1' ),
$this->not()->expectedField( 'value', 'test_meta_1' ),
$this->not()->expectedField( 'value', '75' ),
]
),
];
2023-06-21 13:21:29 -04:00
$this->assertQuerySuccessful( $response, $expected );
/**
* Assertion Three
*
* Query w/ "keysIn" filter
*/
$variables = [
2023-06-21 13:21:29 -04:00
'id' => $this->toRelayId( 'shop_coupon', $coupon_id ),
'keysIn' => [ 'meta_2' ],
];
2023-06-21 13:21:29 -04:00
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
2023-06-21 13:21:29 -04:00
$this->expectedObject(
'coupon.metaData.0',
[
$this->expectedField( 'key', 'meta_2' ),
$this->expectedField( 'value', 'test_meta_2' ),
]
),
$this->expectedObject(
'coupon.metaData.#',
[
$this->not()->expectedField( 'key', 'meta_1' ),
$this->not()->expectedField( 'value', 'test_meta_1' ),
$this->not()->expectedField( 'value', '75' ),
]
),
];
2019-08-19 09:52:16 -04:00
2023-06-21 13:21:29 -04:00
$this->assertQuerySuccessful( $response, $expected );
/**
* Assertion Four
*
* Query w/ "key" filter and "multiple" set to true to get non-unique results.
*/
$variables = [
2023-06-21 13:21:29 -04:00
'id' => $this->toRelayId( 'shop_coupon', $coupon_id ),
'key' => 'meta_1',
'multiple' => true,
];
2023-06-21 13:21:29 -04:00
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
2023-06-21 13:21:29 -04:00
$this->expectedObject(
'coupon.metaData.#',
[
$this->expectedField( 'key', 'meta_1' ),
$this->expectedField( 'value', 'test_meta_1' ),
]
),
$this->expectedObject(
'coupon.metaData.#',
[
$this->expectedField( 'key', 'meta_1' ),
$this->expectedField( 'value', '75' ),
]
),
$this->expectedObject(
'coupon.metaData.#',
[
$this->not()->expectedField( 'key', 'meta_2' ),
$this->not()->expectedField( 'value', 'test_meta_2' ),
]
),
];
2023-06-21 13:21:29 -04:00
$this->assertQuerySuccessful( $response, $expected );
/**
* Assertion Five
*
* Query w/ "keysIn" filter and "multiple" set to true to get non-unique results.
*/
$variables = [
2023-06-21 13:21:29 -04:00
'id' => $this->toRelayId( 'shop_coupon', $coupon_id ),
'keysIn' => [ 'meta_1' ],
'multiple' => true,
];
2023-06-21 13:21:29 -04:00
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
2023-06-21 13:21:29 -04:00
$this->expectedObject(
'coupon.metaData.#',
[
$this->expectedField( 'key', 'meta_1' ),
$this->expectedField( 'value', 'test_meta_1' ),
]
),
$this->expectedObject(
'coupon.metaData.#',
[
$this->expectedField( 'key', 'meta_1' ),
$this->expectedField( 'value', '75' ),
]
),
$this->expectedObject(
'coupon.metaData.#',
[
$this->not()->expectedField( 'key', 'meta_2' ),
$this->not()->expectedField( 'value', 'test_meta_2' ),
]
),
];
2023-06-21 13:21:29 -04:00
$this->assertQuerySuccessful( $response, $expected );
/**
* Assertion Six
*
* Query w/o filters and "multiple" set to true to get non-unique results.
*/
$variables = [
2023-06-21 13:21:29 -04:00
'id' => $this->toRelayId( 'shop_coupon', $coupon_id ),
'multiple' => true,
];
2023-06-21 13:21:29 -04:00
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
2023-06-21 13:21:29 -04:00
$this->expectedObject(
'coupon.metaData.#',
[
$this->expectedField( 'key', 'meta_1' ),
$this->expectedField( 'value', 'test_meta_1' ),
]
),
$this->expectedObject(
'coupon.metaData.#',
[
$this->expectedField( 'key', 'meta_2' ),
$this->expectedField( 'value', 'test_meta_2' ),
]
),
$this->expectedObject(
'coupon.metaData.#',
[
$this->expectedField( 'key', 'meta_1' ),
$this->expectedField( 'value', '75' ),
]
),
];
2023-06-21 13:21:29 -04:00
$this->assertQuerySuccessful( $response, $expected );
}
2019-08-19 09:52:16 -04:00
public function testCustomerMetaDataQueries() {
2023-06-21 13:21:29 -04:00
// Create Customer with meta data.
$customer_id = $this->factory->customer->create(
[
'meta_data' => [
[
'id' => 0,
'key' => 'meta_1',
'value' => 'test_meta_1',
],
[
'id' => 0,
'key' => 'meta_2',
'value' => 'test_meta_2',
],
],
]
);
$query = '
2019-08-19 09:52:16 -04:00
query {
customer {
id
metaData {
key
value
}
2019-08-19 09:52:16 -04:00
}
}
';
2020-06-12 16:50:21 -04:00
/**
* Assertion One
*/
2023-06-21 13:21:29 -04:00
$this->loginAs( $customer_id );
$response = $this->graphql( compact( 'query' ) );
$expected = [
$this->expectedField( 'customer.id', $this->toRelayId( 'user', $customer_id ) ),
2023-06-21 13:21:29 -04:00
$this->expectedObject(
'customer.metaData.#',
[
$this->expectedField( 'key', 'meta_1' ),
$this->expectedField( 'value', 'test_meta_1' ),
]
),
$this->expectedObject(
'customer.metaData.#',
[
$this->expectedField( 'key', 'meta_2' ),
$this->expectedField( 'value', 'test_meta_2' ),
]
),
];
2023-06-21 13:21:29 -04:00
$this->assertQuerySuccessful( $response, $expected );
}
public function testOrderMetaDataQueries() {
2023-06-21 13:21:29 -04:00
// Create Order with meta data.
$meta_data = [
[
'id' => 0,
'key' => 'meta_1',
'value' => 'test_meta_1',
],
[
'id' => 0,
'key' => 'meta_2',
'value' => 'test_meta_2',
],
];
$customer_id = $this->factory->customer->create( [ 'meta_data' => $meta_data ] );
$order_id = $this->factory->order->createNew(
[
'customer_id' => $customer_id,
'meta_data' => $meta_data,
]
);
$this->factory->order->add_fee( $order_id, compact( 'meta_data' ) );
$query = '
query ($id: ID!) {
order(id: $id) {
id
metaData {
key
value
}
feeLines {
nodes {
metaData {
key
value
}
}
}
}
}
';
2023-06-21 13:21:29 -04:00
$this->loginAsShopManager();
2020-06-12 16:50:21 -04:00
/**
* Assertion One
*/
2023-06-21 13:21:29 -04:00
$variables = [ 'id' => $this->toRelayId( 'order', $order_id ) ];
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
2023-06-21 13:21:29 -04:00
$this->expectedField( 'order.id', $this->toRelayId( 'order', $order_id ) ),
$this->expectedObject(
'order.metaData.#',
[
$this->expectedField( 'key', 'meta_1' ),
$this->expectedField( 'value', 'test_meta_1' ),
]
),
$this->expectedObject(
'order.metaData.#',
[
$this->expectedField( 'key', 'meta_2' ),
$this->expectedField( 'value', 'test_meta_2' ),
]
),
$this->expectedObject(
'order.feeLines.nodes.0.metaData.#',
[
$this->expectedField( 'key', 'meta_1' ),
$this->expectedField( 'value', 'test_meta_1' ),
]
),
$this->expectedObject(
'order.feeLines.nodes.0.metaData.#',
[
$this->expectedField( 'key', 'meta_2' ),
$this->expectedField( 'value', 'test_meta_2' ),
]
),
];
2019-08-19 09:52:16 -04:00
2023-06-21 13:21:29 -04:00
$this->assertQuerySuccessful( $response, $expected );
}
2019-08-19 09:52:16 -04:00
public function testProductMetaDataQueries() {
2023-06-21 13:21:29 -04:00
// Create Product with meta data.
$meta_data = [
[
'id' => 0,
'key' => 'meta_1',
'value' => 'test_meta_1',
],
[
'id' => 0,
'key' => 'meta_2',
'value' => 'test_meta_2',
],
];
$product_id = $this->factory->product->createVariable( compact( 'meta_data' ) );
$query = '
2019-08-19 09:52:16 -04:00
query ($id: ID!) {
product(id: $id) {
2019-10-17 21:39:38 -04:00
... on VariableProduct {
id
metaData {
key
value
}
}
2019-08-19 09:52:16 -04:00
}
}
';
2020-06-12 16:50:21 -04:00
/**
* Assertion One
*/
$variables = [ 'id' => $this->toRelayId( 'post', $product_id ) ];
2023-06-21 13:21:29 -04:00
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
$this->expectedField( 'product.id', $this->toRelayId( 'post', $product_id ) ),
2023-06-21 13:21:29 -04:00
$this->expectedObject(
'product.metaData.#',
[
$this->expectedField( 'key', 'meta_1' ),
$this->expectedField( 'value', 'test_meta_1' ),
]
),
$this->expectedObject(
'product.metaData.#',
[
$this->expectedField( 'key', 'meta_2' ),
$this->expectedField( 'value', 'test_meta_2' ),
]
),
];
2019-08-19 09:52:16 -04:00
2023-06-21 13:21:29 -04:00
$this->assertQuerySuccessful( $response, $expected );
}
2019-08-19 09:52:16 -04:00
public function testProductVariationMetaDataQueries() {
2023-06-21 13:21:29 -04:00
// Create Product with meta data.
$meta_data = [
[
'id' => 0,
'key' => 'meta_1',
'value' => 'test_meta_1',
],
[
'id' => 0,
'key' => 'meta_2',
'value' => 'test_meta_2',
],
];
$product_id = $this->factory->product->createVariable( compact( 'meta_data' ) );
$product_ids = $this->factory->product_variation->createSome( $product_id, compact( 'meta_data' ) );
$variation_id = $product_ids['variations'][0];
$query = '
2019-08-19 09:52:16 -04:00
query ($id: ID!) {
productVariation(id: $id) {
id
metaData {
key
value
}
2019-08-19 09:52:16 -04:00
}
}
';
2020-06-12 16:50:21 -04:00
/**
* Assertion One
*/
$variables = [ 'id' => $this->toRelayId( 'post', $variation_id ) ];
2023-06-21 13:21:29 -04:00
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
$this->expectedField( 'productVariation.id', $this->toRelayId( 'post', $variation_id ) ),
2023-06-21 13:21:29 -04:00
$this->expectedObject(
'productVariation.metaData.#',
[
$this->expectedField( 'key', 'meta_1' ),
$this->expectedField( 'value', 'test_meta_1' ),
]
),
$this->expectedObject(
'productVariation.metaData.#',
[
$this->expectedField( 'key', 'meta_2' ),
$this->expectedField( 'value', 'test_meta_2' ),
]
),
];
2019-08-19 09:52:16 -04:00
2023-06-21 13:21:29 -04:00
$this->assertQuerySuccessful( $response, $expected );
}
2019-08-19 09:52:16 -04:00
public function testRefundMetaDataQueries() {
2023-06-21 13:21:29 -04:00
$meta_data = [
[
'id' => 0,
'key' => 'meta_1',
'value' => 'test_meta_1',
],
[
'id' => 0,
'key' => 'meta_2',
'value' => 'test_meta_2',
],
];
$customer_id = $this->factory->customer->create( [ 'meta_data' => $meta_data ] );
$order_id = $this->factory->order->createNew(
[
'customer_id' => $customer_id,
'meta_data' => $meta_data,
]
);
$refund_id = $this->factory->refund->createNew( $order_id, compact( 'meta_data' ) );
$query = '
query refundQuery( $id: ID! ) {
refund( id: $id ) {
id
metaData {
key
value
}
}
}
';
2020-06-12 16:50:21 -04:00
/**
* Assertion One
*/
2023-06-21 13:21:29 -04:00
$this->loginAs( $customer_id );
$variables = [ 'id' => $this->toRelayId( 'order', $refund_id ) ];
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
2023-06-21 13:21:29 -04:00
$this->expectedField( 'refund.id', $this->toRelayId( 'order', $refund_id ) ),
$this->expectedObject(
'refund.metaData.#',
[
$this->expectedField( 'key', 'meta_1' ),
$this->expectedField( 'value', 'test_meta_1' ),
]
),
$this->expectedObject(
'refund.metaData.#',
[
$this->expectedField( 'key', 'meta_2' ),
$this->expectedField( 'value', 'test_meta_2' ),
]
),
];
2019-08-19 09:52:16 -04:00
2023-06-21 13:21:29 -04:00
$this->assertQuerySuccessful( $response, $expected );
}
2020-06-12 16:50:21 -04:00
}