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

224 lines
7.4 KiB
PHP

<?php
class ProductAttributeMutationsTest extends \Tests\WPGraphQL\WooCommerce\TestCase\WooGraphQLTestCase {
public function testCreateProductAttribute() {
$query = '
mutation ($input: CreateProductAttributeInput!) {
createProductAttribute(input: $input) {
attribute {
id
name
label
type
orderBy
hasArchives
}
}
}
';
$variables = [
'input' => [
'name' => 'Fabric',
'slug' => 'fabric',
'orderBy' => 'menu_order',
'hasArchives' => false,
],
];
// 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(
'createProductAttribute.attribute',
[
$this->expectedField( 'id', self::NOT_NULL ),
$this->expectedField( 'name', 'fabric' ),
$this->expectedField( 'label', 'Fabric' ),
$this->expectedField( 'type', 'select' ),
$this->expectedField( 'orderBy', 'menu_order' ),
$this->expectedField( 'hasArchives', false ),
]
),
];
$this->assertQuerySuccessful( $response, $expected );
}
public function testUpdateProductAttribute() {
$query = '
mutation ($input: CreateProductAttributeInput!) {
createProductAttribute(input: $input) {
attribute {
id
name
label
type
orderBy
hasArchives
}
}
}
';
$variables = [
'input' => [
'name' => 'Fabric',
'slug' => 'fabric',
'orderBy' => 'menu_order',
'hasArchives' => false,
],
];
$this->loginAsShopManager();
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
$this->expectedObject(
'createProductAttribute.attribute',
[
$this->expectedField( 'id', self::NOT_NULL ),
$this->expectedField( 'hasArchives', false ),
]
),
];
$this->assertQuerySuccessful( $response, $expected );
$attribute_id = $this->lodashGet( $response, 'data.createProductAttribute.attribute.id' );
$this->assertNotEmpty( $attribute_id );
$query = '
mutation ($input: UpdateProductAttributeInput!) {
updateProductAttribute(input: $input) {
attribute {
id
name
label
type
orderBy
hasArchives
}
}
}
';
$variables = [
'input' => [
'id' => $attribute_id,
'name' => 'Fabric',
'slug' => 'fabric',
'orderBy' => 'menu_order',
'hasArchives' => true,
],
];
// Assert mutation fails as unauthenticated user.
$this->loginAs( 0 );
$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(
'updateProductAttribute.attribute',
[
$this->expectedField( 'id', $attribute_id ),
$this->expectedField( 'name', 'fabric' ),
$this->expectedField( 'label', 'Fabric' ),
$this->expectedField( 'type', 'select' ),
$this->expectedField( 'orderBy', 'menu_order' ),
$this->expectedField( 'hasArchives', true ),
]
),
];
$this->assertQuerySuccessful( $response, $expected );
}
public function testDeleteProductAttribute() {
$query = '
mutation ($input: CreateProductAttributeInput!) {
createProductAttribute(input: $input) {
attribute {
id
}
}
}
';
$variables = [
'input' => [
'name' => 'Fabric',
'slug' => 'fabric',
'orderBy' => 'menu_order',
'hasArchives' => false,
],
];
$this->loginAsShopManager();
$response = $this->graphql( compact( 'query', 'variables' ) );
$this->assertQuerySuccessful( $response );
$attribute_id = $this->lodashGet( $response, 'data.createProductAttribute.attribute.id' );
$this->assertNotEmpty( $attribute_id );
$query = '
mutation ($input: DeleteProductAttributeInput!) {
deleteProductAttribute(input: $input) {
attribute {
id
}
}
}
';
$variables = [
'input' => [
'id' => $attribute_id,
],
];
// Assert mutation fails as unauthenticated user.
$this->loginAs( 0 );
$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(
'deleteProductAttribute.attribute',
[
$this->expectedField( 'id', $attribute_id ),
]
),
];
$this->assertQuerySuccessful( $response, $expected );
}
}