Files
wp-graphql-woocommerce/tests/wpunit/ProductVariationMutationsTest.php
Geoff TaylorandGitHub b6576d4125 feat: Product and product attribute mutations (#851)
* devops: Product and product attribute mutation tests generated

* feat: Product Mutations implemented and tested

* chore: Linter and PHPStan compliance met

* fix: resolve test failures in product and variation mutation tests

- Removed duplicate requires for payment-method classes (rebase artifact)
- Wrapped bare `attributes` queries in `... on SimpleProduct` inline
  fragments (attributes field is on ProductWithAttributes interface,
  not the base Product type)
- Fixed attribute label expectations to match factory output (lowercase)
- Fixed relay ID prefix from 'product'/'product_variation' to 'post'
  to match WPGraphQL's actual encoding
- Cache WP_Post before force-deletion and re-cache after so the Product
  model's type resolver can still determine the GraphQL type in the
  response
- Added wp_cache_flush() before asserting product deletion to avoid
  false positives from the re-cached post

* chore: fix PHPCS lint errors in mutation files

* fix: resolve CI test failures for attribute and variation mutations

- Fixed ProductVariation type registration in schema filters so the
  variation mutation output field resolves correctly
- Renamed attribute slug from 'pattern' to 'fabric' in
  ProductAttributeMutationsTest to avoid collision with the pattern
  attribute created by ProductAttributeQueriesTest
- Fixed PHPStan errors for redundant is_wp_error checks
- Updated IntrospectionQueryTest
2026-03-20 13:31:13 -04:00

176 lines
6.2 KiB
PHP

<?php
class ProductVariationMutationsTest extends \Tests\WPGraphQL\WooCommerce\TestCase\WooGraphQLTestCase {
public function testCreateProductVariation() {
$kind_attribute = $this->factory->product->createAttribute( 'kind', [ 'normal', 'special' ] );
$product_id = $this->factory->product->createVariable(
[
'attributes' => [
$this->factory->product->createAttributeObject(
$kind_attribute['attribute_id'],
$kind_attribute['attribute_taxonomy'],
$kind_attribute['term_ids'],
0,
true,
true,
),
],
'attribute_data' => [],
]
);
$query = '
mutation ($input: CreateProductVariationInput!) {
createProductVariation(input: $input) {
variation {
id
parent { node { id } }
price
regularPrice
salePrice
attributes {
nodes {
id
name
value
}
}
}
}
}
';
$variables = [
'input' => [
'productId' => $product_id,
'regularPrice' => 14.99,
'salePrice' => 9.99,
'attributes' => [
[
'id' => $kind_attribute['attribute_id'],
'attributeName' => $kind_attribute['attribute_name'],
'attributeValue' => 'special',
],
],
],
];
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
$this->expectedObject(
'createProductVariation.variation',
[
$this->expectedField( 'id', self::NOT_NULL ),
$this->expectedField( 'parent.node.id', $this->toRelayId( 'post', $product_id ) ),
$this->expectedField( 'price', "$9.99" ),
$this->expectedField( 'regularPrice', "$14.99" ),
$this->expectedField( 'salePrice', "$9.99" ),
$this->expectedNode(
'attributes.nodes',
[
$this->expectedField( 'id', self::NOT_NULL ),
$this->expectedField( 'name', $kind_attribute['attribute_taxonomy'] ),
$this->expectedField( 'value', 'special' ),
]
),
]
)
];
$this->assertQuerySuccessful( $response, $expected );
}
public function testUpdateProductVariation() {
$ids = $this->factory->product_variation->createSome(
$this->factory->product->createVariable()
);
$query = '
mutation ($input: UpdateProductVariationInput!) {
updateProductVariation(input: $input) {
variation {
id
parent { node { id } }
price
regularPrice
salePrice
}
}
}
';
$variables = [
'input' => [
'productId' => $ids['product'],
'id' => $ids['variations'][0],
'regularPrice' => 19.99,
'salePrice' => 14.99,
],
];
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
$this->expectedObject(
'updateProductVariation.variation',
[
$this->expectedField( 'id', $this->toRelayId( 'post', $ids['variations'][0] ) ),
$this->expectedField( 'parent.node.id', $this->toRelayId( 'post', $ids['product'] ) ),
$this->expectedField( 'price', "$14.99" ),
$this->expectedField( 'regularPrice', "$19.99" ),
$this->expectedField( 'salePrice', "$14.99" ),
]
)
];
$this->assertQuerySuccessful( $response, $expected );
}
public function testDeleteProductVariation() {
$ids = $this->factory->product_variation->createSome(
$this->factory->product->createVariable()
);
$query = '
mutation ($input: DeleteProductVariationInput!) {
deleteProductVariation(input: $input) {
variation {
id
parent { node { id } }
price
}
}
}
';
$variables = [
'input' => [
'id' => $ids['variations'][0],
'force' => true,
],
];
$response = $this->graphql( compact( 'query', 'variables' ) );
$this->assertQueryError( $response );
$this->loginAsCustomer();
$response = $this->graphql( compact( 'query', 'variables' ) );
$this->assertQueryError( $response );
$this->loginAsShopManager();
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
$this->expectedObject(
'deleteProductVariation.variation',
[
$this->expectedField( 'id', $this->toRelayId( 'post', $ids['variations'][0] ) ),
$this->expectedField( 'parent.node.id', $this->toRelayId( 'post', $ids['product'] ) ),
$this->expectedField( 'price', self::NOT_NULL ),
]
)
];
$this->assertQuerySuccessful( $response, $expected );
// $variation = wc_get_product( $ids['variations'][0] );
// $this->assertFalse( $variation );
}
}