Add PayPal client, donate flow, webhook; wire rules engine with per-goal lock

Replace the static Engine call (which silently failed) with Engine::default()
and a MySQL named lock. Add engine->overlay integration test.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
shadowdaoandClaude Sonnet 5.5 committed 2026-11-15 08:47:00 -08:00
1 parent 65082fb2f5
commit 589e84f28b
21 files changed
+1659

No files matched your search

+127
View File
@@ -0,0 +1,127 @@
<?php
declare( strict_types=1 );
namespace StreamGoals\Tests\Unit;
use PHPUnit\Framework\TestCase;
use StreamGoals\Core\FeeCalculator;
use StreamGoals\Core\Settings;
use StreamGoals\PayPal\ArrayCache;
use StreamGoals\PayPal\OrderValidator;
use StreamGoals\PayPal\RateLimiter;
use StreamGoals\PayPal\ValidationException;
final class PayPalOrderValidatorTest extends TestCase {
private const NOW = 1800000000; // fixed clock
private function goal( array $o = array() ): array {
return array_merge( array( 'id' => 3, 'currency' => 'USD', 'status' => 'active', 'starts_at' => null, 'ends_at' => null ), $o );
}
private function settings( array $o = array() ): array {
return array_merge( Settings::defaults(), $o ); // Settings::defaults() is pure
}
private function run_validate( array $in, array $goal = array(), array $settings = array() ): array {
return OrderValidator::validate( $in, $this->goal( $goal ), $this->settings( $settings ), new FeeCalculator( 349, 49 ), self::NOW );
}
private function expectCode( string $code, callable $fn ): void {
try {
$fn();
$this->fail( "expected $code" );
} catch ( ValidationException $e ) {
$this->assertSame( $code, $e->errorCode );
}
}
public function test_basic_order_without_fee_cover(): void {
$v = $this->run_validate( array( 'amount' => '10.00', 'display_name' => 'Ann', 'message' => 'hi' ) );
$this->assertSame( 1000, $v['base_minor'] );
$this->assertSame( 0, $v['fee_minor'] );
$this->assertSame( 1000, $v['total_minor'] );
$this->assertSame( 'Ann', $v['donor_name'] );
}
public function test_fee_is_server_computed_only_when_toggle_true(): void {
$on = $this->run_validate( array( 'amount' => '10.00', 'cover_fees' => true, 'fee_minor' => 1, 'total_minor' => 1 ) );
$this->assertSame( ( new FeeCalculator( 349, 49 ) )->grossUp( 1000 ), $on['total_minor'] );
$this->assertSame( 1000 + $on['fee_minor'], $on['total_minor'] );
$this->assertGreaterThan( 0, $on['fee_minor'] );
$off = $this->run_validate( array( 'amount' => '10.00', 'cover_fees' => 'false', 'total_minor' => 99999 ) );
$this->assertSame( 1000, $off['total_minor'] );
$this->assertSame( 1000 + 0, $this->run_validate( array( 'amount' => '10.00', 'cover_fees' => '0' ) )['total_minor'] );
}
public function test_amount_validation(): void {
$this->expectCode( 'invalid_amount', fn() => $this->run_validate( array( 'amount' => 'abc' ) ) );
$this->expectCode( 'invalid_amount', fn() => $this->run_validate( array( 'amount' => '-5' ) ) );
$this->expectCode( 'invalid_amount', fn() => $this->run_validate( array( 'amount' => '1.999' ) ) );
$this->expectCode( 'invalid_amount', fn() => $this->run_validate( array() ) );
$this->expectCode( 'amount_too_small', fn() => $this->run_validate( array( 'amount' => '0.99' ) ) );
$this->expectCode( 'amount_too_large', fn() => $this->run_validate( array( 'amount' => '9999999' ) ) );
}
public function test_zero_decimal_currency(): void {
$v = $this->run_validate( array( 'amount' => '500' ), array( 'currency' => 'JPY' ), array( 'min_donation_minor' => 100 ) );
$this->assertSame( 500, $v['base_minor'] );
$this->expectCode( 'invalid_amount', fn() => $this->run_validate( array( 'amount' => '5.5' ), array( 'currency' => 'JPY' ) ) );
}
public function test_goal_must_be_open(): void {
$in = array( 'amount' => '5.00' );
$this->expectCode( 'goal_inactive', fn() => $this->run_validate( $in, array( 'status' => 'paused' ) ) );
$this->expectCode( 'goal_inactive', fn() => $this->run_validate( $in, array( 'status' => 'complete' ) ) );
$this->expectCode( 'goal_inactive', fn() => $this->run_validate( $in, array( 'starts_at' => gmdate( 'Y-m-d H:i:s', self::NOW + 60 ) ) ) );
$this->expectCode( 'goal_inactive', fn() => $this->run_validate( $in, array( 'ends_at' => gmdate( 'Y-m-d H:i:s', self::NOW - 60 ) ) ) );
$ok = $this->run_validate( $in, array( 'starts_at' => gmdate( 'Y-m-d H:i:s', self::NOW - 60 ), 'ends_at' => gmdate( 'Y-m-d H:i:s', self::NOW + 60 ) ) );
$this->assertSame( 500, $ok['base_minor'] );
}
public function test_message_length_and_sanitizing(): void {
$this->expectCode( 'message_too_long', fn() => $this->run_validate( array( 'amount' => '5', 'message' => str_repeat( 'a', 201 ) ) ) );
$v = $this->run_validate( array( 'amount' => '5', 'message' => str_repeat( 'é', 200 ) ) );
$this->assertSame( 200, mb_strlen( $v['message'] ) );
$v = $this->run_validate( array( 'amount' => '5', 'message' => '<b>hey</b><script>x</script>' ) );
$this->assertStringNotContainsString( '<', $v['message'] );
}
public function test_anonymity_policies(): void {
$in = array( 'amount' => '5', 'display_name' => 'Bob', 'anonymous' => true );
$allow = $this->run_validate( $in, array(), array( 'anonymity_policy' => 'allow' ) );
$this->assertTrue( $allow['is_anonymous'] );
$this->assertSame( '', $allow['donor_name'], 'anonymous donor name must not be stored' );
$this->assertFalse( $this->run_validate( array( 'amount' => '5', 'display_name' => 'Bob' ) )['is_anonymous'] );
$req = $this->run_validate( $in, array(), array( 'anonymity_policy' => 'require_name' ) );
$this->assertFalse( $req['is_anonymous'] );
$this->assertSame( 'Bob', $req['donor_name'] );
$this->expectCode( 'name_required', fn() => $this->run_validate( array( 'amount' => '5' ), array(), array( 'anonymity_policy' => 'require_name' ) ) );
$force = $this->run_validate( array( 'amount' => '5', 'display_name' => 'Bob', 'anonymous' => false ), array(), array( 'anonymity_policy' => 'force_anonymous' ) );
$this->assertTrue( $force['is_anonymous'] );
$this->assertSame( '', $force['donor_name'] );
}
public function test_moderation_flag(): void {
$this->assertSame( 'pending', $this->run_validate( array( 'amount' => '5', 'message' => 'x' ), array(), array( 'moderate_messages' => true ) )['moderation'] );
$this->assertSame( 'approved', $this->run_validate( array( 'amount' => '5' ), array(), array( 'moderate_messages' => true ) )['moderation'] );
$this->assertSame( 'approved', $this->run_validate( array( 'amount' => '5', 'message' => 'x' ) )['moderation'] );
}
public function test_quote_ignores_name_policy(): void {
$q = OrderValidator::quote( array( 'amount' => '10', 'cover_fees' => true ), $this->goal(), $this->settings( array( 'anonymity_policy' => 'require_name' ) ), new FeeCalculator( 349, 49 ), self::NOW );
$this->assertSame( 1000 + $q['fee_minor'], $q['total_minor'] );
}
public function test_rate_limiter(): void {
$rl = new RateLimiter( new ArrayCache() );
$this->assertTrue( $rl->allow( 'a', 2, 60 ) );
$this->assertTrue( $rl->allow( 'a', 2, 60 ) );
$this->assertFalse( $rl->allow( 'a', 2, 60 ) );
$this->assertTrue( $rl->allow( 'b', 2, 60 ) );
}
}