$menu @param array $events @param array $db */ private function svc( array $menu, array $events, array $db, bool $goal = true ): OverlayService { return new OverlayService( new class( $menu, $events, $db, $goal ) implements OverlayDataSource, RecentDonationsSource, ActionsSource { public int $actionQueries = 0; public function __construct( private array $menu, private array $events, private array $db, private bool $goal ) {} public function activeGoal(): ?array { return $this->goal ? array( 'id' => 7, 'currency' => 'USD' ) : 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(); } public function donations( ?int $sinceId, int $limit ): array { return array_values( array_filter( $this->db, static fn( $d ) => $d['id'] > (int) $sinceId ) ); } public function recentDonations( int $limit ): array { return $this->db; } public function menuActions( int $goalId ): array { return $this->menu; } public function actionsFor( array $ids ): array { ++$this->actionQueries; return $this->events; } } ); } private function row( int $id, array $o = array() ): array { return $o + array( 'id' => $id, 'base_minor' => 2500, 'currency' => 'USD', 'donor_name' => 'Al', 'is_anonymous' => 0, 'message' => '', 'status' => 'completed', 'moderation' => 'approved' ); } public function test_menu_lists_actions_with_display_and_mode(): void { $menu = array( array( 'title' => 'Eat a bean', 'description' => 'every flavor', 'amount_minor' => 500, 'mode' => 'every' ), array( 'title' => '25 pushups', 'description' => '', 'amount_minor' => 2500, 'mode' => 'exact' ), ); $out = $this->svc( $menu, array(), array() )->payload( 'actions', 1 ); $this->assertTrue( $out['active'] ); $this->assertSame( '$5.00', $out['actions'][0]['amount_display'] ); $this->assertSame( 'every', $out['actions'][0]['mode'] ); $this->assertSame( 'exact', $out['actions'][1]['mode'] ); $this->assertSame( '25 pushups', $out['actions'][1]['title'] ); } public function test_menu_empty_without_goal_or_actions_and_drops_bad_rows(): void { $this->assertFalse( $this->svc( array(), array(), array() )->payload( 'actions', 1 )['active'] ); $menu = array( array( 'title' => 'x', 'amount_minor' => 500, 'mode' => 'exact' ) ); $this->assertSame( array(), $this->svc( $menu, array(), array(), false )->payload( 'actions', 1 )['actions'] ); $bad = array( array( 'title' => 'Zero', 'amount_minor' => 0 ), array( 'title' => ' ', 'amount_minor' => 500 ) ); $this->assertSame( array(), Payloads::actionsMenu( array( 'currency' => 'USD' ), $bad )['actions'] ); } public function test_menu_sanitizes_hostile_strings(): void { $menu = array( array( 'title' => "Do\x00 it\n now", 'description' => '' . str_repeat( 'd', 400 ), 'amount_minor' => 500, 'mode' => 'weird' ) ); $a = Payloads::actionsMenu( array( 'currency' => 'USD' ), $menu )['actions'][0]; $this->assertStringNotContainsString( '<', $a['title'] . $a['description'] ); $this->assertStringNotContainsString( "\n", $a['title'] ); $this->assertLessThanOrEqual( 255, mb_strlen( $a['description'] ) ); $this->assertSame( 'exact', $a['mode'] ); } public function test_alert_actions_shown_with_times_in_since_and_recent(): void { $events = array( 3 => array( array( 'title' => 'Eat a bean', 'times' => 3 ), array( 'title' => '25 pushups', 'times' => 1 ) ) ); $db = array( $this->row( 3, array( 'is_anonymous' => 1, 'donor_name' => 'Secret' ) ), $this->row( 4 ) ); $svc = $this->svc( array(), $events, $db ); $since = $svc->payload( 'alerts', 1, 0 ); $recent = $svc->payload( 'alerts', 1, null, true ); $this->assertSame( $since['alerts'], $recent['alerts'] ); $a = $recent['alerts'][0]; $this->assertSame( array( array( 'title' => 'Eat a bean', 'times' => 3 ), array( 'title' => '25 pushups', 'times' => 1 ) ), $a['actions'] ); $this->assertSame( 'Anonymous', $a['name'] ); $this->assertStringNotContainsString( 'Secret', json_encode( $recent ) ); $this->assertSame( array(), $recent['alerts'][1]['actions'] ); } public function test_alert_actions_use_one_query_and_baseline_none(): void { $db = array( $this->row( 3 ), $this->row( 4 ) ); $svc = $this->svc( array(), array(), $db ); $svc->payload( 'alerts', 1, null, true ); $this->assertSame( array(), $svc->payload( 'alerts', 1, null )['alerts'] ); } public function test_alert_actions_sanitized_capped_and_clamped(): void { $ev = array( array( 'title' => 'Hi', 'times' => 0 ), array( 'title' => '', 'times' => 2 ), 'junk', array( 'title' => str_repeat( 'é', 200 ), 'times' => 99999 ) ); for ( $i = 0; $i < 10; $i++ ) { $ev[] = array( 'title' => "T$i", 'times' => 1 ); } $out = Payloads::actionEvents( $ev ); $this->assertCount( 5, $out ); $this->assertSame( array( 'title' => 'Hi', 'times' => 1 ), $out[0] ); $this->assertSame( 80, mb_strlen( $out[1]['title'] ) ); $this->assertSame( 999, $out[1]['times'] ); $this->assertSame( array(), Payloads::actionEvents( 'nope' ) ); } public function test_etag_changes_when_actions_appear(): void { $db = array( $this->row( 3 ) ); $none = $this->svc( array(), array(), $db )->payload( 'alerts', 1, null, true ); $with = $this->svc( array(), array( 3 => array( array( 'title' => 'Bean', 'times' => 1 ) ) ), $db )->payload( 'alerts', 1, null, true ); $this->assertNotSame( Payloads::etag( $none ), Payloads::etag( $with ) ); $m1 = $this->svc( array(), array(), array() )->payload( 'actions', 1 ); $m2 = $this->svc( array( array( 'title' => 'Bean', 'amount_minor' => 500, 'mode' => 'exact' ) ), array(), array() )->payload( 'actions', 1 ); $this->assertNotSame( Payloads::etag( $m1 ), Payloads::etag( $m2 ) ); } public function test_source_without_actions_capability_still_works(): void { $svc = new OverlayService( new class implements OverlayDataSource { public function activeGoal(): ?array { return array( 'id' => 1, '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 array(); } public function bonusesFor( array $ids ): array { return array(); } } ); $this->assertFalse( $svc->payload( 'actions', 1 )['active'] ); } }