Files
wp-graphql-woocommerce/tests/wpunit/ProductAttributeTermMutationsTest.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

170 lines
6.1 KiB
PHP

<?php
class ProductAttributeTermMutationsTest extends \Tests\WPGraphQL\WooCommerce\TestCase\WooGraphQLTestCase {
public function testCreateProductAttributeTerm() {
$kind_attribute = $this->factory->product->createAttribute( 'kind', [ 'normal', 'special' ] );
$query = '
mutation ($input: CreateProductAttributeTermInput!) {
createProductAttributeTerm(input: $input) {
term {
id
name
slug
description
menuOrder
count
}
}
}
';
$variables = [
'input' => [
'attributeId' => $kind_attribute['attribute_id'],
'name' => 'Hated',
'slug' => 'hated',
'description' => 'Hated by all',
'menuOrder' => 2,
],
];
// Assert mutation fails as unauthenticated user.
$response = $this->graphql( compact( 'query', 'variables' ) );
$this->assertQueryError( $response );
// Assert mutation fails as authenticated user without proper capabilities.
$this->loginAsCustomer();
$response = $this->graphql( compact( 'query', 'variables' ) );
$this->assertQueryError( $response );
// Assert mutation succeeds as authenticated user with proper capabilities.
$this->loginAsShopManager();
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
$this->expectedObject(
'createProductAttributeTerm.term',
[
$this->expectedField( 'id', self::NOT_NULL ),
$this->expectedField( 'name', 'Hated' ),
$this->expectedField( 'slug', 'hated' ),
$this->expectedField( 'description', 'Hated by all' ),
$this->expectedField( 'menuOrder', 2 ),
$this->expectedField( 'count', 0 ),
]
)
];
$this->assertQuerySuccessful( $response, $expected );
}
public function testUpdateProductAttributeTerm() {
$kind_attribute = $this->factory->product->createAttribute( 'kind', [ 'normal', 'special', 'hated' ] );
$hated_term_id = get_term_by( 'slug', 'hated', 'pa_kind' )->term_id;
$query = '
mutation ($input: UpdateProductAttributeTermInput!) {
updateProductAttributeTerm(input: $input) {
term {
id
name
slug
description
menuOrder
count
}
}
}
';
$variables = [
'input' => [
'attributeId' => $kind_attribute['attribute_id'],
'id' => $hated_term_id,
'name' => 'Loved',
'slug' => 'loved',
'description' => 'Loved by all',
'menuOrder' => 0,
],
];
// Assert mutation fails as unauthenticated user.
$response = $this->graphql( compact( 'query', 'variables' ) );
$this->assertQueryError( $response );
// Assert mutation fails as authenticated user without proper capabilities.
$this->loginAsCustomer();
$response = $this->graphql( compact( 'query', 'variables' ) );
$this->assertQueryError( $response );
// Assert mutation succeeds as authenticated user with proper capabilities.
$this->loginAsShopManager();
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
$this->expectedObject(
'updateProductAttributeTerm.term',
[
$this->expectedField( 'id', self::NOT_NULL ),
$this->expectedField( 'name', 'Loved' ),
$this->expectedField( 'slug', 'loved' ),
$this->expectedField( 'description', 'Loved by all' ),
$this->expectedField( 'menuOrder', 0 ),
$this->expectedField( 'count', 0 ),
]
)
];
$this->assertQuerySuccessful( $response, $expected );
}
public function testDeleteProductAttributeTerm() {
$kind_attribute = $this->factory->product->createAttribute( 'kind', [ 'normal', 'special', 'hated' ] );
$hated_term_id = get_term_by( 'slug', 'hated', 'pa_kind' )->term_id;
$query = '
mutation ($input: DeleteProductAttributeTermInput!) {
deleteProductAttributeTerm(input: $input) {
term {
id
slug
}
}
}
';
$variables = [
'input' => [
'attributeId' => $kind_attribute['attribute_id'],
'id' => $hated_term_id,
],
];
// Assert mutation fails as unauthenticated user.
$response = $this->graphql( compact( 'query', 'variables' ) );
$this->assertQueryError( $response );
// Assert mutation fails as authenticated user without proper capabilities.
$this->loginAsCustomer();
$response = $this->graphql( compact( 'query', 'variables' ) );
$this->assertQueryError( $response );
// Assert mutation succeeds as authenticated user with proper capabilities.
$this->loginAsShopManager();
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
$this->expectedObject(
'deleteProductAttributeTerm.term',
[
$this->expectedField( 'id', self::NOT_NULL ),
$this->expectedField( 'slug', 'hated' ),
]
)
];
$this->assertQuerySuccessful( $response, $expected );
}
}