diff --git a/lang/en/validation.php b/lang/en/validation.php index 7f7895d..47a65e5 100644 --- a/lang/en/validation.php +++ b/lang/en/validation.php @@ -6,6 +6,7 @@ 'end_date_after_start' => 'The :attribute must be a date after start.', 'end_date_greater_than' => 'The :attribute must be a date greater than :value.', 'is_paused' => 'The beam is paused.', + 'passes_condition' => 'A condition to claim has not been met.', 'passes_conditions' => 'Not all the conditions to claim have been met.', 'scan_limit' => 'You have reached the maximum limit to retry.', 'start_date_after_end' => 'The :attribute must be a date before end.', diff --git a/src/GraphQL/Mutations/ClaimBeamMutation.php b/src/GraphQL/Mutations/ClaimBeamMutation.php index efb637a..fe172b0 100644 --- a/src/GraphQL/Mutations/ClaimBeamMutation.php +++ b/src/GraphQL/Mutations/ClaimBeamMutation.php @@ -3,12 +3,12 @@ namespace Enjin\Platform\Beam\GraphQL\Mutations; use Closure; +use Enjin\Platform\Beam\GraphQL\Traits\HasBeamClaimConditions; use Enjin\Platform\Beam\GraphQL\Traits\HasBeamCommonFields; use Enjin\Platform\Beam\Rules\CanClaim; use Enjin\Platform\Beam\Rules\NotExpired; use Enjin\Platform\Beam\Rules\NotOwner; use Enjin\Platform\Beam\Rules\NotPaused; -use Enjin\Platform\Beam\Rules\PassesClaimConditions; use Enjin\Platform\Beam\Rules\SingleUseCodeExist; use Enjin\Platform\Beam\Rules\VerifySignedMessage; use Enjin\Platform\Beam\Services\BeamService; @@ -23,6 +23,7 @@ class ClaimBeamMutation extends Mutation implements PlatformPublicGraphQlOperation { use HasBeamCommonFields; + use HasBeamClaimConditions; /** * Get the mutation's attributes. @@ -98,14 +99,13 @@ protected function rules(array $args = []): array return [ 'code' => [ - 'bail', 'filled', 'max:1024', new NotExpired($beamCode), $singleUse ? new SingleUseCodeExist() : '', new CanClaim($singleUse), new NotPaused($beamCode), - new PassesClaimConditions($singleUse), + ...$this->getClaimConditionRules($singleUse), ], 'account' => ['filled', new ValidSubstrateAccount(), new NotOwner($singleUse)], 'signature' => ['sometimes', new VerifySignedMessage()], diff --git a/src/GraphQL/Traits/HasBeamClaimConditions.php b/src/GraphQL/Traits/HasBeamClaimConditions.php new file mode 100644 index 0000000..374250a --- /dev/null +++ b/src/GraphQL/Traits/HasBeamClaimConditions.php @@ -0,0 +1,18 @@ +map(fn ($condition) => new PassesClaimCondition($condition, $singleUse)) + ->toArray(); + } +} diff --git a/src/Rules/PassesClaimCondition.php b/src/Rules/PassesClaimCondition.php new file mode 100644 index 0000000..a1c4a48 --- /dev/null +++ b/src/Rules/PassesClaimCondition.php @@ -0,0 +1,40 @@ +function)($attribute, $value, $this->singleUse, $this->data); + + if (true !== $result) { + $fail(is_string($result) ? $result : __('enjin-platform-beam::validation.passes_condition')); + } + } +} diff --git a/tests/Feature/GraphQL/Mutations/ClaimBeamTest.php b/tests/Feature/GraphQL/Mutations/ClaimBeamTest.php index 744c48f..13c9903 100644 --- a/tests/Feature/GraphQL/Mutations/ClaimBeamTest.php +++ b/tests/Feature/GraphQL/Mutations/ClaimBeamTest.php @@ -175,7 +175,7 @@ function ($attribute, $code, $singleUse, $data) { public function test_it_cannot_claim_beam_with_single_condition_that_fails(): void { PassesClaimConditions::addConditionalFunctions(function ($attribute, $code, $singleUse, $data) { - return CryptoSignatureType::SR25519->name == $data['cryptoSignatureType']; + return CryptoSignatureType::SR25519->name == $data['cryptoSignatureType'] ? true : 'Signature is not SR25519.'; }); $this->assertNotEmpty(PassesClaimConditions::getConditionalFunctions()); @@ -201,7 +201,7 @@ public function test_it_cannot_claim_beam_with_single_condition_that_fails(): vo $this->assertNotEmpty($response); - $this->assertArraySubset(['code' => ['Not all the conditions to claim have been met.']], $response['error']); + $this->assertArraySubset(['code' => ['Signature is not SR25519.']], $response['error']); PassesClaimConditions::clearConditionalFunctions(); $this->assertEmpty(PassesClaimConditions::getConditionalFunctions()); @@ -214,10 +214,10 @@ public function test_it_cannot_claim_beam_with_multiple_conditions_that_fail(): { $functions = collect([ function ($attribute, $code, $singleUse, $data) { - return 'code' == $attribute; + return 'code' == $data[$attribute]; }, function ($attribute, $code, $singleUse, $data) { - return CryptoSignatureType::SR25519->name == $data['cryptoSignatureType']; + return CryptoSignatureType::SR25519->name == $data['cryptoSignatureType'] ? true : 'Signature is not SR25519.'; }, ]); @@ -246,7 +246,10 @@ function ($attribute, $code, $singleUse, $data) { $this->assertNotEmpty($response); - $this->assertArraySubset(['code' => ['Not all the conditions to claim have been met.']], $response['error']); + $this->assertArraySubset(['code' => [ + 'A condition to claim has not been met.', + 'Signature is not SR25519.', + ]], $response['error']); PassesClaimConditions::clearConditionalFunctions(); $this->assertEmpty(PassesClaimConditions::getConditionalFunctions());