diff --git a/CHANGELOG.md b/CHANGELOG.md index 25d37c8..f7746bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,13 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [0.1.2] Unpublished +## [0.1.1] Unpublished ### Added - Support BuddyPress Community Visibility, "Private Site", feature [#118](https://github.com/renatonascalves/wp-graphql-buddypress/issues/118) - -## [0.1.1] +- Add `resendSignupEmail` mutation to resend activation email [#127](https://github.com/renatonascalves/wp-graphql-buddypress/issues/127) ### Updated diff --git a/src/Mutation/Signup/SignupResendEmail.php b/src/Mutation/Signup/SignupResendEmail.php new file mode 100644 index 0000000..1b372ea --- /dev/null +++ b/src/Mutation/Signup/SignupResendEmail.php @@ -0,0 +1,90 @@ + self::get_input_fields(), + 'outputFields' => self::get_output_fields(), + 'mutateAndGetPayload' => self::mutate_and_get_payload(), + ] + ); + } + + /** + * Defines the mutation input fields. + * + * @return array + */ + public static function get_input_fields(): array { + return [ + 'id' => [ + 'type' => 'ID', + 'description' => __( 'The globally unique identifier for the signup.', 'wp-graphql-buddypress' ), + ], + 'databaseId' => [ + 'type' => 'Int', + 'description' => __( 'The id field that matches the BP_Signup->id field.', 'wp-graphql-buddypress' ), + ], + ]; + } + + /** + * Defines the mutation output fields. + * + * @return array + */ + public static function get_output_fields(): array { + return [ + 'sent' => [ + 'type' => 'Boolean', + 'description' => __( 'The status of the action.', 'wp-graphql-buddypress' ), + 'resolve' => function ( array $payload ) { + return (bool) $payload['sent']; + }, + ], + ]; + } + + /** + * Defines the mutation data modification closure. + * + * @return callable + */ + public static function mutate_and_get_payload(): callable { + return function ( array $input ) { + + // Check and get the signup. + $signup = SignupHelper::get_signup_from_input( $input ); + + // Resend email. + $retval = BP_Signup::resend( [ $signup->id ] ); + + if ( ! empty( $retval['errors'] ) ) { + throw new UserError( esc_html__( 'Your account has already been activated.', 'wp-graphql-buddypress' ) ); + } + + return [ 'sent' => true ]; + }; + } +} diff --git a/src/TypeRegistry.php b/src/TypeRegistry.php index 5f1bd40..4b7a6e5 100644 --- a/src/TypeRegistry.php +++ b/src/TypeRegistry.php @@ -96,6 +96,7 @@ use WPGraphQL\Extensions\BuddyPress\Mutation\Attachment\AttachmentCoverUpload; use WPGraphQL\Extensions\BuddyPress\Mutation\Attachment\AttachmentAvatarDelete; use WPGraphQL\Extensions\BuddyPress\Mutation\Attachment\AttachmentAvatarUpload; +use WPGraphQL\Extensions\BuddyPress\Mutation\Signup\SignupResendEmail; /** * Class TypeRegistry @@ -337,6 +338,7 @@ public static function graphql_register_types( $type_registry ): void { SignupDelete::register_mutation(); SignupActivate::register_mutation(); SignupCreate::register_mutation(); + SignupResendEmail::register_mutation(); } } diff --git a/tests/testcases/signup/resendEmailSignup.php b/tests/testcases/signup/resendEmailSignup.php new file mode 100644 index 0000000..fa911b2 --- /dev/null +++ b/tests/testcases/signup/resendEmailSignup.php @@ -0,0 +1,79 @@ +set_user(); + + $a = $this->create_signup_id(); + + $this->assertQuerySuccessful( $this->resend_signup( $a ) ) + ->hasField( 'sent', true ); + } + + public function test_resend_with_invalid_signup_id() { + $this->assertQueryFailed( $this->resend_signup( GRAPHQL_TESTS_IMPOSSIBLY_HIGH_NUMBER ) ) + ->expectedErrorMessage( 'This signup does not exist.' ); + } + + public function test_resend_with_active_signup() { + $this->skipWithMultisite(); + + $a = $this->create_signup_id(); + + $signup = new BP_Signup( $a ); + + // Activate the signup. + bp_core_activate_signup( $signup->activation_key ); + + $this->assertQueryFailed( $this->resend_signup( $a ) ) + ->expectedErrorMessage( 'Your account has already been activated.' ); + } + + /** + * Resend signup email mutation. + * + * @param int|null $signup_id Signup ID. + * @return array + */ + protected function resend_signup( $signup_id = null ): array { + $query = ' + mutation resendSignupEmailTest( + $clientMutationId:String! + $databaseId:Int + ) { + resendSignupEmail( + input: { + clientMutationId: $clientMutationId + databaseId: $databaseId + } + ) + { + clientMutationId + sent + } + } + '; + + $variables = [ + 'clientMutationId' => $this->client_mutation_id, + 'databaseId' => $signup_id, + ]; + + return $this->graphql( compact( 'query', 'variables' ) ); + } +}