Files
wp-donations/tests/Unit/OverlayTiersTest.php
T

119 lines
6.4 KiB
PHP
Raw Permalink Normal View History

<?php
declare( strict_types=1 );
namespace StreamGoals\Tests\Unit;
use PHPUnit\Framework\TestCase;
use StreamGoals\Overlay\OverlayDataSource;
use StreamGoals\Overlay\OverlayService;
use StreamGoals\Overlay\Payloads;
use StreamGoals\Overlay\RecentDonationsSource;
use StreamGoals\Overlay\TiersSource;
final class OverlayTiersTest extends TestCase {
private function tiers( array $reached = array() ): array {
$rows = array( array( 1, 10000, 'Giveaway' ), array( 2, 25000, 'Microphone' ), array( 3, 50000, '24h stream' ) );
return array_map( static fn( $r ) => array( 'id' => $r[0], 'target_minor' => $r[1], 'title' => $r[2], 'description' => 'd' . $r[0], 'reached_at' => in_array( $r[0], $reached, true ) ? '2026-01-01 00:00:00' : null ), $rows );
}
private function svc( array $tiers, int $raised, array $db = array(), array $unlocked = array(), ?object &$probe = null ): OverlayService {
$probe = new class( $tiers, $raised, $db, $unlocked ) implements OverlayDataSource, RecentDonationsSource, TiersSource {
public int $reachedQueries = 0;
public function __construct( private array $tiers, private int $raised, private array $db, private array $unlocked ) {}
public function activeGoal(): ?array { return array( 'id' => 7, 'title' => 'Stream', 'currency' => 'USD', 'target_minor' => 50000 ); }
public function raisedMinor( array $goal ): int { return $this->raised; }
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 $this->db; }
public function recentDonations( int $limit ): array { return $this->db; }
public function tiersFor( int $goalId ): array { return $this->tiers; }
public function tiersReachedFor( array $ids ): array { ++$this->reachedQueries; return $this->unlocked; }
};
return new OverlayService( $probe );
}
private function row( int $id ): array {
return array( 'id' => $id, 'base_minor' => 2500, 'currency' => 'USD', 'donor_name' => 'Al', 'is_anonymous' => 0, 'message' => '', 'status' => 'completed', 'moderation' => 'approved' );
}
public function test_untiered_goal_payload_is_unchanged(): void {
$g = Payloads::goal( array( 'title' => 'G', 'currency' => 'USD', 'target_minor' => 20000 ), 5000 );
$this->assertArrayNotHasKey( 'tiers', $g );
$this->assertSame( 25, $g['percent'] );
$this->assertSame( $g, Payloads::goal( array( 'title' => 'G', 'currency' => 'USD', 'target_minor' => 20000 ), 5000, array() ) );
$this->assertArrayNotHasKey( 'tiers', $this->svc( array(), 100 )->payload( 'goal', 1 ) );
}
public function test_tiered_goal_percent_is_within_tier_and_displays(): void {
$g = $this->svc( $this->tiers( array( 1 ) ), 17500 )->payload( 'goal', 1 );
$this->assertSame( 50, $g['percent'] );
$this->assertSame( 17500, $g['raised_minor'] );
$this->assertSame( '$175.00', $g['raised_display'] );
$this->assertSame( 25000, $g['target_minor'] );
$this->assertSame( '$250.00', $g['target_display'] );
$t = $g['tiers'];
$this->assertSame( 3, $t['count'] );
$this->assertSame( 2, $t['index'] );
$this->assertFalse( $t['all_reached'] );
$this->assertSame( array( 'title' => 'Microphone', 'description' => 'd2', 'target_minor' => 25000, 'target_display' => '$250.00', 'floor_minor' => 10000 ), $t['current'] );
}
public function test_tiered_goal_below_first_tier_and_all_reached(): void {
$g = $this->svc( $this->tiers(), 2500 )->payload( 'goal', 1 );
$this->assertSame( 25, $g['percent'] );
$this->assertSame( 1, $g['tiers']['index'] );
$this->assertSame( 0, $g['tiers']['current']['floor_minor'] );
$all = $this->svc( $this->tiers( array( 1, 2, 3 ) ), 60000 )->payload( 'goal', 1 );
$this->assertSame( 100, $all['percent'] );
$this->assertTrue( $all['tiers']['all_reached'] );
$this->assertNull( $all['tiers']['current'] );
$this->assertSame( 3, $all['tiers']['index'] );
$this->assertSame( '$500.00', $all['target_display'] );
$this->assertSame( '$600.00', $all['raised_display'] );
}
public function test_tier_text_is_sanitized(): void {
$t = $this->tiers();
$t[0]['title'] = '<script>x</script>Hi' . str_repeat( 'a', 300 );
$g = $this->svc( $t, 0 )->payload( 'goal', 1 );
$this->assertStringNotContainsString( '<', $g['tiers']['current']['title'] );
$this->assertLessThanOrEqual( 120, mb_strlen( $g['tiers']['current']['title'] ) );
}
public function test_alert_tiers_in_since_and_recent_with_one_query(): void {
$probe = null;
$svc = $this->svc( array(), 0, array( $this->row( 3 ), $this->row( 4 ) ), array( 3 => array( array( 'title' => 'Giveaway' ), array( 'title' => 'Microphone' ) ) ), $probe );
$since = $svc->payload( 'alerts', 1, 0 );
$this->assertSame( 1, $probe->reachedQueries );
$recent = $svc->payload( 'alerts', 1, null, true );
$this->assertSame( 2, $probe->reachedQueries );
$this->assertSame( $since['alerts'], $recent['alerts'] );
$this->assertSame( array( array( 'title' => 'Giveaway' ), array( 'title' => 'Microphone' ) ), $recent['alerts'][0]['tiers'] );
$this->assertSame( array(), $recent['alerts'][1]['tiers'] );
$svc->payload( 'alerts', 1, null ); // baseline poll: no lookup
$this->assertSame( 2, $probe->reachedQueries );
}
public function test_alert_tier_events_sanitized_and_capped(): void {
$ev = array( array( 'title' => '<b>Hi</b>' ), array( 'title' => '' ), 'junk', array( 'title' => str_repeat( 'é', 200 ) ) );
for ( $i = 0; $i < 9; $i++ ) {
$ev[] = array( 'title' => "T$i" );
}
$out = Payloads::tierEvents( $ev );
$this->assertCount( 5, $out );
$this->assertSame( array( 'title' => 'Hi' ), $out[0] );
$this->assertSame( 80, mb_strlen( $out[1]['title'] ) );
$this->assertSame( array(), Payloads::tierEvents( 'nope' ) );
}
public function test_etag_changes_when_a_tier_unlocks_or_current_tier_moves(): void {
$a = Payloads::etag( $this->svc( array(), 0, array( $this->row( 3 ) ) )->payload( 'alerts', 1, null, true ) );
$b = Payloads::etag( $this->svc( array(), 0, array( $this->row( 3 ) ), array( 3 => array( array( 'title' => 'X' ) ) ) )->payload( 'alerts', 1, null, true ) );
$this->assertNotSame( $a, $b );
$g1 = Payloads::etag( $this->svc( $this->tiers(), 9000 )->payload( 'goal', 1 ) );
$g2 = Payloads::etag( $this->svc( $this->tiers( array( 1 ) ), 10000 )->payload( 'goal', 1 ) );
$this->assertNotSame( $g1, $g2 );
}
}