$id, 'goal_id' => 0, 'trigger_type' => 'amount', 'min_minor' => 100, 'action_id' => 0, 'visual_id' => 10, 'visual_type' => 'image', 'sound_id' => 12, 'volume' => 60, 'duration_ms' => 7000, 'message' => '', 'enabled' => 1, 'sort' => 0 ); } private function row( int $id, array $o = array() ): array { return $o + array( 'id' => $id, 'goal_id' => 7, 'base_minor' => 2500, 'currency' => 'USD', 'donor_name' => 'Al', 'is_anonymous' => 0, 'message' => '', 'status' => 'completed', 'moderation' => 'approved' ); } private function files(): array { return array( 10 => array( 'url' => 'https://cdn.test/a.gif', 'mime' => 'image/gif' ), 11 => array( 'url' => 'https://cdn.test/v.mp4', 'mime' => 'video/mp4' ), 12 => array( 'url' => 'https://cdn.test/s.mp3', 'mime' => 'audio/mpeg' ), 13 => array( 'url' => 'https://cdn.test/x.svg', 'mime' => 'image/svg+xml' ), 14 => array( 'url' => 'javascript:alert(1)', 'mime' => 'image/png' ), ); } private function build( array $def, array $row = array(), array $triggered = array() ): ?array { $files = $this->files(); return AlertMedia::build( $row ?: $this->row( 1 ), 'Al', '$25.00', array( $def ), $triggered, static fn( int $id ): ?array => $files[ $id ] ?? null ); } public function test_shape_for_image_and_sound(): void { $m = $this->build( $this->def( 1, array( 'message' => '{name} gave {amount}' ) ) ); $this->assertSame( array( 'visual' => array( 'type' => 'image', 'url' => 'https://cdn.test/a.gif' ), 'sound' => array( 'url' => 'https://cdn.test/s.mp3' ), 'volume' => 60, 'duration_ms' => 7000, 'message' => 'Al gave $25.00', 'frameless' => false, ), $m ); } public function test_video_duration_is_capped_and_type_comes_from_mime(): void { $m = $this->build( $this->def( 1, array( 'visual_id' => 11, 'visual_type' => 'image', 'duration_ms' => 2000 ) ) ); $this->assertSame( 'video', $m['visual']['type'] ); $this->assertSame( 30000, $m['duration_ms'] ); $this->assertNull( $m['message'] ); } public function test_invalid_media_dropped_silently(): void { $m = $this->build( $this->def( 1, array( 'visual_id' => 13, 'sound_id' => 99 ) ) ); $this->assertNull( $m, 'nothing usable and no message: plain alert' ); $m = $this->build( $this->def( 1, array( 'visual_id' => 13, 'sound_id' => 12 ) ) ); $this->assertNull( $m['visual'] ); $this->assertSame( 'https://cdn.test/s.mp3', $m['sound']['url'] ); $m = $this->build( $this->def( 1, array( 'visual_id' => 14, 'sound_id' => 0, 'message' => 'hi' ) ) ); $this->assertNull( $m['visual'], 'non-http url dropped' ); $this->assertSame( 'hi', $m['message'] ); $this->assertNull( $this->build( $this->def( 1, array( 'sound_id' => 10, 'visual_id' => 12 ) ) ), 'wrong kinds in wrong slots' ); } public function test_urls_come_only_from_callback_not_from_def_or_row(): void { $def = $this->def( 1, array( 'url' => 'https://evil.test/x.png', 'visual_url' => 'https://evil.test/y.png' ) ); $row = $this->row( 1, array( 'media' => array( 'visual' => array( 'type' => 'image', 'url' => 'https://evil.test/z.png' ) ) ) ); $json = json_encode( $this->build( $def, $row ) ); $this->assertStringNotContainsString( 'evil.test', (string) $json ); $this->assertStringContainsString( 'cdn.test\/a.gif', (string) $json ); $boom = AlertMedia::build( $this->row( 1 ), 'Al', '$1', array( $this->def( 1 ) ), array(), static function ( int $id ): ?array { throw new \RuntimeException( 'db' ); } ); $this->assertNull( $boom ); } public function test_reward_token_and_reward_priority(): void { $trig = array( array( 'action_id' => 5, 'amount_minor' => 500, 'title' => 'Eat bean' ), array( 'action_id' => 6, 'amount_minor' => 2500, 'title' => 'Pushups' ) ); $defs = array( $this->def( 1, array( 'message' => 'amt {reward}' ) ), $this->def( 2, array( 'trigger_type' => 'reward', 'action_id' => 5, 'message' => 'rw {reward}' ) ) ); $files = $this->files(); $m = AlertMedia::build( $this->row( 1 ), 'Al', '$25.00', $defs, $trig, static fn( int $id ): ?array => $files[ $id ] ?? null ); $this->assertSame( 'rw Eat bean', $m['message'] ); $m = AlertMedia::build( $this->row( 1 ), 'Al', '$25.00', array( $defs[0] ), $trig, static fn( int $id ): ?array => $files[ $id ] ?? null ); $this->assertSame( 'amt Pushups', $m['message'], 'amount def gets the highest triggered reward' ); } /** Service-level: one defs query, one triggered query, goal 0 for tests, anonymity/moderation intact. */ private function svc( array $defs, array $db, array $triggered = array() ): array { $src = new class( $defs, $db, $triggered, $this->files() ) implements OverlayDataSource, RecentDonationsSource, ActionsSource, AlertMediaSource { public int $defQueries = 0; public int $trigQueries = 0; public function __construct( public array $defs, private array $db, private array $trig, private array $files ) {} public function activeGoal(): ?array { return array( 'id' => 7, '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 bonusesFor( array $ids ): array { return array(); } public function donations( ?int $sinceId, int $limit ): array { return array_values( array_filter( $this->db, fn( $d ) => $d['id'] > (int) $sinceId ) ); } public function recentDonations( int $limit ): array { return $this->db; } public function menuActions( int $goalId ): array { return array(); } public function actionsFor( array $ids ): array { return array(); } public function alertDefs(): array { ++$this->defQueries; return $this->defs; } public function triggeredActionsFor( array $ids ): array { ++$this->trigQueries; return $this->trig; } public function attachment( int $id ): ?array { return $this->files[ $id ] ?? null; } }; return array( new OverlayService( $src ), $src ); } public function test_payload_carries_media_in_recent_and_since_modes_with_single_queries(): void { [ $svc, $src ] = $this->svc( array( $this->def( 1 ) ), array( $this->row( 1 ), $this->row( 2 ), $this->row( 3, array( 'goal_id' => 0 ) ) ) ); $out = $svc->payload( 'alerts', 1, null, true ); $this->assertSame( 'https://cdn.test/a.gif', $out['alerts'][0]['media']['visual']['url'] ); $this->assertSame( 1, $src->defQueries ); $this->assertSame( 1, $src->trigQueries ); [ $svc2 ] = $this->svc( array( $this->def( 1 ) ), array( $this->row( 1 ), $this->row( 2 ) ) ); $this->assertSame( 60, $svc2->payload( 'alerts', 1, 0, false )['alerts'][1]['media']['volume'] ); } public function test_goal_scoping_in_payload_and_test_donations(): void { $defs = array( $this->def( 1, array( 'goal_id' => 7 ) ) ); [ $svc ] = $this->svc( $defs, array( $this->row( 1 ), $this->row( 2, array( 'goal_id' => 0 ) ), $this->row( 3, array( 'goal_id' => 9 ) ) ) ); $a = $svc->payload( 'alerts', 1, null, true )['alerts']; $this->assertNotNull( $a[0]['media'] ); $this->assertNull( $a[1]['media'], 'test donation (goal 0) does not use goal-7 defs' ); $this->assertNull( $a[2]['media'] ); } public function test_no_defs_means_media_null_and_no_trigger_query(): void { [ $svc, $src ] = $this->svc( array(), array( $this->row( 1 ) ) ); $this->assertNull( $svc->payload( 'alerts', 1, null, true )['alerts'][0]['media'] ); $this->assertSame( 0, $src->trigQueries ); } public function test_moderation_and_anonymity_unchanged(): void { $rows = array( $this->row( 1, array( 'is_anonymous' => 1, 'donor_name' => 'Secret' ) ), $this->row( 2, array( 'moderation' => 'pending' ) ) ); [ $svc ] = $this->svc( array( $this->def( 1, array( 'message' => '{name}!' ) ) ), $rows ); $a = $svc->payload( 'alerts', 1, null, true )['alerts']; $this->assertCount( 1, $a ); $this->assertSame( 'Anonymous', $a[0]['name'] ); $this->assertSame( 'Anonymous!', $a[0]['media']['message'] ); $this->assertStringNotContainsString( 'Secret', (string) json_encode( $a ) ); } public function test_etag_changes_with_media_definitions_even_without_alerts(): void { [ $a ] = $this->svc( array( $this->def( 1 ) ), array() ); [ $b ] = $this->svc( array( $this->def( 1, array( 'volume' => 10 ) ) ), array() ); [ $none ] = $this->svc( array(), array() ); $pa = $a->payload( 'alerts', 1, 0, false ); $ea = Payloads::etag( $pa, $a->etagSalt( 'alerts' ) ); $this->assertNotSame( $ea, Payloads::etag( $pa, $b->etagSalt( 'alerts' ) ) ); $this->assertNotSame( $ea, Payloads::etag( $pa, $none->etagSalt( 'alerts' ) ) ); $this->assertSame( Payloads::etag( $pa ), Payloads::etag( $pa, $none->etagSalt( 'alerts' ) ), 'no defs: unchanged etag' ); $this->assertSame( '', $a->etagSalt( 'goal' ) ); } public function test_etag_changes_when_attachment_url_changes(): void { $p1 = array( 'alerts' => array( array( 'id' => 1, 'media' => array( 'visual' => array( 'url' => 'https://a/1' ) ) ) ) ); $p2 = array( 'alerts' => array( array( 'id' => 1, 'media' => array( 'visual' => array( 'url' => 'https://a/2' ) ) ) ) ); $this->assertNotSame( Payloads::etag( $p1 ), Payloads::etag( $p2 ) ); } public function test_payloads_alerts_calls_media_callback_with_public_name_only_for_shown_rows(): void { $seen = array(); $cb = static function ( array $r, string $name, string $amount ) use ( &$seen ): ?array { $seen[] = array( $r['id'], $name, $amount ); return null; }; Payloads::alerts( array( $this->row( 1, array( 'is_anonymous' => 1 ) ), $this->row( 2, array( 'moderation' => 'rejected' ) ) ), null, 10, true, $cb ); $this->assertSame( array( array( 1, 'Anonymous', '$25.00' ) ), $seen ); } public function test_frameless_passes_through_only_with_a_visual(): void { $this->assertTrue( $this->build( $this->def( 1, array( 'frameless' => 1 ) ) )['frameless'] ); $this->assertFalse( $this->build( $this->def( 1, array( 'frameless' => 0 ) ) )['frameless'] ); $this->assertFalse( $this->build( $this->def( 1, array( 'frameless' => 1, 'visual_id' => 0, 'message' => 'hi' ) ) )['frameless'], 'no visual, nothing to unframe' ); $this->assertFalse( $this->build( $this->def( 1, array( 'frameless' => 1, 'visual_id' => 13 ) ) )['frameless'], 'rejected media never unframes' ); } public function test_media_url_gets_version_suffix_from_server_data_only(): void { $files = array( 10 => array( 'url' => 'https://cdn.test/a.png', 'mime' => 'image/png', 'v' => 1700000000 ), 11 => array( 'url' => 'https://cdn.test/a.png?x=1', 'mime' => 'image/png', 'modified' => '2024-01-02 03:04:05' ) ); $get = static fn( int $id ): ?array => $files[ $id ] ?? null; $m = AlertMedia::fromDef( $this->def( 1, array( 'sound_id' => 0 ) ), 'Al', '$1', array(), $get ); $this->assertSame( 'https://cdn.test/a.png?v=1700000000', $m['visual']['url'] ); $m = AlertMedia::fromDef( $this->def( 1, array( 'visual_id' => 11, 'sound_id' => 0 ) ), 'Al', '$1', array(), $get ); $this->assertSame( 'https://cdn.test/a.png?x=1&v=' . strtotime( '2024-01-02 03:04:05 UTC' ), $m['visual']['url'] ); $row = $this->row( 1, array( 'media' => array( 'visual' => array( 'type' => 'image', 'url' => 'https://evil.test/z.png' ) ) ) ); $this->assertNotSame( 'https://evil.test/z.png', $this->build( $this->def( 1 ), $row )['visual']['url'] ); } }