Files
coruna-lab/tests/Feature/ChannelEmbedZipTest.php
2026-10-07 05:19:52 +08:00

97 lines
3.7 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\Admin;
use App\Models\Channel;
use App\Services\ChannelEmbedZipService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
use ZipArchive;
class ChannelEmbedZipTest extends TestCase
{
use RefreshDatabase;
private const CHANNEL_ID = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
protected function setUp(): void
{
parent::setUp();
config([
'coruna.channel_builder.artifact_root' => storage_path('app/channel-artifacts-test'),
'coruna.channel_domains' => ['cdn.example.com'],
'coruna.static_site.scheme' => 'https',
]);
}
#[Test]
public function admin_can_download_browser_embed_zip(): void
{
$dir = storage_path('app/channel-artifacts-test/web/'.self::CHANNEL_ID);
if (! is_dir($dir) && ! mkdir($dir, 0775, true) && ! is_dir($dir)) {
$this->fail('unable to create embed fixture dir');
}
file_put_contents($dir.'/index.js', "/* coruna-embed-boot */\nvar STAT_ORIGIN = '';\nvar CHANNEL = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';\n");
file_put_contents($dir.'/payload.js', 'function cAsUcoxco(){}');
file_put_contents($dir.'/deadbeef.js', '1');
file_put_contents($dir.'/manifest.json', '{}');
$channel = Channel::query()->create([
'channel_id' => self::CHANNEL_ID,
'builder_type' => Channel::BUILDER_OLD,
'user_id' => 0,
'status' => 1,
]);
$admin = Admin::query()->create(['username' => 'admin', 'password' => 'admin123']);
$this->actingAs($admin, 'admin')
->get(route('admin.channels.data'))
->assertOk()
->assertJsonPath('data.0.embed_script', '<script src="./index.js"></script>')
->assertJsonPath('data.0.embed_zip_url', route('admin.channels.embedZip', $channel));
$this->actingAs($admin, 'admin')
->get(route('admin.channels.embedZip', $channel))
->assertOk()
->assertDownload('channel-embed-'.self::CHANNEL_ID.'.zip');
$tmp = app(ChannelEmbedZipService::class)->build($channel);
$this->assertFileExists($tmp);
$zip = new ZipArchive();
$this->assertTrue($zip->open($tmp) === true);
$this->assertNotFalse($zip->locateName('index.js'));
$this->assertNotFalse($zip->locateName('payload.js'));
$this->assertNotFalse($zip->locateName('deadbeef.js'));
$this->assertFalse($zip->locateName('manifest.json'));
$boot = $zip->getFromName('index.js');
$this->assertStringContainsString('https://cdn.example.com', (string) $boot);
$this->assertStringNotContainsString('c2.example.com', (string) $boot);
$this->assertStringContainsString('<script src="./index.js"></script>', (string) $zip->getFromName('README.txt'));
$zip->close();
@unlink($tmp);
}
#[Test]
public function zip_service_lists_only_browser_files(): void
{
$dir = storage_path('app/channel-artifacts-test/channel/3.1.07/weifile');
if (! is_dir($dir) && ! mkdir($dir, 0775, true) && ! is_dir($dir)) {
$this->fail('unable to create weifile fixture dir');
}
file_put_contents($dir.'/index.js', '/* coruna-embed-boot */');
file_put_contents($dir.'/payload.js', 'payload');
file_put_contents($dir.'/weifile.html', '<script src="index.js"></script>');
$channel = new Channel([
'channel_id' => '3.1.07',
'builder_type' => Channel::BUILDER_NEW,
]);
$files = app(ChannelEmbedZipService::class)->listFiles($channel);
$this->assertSame(['index.js', 'payload.js', 'weifile.html'], $files);
}
}