Merge pull request 'Load shop styling on rendered native HTML pages' (#576) from dev/MC:design/shop-response-hook-20261125 into main
All checks were successful
DuneRealms Web CI / web (push) Successful in 3m33s
All checks were successful
DuneRealms Web CI / web (push) Successful in 3m33s
This commit is contained in:
commit
86ce3baded
4 changed files with 74 additions and 1 deletions
|
|
@ -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,'</body')===false||str_contains($response->content,'/oscommerce-inner.js'))return;
|
||||
$response->content=preg_replace('~</body\s*>~i','<script src="/oscommerce-inner.js?v=realm-design-1" defer></script></body>',$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();
|
||||
|
|
|
|||
41
website/ops/oscommerce/test-shop-design-bootstrap.php
Normal file
41
website/ops/oscommerce/test-shop-design-bootstrap.php
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
namespace yii\base { class Application { const EVENT_BEFORE_REQUEST='beforeRequest'; } }
|
||||
namespace yii\web {
|
||||
class Response {
|
||||
const EVENT_AFTER_PREPARE='afterPrepare';const FORMAT_HTML='html';
|
||||
public $statusCode=200;public $format='html';public $content='';public $handler;
|
||||
public function on($event,$handler):void { if($event!==self::EVENT_AFTER_PREPARE)throw new \RuntimeException('Wrong response event');$this->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','<html><body>Shop</body></html>',true],
|
||||
['/catalog/product?products_id=1',200,'html','<html><body>Product</body></html>',true],
|
||||
['/catalog/shopping-cart',200,'html','<html><body>Cart</body></html>',true],
|
||||
['/catalog/checkout',200,'html','<HTML><BODY>Checkout</BODY></HTML>',true],
|
||||
['/catalog/dune-shop',200,'html','<body><script src="/oscommerce-inner.js?v=existing"></script></body>',false],
|
||||
['/catalog/dune-shop/sales-feed',200,'json','{"items":[]}',false],
|
||||
['/catalog/account',303,'html','<html><body>Redirect</body></html>',false],
|
||||
['/catalog/product',500,'html','<html><body>Error</body></html>',false],
|
||||
['/catalog/product',200,'html','<section>Partial response</section>',false],
|
||||
['/catalog/download',200,'raw','<body>Raw download</body>',false],
|
||||
['/admin/',200,'html','<html><body>Admin</body></html>',false],
|
||||
['/dune-login.php',200,'html','<html><body>Identity handoff</body></html>',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></script></body>'))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";
|
||||
}
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
10
website/ops/test_native_shop_design.py
Normal file
10
website/ops/test_native_shop_design.py
Normal file
|
|
@ -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()
|
||||
Loading…
Reference in a new issue

Staff