52 lines
6.8 KiB
PHP
52 lines
6.8 KiB
PHP
<?php
|
|
namespace Tests\Feature\SharedThemes;
|
|
use App\Support\{InstanceRuntime,Setting,SiteProfile};
|
|
use App\Services\ThemeService;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Foundation\Bootstrap\RegisterProviders;
|
|
use Illuminate\Support\Facades\{Cache,Schema,Http,File};
|
|
use Illuminate\Validation\ValidationException;
|
|
use Tests\TestCase;
|
|
class SharedThemesTest extends TestCase
|
|
{
|
|
public function createApplication(){
|
|
$app=require dirname(__DIR__,3).'/bootstrap/app.php';
|
|
$app->beforeBootstrapping(RegisterProviders::class,function($app){$app['config']->set(['database.default'=>'sqlite','database.connections.sqlite.database'=>':memory:','cache.default'=>'array','cache.stores.redis'=>['driver'=>'array'],'queue.default'=>'sync','app.key'=>'base64:'.base64_encode(str_repeat('t',32)),'site.theme'=>null,'site.name'=>null,'site.logo'=>null,'site.description'=>null]);});
|
|
$app->make(\Illuminate\Contracts\Console\Kernel::class)->bootstrap();return $app;
|
|
}
|
|
protected function setUp():void{
|
|
parent::setUp();Schema::create('v2_plugins',fn(Blueprint $t)=>[$t->id(),$t->boolean('is_enabled')->default(false)]);Schema::create('v2_settings',fn(Blueprint $t)=>[$t->id(),$t->string('name'),$t->text('value')->nullable(),$t->timestamps()]);Cache::store('redis')->flush();$this->app->forgetInstance(Setting::class);Http::preventStrayRequests();
|
|
}
|
|
public function test_all_runtime_themes_share_one_canonical_root_and_draft_is_hidden():void{
|
|
$s=new ThemeService;$names=array_keys($s->getList());foreach(['Xboard','Stellar','Fuc','EZ','Hardnerd'] as $name){$this->assertContains($name,$names);$this->assertSame(base_path('theme/'.$name),$s->getThemePath($name));}$this->assertNotContains('tpl-a',$names);$this->assertNull($s->getThemePath('../.env'));
|
|
}
|
|
public function test_arbitrary_instance_can_select_any_installed_theme_without_mutating_cached_settings():void{
|
|
admin_setting(['frontend_theme'=>'Stellar','app_name'=>'Original']);admin_setting('frontend_theme');$before=Cache::store('redis')->get(Setting::CACHE_KEY);config(['site.profile'=>'independent','site.theme'=>'Fuc','site.name'=>'Other']);$this->app->forgetInstance(Setting::class);$this->assertSame('Fuc',admin_setting('frontend_theme'));$this->assertSame('Other',admin_setting('app_name'));$this->assertSame($before,Cache::store('redis')->get(Setting::CACHE_KEY));config(['site.theme'=>null,'site.name'=>null]);$this->app->forgetInstance(Setting::class);$this->assertSame('Stellar',admin_setting('frontend_theme'));
|
|
}
|
|
public function test_instance_theme_lock_rejects_admin_override_before_writing():void{
|
|
config(['site.theme'=>'EZ']);$this->expectException(ValidationException::class);(new ThemeService)->switch('Stellar');
|
|
}
|
|
public function test_switch_keeps_other_instance_assets_and_excludes_source_dependencies_and_secrets():void{
|
|
$name='SharedFixture'.bin2hex(random_bytes(4));$source=base_path('theme/'.$name);$public=sys_get_temp_dir().'/xb-theme-test-'.bin2hex(random_bytes(4));$oldPublic=public_path();
|
|
foreach(['assets/.vite','source','adapter','node_modules','.git'] as $d)File::ensureDirectoryExists($source.'/'.$d);
|
|
File::put($source.'/config.json',json_encode(['name'=>$name,'version'=>'1.0','configs'=>[]]));File::put($source.'/dashboard.blade.php','server-only');File::put($source.'/assets/runtime.js','public-runtime');File::put($source.'/assets/.vite/manifest.json','{}');File::put($source.'/source/secret.js','must-not-publish');File::put($source.'/.env','must-not-publish');
|
|
$this->app->usePublicPath($public);File::ensureDirectoryExists($public.'/theme/Stellar');File::put($public.'/theme/Stellar/keep.txt','other-instance');admin_setting(['current_theme'=>'Stellar']);
|
|
try{(new ThemeService)->switch($name);$this->assertFileExists($public.'/theme/Stellar/keep.txt');$this->assertFileExists($public.'/theme/'.$name.'/assets/runtime.js');$this->assertFileExists($public.'/theme/'.$name.'/assets/.vite/manifest.json');foreach(['source','adapter','node_modules','.git','.env','dashboard.blade.php','config.json'] as $blocked)$this->assertFileDoesNotExist($public.'/theme/'.$name.'/'.$blocked);}
|
|
finally{$this->app->usePublicPath($oldPublic);File::deleteDirectory($source);File::deleteDirectory($public);}
|
|
}
|
|
public function test_switch_can_reuse_identical_read_only_assets_from_a_git_release():void{
|
|
$name='ReadonlyFixture'.bin2hex(random_bytes(4));$source=base_path('theme/'.$name);$public=sys_get_temp_dir().'/xb-readonly-theme-'.bin2hex(random_bytes(4));$oldPublic=public_path();
|
|
File::ensureDirectoryExists($source.'/assets');File::put($source.'/config.json',json_encode(['name'=>$name,'version'=>'1.0','configs'=>[]]));File::put($source.'/dashboard.blade.php','view');File::put($source.'/assets/runtime.js','immutable-runtime');$this->app->usePublicPath($public);
|
|
try{$service=new ThemeService;$service->publish($name);$asset=$public.'/theme/'.$name.'/assets/runtime.js';chmod($asset,0444);chmod(dirname($asset),0555);$this->assertTrue($service->switch($name));$this->assertSame($name,admin_setting('current_theme'));$this->assertSame('immutable-runtime',File::get($asset));$this->assertSame(0444,fileperms($asset)&0777);}
|
|
finally{if(isset($asset)&&is_file($asset)){chmod(dirname($asset),0755);chmod($asset,0644);}$this->app->usePublicPath($oldPublic);File::deleteDirectory($source);File::deleteDirectory($public);}
|
|
}
|
|
public function test_instance_bootstrap_separates_env_storage_and_framework_caches():void{
|
|
$root=sys_get_temp_dir().'/xb-instance-test-'.bin2hex(random_bytes(4));$keys=['XB_INSTANCE','APP_CONFIG_CACHE','APP_ROUTES_CACHE','APP_EVENTS_CACHE','APP_SERVICES_CACHE','APP_PACKAGES_CACHE'];$saved=[];foreach($keys as $k)$saved[$k]=[$_SERVER[$k]??null,$_ENV[$k]??null,getenv($k)];
|
|
try{foreach(['first','second'] as $id){File::ensureDirectoryExists($root.'/instances/'.$id);File::put($root.'/instances/'.$id.'/.env','APP_NAME=test');$_SERVER['XB_INSTANCE']=$id;$app=new \Illuminate\Foundation\Application($root);InstanceRuntime::configure($app);$paths[$id]=[$app->environmentFilePath(),$app->storagePath(),$app->getCachedConfigPath(),$app->getCachedRoutesPath(),$app->getCachedServicesPath()];$this->assertStringContainsString('/instances/'.$id.'/',$paths[$id][2]);}$this->assertSame([],array_intersect($paths['first'],$paths['second']));}
|
|
finally{foreach($saved as $k=>[$server,$env,$value]){if($server===null)unset($_SERVER[$k]);else $_SERVER[$k]=$server;if($env===null)unset($_ENV[$k]);else $_ENV[$k]=$env;if($value===false)putenv($k);else putenv($k.'='.$value);}File::deleteDirectory($root);}
|
|
}
|
|
public function test_instance_bootstrap_rejects_unknown_or_traversing_selectors():void{
|
|
$before=$_SERVER['XB_INSTANCE']??null;$_SERVER['XB_INSTANCE']='../hn';try{$this->expectException(\InvalidArgumentException::class);InstanceRuntime::configure(new \Illuminate\Foundation\Application(base_path()));}finally{if($before===null)unset($_SERVER['XB_INSTANCE']);else $_SERVER['XB_INSTANCE']=$before;}
|
|
}
|
|
}
|