diff --git a/website/ops/oscommerce/DuneAccount.php b/website/ops/oscommerce/DuneAccount.php index 7a90f7da..d49ca00e 100644 --- a/website/ops/oscommerce/DuneAccount.php +++ b/website/ops/oscommerce/DuneAccount.php @@ -16,7 +16,16 @@ final class DuneAccount { if($status!==200) throw new \yii\web\HttpException($status===401?401:503,'DuneRealms sign-in is unavailable. Please sign in again.'); return json_decode($body,true,32,JSON_THROW_ON_ERROR); } - public function bootstrap($app):void { $app->on(\yii\base\Application::EVENT_BEFORE_REQUEST,fn()=>self::handle($app)); } + // DUNE SHOP DESIGN BOOTSTRAP START + public function bootstrap($app):void { + $app->on(\yii\base\Application::EVENT_BEFORE_REQUEST,fn()=>self::handle($app)); + $app->response->on(\yii\web\Response::EVENT_AFTER_PREPARE,static function($event):void { + $response=$event->sender;$path=parse_url($_SERVER['REQUEST_URI']??'/',PHP_URL_PATH); + if(!is_string($path)||!str_starts_with($path,'/catalog/')||$response->statusCode<200||$response->statusCode>=300||$response->format!==\yii\web\Response::FORMAT_HTML||!is_string($response->content)||stripos($response->content,'content,'/oscommerce-inner.js'))return; + $response->content=preg_replace('~~i','',$response->content,1); + }); + } + // DUNE SHOP DESIGN BOOTSTRAP END private static function redirect($app,string $url):never { $app->response->redirect($url,303)->send();$app->end();exit; } private static function start($app,string $next='/catalog/'):never { $state=self::token();$nonce=self::token(); diff --git a/website/ops/oscommerce/test-shop-design-bootstrap.php b/website/ops/oscommerce/test-shop-design-bootstrap.php new file mode 100644 index 00000000..f757e1a8 --- /dev/null +++ b/website/ops/oscommerce/test-shop-design-bootstrap.php @@ -0,0 +1,41 @@ +handler=$handler; } + } +} +namespace { + require $argv[1]??__DIR__.'/DuneAccount.php'; + $app=new class { + public $response;public $authenticationHooks=0; + public function __construct(){ $this->response=new \yii\web\Response(); } + public function on($event,$handler):void { if($event!==\yii\base\Application::EVENT_BEFORE_REQUEST||!is_callable($handler))throw new \RuntimeException('Authentication hook changed');$this->authenticationHooks++; } + }; + (new \frontend\components\DuneAccount())->bootstrap($app); + if($app->authenticationHooks!==1)throw new \RuntimeException('Authentication hook missing'); + $cases=[ + ['/catalog/dune-shop',200,'html','Shop',true], + ['/catalog/product?products_id=1',200,'html','Product',true], + ['/catalog/shopping-cart',200,'html','Cart',true], + ['/catalog/checkout',200,'html','Checkout',true], + ['/catalog/dune-shop',200,'html','',false], + ['/catalog/dune-shop/sales-feed',200,'json','{"items":[]}',false], + ['/catalog/account',303,'html','Redirect',false], + ['/catalog/product',500,'html','Error',false], + ['/catalog/product',200,'html','
Partial response
',false], + ['/catalog/download',200,'raw','Raw download',false], + ['/admin/',200,'html','Admin',false], + ['/dune-login.php',200,'html','Identity handoff',false], + ]; + foreach($cases as [$path,$status,$format,$content,$decorate]){ + $_SERVER['REQUEST_URI']=$path;$r=$app->response;$r->statusCode=$status;$r->format=$format;$r->content=$content; + ($r->handler)((object)['sender'=>$r]); + if($decorate){if(substr_count($r->content,'src="/oscommerce-inner.js?v=realm-design-1"')!==1||!str_contains($r->content,' defer>'))throw new \RuntimeException('Missing single presentation bridge: '.$path);} + elseif($r->content!==$content)throw new \RuntimeException('Non-page response changed: '.$path); + if($r->statusCode!==$status||$r->format!==$format)throw new \RuntimeException('Response contract changed'); + } + echo count($cases)." native response cases passed; authentication hook retained\n"; +} diff --git a/website/ops/publish-realm-design.py b/website/ops/publish-realm-design.py index 8050b889..3c235b1a 100644 --- a/website/ops/publish-realm-design.py +++ b/website/ops/publish-realm-design.py @@ -44,6 +44,19 @@ shop_inner=(shop_app/'oscommerce-inner.js').resolve(strict=True);assert shop_inn # Keep the native resizing/checkout bridge and apply the same design inside it. changes['shop-native/oscommerce-inner.js']=append(shop_inner.read_text(),script) targets['shop-native/oscommerce-inner.js']=shop_inner +account=(shop_app/'lib/frontend/components/DuneAccount.php').resolve(strict=True);assert account.is_relative_to(shop_app) +original_account=account.read_text();source_account=(release/'ops/oscommerce/DuneAccount.php').read_text() +bootstrap_pattern=r' // DUNE SHOP DESIGN BOOTSTRAP START[\s\S]*? // DUNE SHOP DESIGN BOOTSTRAP END' +bootstrap=re.search(bootstrap_pattern,source_account).group(0) +if 'DUNE SHOP DESIGN BOOTSTRAP START' in original_account: + assert len(re.findall(bootstrap_pattern,original_account))==1 + account_content=re.sub(bootstrap_pattern,lambda _:bootstrap,original_account,count=1) +else: + old_bootstrap=' public function bootstrap($app):void { $app->on(\\yii\\base\\Application::EVENT_BEFORE_REQUEST,fn()=>self::handle($app)); }' + assert original_account.count(old_bootstrap)==1 + account_content=original_account.replace(old_bootstrap,bootstrap,1) + assert account_content.replace(bootstrap,old_bootstrap,1)==original_account +changes['shop-native/DuneAccount.php']=account_content;targets['shop-native/DuneAccount.php']=account for kind,document in [('clans','clan-center-index.html'),('corp','company-center-index.html')]: root=pathlib.Path('/var/www/dunerealms-'+kind).resolve(strict=True) name=kind+'-portal/index.html';changes[name]=(release/'ops'/document).read_text();targets[name]=(root/'index.html').resolve(strict=True) diff --git a/website/ops/test_native_shop_design.py b/website/ops/test_native_shop_design.py new file mode 100644 index 00000000..304f53d8 --- /dev/null +++ b/website/ops/test_native_shop_design.py @@ -0,0 +1,10 @@ +import pathlib,shutil,subprocess,unittest + +class NativeShopDesign(unittest.TestCase): + @unittest.skipUnless(shutil.which('php'),'PHP is checked on the native shop host') + def test_response_hook_preserves_non_page_responses(self): + root=pathlib.Path(__file__).resolve().parent/'oscommerce' + result=subprocess.run(['php',str(root/'test-shop-design-bootstrap.php'),str(root/'DuneAccount.php')],capture_output=True,text=True,check=True) + self.assertIn('12 native response cases passed',result.stdout) + +if __name__=='__main__':unittest.main()