Make bonus rules visible: highlighted BONUS line on the alerts overlay, streamgoals_bonus action
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1e2ca96eed
commit
c7f2dcffeb
9 files changed
+187
-3
No files matched your search
@@ -39,6 +39,7 @@ final class EngineToOverlayIntegrationTest extends TestCase {
|
||||
}
|
||||
public function counter( int $goalId ): array { return array( 'label' => 'Pies', 'value' => $this->s->getInt( $goalId, 'counter:2' ) ); }
|
||||
public function donations( ?int $sinceId, int $limit ): array { return array(); }
|
||||
public function bonusesFor( array $donationIds ): array { return array(); }
|
||||
} );
|
||||
|
||||
$d = new Donation( 7, 1, 2500, 0, 2500, 'USD' ); // $25 -> +25 min, counter +2
|
||||
@@ -53,4 +54,55 @@ final class EngineToOverlayIntegrationTest extends TestCase {
|
||||
$engine->reverseDonation( $d );
|
||||
$this->assertSame( 0, $svc->payload( 'counter', $now )['value'] );
|
||||
}
|
||||
|
||||
private function bonusOverlay( FakeEventStore $events, array $donations ): OverlayService {
|
||||
return new OverlayService( new class( $events, $donations ) implements OverlayDataSource {
|
||||
public function __construct( private FakeEventStore $ev, private array $rows ) {}
|
||||
public function activeGoal(): ?array { return array( 'id' => 1, 'title' => 'G', 'target_minor' => 5000, 'currency' => 'USD' ); }
|
||||
public function raisedMinor( array $goal ): int { return 0; }
|
||||
public function stateValue( int $goalId, string $key ): ?string { return null; }
|
||||
public function counter( int $goalId ): array { return array( 'label' => '', 'value' => 0 ); }
|
||||
public function donations( ?int $sinceId, int $limit ): array { return $this->rows; }
|
||||
public function bonusesFor( array $donationIds ): array {
|
||||
$out = array();
|
||||
foreach ( $this->ev->events as $k => $e ) {
|
||||
[ , $did ] = array_map( 'intval', explode( ':', $k ) );
|
||||
if ( 1 === $e[0] && ! $e[1] && in_array( $did, $donationIds, true ) ) {
|
||||
$out[ $did ][] = 'big_donation';
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
public function test_bonus_over_threshold_visible_in_alerts_below_is_not_and_refund_removes(): void {
|
||||
$events = new FakeEventStore();
|
||||
$fired = array();
|
||||
$engine = new Engine(
|
||||
new FakeRuleStore( array( new RuleDef( 3, 1, 'bonus', array( 'min_minor' => 5000, 'event' => 'big_donation' ) ) ) ),
|
||||
$events,
|
||||
new FakeStateRepository(),
|
||||
null,
|
||||
function ( string $event, RuleDef $r, Donation $d, bool $rev ) use ( &$fired ): void {
|
||||
$fired[] = array( $event, $d->id, $rev );
|
||||
}
|
||||
);
|
||||
$big = new Donation( 10, 1, 5000, 0, 5000, 'USD' );
|
||||
$small = new Donation( 11, 1, 4999, 0, 4999, 'USD' );
|
||||
$engine->applyDonation( $big );
|
||||
$engine->applyDonation( $small );
|
||||
$this->assertSame( array( array( 'big_donation', 10, false ) ), $fired );
|
||||
|
||||
$row = static fn( int $id ): array => array( 'id' => $id, 'base_minor' => 5000, 'currency' => 'USD', 'donor_name' => 'A',
|
||||
'is_anonymous' => 0, 'message' => '', 'status' => 'completed', 'moderation' => 'approved' );
|
||||
$svc = $this->bonusOverlay( $events, array( $row( 10 ), $row( 11 ) ) );
|
||||
$alerts = $svc->payload( 'alerts', 1, 0 )['alerts'];
|
||||
$this->assertSame( array( 'Big donation' ), $alerts[0]['bonuses'] );
|
||||
$this->assertSame( array(), $alerts[1]['bonuses'] );
|
||||
|
||||
$engine->reverseDonation( $big );
|
||||
$this->assertSame( array(), $svc->payload( 'alerts', 1, 0 )['alerts'][0]['bonuses'] );
|
||||
$this->assertSame( true, $fired[1][2] );
|
||||
}
|
||||
}
|
||||
@@ -96,6 +96,41 @@ final class OverlayPayloadsTest extends TestCase {
|
||||
$this->assertSame( Payloads::etag( $g ), Payloads::etag( $g ) );
|
||||
}
|
||||
|
||||
public function test_alert_bonuses_humanized_and_empty_when_none(): void {
|
||||
$out = Payloads::alerts( array( $this->don( 1, array( 'bonuses' => array( 'big_donation', 'mega-win' ) ) ), $this->don( 2 ) ), 0 );
|
||||
$this->assertSame( array( 'Big donation', 'Mega win' ), $out['alerts'][0]['bonuses'] );
|
||||
$this->assertSame( array(), $out['alerts'][1]['bonuses'] );
|
||||
}
|
||||
|
||||
public function test_alert_bonuses_sanitize_hostile_event(): void {
|
||||
$out = Payloads::alerts( array( $this->don( 1, array( 'bonuses' => array( '<img src=x onerror=alert(1)>', '"><script>', '<>', 42, 'big_donation', 'BIG_DONATION' ) ) ) ), 0 );
|
||||
$b = $out['alerts'][0]['bonuses'];
|
||||
$this->assertSame( 'Imgsrcxonerroralert1', $b[0] );
|
||||
foreach ( $b as $label ) {
|
||||
$this->assertMatchesRegularExpression( '/^[A-Za-z0-9 ]+$/', $label );
|
||||
}
|
||||
$this->assertSame( 1, count( array_keys( $b, 'Big donation', true ) ) ); // deduped
|
||||
}
|
||||
|
||||
public function test_bonuses_do_not_affect_masking_moderation_or_since(): void {
|
||||
$rows = array(
|
||||
$this->don( 1, array( 'bonuses' => array( 'big_donation' ) ) ),
|
||||
$this->don( 2, array( 'is_anonymous' => 1, 'donor_name' => 'Secret Sam', 'bonuses' => array( 'big_donation' ) ) ),
|
||||
$this->don( 3, array( 'moderation' => 'pending', 'bonuses' => array( 'big_donation' ) ) ),
|
||||
);
|
||||
$out = Payloads::alerts( $rows, 1 );
|
||||
$this->assertSame( array( 2 ), array_column( $out['alerts'], 'id' ) );
|
||||
$this->assertSame( 'Anonymous', $out['alerts'][0]['name'] );
|
||||
$this->assertStringNotContainsString( 'Secret Sam', json_encode( $out ) );
|
||||
$this->assertSame( array(), Payloads::alerts( $rows, null )['alerts'] );
|
||||
}
|
||||
|
||||
public function test_etag_changes_when_bonus_added(): void {
|
||||
$plain = Payloads::alerts( array( $this->don( 5 ) ), 0 );
|
||||
$bonus = Payloads::alerts( array( $this->don( 5, array( 'bonuses' => array( 'big_donation' ) ) ) ), 0 );
|
||||
$this->assertNotSame( Payloads::etag( $plain ), Payloads::etag( $bonus ) );
|
||||
}
|
||||
|
||||
public function test_style_sanitizes(): void {
|
||||
$s = Style::fromQuery( array( 'theme' => 'bogus', 'accent' => '#FF00AA', 'color' => 'red;}</style>', 'bg' => '11223344',
|
||||
'font' => 'x;}body{', 'scale' => '99' ) );
|
||||
@@ -114,6 +149,7 @@ final class OverlayPayloadsTest extends TestCase {
|
||||
public function raisedMinor( array $goal ): int { return 250; }
|
||||
public function stateValue( int $goalId, string $key ): ?string { return 'timer_ends_at' === $key ? '2000' : null; }
|
||||
public function counter( int $goalId ): array { return array( 'label' => 'Pies', 'value' => 4 ); }
|
||||
public function bonusesFor( array $donationIds ): array { return array( 3 => array( 'big_donation' ) ); }
|
||||
public function donations( ?int $sinceId, int $limit ): array { return array( array( 'id' => 3, 'base_minor' => 100, 'currency' => 'USD', 'donor_name' => 'Z', 'is_anonymous' => 1, 'message' => '', 'status' => 'completed', 'moderation' => 'approved' ) ); }
|
||||
};
|
||||
$svc = new OverlayService( $src );
|
||||
@@ -121,6 +157,7 @@ final class OverlayPayloadsTest extends TestCase {
|
||||
$this->assertSame( 2000, $svc->payload( 'timer', 1500 )['ends_at'] );
|
||||
$this->assertSame( 4, $svc->payload( 'counter', 1 )['value'] );
|
||||
$this->assertSame( 'Anonymous', $svc->payload( 'alerts', 1, 0 )['alerts'][0]['name'] );
|
||||
$this->assertSame( array( 'Big donation' ), $svc->payload( 'alerts', 1, 0 )['alerts'][0]['bonuses'] );
|
||||
$this->assertNull( $svc->payload( 'nope', 1 ) );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user