93 lines
4.7 KiB
PHP
93 lines
4.7 KiB
PHP
<?php
|
|||
|
|
declare( strict_types=1 );
|
||
|
|
|
||
|
|
namespace StreamGoals\Tests\Unit;
|
||
|
|
|
||
|
|
use PHPUnit\Framework\TestCase;
|
||
|
|
use StreamGoals\Update\ReleaseParser;
|
||
|
|
|
||
|
|
final class UpdateReleaseParserTest extends TestCase {
|
||
|
|
|
||
|
|
private const HOST = 'repo.anhonesthost.net';
|
||
|
|
private const ZIP = 'https://repo.anhonesthost.net/wp-plugins/wp-donations/releases/download/2026.11.15-1941/streamgoals-donate.zip';
|
||
|
|
|
||
|
|
private function release( array $over = array() ): array {
|
||
|
|
return array_merge(
|
||
|
|
array(
|
||
|
|
'tag_name' => '2026.11.15-1941',
|
||
|
|
'draft' => false,
|
||
|
|
'prerelease' => false,
|
||
|
|
'body' => "## What's New\n\n* one\n* two",
|
||
|
|
'html_url' => 'https://repo.anhonesthost.net/wp-plugins/wp-donations/releases/tag/2026.11.15-1941',
|
||
|
|
'published_at' => '2026-11-15T19:41:00Z',
|
||
|
|
'assets' => array( array( 'name' => 'streamgoals-donate.zip', 'browser_download_url' => self::ZIP ) ),
|
||
|
|
),
|
||
|
|
$over
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_valid_release(): void {
|
||
|
|
$r = ReleaseParser::parse( $this->release(), self::HOST );
|
||
|
|
$this->assertSame( '2026.11.15-1941', $r['version'] );
|
||
|
|
$this->assertSame( self::ZIP, $r['package'] );
|
||
|
|
$this->assertSame( '2026-11-15T19:41:00Z', $r['published'] );
|
||
|
|
$this->assertStringContainsString( 'releases/tag', $r['url'] );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_draft_and_prerelease_ignored(): void {
|
||
|
|
$this->assertNull( ReleaseParser::parse( $this->release( array( 'draft' => true ) ), self::HOST ) );
|
||
|
|
$this->assertNull( ReleaseParser::parse( $this->release( array( 'prerelease' => true ) ), self::HOST ) );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_bad_tag(): void {
|
||
|
|
foreach ( array( 'nightly', '2026.1.15-1941', '', null, array() ) as $t ) {
|
||
|
|
$this->assertNull( ReleaseParser::parse( $this->release( array( 'tag_name' => $t ) ), self::HOST ) );
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_v_prefix_and_semver(): void {
|
||
|
|
$this->assertSame( '2026.11.15-1941', ReleaseParser::parse( $this->release( array( 'tag_name' => 'v2026.11.15-1941' ) ), self::HOST )['version'] );
|
||
|
|
$this->assertSame( '1.2.3', ReleaseParser::parse( $this->release( array( 'tag_name' => 'v1.2.3' ) ), self::HOST )['version'] );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_wrong_host_and_non_https_rejected(): void {
|
||
|
|
foreach ( array( 'https://evil.example/streamgoals-donate.zip', 'http://repo.anhonesthost.net/x.zip', 'https://repo.anhonesthost.net@evil.example/x.zip', 'https://repo.anhonesthost.net.evil.example/x.zip', '//repo.anhonesthost.net/x.zip' ) as $u ) {
|
||
|
|
$rel = $this->release( array( 'assets' => array( array( 'name' => 'streamgoals-donate.zip', 'browser_download_url' => $u ) ) ) );
|
||
|
|
$this->assertNull( ReleaseParser::parse( $rel, self::HOST ), $u );
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_missing_asset(): void {
|
||
|
|
$this->assertNull( ReleaseParser::parse( $this->release( array( 'assets' => array() ) ), self::HOST ) );
|
||
|
|
$this->assertNull( ReleaseParser::parse( $this->release( array( 'assets' => array( array( 'name' => 'notes.txt', 'browser_download_url' => self::ZIP ) ) ) ), self::HOST ) );
|
||
|
|
$this->assertNull( ReleaseParser::parse( array( 'tag_name' => '1.0.0' ), self::HOST ) );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_picks_right_asset(): void {
|
||
|
|
$other = 'https://repo.anhonesthost.net/dl/other.zip';
|
||
|
|
$a = array( array( 'name' => 'source.zip', 'browser_download_url' => $other ), array( 'name' => 'streamgoals-donate.zip', 'browser_download_url' => self::ZIP ) );
|
||
|
|
$this->assertSame( self::ZIP, ReleaseParser::parse( $this->release( array( 'assets' => $a ) ), self::HOST )['package'] );
|
||
|
|
$this->assertSame( $other, ReleaseParser::parse( $this->release( array( 'assets' => array( $a[0] ) ) ), self::HOST )['package'] );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_version_comparison(): void {
|
||
|
|
$this->assertTrue( ReleaseParser::isNewer( '2026.11.15-1830', '2026.11.15-0930' ) );
|
||
|
|
$this->assertFalse( ReleaseParser::isNewer( '2026.11.15-0930', '2026.11.15-1830' ) );
|
||
|
|
$this->assertFalse( ReleaseParser::isNewer( '2026.11.15-0930', '2026.11.15-0930' ) );
|
||
|
|
$this->assertTrue( ReleaseParser::isNewer( 'v2026.11.16-0001', '2026.11.15-2359' ) );
|
||
|
|
$this->assertTrue( ReleaseParser::isNewer( '2026.11.15-1941', '0.1.0' ) );
|
||
|
|
$this->assertFalse( ReleaseParser::isNewer( 'garbage', '0.1.0' ) );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_changelog_escapes_html(): void {
|
||
|
|
$h = ReleaseParser::changelogHtml( "<script>alert(1)</script>\n\n* <b>bold</b> & \"q\"\n* two" );
|
||
|
|
$this->assertStringNotContainsString( '<script', $h );
|
||
|
|
$this->assertStringNotContainsString( '<b>', $h );
|
||
|
|
$this->assertStringContainsString( '<script>', $h );
|
||
|
|
$this->assertStringContainsString( '<ul><li><b>bold</b> & "q"</li><li>two</li></ul>', $h );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_changelog_paragraphs_and_headings(): void {
|
||
|
|
$this->assertSame( '<h4>What's New</h4><p>a b</p><ul><li>x</li></ul>', ReleaseParser::changelogHtml( "## What's New\n\na\nb\n\n- x" ) );
|
||
|
|
}
|
||
|
|
}
|