Files
wp-donations/tests/Unit/AlertDefInputTest.php
shadowdaoandClaude Sonnet 5.5 afa768fa42
Test and Release / test (push) Successful in 1m17s
Test and Release / release (push) Successful in 5s
Overlays refresh themselves after updates; media URLs versioned; frameless (transparent) alerts
- Overlay\Build id (plugin version + admin epoch) in X-SG-Build; overlay.js reloads once on mismatch (loop-guarded); 'Refresh all overlays now'
- Media URLs carry ?v=<mtime>
- Alert option 'Frameless' (schema v5): no box so transparent PNG/GIF/WebP and VP9-alpha WebM show through; admin preview with checkerboard and ffmpeg help

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 13:41:18 -08:00

117 lines
6.2 KiB
PHP

<?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'] );
}
public function test_frameless_checkbox_parsing_defaults_off(): void {
$this->assertSame( 0, $this->run_( $this->post() )['data']['frameless'] );
foreach ( array( '1', 'on', 1, true, 'true' ) as $on ) {
$this->assertSame( 1, $this->run_( $this->post( array( 'frameless' => $on ) ) )['data']['frameless'], 'on: ' . var_export( $on, true ) );
}
foreach ( array( '0', '', 'off', array( '1' ), 'banana' ) as $off ) {
$this->assertSame( 0, $this->run_( $this->post( array( 'frameless' => $off ) ) )['data']['frameless'], 'off: ' . var_export( $off, true ) );
}
}
}