Alert media: images/GIFs/videos/sounds tied to donation amount or reward; release tag collision fix
Test and Release / test (push) Successful in 1m20s
Test and Release / release (push) Successful in 4s

- Alerts\Resolver/Media/Template/AlertMedia/DefInput/DefRepository; Alerts admin screen with Media Library picker and live preview
- Overlay: media resolved server-side from attachment IDs (MIME whitelist), playback with timeouts and autoplay-failure fallbacks
- Workflow: pick the next free minute if the version tag already exists (two pushes in one minute failed 'Create release')

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
shadowdaoandClaude Sonnet 5.5 committed 2026-11-15 13:01:31 -08:00
1 parent 1201db175b
commit 2c16a9ebbb
22 files changed
+1591 -22

No files matched your search

+106
View File
@@ -0,0 +1,106 @@
<?php
declare( strict_types=1 );
namespace StreamGoals\Tests\Unit;
use PHPUnit\Framework\TestCase;
use StreamGoals\Alerts\DefInput;
final class AlertDefInputTest extends TestCase {
private function att(): callable {
$files = array(
10 => array( 'mime' => 'image/gif', 'size' => 1000 ),
11 => array( 'mime' => 'video/mp4', 'size' => 30 * 1024 * 1024 ),
12 => array( 'mime' => 'audio/mpeg', 'size' => 5000 ),
13 => array( 'mime' => 'image/svg+xml', 'size' => 10 ),
14 => array( 'mime' => 'text/html', 'size' => 10 ),
15 => array( 'mime' => 'image/png', 'size' => 10 ),
);
return static fn( int $id ): ?array => $files[ $id ] ?? null;
}
private function post( array $o = array() ): array {
return $o + array( 'title' => 'Big', 'goal_id' => '0', 'trigger_type' => 'amount', 'amount' => '10.00', 'visual_id' => '10', 'sound_id' => '12',
'volume' => '70', 'duration' => '8', 'message' => '{name} sent {amount}', 'sort' => '' );
}
private function run_( array $post, int $min = 100 ): array {
return DefInput::validate( $post, $min, 'USD', array( 3 ), array( 5 ), $this->att(), 40 );
}
public function test_valid_amount_def(): void {
$r = $this->run_( $this->post() );
$this->assertNull( $r['error'] );
$d = $r['data'];
$this->assertSame( array( 0, 'amount', 1000, 0, 10, 'image', 12, 70, 8000, 40 ), array( $d['goal_id'], $d['trigger_type'], $d['min_minor'], $d['action_id'], $d['visual_id'], $d['visual_type'], $d['sound_id'], $d['volume'], $d['duration_ms'], $d['sort'] ) );
$this->assertSame( array(), $r['warnings'] );
}
public function test_valid_reward_def_ignores_amount(): void {
$r = $this->run_( $this->post( array( 'trigger_type' => 'reward', 'action_id' => '5', 'amount' => '' ) ) );
$this->assertNull( $r['error'] );
$this->assertSame( array( 'reward', 0, 5 ), array( $r['data']['trigger_type'], $r['data']['min_minor'], $r['data']['action_id'] ) );
}
public function test_reward_must_be_enabled_one(): void {
$this->assertNotNull( $this->run_( $this->post( array( 'trigger_type' => 'reward', 'action_id' => '6' ) ) )['error'] );
$this->assertNotNull( $this->run_( $this->post( array( 'trigger_type' => 'reward', 'action_id' => '0' ) ) )['error'] );
}
public function test_amount_must_reach_effective_minimum(): void {
$this->assertNotNull( $this->run_( $this->post( array( 'amount' => '0.50' ) ), 100 )['error'] );
$this->assertNull( $this->run_( $this->post( array( 'amount' => '1.00' ) ), 100 )['error'] );
$this->assertNotNull( $this->run_( $this->post( array( 'amount' => 'abc' ) ) )['error'] );
$this->assertNotNull( $this->run_( $this->post( array( 'amount' => '' ) ) )['error'] );
}
public function test_rejects_bad_mime_and_missing_attachments(): void {
foreach ( array( 13, 14, 12, 99 ) as $bad ) {
$this->assertNotNull( $this->run_( $this->post( array( 'visual_id' => (string) $bad ) ) )['error'], "visual $bad" );
}
foreach ( array( 13, 10, 11, 99 ) as $bad ) {
$this->assertNotNull( $this->run_( $this->post( array( 'sound_id' => (string) $bad ) ) )['error'], "sound $bad" );
}
$this->assertNotNull( $this->run_( $this->post( array( 'visual_id' => 'x' ) ) )['error'] );
}
public function test_media_is_optional_and_type_is_detected_server_side(): void {
$r = $this->run_( $this->post( array( 'visual_id' => '0', 'sound_id' => '0', 'visual_type' => 'video' ) ) );
$this->assertSame( array( 0, '', 0 ), array( $r['data']['visual_id'], $r['data']['visual_type'], $r['data']['sound_id'] ) );
$r = $this->run_( $this->post( array( 'visual_id' => '15', 'visual_type' => 'video' ) ) );
$this->assertSame( 'image', $r['data']['visual_type'], 'client-supplied type is ignored' );
}
public function test_big_files_warn_but_do_not_block(): void {
$r = $this->run_( $this->post( array( 'visual_id' => '11' ) ) );
$this->assertNull( $r['error'] );
$this->assertSame( 'video', $r['data']['visual_type'] );
$this->assertCount( 1, $r['warnings'] );
$this->assertStringContainsString( 'OBS', $r['warnings'][0] );
}
public function test_numeric_ranges(): void {
foreach ( array( '-1', '101', 'x' ) as $v ) {
$this->assertNotNull( $this->run_( $this->post( array( 'volume' => $v ) ) )['error'], "volume $v" );
}
$this->assertSame( 0, $this->run_( $this->post( array( 'volume' => '0' ) ) )['data']['volume'] );
foreach ( array( '0', '61', '0.5', 'x', '' , '-2' ) as $v ) {
$this->assertNotNull( $this->run_( $this->post( array( 'duration' => $v ) ) )['error'], "duration '$v'" );
}
$this->assertSame( 60000, $this->run_( $this->post( array( 'duration' => '60' ) ) )['data']['duration_ms'] );
$this->assertSame( 1000, $this->run_( $this->post( array( 'duration' => '1' ) ) )['data']['duration_ms'] );
}
public function test_title_goal_message_sort(): void {
$this->assertNotNull( $this->run_( $this->post( array( 'title' => ' ' ) ) )['error'] );
$this->assertNotNull( $this->run_( $this->post( array( 'title' => str_repeat( 'a', 121 ) ) ) )['error'] );
$this->assertNotNull( $this->run_( $this->post( array( 'goal_id' => '99' ) ) )['error'] );
$this->assertSame( 3, $this->run_( $this->post( array( 'goal_id' => '3' ) ) )['data']['goal_id'] );
$this->assertNotNull( $this->run_( $this->post( array( 'trigger_type' => 'nope' ) ) )['error'] );
$this->assertNotNull( $this->run_( $this->post( array( 'message' => str_repeat( 'm', 256 ) ) ) )['error'] );
$this->assertNotNull( $this->run_( $this->post( array( 'sort' => '100000' ) ) )['error'] );
$this->assertStringNotContainsString( '<', $this->run_( $this->post( array( 'title' => '<script>x</script>Hi', 'message' => '<b>hi</b>' ) ) )['data']['title'] );
$this->assertStringNotContainsString( '<', $this->run_( $this->post( array( 'message' => '<b>hi</b>{name}' ) ) )['data']['message'] );
}
}
+91
View File
@@ -0,0 +1,91 @@
<?php
declare( strict_types=1 );
namespace StreamGoals\Tests\Unit;
use PHPUnit\Framework\TestCase;
use StreamGoals\Alerts\Media;
use StreamGoals\Alerts\Template;
final class AlertHelpersTest extends TestCase {
public function test_mime_whitelist_and_kinds(): void {
foreach ( array( 'image/png', 'image/jpeg', 'image/gif', 'image/webp' ) as $m ) {
$this->assertSame( 'image', Media::kind( $m ) );
$this->assertSame( 'image', Media::visualType( $m ) );
}
foreach ( array( 'video/mp4', 'video/webm' ) as $m ) {
$this->assertSame( 'video', Media::visualType( $m ) );
}
foreach ( array( 'audio/mpeg', 'audio/ogg', 'audio/wav' ) as $m ) {
$this->assertTrue( Media::isSound( $m ) );
$this->assertNull( Media::visualType( $m ) );
}
$this->assertTrue( Media::isAllowed( 'IMAGE/PNG' ) );
}
/** @dataProvider hostileMimes */
public function test_hostile_mimes_rejected( mixed $mime ): void {
$this->assertFalse( Media::isAllowed( $mime ) );
$this->assertNull( Media::visualType( $mime ) );
$this->assertFalse( Media::isSound( $mime ) );
}
public static function hostileMimes(): array {
return array(
array( 'image/svg+xml' ), array( 'text/html' ), array( 'application/javascript' ), array( '' ), array( null ), array( 123 ),
array( array( 'image/png' ) ), array( 'image/png; charset=x' ), array( "image/png\0.html" ), array( ' image/png' ), array( 'image/*' ),
array( 'video/x-msvideo' ), array( 'audio/x-wav' ), array( 'image/png,text/html' ),
);
}
public function test_duration_clamping(): void {
$this->assertSame( 6000, Media::clampDurationMs( 'abc' ) );
$this->assertSame( 1000, Media::clampDurationMs( 5 ) );
$this->assertSame( 1000, Media::clampDurationMs( -9999 ) );
$this->assertSame( 60000, Media::clampDurationMs( 9999999 ) );
$this->assertSame( 12000, Media::clampDurationMs( '12000' ) );
$this->assertSame( 8000, Media::secondsToMs( '8' ) );
$this->assertSame( 1500, Media::secondsToMs( '1.5' ) );
$this->assertNull( Media::secondsToMs( 'x' ) );
$this->assertNull( Media::secondsToMs( '-3' ) );
$this->assertNull( Media::secondsToMs( array() ) );
$this->assertSame( 100, Media::clampVolume( 500 ) );
$this->assertSame( 0, Media::clampVolume( -5 ) );
$this->assertSame( 80, Media::clampVolume( 'loud' ) );
}
public function test_safe_url(): void {
$this->assertSame( 'https://x.test/a.png', Media::safeUrl( 'https://x.test/a.png' ) );
foreach ( array( 'javascript:alert(1)', 'data:text/html,x', '//evil.test/x', '/relative.png', 'https://x.test/a b', "https://x.test/\"onerror=", '', null, 5 ) as $bad ) {
$this->assertNull( Media::safeUrl( $bad ), var_export( $bad, true ) );
}
}
public function test_template_tokens(): void {
$this->assertSame( 'Al sent $5.00 for Bean!', Template::render( '{name} sent {amount} for {reward}!', 'Al', '$5.00', 'Bean' ) );
$this->assertSame( 'Al sent', Template::render( '{Name} {nope} sent', 'Al', '$5', '' ), 'unknown tokens dropped, case-insensitive' );
$this->assertSame( '', Template::render( '', 'Al', '$5', '' ) );
$this->assertSame( '', Template::render( ' ', 'Al', '$5', '' ) );
$this->assertSame( 'Anonymous rocks', Template::render( '{name} rocks', '', '$5', '' ) );
}
public function test_template_is_html_free_and_not_reexpanded(): void {
$out = Template::render( '<b>{name}</b><script>alert(1)</script> {amount}', '<img src=x onerror=alert(1)>Al', '$5', '' );
$this->assertStringNotContainsString( '<', $out );
$this->assertStringNotContainsString( '>', $out );
$this->assertStringContainsString( 'Al', $out );
$this->assertSame( '{amount} 5', Template::render( '{name} {amount}', '{amount}', '5', '' ) );
$this->assertStringNotContainsString( '9', Template::render( '{name}', 'x{amount}', '9', '' ) );
$this->assertStringNotContainsString( "\n", Template::render( "a\r\n\t{name}\x00\u{202E}", 'Al', '$5', '' ) );
$this->assertStringNotContainsString( '<', Template::render( '<<b>script>x', 'Al', '$5', '' ) );
}
public function test_template_length_capped(): void {
$this->assertLessThanOrEqual( 200, mb_strlen( Template::render( str_repeat( 'x', 5000 ), 'Al', '$5', '' ) ) );
$long = str_repeat( 'N', 500 );
$this->assertLessThanOrEqual( 60, mb_strlen( Template::render( '{name}', $long, '$5', '' ) ) );
$this->assertLessThanOrEqual( 80, mb_strlen( Template::render( '{reward}', 'Al', '$5', $long ) ) );
$this->assertLessThanOrEqual( 10, mb_strlen( Template::render( str_repeat( 'x', 50 ), 'Al', '$5', '', 10 ) ) );
}
}
+76
View File
@@ -0,0 +1,76 @@
<?php
declare( strict_types=1 );
namespace StreamGoals\Tests\Unit;
use PHPUnit\Framework\TestCase;
use StreamGoals\Alerts\Resolver;
final class AlertResolverTest extends TestCase {
private function def( int $id, array $o = array() ): array {
return $o + array( 'id' => $id, 'goal_id' => 0, 'trigger_type' => 'amount', 'min_minor' => 100, 'action_id' => 0, 'enabled' => 1, 'sort' => 0 );
}
public function test_highest_matching_amount_wins(): void {
$defs = array( $this->def( 1, array( 'min_minor' => 100 ) ), $this->def( 2, array( 'min_minor' => 1000 ) ), $this->def( 3, array( 'min_minor' => 5000 ) ) );
$this->assertSame( 2, Resolver::pick( 1, 1000, array(), $defs )['id'] );
$this->assertSame( 2, Resolver::pick( 1, 4999, array(), $defs )['id'] );
$this->assertSame( 3, Resolver::pick( 1, 99999, array(), $defs )['id'] );
$this->assertSame( 1, Resolver::pick( 1, 100, array(), $defs )['id'] );
}
public function test_none_when_below_every_minimum_or_no_defs(): void {
$this->assertNull( Resolver::pick( 1, 99, array(), array( $this->def( 1 ) ) ) );
$this->assertNull( Resolver::pick( 1, 5000, array(), array() ) );
}
public function test_reward_beats_amount(): void {
$defs = array( $this->def( 1, array( 'min_minor' => 100 ) ), $this->def( 2, array( 'trigger_type' => 'reward', 'action_id' => 9 ) ) );
$this->assertSame( 2, Resolver::pick( 1, 5000, array( 9 ), $defs )['id'] );
$this->assertSame( 1, Resolver::pick( 1, 5000, array( 8 ), $defs )['id'], 'other reward does not match' );
$this->assertSame( 1, Resolver::pick( 1, 5000, array(), $defs )['id'] );
}
public function test_reward_def_matches_even_when_base_is_tiny(): void {
$defs = array( $this->def( 2, array( 'trigger_type' => 'reward', 'action_id' => 9, 'min_minor' => 0 ) ) );
$this->assertSame( 2, Resolver::pick( 1, 1, array( 9 ), $defs )['id'] );
}
public function test_several_rewards_highest_reward_amount_then_sort_then_id(): void {
$defs = array(
$this->def( 1, array( 'trigger_type' => 'reward', 'action_id' => 5, 'sort' => 0 ) ),
$this->def( 2, array( 'trigger_type' => 'reward', 'action_id' => 6, 'sort' => 9 ) ),
);
$this->assertSame( 2, Resolver::pick( 1, 5000, array( 5, 6 ), $defs, array( 5 => 500, 6 => 2500 ) )['id'] );
$this->assertSame( 1, Resolver::pick( 1, 5000, array( 5, 6 ), $defs, array( 5 => 500, 6 => 500 ) )['id'], 'tie: lower sort' );
$defs[1]['sort'] = 0;
$this->assertSame( 1, Resolver::pick( 1, 5000, array( 5, 6 ), $defs )['id'], 'tie: lower id' );
}
public function test_goal_scoping(): void {
$defs = array( $this->def( 1, array( 'goal_id' => 7, 'min_minor' => 100 ) ), $this->def( 2, array( 'goal_id' => 0, 'min_minor' => 100, 'sort' => 5 ) ) );
$this->assertSame( 1, Resolver::pick( 7, 500, array(), $defs )['id'] );
$this->assertSame( 2, Resolver::pick( 8, 500, array(), $defs )['id'], 'goal 8 never sees goal 7 defs' );
$this->assertSame( 2, Resolver::pick( 0, 500, array(), $defs )['id'], 'test donations (goal 0) use goal-0 defs only' );
$only7 = array( $defs[0] );
$this->assertNull( Resolver::pick( 0, 500, array(), $only7 ) );
}
public function test_disabled_ignored(): void {
$defs = array( $this->def( 1, array( 'enabled' => 0, 'min_minor' => 1000 ) ), $this->def( 2, array( 'min_minor' => 100 ) ),
$this->def( 3, array( 'trigger_type' => 'reward', 'action_id' => 9, 'enabled' => 0 ) ) );
$this->assertSame( 2, Resolver::pick( 1, 5000, array( 9 ), $defs )['id'] );
}
public function test_string_columns_from_db_are_handled_and_unknown_trigger_ignored(): void {
$defs = array( array( 'id' => '4', 'goal_id' => '0', 'trigger_type' => 'amount', 'min_minor' => '500', 'enabled' => '1', 'sort' => '0' ),
array( 'id' => '5', 'trigger_type' => 'bogus', 'enabled' => 1 ) );
$this->assertSame( 4, Resolver::pick( 3, 500, array(), $defs )['id'] );
}
public function test_equal_minimum_tie_breaks_by_sort_then_id(): void {
$defs = array( $this->def( 1, array( 'sort' => 5 ) ), $this->def( 2, array( 'sort' => 1 ) ) );
$this->assertSame( 2, Resolver::pick( 1, 500, array(), $defs )['id'] );
}
}
+172
View File
@@ -0,0 +1,172 @@
<?php
declare( strict_types=1 );
namespace StreamGoals\Tests\Unit;
use PHPUnit\Framework\TestCase;
use StreamGoals\Alerts\AlertMedia;
use StreamGoals\Overlay\ActionsSource;
use StreamGoals\Overlay\AlertMediaSource;
use StreamGoals\Overlay\OverlayDataSource;
use StreamGoals\Overlay\OverlayService;
use StreamGoals\Overlay\Payloads;
use StreamGoals\Overlay\RecentDonationsSource;
final class OverlayAlertMediaTest extends TestCase {
private function def( int $id, array $o = array() ): array {
return $o + array( 'id' => $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',
), $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 );
}
}