Drop refund handling, finalize captures via webhook, add profanity filter
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
35f5d1acfb
commit
2e3abf33d9
11 files changed
+420
-82
No files matched your search
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace StreamGoals\Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use StreamGoals\Core\Money;
|
||||
use StreamGoals\PayPal\CaptureFinalizer;
|
||||
use StreamGoals\PayPal\WebhookEvent;
|
||||
|
||||
final class PayPalCaptureFinalizerTest extends TestCase {
|
||||
|
||||
/** In-memory donation with an atomic pending->completed transition. */
|
||||
private function store( string $status = 'pending' ): object {
|
||||
return new class( $status ) {
|
||||
public array $row;
|
||||
public int $applied = 0;
|
||||
public function __construct( string $status ) {
|
||||
$this->row = array( 'id' => 7, 'status' => $status, 'total_minor' => 500, 'currency' => 'USD', 'paypal_capture_id' => '' );
|
||||
}
|
||||
public function mark( int $id, string $cap ): bool {
|
||||
if ( 'pending' !== $this->row['status'] ) {
|
||||
return false;
|
||||
}
|
||||
$this->row['status'] = 'completed';
|
||||
$this->row['paypal_capture_id'] = $cap;
|
||||
return true;
|
||||
}
|
||||
public function notify( int $id ): void {
|
||||
$this->applied++;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private function run_finalize( object $s, string $status, string $cap, ?Money $amt ): string {
|
||||
return CaptureFinalizer::finalize( $s->row, $status, $cap, $amt, array( $s, 'mark' ), array( $s, 'notify' ) );
|
||||
}
|
||||
|
||||
public function test_pending_capture_status_stays_pending(): void {
|
||||
$s = $this->store();
|
||||
$this->assertSame( 'pending', $this->run_finalize( $s, 'PENDING', 'CAP1', new Money( 500, 'USD' ) ) );
|
||||
$this->assertSame( 'pending', $s->row['status'] );
|
||||
$this->assertSame( 0, $s->applied );
|
||||
}
|
||||
|
||||
public function test_amount_or_currency_mismatch_or_missing_id_stays_pending(): void {
|
||||
foreach ( array( array( 'CAP1', new Money( 499, 'USD' ) ), array( 'CAP1', new Money( 500, 'EUR' ) ), array( 'CAP1', null ), array( '', new Money( 500, 'USD' ) ) ) as [ $cap, $amt ] ) {
|
||||
$s = $this->store();
|
||||
$this->assertSame( 'pending', $this->run_finalize( $s, 'COMPLETED', $cap, $amt ) );
|
||||
$this->assertSame( 'pending', $s->row['status'] );
|
||||
$this->assertSame( 0, $s->applied );
|
||||
}
|
||||
}
|
||||
|
||||
public function test_completed_webhook_finalizes_pending_donation_exactly_once(): void {
|
||||
$s = $this->store();
|
||||
// Capture endpoint saw PENDING first...
|
||||
$this->assertSame( 'pending', $this->run_finalize( $s, 'PENDING', '', null ) );
|
||||
// ...then the COMPLETED webhook arrives, and is replayed twice.
|
||||
$ev = WebhookEvent::fromArray( array(
|
||||
'id' => 'E1', 'event_type' => 'PAYMENT.CAPTURE.COMPLETED',
|
||||
'resource' => array( 'id' => 'CAP1', 'custom_id' => '7', 'amount' => array( 'value' => '5.00', 'currency_code' => 'USD' ) ),
|
||||
) );
|
||||
$this->assertSame( 'completed', $this->run_finalize( $s, 'COMPLETED', $ev->captureId, $ev->amount ) );
|
||||
$this->assertSame( 'duplicate', $this->run_finalize( $s, 'COMPLETED', $ev->captureId, $ev->amount ) );
|
||||
$this->assertSame( 'duplicate', $this->run_finalize( $s, 'COMPLETED', $ev->captureId, $ev->amount ) );
|
||||
$this->assertSame( 'completed', $s->row['status'] );
|
||||
$this->assertSame( 'CAP1', $s->row['paypal_capture_id'] );
|
||||
$this->assertSame( 1, $s->applied );
|
||||
}
|
||||
|
||||
public function test_lost_race_does_not_apply_twice(): void {
|
||||
$s = $this->store();
|
||||
$lost = CaptureFinalizer::finalize( $s->row, 'COMPLETED', 'CAP1', new Money( 500, 'USD' ), static fn(): bool => false, array( $s, 'notify' ) );
|
||||
$this->assertSame( 'duplicate', $lost );
|
||||
$this->assertSame( 0, $s->applied );
|
||||
}
|
||||
|
||||
public function test_non_pending_rows_are_never_touched(): void {
|
||||
foreach ( array( 'completed', 'refunded', 'reversed' ) as $st ) {
|
||||
$s = $this->store( $st );
|
||||
$this->assertSame( 'duplicate', $this->run_finalize( $s, 'COMPLETED', 'CAP1', new Money( 500, 'USD' ) ) );
|
||||
$this->assertSame( $st, $s->row['status'] );
|
||||
$this->assertSame( 0, $s->applied );
|
||||
}
|
||||
}
|
||||
|
||||
public function test_refund_events_are_not_registered_or_parsed_specially(): void {
|
||||
$this->assertSame( array( 'PAYMENT.CAPTURE.COMPLETED' ), \StreamGoals\PayPal\Client::WEBHOOK_EVENTS );
|
||||
$ev = WebhookEvent::fromArray( array( 'id' => 'E2', 'event_type' => 'PAYMENT.CAPTURE.REFUNDED', 'resource' => array( 'id' => 'REF1' ) ) );
|
||||
$this->assertSame( 'PAYMENT.CAPTURE.REFUNDED', $ev->type ); // controller returns 'ignored' (HTTP 200) for it
|
||||
}
|
||||
}
|
||||
@@ -160,13 +160,13 @@ final class PayPalClientTest extends TestCase {
|
||||
$this->assertFalse( $c->verifyWebhook( $headers, $raw, 'WHID' ) );
|
||||
}
|
||||
|
||||
public function test_create_webhook_registers_three_events(): void {
|
||||
public function test_create_webhook_registers_only_completed_event(): void {
|
||||
$c = $this->client();
|
||||
$this->http->queue = array( $this->token(), FakeTransport::json( 201, array( 'id' => 'WH77' ) ) );
|
||||
$this->assertSame( 'WH77', $c->createWebhook( 'https://x.test/wp-json/streamgoals/v1/webhook' ) );
|
||||
$body = json_decode( $this->http->calls[1]['body'], true );
|
||||
$names = array_column( $body['event_types'], 'name' );
|
||||
$this->assertSame( array( 'PAYMENT.CAPTURE.COMPLETED', 'PAYMENT.CAPTURE.REFUNDED', 'PAYMENT.CAPTURE.REVERSED' ), $names );
|
||||
$this->assertSame( array( 'PAYMENT.CAPTURE.COMPLETED' ), $names );
|
||||
}
|
||||
|
||||
public function test_test_connection_messages(): void {
|
||||
@@ -197,15 +197,5 @@ final class PayPalClientTest extends TestCase {
|
||||
$this->assertSame( '7', $done->customId );
|
||||
$this->assertSame( 'ORD', $done->orderId );
|
||||
$this->assertSame( 500, $done->amount->minor );
|
||||
|
||||
$ref = WebhookEvent::fromArray( array(
|
||||
'id' => 'E2', 'event_type' => 'PAYMENT.CAPTURE.REFUNDED',
|
||||
'resource' => array( 'id' => 'REF1', 'links' => array(
|
||||
array( 'rel' => 'self', 'href' => 'https://api/v2/payments/refunds/REF1' ),
|
||||
array( 'rel' => 'up', 'href' => 'https://api/v2/payments/captures/CAP1' ),
|
||||
) ),
|
||||
) );
|
||||
$this->assertSame( 'CAP1', $ref->captureId );
|
||||
$this->assertNull( $ref->amount );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace StreamGoals\Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use StreamGoals\Core\ProfanityFilter;
|
||||
use StreamGoals\Core\Settings;
|
||||
use StreamGoals\PayPal\OrderValidator;
|
||||
use StreamGoals\Core\FeeCalculator;
|
||||
|
||||
final class ProfanityFilterTest extends TestCase {
|
||||
|
||||
private function f( array $o = array() ): ProfanityFilter {
|
||||
return new ProfanityFilter( array_merge( Settings::defaults(), $o ) );
|
||||
}
|
||||
|
||||
public function test_builtin_masks_with_equal_length_case_insensitive(): void {
|
||||
$this->assertSame( 'what the **** is this', $this->f()->filter( 'what the shit is this' ) );
|
||||
$this->assertSame( 'what the **** is this', $this->f()->filter( 'what the SHiT is this' ) );
|
||||
}
|
||||
|
||||
public function test_whole_word_only_scunthorpe(): void {
|
||||
$f = $this->f( array( 'profanity_words' => "ass\ncock" ) );
|
||||
foreach ( array( 'Scunthorpe', 'class', 'assist', 'Cockburn', 'bassist', 'passage', 'shitake' ) as $w ) {
|
||||
$this->assertSame( $w, $f->filter( $w ), $w );
|
||||
}
|
||||
$this->assertSame( 'a *** b', $f->filter( 'a ass b' ) );
|
||||
$this->assertSame( '***, ***!', $f->filter( 'ass, ass!' ) );
|
||||
}
|
||||
|
||||
public function test_leetspeak(): void {
|
||||
$f = $this->f();
|
||||
$this->assertSame( '****', $f->filter( 'sh1t' ) );
|
||||
$this->assertSame( '****', $f->filter( '$h!t' ) );
|
||||
$f2 = $this->f( array( 'profanity_words' => 'bad' ) );
|
||||
$this->assertSame( '***', $f2->filter( 'b@d' ) );
|
||||
$this->assertSame( '***', $f2->filter( 'B4D' ) );
|
||||
$f3 = $this->f( array( 'profanity_words' => 'fool' ) );
|
||||
$this->assertSame( '****', $f3->filter( 'f00l' ) );
|
||||
}
|
||||
|
||||
public function test_pure_numbers_and_trailing_punctuation_untouched(): void {
|
||||
$f = $this->f( array( 'profanity_words' => "ass\nsose" ) );
|
||||
$this->assertSame( '455 and 5053', $f->filter( '455 and 5053' ) ); // "ass"/"sose" via leet, but no letters
|
||||
$this->assertSame( 'wow! great!!', $f->filter( 'wow! great!!' ) );
|
||||
$this->assertSame( 'hello!! ***!!', $f->filter( 'hello!! ass!!' ) );
|
||||
}
|
||||
|
||||
public function test_unicode(): void {
|
||||
$f = $this->f( array( 'profanity_words' => "scheiße\nмат\nÄRGER" ) );
|
||||
$this->assertSame( 'oh ******* no', $f->filter( 'oh Scheiße no' ) );
|
||||
$this->assertSame( 'это ***', $f->filter( 'это МАТ' ) );
|
||||
$this->assertSame( '*****!', $f->filter( 'ärger!' ) );
|
||||
$this->assertSame( 'матрос', $f->filter( 'матрос' ) );
|
||||
$this->assertSame( '😀 *** 😀', $f->filter( '😀 мат 😀' ) );
|
||||
}
|
||||
|
||||
public function test_streamer_words_parsing(): void {
|
||||
$raw = "# comment\n\n foo \r\nBAR\n#baz\ntwo words\n" . str_repeat( 'x', 41 ) . "\nfoo\n";
|
||||
$this->assertSame( array( 'foo', 'BAR' ), ProfanityFilter::parseWords( $raw ) );
|
||||
$f = $this->f( array( 'profanity_use_builtin' => false, 'profanity_words' => $raw ) );
|
||||
$this->assertSame( '*** *** baz words', $f->filter( 'foo bar baz words' ) );
|
||||
}
|
||||
|
||||
public function test_word_count_cap(): void {
|
||||
$raw = implode( "\n", array_map( static fn( $i ) => 'w' . $i, range( 1, 2000 ) ) );
|
||||
$this->assertLessThanOrEqual( ProfanityFilter::MAX_WORDS, count( ProfanityFilter::parseWords( $raw ) ) );
|
||||
}
|
||||
|
||||
public function test_empty_list_and_builtin_off(): void {
|
||||
$f = $this->f( array( 'profanity_use_builtin' => false, 'profanity_words' => '' ) );
|
||||
$this->assertFalse( $f->isEnabled() );
|
||||
$this->assertSame( 'shit happens', $f->filter( 'shit happens' ) );
|
||||
$f = $this->f( array( 'profanity_use_builtin' => false, 'profanity_words' => 'zzz' ) );
|
||||
$this->assertSame( 'shit ***', $f->filter( 'shit zzz' ) );
|
||||
}
|
||||
|
||||
public function test_master_switch_off(): void {
|
||||
$f = $this->f( array( 'profanity_filter' => false, 'profanity_words' => 'foo' ) );
|
||||
$this->assertSame( 'shit foo', $f->filter( 'shit foo' ) );
|
||||
$f = $this->f( array( 'profanity_filter' => '0' ) );
|
||||
$this->assertSame( 'shit', $f->filter( 'shit' ) );
|
||||
}
|
||||
|
||||
public function test_never_throws_on_odd_input(): void {
|
||||
$f = $this->f();
|
||||
$this->assertSame( '', $f->filter( '' ) );
|
||||
$bad = "ok \xC3\x28 shit \xFF";
|
||||
$out = $f->filter( $bad );
|
||||
$this->assertIsString( $out );
|
||||
$this->assertStringContainsString( '****', $out );
|
||||
$this->assertIsString( $f->filter( str_repeat( 'a', 100000 ) ) );
|
||||
$this->assertSame( '\\ $ @ ! ? ^ (', $f->filter( '\\ $ @ ! ? ^ (' ) );
|
||||
$weird = new ProfanityFilter( array( 'profanity_words' => array( 'x' ), 'profanity_filter' => null ) );
|
||||
$this->assertIsString( $weird->filter( 'x' ) );
|
||||
$this->assertIsString( ( new ProfanityFilter( array() ) )->filter( 'shit' ) );
|
||||
}
|
||||
|
||||
public function test_validator_filters_name_and_message_before_storing(): void {
|
||||
$goal = array( 'id' => 3, 'currency' => 'USD', 'status' => 'active', 'starts_at' => null, 'ends_at' => null );
|
||||
$s = array_merge( Settings::defaults(), array( 'profanity_words' => 'jerk' ) );
|
||||
$v = OrderValidator::validate( array( 'amount' => '10.00', 'display_name' => 'Big Jerk', 'message' => 'you shit, class act' ), $goal, $s, new FeeCalculator( 349, 49 ), 1800000000 );
|
||||
$this->assertSame( 'Big ****', $v['donor_name'] );
|
||||
$this->assertSame( 'you ****, class act', $v['message'] );
|
||||
|
||||
$s['profanity_filter'] = false;
|
||||
$v = OrderValidator::validate( array( 'amount' => '10.00', 'display_name' => 'Big Jerk', 'message' => 'shit' ), $goal, $s, new FeeCalculator( 349, 49 ), 1800000000 );
|
||||
$this->assertSame( 'Big Jerk', $v['donor_name'] );
|
||||
$this->assertSame( 'shit', $v['message'] );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user