Files
api/services/nginx/app/tests/Unit/Selfserve/SelfservePropertyGatePermissionBypassTest.php
T

122 lines
4.3 KiB
PHP

<?php
app_require('routes/moduleSelfServeRoute.php');
app_require('modules/selfserve/classes/selfserve_lane.php');
app_require('objects/department_lanes_o.php');
app_require('classes/object_property.php');
use classes\object_property;
use modules\selfserve\classes\selfserve_lane;
use objects\department_lanes_o;
use routes\moduleSelfServeRoute;
class SelfservePropertyGatePermissionBypassRouteHarness extends moduleSelfServeRoute
{
public bool $activeWashResult = false;
/** @var array<string,bool> */
public array $permissions = [
'list_own_department_selfserve_vehicle_conditions' => true,
];
/** @var array<int,array{department_id:int,customer_number:int}> */
public array $activeWashChecks = [];
public function hasPermission(string|\classes\permission_node $permission, int $customer_number = null): bool
{
$key = $permission instanceof \classes\permission_node ? (string)$permission->permission : $permission;
return $this->permissions[$key] ?? false;
}
public function canUsePropertyGate(selfserve_lane $lane, int $customer_number): bool
{
return $this->canCustomerUsePropertyGateForLane($lane, $customer_number);
}
protected function isLaneSelfServeOperationallyEnabled(selfserve_lane $lane): bool
{
return true;
}
protected function customerHasActiveSelfServeWashInDepartment(int $department_id, int $customer_number): bool
{
$this->activeWashChecks[] = [
'department_id' => $department_id,
'customer_number' => $customer_number,
];
return $this->activeWashResult;
}
}
class SelfservePropertyGatePermissionBypassDepartmentLaneFake extends department_lanes_o
{
public function __construct(int $department_id)
{
$this->id = -1;
$this->department = new object_property('department_lanes', -1, 'department', 'int');
$this->department->set($department_id);
}
public function structure(): void
{
// Skip database bootstrap for this unit test.
}
}
function selfserve_property_gate_permission_lane_for_department(int $department_id): selfserve_lane
{
$reflection = new ReflectionClass(selfserve_lane::class);
/** @var selfserve_lane $lane */
$lane = $reflection->newInstanceWithoutConstructor();
$lane->id = 77;
$lane->department_lane = new SelfservePropertyGatePermissionBypassDepartmentLaneFake($department_id);
return $lane;
}
it('allows property gate commands for customers with an active wash in the target department', function (): void {
$route = new SelfservePropertyGatePermissionBypassRouteHarness();
$route->activeWashResult = true;
$lane = selfserve_property_gate_permission_lane_for_department(6);
expect($route->canUsePropertyGate($lane, 12345679))->toBeTrue();
expect($route->activeWashChecks)->toBe([
[
'department_id' => 6,
'customer_number' => 12345679,
],
]);
});
it('does not bypass property gate permissions without a positive customer number', function (): void {
$route = new SelfservePropertyGatePermissionBypassRouteHarness();
$route->activeWashResult = true;
$lane = selfserve_property_gate_permission_lane_for_department(6);
expect($route->canUsePropertyGate($lane, 0))->toBeFalse();
expect($route->activeWashChecks)->toBe([]);
});
it('does not bypass property gate permissions without customer self-serve permission', function (): void {
$route = new SelfservePropertyGatePermissionBypassRouteHarness();
$route->activeWashResult = true;
$route->permissions = [];
$lane = selfserve_property_gate_permission_lane_for_department(6);
expect($route->canUsePropertyGate($lane, 12345679))->toBeFalse();
expect($route->activeWashChecks)->toBe([]);
});
it('does not bypass property gate permissions when the customer has no active wash in the target department', function (): void {
$route = new SelfservePropertyGatePermissionBypassRouteHarness();
$route->activeWashResult = false;
$lane = selfserve_property_gate_permission_lane_for_department(6);
expect($route->canUsePropertyGate($lane, 12345679))->toBeFalse();
expect($route->activeWashChecks)->toBe([
[
'department_id' => 6,
'customer_number' => 12345679,
],
]);
});