web/website/ops/oscommerce/test-shop-design-bootstrap.php

48 lines
3.8 KiB
PHP

<?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>','cache',[],'<body><script src="/oscommerce-inner.js?v=realm-design-1"></script></body>'],
['/catalog/dune-shop',200,'html','<body><script src="/oscommerce-inner.js"></script></body>','cache',[],'<body><script src="/oscommerce-inner.js?v=realm-design-1"></script></body>'],
['/catalog/dune-shop',200,'html','<body><script src="/oscommerce-inner.js?v=realm-design-1"></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/dune-shop',200,'html','<section>Native document</section>',true,['HTTP_SEC_FETCH_DEST'=>'iframe']],
['/catalog/product',200,'html','<section>Partial response</section>',false,['HTTP_X_REQUESTED_WITH'=>'XMLHttpRequest']],
['/catalog/product',200,'html','<section>Fetch response</section>',false,['HTTP_SEC_FETCH_DEST'=>'empty']],
['/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 $case){
[$path,$status,$format,$content,$decorate]=$case;
unset($_SERVER['HTTP_X_REQUESTED_WITH'],$_SERVER['HTTP_SEC_FETCH_DEST']);foreach($case[5]??[] as $header=>$value)$_SERVER[$header]=$value;
$_SERVER['REQUEST_URI']=$path;$r=$app->response;$r->statusCode=$status;$r->format=$format;$r->content=$content;
($r->handler)((object)['sender'=>$r]);
if($decorate==='cache'){if($r->content!==$case[6])throw new \RuntimeException('Existing bridge was duplicated or cache version retained');}
elseif($decorate){if(substr_count($r->content,'src="/oscommerce-inner.js?v=realm-design-1"')!==1||!str_contains($r->content,' defer></script>'))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";
}