$id, 'base_minor' => 500, 'currency' => 'USD', 'donor_name' => 'Al', 'is_anonymous' => 0, 'message' => '', 'status' => 'completed', 'moderation' => 'approved' ); } /** Donation 1 is older than the recent window; only 2 is "recent". */ private function svc( array $db, array $requests ): OverlayService { return new OverlayService( new class( $db, $requests ) implements OverlayDataSource, RecentDonationsSource, ReplaySource { public function __construct( private array $db, private array $requests ) {} public function activeGoal(): ?array { return null; } 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 bonusesFor( array $ids ): array { return array_fill_keys( $ids, array( 'big_donation' ) ); } public function donations( ?int $sinceId, int $limit ): array { return array(); } public function recentDonations( int $limit ): array { return array_slice( $this->db, -1 ); } public function pendingReplays( int $now ): array { return $this->requests; } public function donationsByIds( array $ids ): array { return array_values( array_filter( $this->db, static fn( $d ) => in_array( $d['id'], $ids, true ) ) ); } } ); } public function test_old_donation_is_replayed_with_its_extras_once_per_request(): void { $svc = $this->svc( array( $this->row( 1 ), $this->row( 2 ) ), array( array( 'seq' => 100, 'id' => 1 ), array( 'seq' => 101, 'id' => 1 ) ) ); $out = $svc->payload( 'alerts', 1, null, true ); $this->assertSame( array( 2 ), array_column( $out['alerts'], 'id' ) ); $this->assertSame( array( 100, 101 ), array_column( $out['replays'], 'replay' ) ); $this->assertSame( array( 1, 1 ), array_column( $out['replays'], 'id' ) ); $this->assertSame( array( 'Big donation' ), $out['replays'][0]['bonuses'] ); $this->assertSame( array( 'Big donation' ), $out['alerts'][0]['bonuses'] ); } public function test_no_requests_leaves_the_payload_and_etag_unchanged(): void { $out = $this->svc( array( $this->row( 2 ) ), array() )->payload( 'alerts', 1, null, true ); $this->assertSame( array( 'alerts', 'last_id' ), array_keys( $out ) ); $with = $this->svc( array( $this->row( 2 ) ), array( array( 'seq' => 5, 'id' => 2 ) ) )->payload( 'alerts', 1, null, true ); $this->assertNotSame( Payloads::etag( $out ), Payloads::etag( $with ) ); } public function test_held_rejected_and_pending_donations_are_never_replayed_and_names_stay_masked(): void { $rows = array( $this->row( 1, array( 'moderation' => 'pending' ) ), $this->row( 2, array( 'moderation' => 'rejected' ) ), $this->row( 3, array( 'status' => 'pending' ) ), $this->row( 4, array( 'is_anonymous' => 1, 'donor_name' => 'Secret' ) ) ); $requests = array_map( static fn( $i ) => array( 'seq' => $i, 'id' => $i ), array( 1, 2, 3, 4, 99 ) ); $out = Payloads::replays( $rows, $requests ); $this->assertSame( array( 4 ), array_column( $out, 'id' ) ); $this->assertSame( 'Anonymous', $out[0]['name'] ); foreach ( array_slice( $rows, 0, 3 ) as $r ) { $this->assertFalse( DonationsPage::canReplay( $r ) ); } $this->assertTrue( DonationsPage::canReplay( $rows[3] ) ); } public function test_queue_numbers_increase_expire_and_are_capped(): void { $q = Replays::push( false, 7, 1000 ); $q = Replays::push( $q, 7, 1000 ); $this->assertSame( array( 1000, 1001 ), array_column( $q, 'seq' ) ); $this->assertCount( 2, Replays::active( $q, 1000 + Replays::TTL - 1 ) ); $this->assertSame( array(), Replays::active( $q, 1000 + Replays::TTL ) ); $this->assertSame( array( 2000 ), array_column( Replays::push( $q, 8, 2000 ), 'seq' ) ); for ( $i = 0; $i < 30; $i++ ) { $q = Replays::push( $q, 7, 1000 ); } $this->assertCount( Replays::MAX, $q ); $this->assertSame( array(), Replays::active( array( 'junk', array( 'seq' => 1 ) ), 1000 ) ); } }