Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mutation to resend activation email #128

Merged
merged 2 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
90 changes: 90 additions & 0 deletions src/Mutation/Signup/SignupResendEmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* SignupResendEmail Mutation.
*
* @package WPGraphQL\Extensions\BuddyPress\Mutation\Signup
* @since 0.1.1
*/

namespace WPGraphQL\Extensions\BuddyPress\Mutation\Signup;

use GraphQL\Error\UserError;
use WPGraphQL\Extensions\BuddyPress\Data\SignupHelper;
use BP_Signup;

/**
* SignupResendEmail Class.
*/
class SignupResendEmail {

/**
* Registers the SignupResendEmail mutation.
*/
public static function register_mutation(): void {
register_graphql_mutation(
'resendSignupEmail',
[
'inputFields' => 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 ];
};
}
}
2 changes: 2 additions & 0 deletions src/TypeRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
}

Expand Down
79 changes: 79 additions & 0 deletions tests/testcases/signup/resendEmailSignup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/**
* Test_Signup_resendEmailSignup_Mutation Class.
*
* @group renato
*/
class Test_Signup_resendEmailSignup_Mutation extends WPGraphQL_BuddyPress_UnitTestCase {

/**
* Set up.
*/
public function setUp() : void {
parent::setUp();

add_filter( 'bp_get_signup_allowed', '__return_true' );
}

public function test_signup_resend_email() {
$this->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' ) );
}
}
Loading