diff --git a/.github/workflows/testing-integration.yml b/.github/workflows/testing-integration.yml index b6e896dd..2c8f1b61 100644 --- a/.github/workflows/testing-integration.yml +++ b/.github/workflows/testing-integration.yml @@ -10,6 +10,7 @@ on: pull_request: branches: - develop + - release/v0.5.1 jobs: run: diff --git a/.github/workflows/wordpress-coding-standards.yml b/.github/workflows/wordpress-coding-standards.yml index 4342619a..6be12341 100644 --- a/.github/workflows/wordpress-coding-standards.yml +++ b/.github/workflows/wordpress-coding-standards.yml @@ -8,6 +8,7 @@ on: pull_request: branches: - develop + - release/v0.5.1 jobs: run: diff --git a/includes/class-type-registry.php b/includes/class-type-registry.php index 22747148..35e0a6c4 100644 --- a/includes/class-type-registry.php +++ b/includes/class-type-registry.php @@ -118,5 +118,8 @@ class Type_Registry { \WPGraphQL\WooCommerce\Mutation\Order_Delete::register_mutation(); \WPGraphQL\WooCommerce\Mutation\Order_Delete_Items::register_mutation(); \WPGraphQL\WooCommerce\Mutation\Checkout::register_mutation(); + \WPGraphQL\WooCommerce\Mutation\Review_Write::register_mutation(); + \WPGraphQL\WooCommerce\Mutation\Review_Update::register_mutation(); + \WPGraphQL\WooCommerce\Mutation\Review_Delete_Restore::register_mutation(); } } diff --git a/includes/mutation/class-review-delete-restore.php b/includes/mutation/class-review-delete-restore.php new file mode 100644 index 00000000..75a28f79 --- /dev/null +++ b/includes/mutation/class-review-delete-restore.php @@ -0,0 +1,158 @@ + self::get_input_fields( true ), + 'outputFields' => self::get_output_fields(), + 'mutateAndGetPayload' => self::mutate_and_get_payload(), + ) + ); + + // Restore mutation. + register_graphql_mutation( + 'restoreReview', + array( + 'inputFields' => self::get_input_fields(), + 'outputFields' => self::get_output_fields( true ), + 'mutateAndGetPayload' => self::mutate_and_get_payload(), + ) + ); + } + + /** + * Defines the mutation input field configuration. + * + * @return array + */ + public static function get_input_fields( $delete = false ) { + $fields = array( + 'id' => array( + 'type' => array( + 'non_null' => 'ID', + ), + 'description' => __( 'The ID of the target product review', 'wp-graphql-woocommerce' ), + ), + ); + + if ( $delete ) { + $fields['forceDelete'] = array( + 'type' => 'Boolean', + 'description' => __( 'Whether the product review should be force deleted instead of being moved to the trash', 'wp-graphql-woocommerce' ), + ); + } + + return $fields; + } + + /** + * Defines the mutation output field configuration. + * + * @return array + */ + public static function get_output_fields( $restore = false ) { + return array( + 'rating' => array( + 'type' => 'Float', + 'description' => __( 'The product rating of the affected product review', 'wp-graphql-woocommerce' ), + 'resolve' => function( $payload ) { + if ( ! isset( $payload['rating'] ) ) { + return null; + } + + return floatval( $payload['rating'] ); + } + ), + 'affectedId' => array( + 'type' => 'Id', + 'description' => __( 'The affected product review ID', 'wp-graphql-woocommerce' ), + 'resolve' => function( $payload ) { + $deleted = (object) $payload['commentObject']; + + return ! empty( $deleted->comment_ID ) ? Relay::toGlobalId( 'comment', absint( $deleted->comment_ID ) ) : null; + }, + ), + 'review' => array( + 'type' => 'Comment', + 'description' => __( 'The affected product review', 'wp-graphql-woocommerce' ), + 'resolve' => function( $payload, $args, AppContext $context, ResolveInfo $info ) use ( $restore ) { + if ( empty( $payload['commentObject'] ) ) { + return null; + } + + if ( $restore ) { + return ! empty( $payload['commentObject']->comment_ID ) + ? DataSource::resolve_comment( absint( $payload['commentObject']->comment_ID ), $context ) + : null; + } + + return $payload['commentObject']; + }, + ) + ); + } + + /** + * Defines the mutation data modification closure. + * + * @return callable + */ + public static function mutate_and_get_payload() { + return function( $input, AppContext $context, ResolveInfo $info ) { + // Retrieve the product review rating for the payload. + $id_parts = Relay::fromGlobalId( $input['id'] ); + if ( empty( $id_parts['id'] ) ) { + throw new UserError( __( 'Invalid Product Review ID provided', 'wp-graphql-woocommerce' ) ); + } + + $rating = get_comment_meta( absint( $id_parts['id'] ), 'rating' ); + + // @codingStandardsIgnoreLine + switch ( $info->fieldName ) { + case 'deleteReview': + $classname = '\WPGraphQL\Mutation\CommentDelete'; + break; + case 'restoreReview': + $classname = '\WPGraphQL\Mutation\CommentRestore'; + break; + } + + // Get the comment mutation resolver + $resolver = $classname::mutate_and_get_payload(); + + // Execute and retrieve payload. + $payload = $resolver( $input, $context, $info ); + + // Add rating to payload. + $payload['rating'] = $rating; + + return $payload; + }; + } +} \ No newline at end of file diff --git a/includes/mutation/class-review-update.php b/includes/mutation/class-review-update.php new file mode 100644 index 00000000..9f9d8598 --- /dev/null +++ b/includes/mutation/class-review-update.php @@ -0,0 +1,91 @@ + self::get_input_fields(), + 'outputFields' => self::get_output_fields(), + 'mutateAndGetPayload' => self::mutate_and_get_payload(), + ) + ); + } + + /** + * Defines the mutation input field configuration + * + * @return array + */ + public static function get_input_fields() { + return array_merge( + Review_Write::get_input_fields(), + array( + 'id' => array( + 'type' => array( 'non_null' => 'ID' ), + 'description' => __( 'The ID of the review being updated.', 'wp-graphql-woocommerce' ), + ), + ) + ); + } + + /** + * Defines the mutation output field configuration + * + * @return array + */ + public static function get_output_fields() { + return Review_Write::get_output_fields(); + } + + /** + * Defines the mutation data modification closure. + * + * @return callable + */ + public static function mutate_and_get_payload() { + return function( $input, AppContext $context, ResolveInfo $info ) { + // Set comment type to "review". + $input['type'] = 'review'; + + $resolver = CommentUpdate::mutate_and_get_payload(); + + $payload = $resolver( $input, $context, $info ); + + // Check if product rating needs updating. + if ( ! empty( $payload['id'] ) && isset( $input['rating'] ) ) { + $success = update_comment_meta( $payload['id'], 'rating', $input['rating'] ); + + if ( ! $success ) { + throw UserError( __( 'Failed to update review rating', 'wp-graphql-woocommerce' ) ); + } + } + + return $payload; + }; + } +} diff --git a/includes/mutation/class-review-write.php b/includes/mutation/class-review-write.php new file mode 100644 index 00000000..d133bb66 --- /dev/null +++ b/includes/mutation/class-review-write.php @@ -0,0 +1,115 @@ + self::get_input_fields(), + 'outputFields' => self::get_output_fields(), + 'mutateAndGetPayload' => self::mutate_and_get_payload(), + ) + ); + } + + /** + * Defines the mutation input field configuration + * + * @return array + */ + public static function get_input_fields() { + $comment_input_fields = CommentCreate::get_input_fields(); + unset( $comment_input_fields['type'] ); + + return array_merge( + $comment_input_fields, + array( + 'rating' => array( + 'type' => array( 'non_null' => 'Float' ), + 'description' => __( 'Product rating', 'wp-graphql-woocommerce' ), + ), + ) + ); + } + + /** + * Defines the mutation output field configuration + * + * @return array + */ + public static function get_output_fields() { + return array( + 'rating' => array( + 'type' => 'Float', + 'description' => __( 'The product rating of the review that was created', 'wp-graphql-woocommerce' ), + 'resolve' => function( $payload ) { + if ( ! isset( $payload['id'] ) || ! absint( $payload['id'] ) ) { + return null; + } + + return (float) get_comment_meta( $payload['id'], 'rating', true ); + } + ), + 'review' => array( + 'type' => 'Comment', + 'description' => __( 'The product review that was created', 'wp-graphql-woocommerce' ), + 'resolve' => function( $payload, $args, AppContext $context ) { + if ( ! isset( $payload['id'] ) || ! absint( $payload['id'] ) ) { + return null; + } + + $comment = get_comment( $payload['id'] ); + return new Comment( $comment ); + }, + ), + ); + } + + /** + * Defines the mutation data modification closure. + * + * @return callable + */ + public static function mutate_and_get_payload() { + return function( $input, AppContext $context, ResolveInfo $info ) { + // Set comment type to "review". + $input['type'] = 'review'; + + $resolver = CommentCreate::mutate_and_get_payload(); + + $payload = $resolver( $input, $context, $info ); + + // Set product rating upon successful creation of the review. + if ( $payload['success'] ) { + add_comment_meta( $payload['id'], 'rating', $input['rating'] ); + } + + return $payload; + }; + } +} diff --git a/tests/_data/config.php b/tests/_data/config.php index dd232630..6b6bbbc7 100644 --- a/tests/_data/config.php +++ b/tests/_data/config.php @@ -8,7 +8,7 @@ if ( ! defined( 'GRAPHQL_DEBUG' ) ) { define( 'GRAPHQL_DEBUG', true ); } -if ( ! defined( 'WPGRAPHQL_WOOCOMMERCE_AUTOLOAD' ) ) { +if ( ! defined( 'WPGRAPHQL_WOOCOMMERCE_AUTOLOAD' ) && false !== getenv( 'WPGRAPHQL_WOOCOMMERCE_AUTOLOAD' ) ) { define( 'WPGRAPHQL_WOOCOMMERCE_AUTOLOAD', getenv( 'WPGRAPHQL_WOOCOMMERCE_AUTOLOAD' ) ); } @@ -16,10 +16,10 @@ if ( ! defined( 'GRAPHQL_JWT_AUTH_SECRET_KEY' ) ) { define( 'GRAPHQL_JWT_AUTH_SECRET_KEY', 'testingtesting123' ); } -if ( ! defined( 'STRIPE_API_PUBLISHABLE_KEY' ) ) { +if ( ! defined( 'STRIPE_API_PUBLISHABLE_KEY' ) && false !== getenv( 'STRIPE_API_PUBLISHABLE_KEY' ) ) { define( 'STRIPE_API_PUBLISHABLE_KEY', getenv( 'STRIPE_API_PUBLISHABLE_KEY' ) ); } -if ( ! defined( 'STRIPE_API_SECRET_KEY' ) ) { +if ( ! defined( 'STRIPE_API_SECRET_KEY' ) && false !== getenv( 'STRIPE_API_SECRET_KEY' ) ) { define( 'STRIPE_API_SECRET_KEY', getenv( 'STRIPE_API_SECRET_KEY' ) ); -} +} \ No newline at end of file diff --git a/tests/_support/Helper/Wpunit.php b/tests/_support/Helper/Wpunit.php index 866d7a7c..2da300de 100644 --- a/tests/_support/Helper/Wpunit.php +++ b/tests/_support/Helper/Wpunit.php @@ -34,7 +34,9 @@ class Wpunit extends \Codeception\Module { codecept_debug( 'ATTRIBUTES_LOADED' ); add_action( 'init_graphql_request', array( __CLASS__, 'shortcode_test_init' ) ); codecept_debug( 'SHORTCODE_INITIALIZED' ); - \Stripe\Stripe::setApiKey( STRIPE_API_SECRET_KEY ); + \Stripe\Stripe::setApiKey( + defined( 'STRIPE_API_SECRET_KEY' ) ? STRIPE_API_SECRET_KEY : getenv( 'STRIPE_API_SECRET_KEY' ) + ); } public function cart() { diff --git a/tests/wpunit/CheckoutMutationTest.php b/tests/wpunit/CheckoutMutationTest.php index 51a2b06a..2dcfd211 100644 --- a/tests/wpunit/CheckoutMutationTest.php +++ b/tests/wpunit/CheckoutMutationTest.php @@ -46,8 +46,12 @@ class CheckoutMutationTest extends \Codeception\TestCase\WPTestCase { 'description' => 'Pay with your credit card via Stripe', 'webhook' => '', 'testmode' => 'yes', - 'test_publishable_key' => STRIPE_API_PUBLISHABLE_KEY, - 'test_secret_key' => STRIPE_API_SECRET_KEY, + 'test_publishable_key' => defined( 'STRIPE_API_PUBLISHABLE_KEY' ) + ? STRIPE_API_PUBLISHABLE_KEY + : getenv( 'STRIPE_API_PUBLISHABLE_KEY' ), + 'test_secret_key' => defined( 'STRIPE_API_SECRET_KEY' ) + ? STRIPE_API_SECRET_KEY + : getenv( 'STRIPE_API_SECRET_KEY' ), 'test_webhook_secret' => '', 'publishable_key' => '', 'secret_key' => '', diff --git a/tests/wpunit/PaymentGatewayQueriesTest.php b/tests/wpunit/PaymentGatewayQueriesTest.php index fd8edc35..d62c29d0 100644 --- a/tests/wpunit/PaymentGatewayQueriesTest.php +++ b/tests/wpunit/PaymentGatewayQueriesTest.php @@ -42,8 +42,12 @@ class PaymentGatewayQueriesTest extends \Codeception\TestCase\WPTestCase 'description' => 'Pay with your credit card via Stripe', 'webhook' => '', 'testmode' => 'yes', - 'test_publishable_key' => STRIPE_API_PUBLISHABLE_KEY, - 'test_secret_key' => STRIPE_API_SECRET_KEY, + 'test_publishable_key' => defined( 'STRIPE_API_PUBLISHABLE_KEY' ) + ? STRIPE_API_PUBLISHABLE_KEY + : getenv( 'STRIPE_API_PUBLISHABLE_KEY' ), + 'test_secret_key' => defined( 'STRIPE_API_SECRET_KEY' ) + ? STRIPE_API_SECRET_KEY + : getenv( 'STRIPE_API_SECRET_KEY' ), 'test_webhook_secret' => '', 'publishable_key' => '', 'secret_key' => '', diff --git a/tests/wpunit/ProductReviewMutationsTest.php b/tests/wpunit/ProductReviewMutationsTest.php new file mode 100644 index 00000000..ecfb19d6 --- /dev/null +++ b/tests/wpunit/ProductReviewMutationsTest.php @@ -0,0 +1,185 @@ +shop_manager = $this->factory->user->create( array( 'role' => 'shop_manager' ) ); + $this->customer = $this->factory->user->create( array( 'role' => 'customer' ) ); + $this->products = $this->getModule('\Helper\Wpunit')->product(); + } + + public function tearDown() { + // your tear down methods here + + // then + parent::tearDown(); + } + + private function run_mutation( $mutation_name, $input = null ) { + $input_type = ucfirst( $mutation_name ) . 'Input!'; + $mutation = " + mutation ( \$input: {$input_type} ) { + {$mutation_name}( input: \$input ) { + clientMutationId + rating + review { + content( format: RAW ) + } + } + } + "; + + $results = graphql( + array( + 'query' => $mutation, + 'variables' => array( 'input' => $input ), + ) + ); + + // Use --debug flag to view + codecept_debug( $results ); + + return $results; + } + + // tests + public function testCreateNewReviewMutation() { + wp_set_current_user( $this->shop_manager ); + $input = array( + 'clientMutationId' => 'some_id', + 'rating' => 1, + 'commentOn' => $this->products->create_simple(), + 'content' => 'It came covered in poop!!!', + 'author' => 'Rude customer', + 'authorEmail' => 'rude-guy@example.com', + 'authorIp' => ':1', + ); + + $actual = $this->run_mutation( 'writeReview', $input ); + $expected = array( + 'data' => array( + 'writeReview' => array( + 'clientMutationId' => 'some_id', + 'rating' => 1.0, + 'review' => array( + 'content' => 'It came covered in poop!!!', + ), + ), + ), + ); + $this->assertEquals( $expected, $actual ); + } + + public function testUpdateReviewMutation() { + wp_set_current_user( $this->shop_manager ); + $comment_id = $this->factory()->comment->create( + array( + 'comment_author' => 'Rude customer', + 'comment_author_email' => 'rude-guy@example.com', + 'comment_post_ID' => $this->products->create_simple(), + 'comment_content' => 'It came covered in poop!!!', + 'comment_approved' => 1, + 'comment_type' => 'review', + ) + ); + update_comment_meta( $comment_id, 'rating', 1 ); + + $input = array( + 'clientMutationId' => 'some_id', + 'rating' => 5, + 'id' => Relay::toGlobalId( 'comment', $comment_id ), + 'content' => 'Turns out it was Nutella. My bad =P', + ); + + $actual = $this->run_mutation( 'updateReview', $input ); + $expected = array( + 'data' => array( + 'updateReview' => array( + 'clientMutationId' => 'some_id', + 'rating' => 5.0, + 'review' => array( + 'content' => 'Turns out it was Nutella. My bad =P', + ), + ), + ), + ); + $this->assertEquals( $expected, $actual ); + } + + public function testDeleteReviewMutation() { + wp_set_current_user( $this->shop_manager ); + $comment_id = $this->factory()->comment->create( + array( + 'comment_author' => 'Rude customer', + 'comment_author_email' => 'rude-guy@example.com', + 'comment_post_ID' => $this->products->create_simple(), + 'comment_content' => 'It came covered in poop!!!', + 'comment_approved' => 1, + 'comment_type' => 'review', + ) + ); + update_comment_meta( $comment_id, 'rating', 1 ); + + $input = array( + 'clientMutationId' => 'some_id', + 'id' => Relay::toGlobalID( 'comment', $comment_id ), + ); + + $actual = $this->run_mutation( 'deleteReview', $input ); + $expected = array( + 'data' => array( + 'deleteReview' => array( + 'clientMutationId' => 'some_id', + 'rating' => 1.0, + 'review' => array( + 'content' => 'It came covered in poop!!!', + ), + ), + ), + ); + $this->assertEquals( $expected, $actual ); + } + + public function testRestoreReviewMutation() { + wp_set_current_user( $this->shop_manager ); + $comment_id = $this->factory()->comment->create( + array( + 'comment_author' => 'Rude customer', + 'comment_author_email' => 'rude-guy@example.com', + 'comment_post_ID' => $this->products->create_simple(), + 'comment_content' => 'It came covered in poop!!!', + 'comment_approved' => 1, + 'comment_type' => 'review', + ) + ); + update_comment_meta( $comment_id, 'rating', 1 ); + + // Trash comment + wp_delete_comment( $comment_id ); + + $input = array( + 'clientMutationId' => 'some_id', + 'id' => Relay::toGlobalID( 'comment', $comment_id ), + ); + + $actual = $this->run_mutation( 'restoreReview', $input ); + $expected = array( + 'data' => array( + 'restoreReview' => array( + 'clientMutationId' => 'some_id', + 'rating' => 1.0, + 'review' => array( + 'content' => 'It came covered in poop!!!', + ), + ), + ), + ); + $this->assertEquals( $expected, $actual ); + } +} \ No newline at end of file diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index 4d35b3d6..b01ae8b0 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -83,6 +83,9 @@ return array( 'WPGraphQL\\WooCommerce\\Mutation\\Order_Delete' => $baseDir . '/includes/mutation/class-order-delete.php', 'WPGraphQL\\WooCommerce\\Mutation\\Order_Delete_Items' => $baseDir . '/includes/mutation/class-order-delete-items.php', 'WPGraphQL\\WooCommerce\\Mutation\\Order_Update' => $baseDir . '/includes/mutation/class-order-update.php', + 'WPGraphQL\\WooCommerce\\Mutation\\Review_Delete_Restore' => $baseDir . '/includes/mutation/class-review-delete-restore.php', + 'WPGraphQL\\WooCommerce\\Mutation\\Review_Update' => $baseDir . '/includes/mutation/class-review-update.php', + 'WPGraphQL\\WooCommerce\\Mutation\\Review_Write' => $baseDir . '/includes/mutation/class-review-write.php', 'WPGraphQL\\WooCommerce\\Type\\WPEnum\\Backorders' => $baseDir . '/includes/type/enum/class-backorders.php', 'WPGraphQL\\WooCommerce\\Type\\WPEnum\\Catalog_Visibility' => $baseDir . '/includes/type/enum/class-catalog-visibility.php', 'WPGraphQL\\WooCommerce\\Type\\WPEnum\\Countries' => $baseDir . '/includes/type/enum/class-countries.php', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 9c38837c..91965222 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -111,6 +111,9 @@ class ComposerStaticInit1d218159c651cbc5020beefb5cc5651a 'WPGraphQL\\WooCommerce\\Mutation\\Order_Delete' => __DIR__ . '/../..' . '/includes/mutation/class-order-delete.php', 'WPGraphQL\\WooCommerce\\Mutation\\Order_Delete_Items' => __DIR__ . '/../..' . '/includes/mutation/class-order-delete-items.php', 'WPGraphQL\\WooCommerce\\Mutation\\Order_Update' => __DIR__ . '/../..' . '/includes/mutation/class-order-update.php', + 'WPGraphQL\\WooCommerce\\Mutation\\Review_Delete_Restore' => __DIR__ . '/../..' . '/includes/mutation/class-review-delete-restore.php', + 'WPGraphQL\\WooCommerce\\Mutation\\Review_Update' => __DIR__ . '/../..' . '/includes/mutation/class-review-update.php', + 'WPGraphQL\\WooCommerce\\Mutation\\Review_Write' => __DIR__ . '/../..' . '/includes/mutation/class-review-write.php', 'WPGraphQL\\WooCommerce\\Type\\WPEnum\\Backorders' => __DIR__ . '/../..' . '/includes/type/enum/class-backorders.php', 'WPGraphQL\\WooCommerce\\Type\\WPEnum\\Catalog_Visibility' => __DIR__ . '/../..' . '/includes/type/enum/class-catalog-visibility.php', 'WPGraphQL\\WooCommerce\\Type\\WPEnum\\Countries' => __DIR__ . '/../..' . '/includes/type/enum/class-countries.php',