igengx
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
require dirname(__DIR__) . '/vendor/topthink/framework/src/helper.php';
|
||||
|
||||
use app\adminapi\controller\tcm\PrescriptionController;
|
||||
use app\adminapi\http\middleware\AuthMiddleware;
|
||||
use app\Request;
|
||||
use app\common\service\esign\EsignClient;
|
||||
use app\common\service\esign\PrescriptionEsignService;
|
||||
use think\facade\Db;
|
||||
|
||||
$app = new think\App();
|
||||
$manager = new think\DbManager();
|
||||
$manager->setConfig(['default' => 'test', 'connections' => ['test' => [
|
||||
'type' => 'sqlite', 'database' => ':memory:', 'prefix' => 'zyt_', 'fields_strict' => true,
|
||||
]]]);
|
||||
$app->instance('think\DbManager', $manager);
|
||||
$config = new think\Config();
|
||||
$defaults = ['enabled' => true, 'environment' => 'sandbox', 'doctor_quicksign_enabled' => false,
|
||||
'review_enabled' => false, 'app_id' => 'fixture', 'app_secret' => 'fixture-secret',
|
||||
'sdk_url' => 'http://127.0.0.1:8091/paas-sdk-service'];
|
||||
$config->set($defaults, 'esign');
|
||||
$app->instance('config', $config);
|
||||
Db::execute('CREATE TABLE zyt_config (id INTEGER PRIMARY KEY,type TEXT,name TEXT,value TEXT,create_time INTEGER,update_time INTEGER)');
|
||||
Db::execute('CREATE TABLE zyt_admin (id INTEGER PRIMARY KEY,root INTEGER,disable INTEGER DEFAULT 0,delete_time INTEGER)');
|
||||
Db::name('admin')->insertAll([['id' => 1, 'root' => 1], ['id' => 2, 'root' => 0]]);
|
||||
|
||||
$checks = 0;
|
||||
$expect = static function (bool $ok, string $message) use (&$checks): void {
|
||||
if (!$ok) {
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
$checks++;
|
||||
};
|
||||
$invoke = static function (string $method, string $action, array $post, int $actor = 1, int $root = 1) use ($app): array {
|
||||
$request = (new Request())->withServer(['REMOTE_ADDR' => '127.0.0.1', 'REQUEST_METHOD' => $method])
|
||||
->setMethod($method)->setController('tcm.Prescription')->setAction($action)->withPost($post)->filter('trim');
|
||||
$request->adminInfo = ['admin_id' => $actor, 'root' => $root, 'login_ip' => '127.0.0.1'];
|
||||
$app->instance('request', $request);
|
||||
$controller = new PrescriptionController($app);
|
||||
$request->controllerObject = $controller;
|
||||
$response = (new AuthMiddleware())->handle($request, static fn () => $controller->{$action}());
|
||||
return json_decode($response->getContent(), true, flags: JSON_THROW_ON_ERROR);
|
||||
};
|
||||
|
||||
$read = static fn () => $invoke('GET', 'esignConfig', [], 2, 0);
|
||||
$save = static fn (bool $enabled) => $invoke('POST', 'esignSetConfig', ['enabled' => $enabled]);
|
||||
$expect($read()['data'] === ['enabled' => true, 'environment' => 'sandbox'], 'ordinary accounts read the .env fallback');
|
||||
$expect($invoke('GET', 'esignSetConfig', [])['code'] === 0, 'GET cannot change the policy');
|
||||
$expect($invoke('POST', 'esignSetConfig', ['enabled' => false], 2, 0)['code'] === 0, 'middleware denies a non-root writer');
|
||||
$expect($invoke('POST', 'esignSetConfig', ['enabled' => false], 2, 1)['code'] === 0, 'stale root claims cannot write');
|
||||
foreach ([[], ['enabled' => 'false'], ['enabled' => 0], ['enabled' => [true]]] as $malformed) {
|
||||
$expect($invoke('POST', 'esignSetConfig', $malformed)['code'] === 0, 'only a JSON boolean can switch modes');
|
||||
}
|
||||
$expect(Db::name('config')->count() === 0, 'rejected setting changes leave storage untouched');
|
||||
|
||||
$expect($save(false)['data'] === ['enabled' => false, 'environment' => 'sandbox']
|
||||
&& $read()['data']['enabled'] === false && !(new EsignClient())->isEnabled(), 'explicit off overrides enabled .env for clients and SDK');
|
||||
$legacy = ['id' => 17, 'audit_status' => 0, 'review_submitted_at' => 123, 'esign_status' => 'legacy'];
|
||||
$expect(!PrescriptionEsignService::requiresSignature($legacy, PrescriptionEsignService::enabled())
|
||||
&& PrescriptionEsignService::canReview($legacy), 'uploaded handwritten prescriptions use ordinary review');
|
||||
$draft = array_replace($legacy, ['esign_required' => 1, 'esign_status' => 'pending']);
|
||||
$expect(!PrescriptionEsignService::canReview($draft), 'CA-enrolled drafts cannot pass as handwritten');
|
||||
|
||||
$config->set(array_replace($defaults, ['enabled' => false, 'app_secret' => '']), 'esign');
|
||||
$expect($save(true)['code'] === 0 && $read()['data']['enabled'] === false, 'invalid SDK config leaves the previous setting active');
|
||||
$config->set(array_replace($defaults, ['enabled' => false]), 'esign');
|
||||
$enabled = $save(true)['data'];
|
||||
$expect($enabled === ['enabled' => true, 'environment' => 'sandbox', 'review_enabled' => true, 'verification_method' => 'quick'],
|
||||
'explicit on enables quick signing and pharmacist CA independently of previous rollout flags');
|
||||
$expect($read()['data'] === $enabled && (new EsignClient())->isEnabled(), 'saved setting is observed across requests and by SDK');
|
||||
$expect(!PrescriptionEsignService::canReview($legacy), 'eligible unsigned draft must satisfy the CA signature after enablement');
|
||||
$expect($save(true)['data'] === $enabled && Db::name('config')->count() === 1, 'repeated saves keep one setting record');
|
||||
|
||||
echo "Prescription CA setting: {$checks} checks passed (isolated SQLite and offline SDK configuration)\n";
|
||||
Reference in New Issue
Block a user