Alert media: images/GIFs/videos/sounds tied to donation amount or reward; release tag collision fix
- 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:
1 parent
1201db175b
commit
2c16a9ebbb
22 files changed
+1591
-22
No files matched your search
@@ -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 ) ) );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user