Hold donations with flagged donor names for moderation; reconcile lost side effects (schema v6)
Test and Release / test (push) Successful in 1m28s
Test and Release / release (push) Successful in 7s

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
shadowdaoandClaude Sonnet 5.5 committed 2026-11-15 17:07:32 -08:00
1 parent 473fe192cd
commit 353f7ca5be
16 files changed
+681 -41

No files matched your search

+92
View File
@@ -0,0 +1,92 @@
<?php
declare( strict_types=1 );
namespace StreamGoals\Tests\Unit;
use PHPUnit\Framework\TestCase;
use StreamGoals\Admin\Format;
use StreamGoals\Core\FeeCalculator;
use StreamGoals\Core\ProfanityFilter;
use StreamGoals\Core\Settings;
use StreamGoals\Overlay\Payloads;
use StreamGoals\PayPal\OrderValidator;
/** A donor name that trips the profanity filter is masked, the donation is held for moderation, and no alert shows it. */
final class DonorNameFlaggingTest extends TestCase {
private function validate( array $in, array $settings = array() ): array {
$goal = array( 'id' => 3, 'currency' => 'USD', 'status' => 'active', 'starts_at' => null, 'ends_at' => null );
return OrderValidator::validate( $in + array( 'amount' => '10.00' ), $goal, array_merge( Settings::defaults(), array( 'moderate_messages' => false ), $settings ), new FeeCalculator( 349, 49 ), 1800000000 );
}
public function test_matches_agrees_with_filter(): void {
$f = new ProfanityFilter( array( 'profanity_words' => 'jerk' ) );
$this->assertTrue( $f->matches( 'Big Jerk' ) );
$this->assertTrue( $f->matches( 'sh1t head' ), 'leetspeak counts' );
$this->assertFalse( $f->matches( 'Scunthorpe class' ) );
$this->assertFalse( $f->matches( '' ) );
$this->assertFalse( ( new ProfanityFilter( array( 'profanity_filter' => false ) ) )->matches( 'shit' ) );
}
public function test_flagged_name_is_masked_and_held_even_when_message_moderation_is_off(): void {
$v = $this->validate( array( 'display_name' => 'Big Jerk', 'message' => 'thanks' ), array( 'profanity_words' => 'jerk' ) );
$this->assertSame( 'Big ****', $v['donor_name'] );
$this->assertSame( 'pending', $v['moderation'] );
$this->assertSame( 'name', $v['moderation_flags'] );
}
public function test_clean_name_and_off_switch_stay_approved(): void {
$v = $this->validate( array( 'display_name' => 'Ann', 'message' => 'hi' ) );
$this->assertSame( array( 'approved', '' ), array( $v['moderation'], $v['moderation_flags'] ) );
$v = $this->validate( array( 'display_name' => 'shit', 'message' => '' ), array( 'profanity_filter' => false ) );
$this->assertSame( array( 'approved', '' ), array( $v['moderation'], $v['moderation_flags'] ) );
$this->assertSame( 'shit', $v['donor_name'] );
}
public function test_message_only_and_both_flags(): void {
$v = $this->validate( array( 'display_name' => 'Ann', 'message' => 'hello' ), array( 'moderate_messages' => true ) );
$this->assertSame( array( 'pending', 'message' ), array( $v['moderation'], $v['moderation_flags'] ) );
$v = $this->validate( array( 'display_name' => 'shit', 'message' => 'hello' ), array( 'moderate_messages' => true ) );
$this->assertSame( array( 'pending', 'name,message' ), array( $v['moderation'], $v['moderation_flags'] ) );
$v = $this->validate( array( 'display_name' => 'shit', 'message' => '' ), array( 'moderate_messages' => true ) );
$this->assertSame( 'name', $v['moderation_flags'], 'empty message is never flagged' );
}
public function test_anonymous_donor_name_never_stored_and_never_flags(): void {
$v = $this->validate( array( 'display_name' => 'shit', 'anonymous' => true, 'message' => '' ) );
$this->assertSame( '', $v['donor_name'] );
$this->assertSame( array( 'approved', '' ), array( $v['moderation'], $v['moderation_flags'] ) );
$v = $this->validate( array( 'display_name' => 'shit', 'message' => '' ), array( 'anonymity_policy' => 'force_anonymous' ) );
$this->assertSame( 'approved', $v['moderation'] );
}
public function test_flagged_label(): void {
$this->assertSame( 'Name', Format::flaggedLabel( array( 'moderation_flags' => 'name' ) ) );
$this->assertSame( 'Message', Format::flaggedLabel( array( 'moderation_flags' => 'message' ) ) );
$this->assertSame( 'Name and message', Format::flaggedLabel( array( 'moderation_flags' => 'name,message' ) ) );
$this->assertSame( 'Message', Format::flaggedLabel( array( 'moderation_flags' => '' ) ), 'rows held before v6' );
$this->assertSame( 'Message', Format::flaggedLabel( array() ) );
}
public function test_alert_for_held_name_is_not_emitted_until_approved_then_shows_masked_name(): void {
$row = array( 'id' => 5, 'base_minor' => 500, 'currency' => 'USD', 'donor_name' => 'Big ****', 'is_anonymous' => 0,
'message' => '', 'status' => 'completed', 'moderation' => 'pending' );
foreach ( array( 'pending', 'rejected' ) as $m ) {
foreach ( array( false, true ) as $recent ) {
$out = Payloads::alerts( array( array( 'moderation' => $m ) + $row ), 0, 10, $recent );
$this->assertSame( array(), $out['alerts'], "$m (recent=" . (int) $recent . ') must hold the whole alert' );
}
}
$out = Payloads::alerts( array( array( 'moderation' => 'approved' ) + $row ), 0 );
$this->assertSame( 'Big ****', $out['alerts'][0]['name'] );
}
public function test_flags_are_wired_through_storage_and_data_sources(): void {
$root = dirname( __DIR__, 2 ) . '/includes/';
$this->assertStringContainsString( "'moderation_flags' => (string)", (string) file_get_contents( $root . 'PayPal/DonationStore.php' ) );
$this->assertStringContainsString( 'moderation_flags VARCHAR(16) NOT NULL DEFAULT', (string) file_get_contents( $root . 'Core/Installer.php' ) );
foreach ( array( 'Overlay/WpdbDataSource.php', 'Core/DonationRepository.php' ) as $f ) {
$this->assertStringContainsString( "moderation = 'approved'", (string) file_get_contents( $root . $f ), $f );
}
}
}