135 lines
7.1 KiB
PHP
135 lines
7.1 KiB
PHP
<?php
|
|
declare( strict_types=1 );
|
|
|
|
namespace StreamGoals\Tests\Unit;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use StreamGoals\Overlay\Payloads;
|
|
use StreamGoals\Overlay\Progress;
|
|
|
|
final class ProgressTest extends TestCase {
|
|
|
|
private function goal( array $over = array() ): array {
|
|
return $over + array( 'id' => 7, 'title' => 'New mic', 'target_minor' => 10000, 'currency' => 'USD', 'status' => 'active', 'close_at_target' => 0, 'counts_fees' => 0 );
|
|
}
|
|
|
|
/** @dataProvider flags */
|
|
public function test_show_progress_attribute_parsing( $raw, bool $expected ): void {
|
|
$this->assertSame( $expected, Progress::flag( $raw, true ) );
|
|
}
|
|
|
|
public static function flags(): array {
|
|
return array(
|
|
array( 'yes', true ), array( 'YES', true ), array( 'true', true ), array( '1', true ), array( 1, true ), array( ' on ', true ),
|
|
array( 'no', false ), array( 'No', false ), array( 'false', false ), array( '0', false ), array( 0, false ), array( 'off', false ),
|
|
array( '', true ), array( null, true ), array( 'banana', true ), array( array( 'x' ), true ),
|
|
);
|
|
}
|
|
|
|
public function test_flag_default_is_respected_for_unknown_values(): void {
|
|
$this->assertFalse( Progress::flag( 'banana', false ) );
|
|
$this->assertFalse( Progress::flag( null, false ) );
|
|
}
|
|
|
|
public function test_percentage_is_clamped_and_target_zero_is_safe(): void {
|
|
$this->assertSame( 0, Progress::payload( $this->goal( array( 'target_minor' => 0 ) ), 500 )['percent'] );
|
|
$this->assertSame( 100, Progress::payload( $this->goal(), 999999 )['percent'] );
|
|
$this->assertSame( 0, Progress::payload( $this->goal(), -50 )['percent'] );
|
|
$this->assertSame( 25, Progress::payload( $this->goal(), 2500 )['percent'] );
|
|
}
|
|
|
|
public function test_numbers_match_the_overlay_goal_payload(): void {
|
|
$p = Progress::payload( $this->goal(), 2500 );
|
|
$o = Payloads::goal( $this->goal(), 2500 );
|
|
foreach ( array( 'raised_minor', 'target_minor', 'percent', 'raised_display', 'target_display', 'currency', 'title' ) as $k ) {
|
|
$this->assertSame( $o[ $k ], $p[ $k ], $k );
|
|
}
|
|
$this->assertSame( '$25.00', $p['raised_display'] );
|
|
$this->assertSame( '$100.00', $p['target_display'] );
|
|
$this->assertFalse( $p['reached'] );
|
|
$this->assertNull( $p['tier'] );
|
|
}
|
|
|
|
public function test_tiered_goal_shows_current_tier_and_matches_overlay(): void {
|
|
$tiers = array(
|
|
array( 'id' => 1, 'target_minor' => 5000, 'title' => 'Giveaway', 'description' => 'secret', 'reached_at' => '2026-01-01 00:00:00' ),
|
|
array( 'id' => 2, 'target_minor' => 20000, 'title' => 'Microphone', 'description' => 'secret', 'reached_at' => null ),
|
|
);
|
|
$g = $this->goal( array( 'target_minor' => 20000 ) );
|
|
$p = Progress::payload( $g, 8000, $tiers );
|
|
$o = Payloads::goal( $g, 8000, $tiers );
|
|
$this->assertSame( $o['percent'], $p['percent'] );
|
|
$this->assertSame( 20000, $p['target_minor'] );
|
|
$this->assertSame( array( 'index' => 2, 'count' => 2, 'title' => 'Microphone', 'target_minor' => 20000, 'target_display' => '$200.00' ), $p['tier'] );
|
|
$this->assertFalse( $p['reached'] );
|
|
$this->assertStringNotContainsString( 'secret', json_encode( $p ) );
|
|
}
|
|
|
|
public function test_reached_states(): void {
|
|
$closed = $this->goal( array( 'status' => 'complete', 'close_at_target' => 1 ) );
|
|
$this->assertTrue( Progress::payload( $closed, 10000 )['reached'] );
|
|
$this->assertTrue( Progress::payload( $closed, 0 )['reached'], 'closed at target wins even if numbers lag' );
|
|
$this->assertTrue( Progress::payload( $this->goal(), 10000 )['reached'], 'raised >= target' );
|
|
$this->assertFalse( Progress::payload( $this->goal( array( 'target_minor' => 0 ) ), 10000 )['reached'], 'no target never counts as reached' );
|
|
$done = array( array( 'id' => 1, 'target_minor' => 5000, 'title' => 'A', 'reached_at' => 'x' ) );
|
|
$p = Progress::payload( $this->goal( array( 'target_minor' => 5000 ) ), 5000, $done );
|
|
$this->assertTrue( $p['reached'] );
|
|
$this->assertNull( $p['tier'] );
|
|
}
|
|
|
|
public function test_public_payload_has_exactly_the_whitelisted_non_pii_keys(): void {
|
|
$goal = $this->goal() + array( 'paypal_secret' => 'x', 'notes' => 'private' );
|
|
$p = Progress::payload( $goal, 100 );
|
|
$this->assertSame(
|
|
array( 'currency', 'goal_id', 'percent', 'raised_display', 'raised_minor', 'reached', 'target_display', 'target_minor', 'tier', 'title' ),
|
|
( static function ( array $a ): array { ksort( $a ); return array_keys( $a ); } )( $p )
|
|
);
|
|
$json = json_encode( $p );
|
|
foreach ( array( 'paypal', 'notes', 'email', 'donor', 'message', 'display_name' ) as $bad ) {
|
|
$this->assertStringNotContainsString( $bad, $json );
|
|
}
|
|
}
|
|
|
|
public function test_html_is_accessible_and_escaped(): void {
|
|
$p = Progress::payload( $this->goal( array( 'title' => '<b>Mic</b>' ) ), 2500 );
|
|
$html = Progress::html( $p, array( 'reached' => 'Goal reached', 'of' => '%1$s of %2$s', 'tier' => 'Tier: %1$s (%2$s)', 'label' => 'Progress: %s' ) );
|
|
$this->assertStringContainsString( 'role="progressbar"', $html );
|
|
$this->assertStringContainsString( 'aria-valuenow="25"', $html );
|
|
$this->assertStringContainsString( 'aria-valuemin="0"', $html );
|
|
$this->assertStringContainsString( 'aria-valuemax="100"', $html );
|
|
$this->assertStringContainsString( 'aria-label="Progress: <b>Mic</b>"', $html );
|
|
$this->assertStringContainsString( '$25.00 of $100.00', $html );
|
|
$this->assertStringContainsString( 'width:25%', $html );
|
|
$this->assertStringNotContainsString( '<b>', $html );
|
|
}
|
|
|
|
public function test_html_reached_and_tier_lines(): void {
|
|
$labels = array( 'reached' => 'Goal reached', 'of' => '%1$s of %2$s', 'tier' => 'Tier: %1$s (%2$s)', 'label' => 'Progress: %s' );
|
|
$closed = Progress::html( Progress::payload( $this->goal( array( 'status' => 'complete', 'close_at_target' => 1 ) ), 10000 ), $labels );
|
|
$this->assertStringContainsString( 'Goal reached', $closed );
|
|
$this->assertStringContainsString( 'sg-progress-reached', $closed );
|
|
$this->assertStringContainsString( 'aria-valuenow="100"', $closed );
|
|
$tiers = array( array( 'id' => 2, 'target_minor' => 20000, 'title' => 'Mic', 'reached_at' => null ) );
|
|
$t = Progress::html( Progress::payload( $this->goal( array( 'target_minor' => 20000 ) ), 100, $tiers ), $labels );
|
|
$this->assertStringContainsString( 'Tier: Mic ($200.00)', $t );
|
|
}
|
|
|
|
public function test_assets_support_reduced_motion_and_polling_rules(): void {
|
|
$root = dirname( __DIR__, 2 ) . '/assets/donate/';
|
|
$css = (string) file_get_contents( $root . 'donate.css' );
|
|
$js = (string) file_get_contents( $root . 'donate.js' );
|
|
$this->assertStringContainsString( 'prefers-reduced-motion', $css );
|
|
$this->assertStringContainsString( 'visibilityState', $js );
|
|
$this->assertStringContainsString( '30000', $js );
|
|
$this->assertStringNotContainsString( 'innerHTML', $js );
|
|
}
|
|
|
|
public function test_route_and_shortcodes_are_registered(): void {
|
|
$php = (string) file_get_contents( dirname( __DIR__, 2 ) . '/includes/Rest/DonateController.php' );
|
|
$this->assertStringContainsString( "'/progress'", $php );
|
|
$this->assertStringContainsString( "add_shortcode( 'streamgoals_progress'", $php );
|
|
$this->assertStringContainsString( "'show_progress' => 'yes'", $php );
|
|
$this->assertMatchesRegularExpression( "/function progress_route.*self::limit\( 'progress'/s", $php );
|
|
}
|
|
}
|