Files
api/services/nginx/app/tests/Unit/Subusers/SubusersRouteManagementContractTest.php
T
Jeppe B ab6c3ba5b6 Fix route permission instance calls (#344)
## Root cause

`route_t::hasPermission()` and `requirePermission()` are instance
methods. Route code was invoking them with `self::`; the new XL Vask
hall-scope helper made that call from a genuinely static context,
causing PHP to throw:

`Non-static method routes\\xlvaskUsageLogsRoute::hasPermission() cannot
be called statically`

## Changes

- Invoke route permission methods through `$this` across all 273
executable legacy calls in 45 route classes.
- Make `xlvaskUsageLogsRoute::allowedHallIdsForUser()` an instance
helper and update all 13 callers.
- Preserve the existing all-scope and own-scope hall selection rules.
- Add a token-aware regression test that rejects executable
`self::hasPermission()` and `self::requirePermission()` calls, while
ignoring comments.
- Add focused XL Vask tests for global scanner hall scope and
group-limited own scope.
- Update affected route contract assertions to the instance-call form.

## Verification

- PHP lint: all 53 changed PHP files
- Focused PHPStan: changed XL Vask route and both new regression tests —
clean
- Focused regression slice: 58 passed, 748 assertions
- Full local unit suite: 1,300 passed, 9,442 assertions (1 unrelated
existing warning, 1 environment skip)
- Full local API suite: 285 passed, 11,704 assertions
- Exact-SHA GitHub Tests workflow: all 7 jobs passed (unit, API,
integration, legacy, edge gateway, and supporting checks)
- Independent exact-SHA QA gate: PASS, no findings
- Independent exact-SHA security gate: PASS, no findings
- Independent exact-SHA reviewer gate: PASS, no findings
- Remote comparison: exactly one commit ahead of
`40b104abed7723a7d1b7028190ecda0e7aeef829`; all 53 remote blob hashes
matched the reviewed worktree

## Delivery state

Draft only for human review. No merge or deployment is included. Qodana
is skipped while the PR remains draft and is therefore not represented
as a passed gate.
2026-08-04 16:04:41 +02:00

158 lines
9.0 KiB
PHP

<?php
it('exposes chauffeur management endpoints on the subusers route', function (): void {
$routeFile = app_path('routes/subusersRoute.php');
expect(is_file($routeFile))->toBeTrue();
$code = (string)file_get_contents($routeFile);
$normalized = preg_replace('/\s+/', ' ', $code);
expect($normalized)->toContain("\$this->post('/subusers/invite', function () {");
expect($normalized)->toContain("\$this->post('/subusers/invite/resend', function () {");
expect($normalized)->toContain("\$this->get('/superuser/subusers', function () {");
expect($normalized)->toContain("\$this->post('/superuser/subusers/invite', function () {");
expect($normalized)->toContain("\$this->post('/superuser/subusers/invite/resend', function () {");
expect($normalized)->toContain("\$this->put('/subusers', function () {");
expect($normalized)->toContain("\$this->put('/subusers/me', function () {");
expect($normalized)->toContain("\$this->get('/subusers/me/verification', function () {");
expect($normalized)->toContain("\$this->post('/subusers/me/verification/{channel}/send', function () {");
expect($normalized)->toContain("\$this->post('/subusers/me/verification/{channel}/verify', function () {");
expect($normalized)->toContain("\$this->post('/superuser/subusers/{subuser_id}/verification/{channel}/send', function () {");
expect($normalized)->toContain("\$this->patch('/superuser/subusers/{subuser_id}/verification/{channel}', function () {");
expect($normalized)->toContain("\$this->post('/superuser/subusers/{subuser_id}/password-guide/{channel}/send', function () {");
expect($normalized)->toContain("\$this->post('/superuser/subusers/{subuser_id}/login-link/{channel}/send', function () {");
expect($normalized)->toContain("\$this->post('/subusers/{subuser_id}/verification/{channel}/send', function () {");
expect($normalized)->toContain("\$this->post('/subusers/password-reset/request', function () {");
expect($normalized)->toContain("\$this->get('/subusers/password-reset/validate', function () {");
expect($normalized)->toContain("\$this->post('/subusers/password-reset/set', function () {");
expect($normalized)->toContain("\$this->post('/subusers/me/password', function () {");
expect($normalized)->toContain("\$this->get('/subusers/access-decision', function () {");
expect($normalized)->toContain("\$this->post('/subusers/access-decision', function () {");
});
it('delivers purpose-bound SMS notifications for grant requests and decisions', function (): void {
$code = (string)file_get_contents(app_path('routes/subusersRoute.php'));
$normalized = preg_replace('/\s+/', ' ', $code);
expect($normalized)->toContain('notifyCustomerOfGrantRequest(');
expect($normalized)->toContain('PURPOSE_GRANT_APPROVE');
expect($normalized)->toContain('PURPOSE_GRANT_DENY');
expect($normalized)->toContain('notifySubuserGrantDecision(');
expect($normalized)->toContain("'decision_notification' => \$delivery");
expect($normalized)->toContain('$this->getPublicRegistrationPending($token)');
expect($normalized)->toContain('$this->notifyCustomerOfGrantRequest(');
});
it('keeps forgot-password responses generic and protects reset tokens by purpose', function (): void {
$code = (string)file_get_contents(app_path('routes/subusersRoute.php'));
$normalized = preg_replace('/\s+/', ' ', $code);
expect($normalized)->toContain('Hvis chaufførkontoen findes, er et nulstillingslink sendt.');
expect($normalized)->toContain('PURPOSE_PASSWORD_RESET');
expect($normalized)->toContain("account_deletion_service::principalIsBlocked('subuser'");
expect($normalized)->toContain('invalidateCurrentSetupToken()');
});
it('includes grant management fields in the subusers payload builder', function (): void {
$routeFile = app_path('routes/subusersRoute.php');
expect(is_file($routeFile))->toBeTrue();
$code = (string)file_get_contents($routeFile);
$normalized = preg_replace('/\s+/', ' ', $code);
expect($normalized)->toContain("'grant_id' =>");
expect($normalized)->toContain("'grant_enabled' =>");
expect($normalized)->toContain("'grant_note' =>");
expect($normalized)->toContain("'grant_permissions' =>");
expect($normalized)->toContain("'setup_required' =>");
expect($normalized)->toContain("'invite_accepted' =>");
expect($normalized)->toContain("'can_resend_invite' =>");
expect($normalized)->toContain("'profile_editable_by_manager' => false");
expect($normalized)->toContain("'access_state' =>");
expect($normalized)->toContain("'email_verified' =>");
expect($normalized)->toContain("'phone_verified' =>");
expect($normalized)->toContain("'verification_state' =>");
});
it('resolves subuser customer names without external lookups during list requests', function (): void {
$routeFile = app_path('routes/subusersRoute.php');
expect(is_file($routeFile))->toBeTrue();
$code = (string)file_get_contents($routeFile);
$normalized = preg_replace('/\s+/', ' ', $code);
expect($normalized)->toContain('getCustomerNames($customerNumbers, false)');
expect($normalized)->toContain('$customerName = $this->resolveCustomerName($customerNumber);');
expect($normalized)->toContain('buildSubuserManagementPayload($subuser, $customerNumber, $customerName)');
expect($normalized)->not->toContain("'customer_name' => (new users_o())->getCustomerName(\$customerNumber)");
expect($normalized)->not->toContain("'name' => (new users_o())->getCustomerName((int)\$grant['billing_customer_number'])");
});
it('links grant disable operations to SUBUSERS_DELETE for own-customer managers', function (): void {
$routeFile = app_path('routes/subusersRoute.php');
expect(is_file($routeFile))->toBeTrue();
$code = (string)file_get_contents($routeFile);
$normalized = preg_replace('/\s+/', ' ', $code);
expect($normalized)->toContain("definePermission('delete_own_subusers', subusers_permission_node_key::SUBUSERS_DELETE)");
expect($normalized)->toContain("requireManagedCustomerScope(subusers_permission_node_key::SUBUSERS_DELETE, \$targetCustomer);");
});
it('parses custom grant writes through raw permissions instead of template expansion', function (): void {
$routeFile = app_path('routes/subusersRoute.php');
expect(is_file($routeFile))->toBeTrue();
$code = (string)file_get_contents($routeFile);
$normalized = preg_replace('/\s+/', ' ', $code);
expect($normalized)->toContain('expandTemplateForWritePayload($templateKey)');
});
it('prevents own-customer managers from editing driver-owned account profiles', function (): void {
$routeFile = app_path('routes/subusersRoute.php');
expect(is_file($routeFile))->toBeTrue();
$code = (string)file_get_contents($routeFile);
$normalized = preg_replace('/\s+/', ' ', $code);
expect($normalized)->toContain("Customers can only manage subuser grants. Drivers own their account profile.");
});
it('scopes superuser invite resend to a selected customer grant', function (): void {
$routeFile = app_path('routes/subusersRoute.php');
expect(is_file($routeFile))->toBeTrue();
$code = (string)file_get_contents($routeFile);
$normalized = preg_replace('/\s+/', ' ', $code);
expect($normalized)->toContain("\$this->post('/superuser/subusers/invite/resend', function () {");
expect($normalized)->toContain("self::requireParameters(['id', 'customer_number']);");
expect($normalized)->toContain("\$customerNumber = (int)self::getParameter('customer_number');");
expect($normalized)->toContain("getGrantForSubuserAndCustomer(\$subuserId, \$customerNumber, true)");
expect($normalized)->toContain("Subuser grant not found for selected customer");
expect($normalized)->toContain("'subuser' => \$this->buildSubuserManagementPayload(\$subuser, \$customerNumber)");
expect($normalized)->toContain("'grant' => \$grant->asArray()");
});
it('only allows invite resend while setup is still pending', function (): void {
$routeFile = app_path('routes/subusersRoute.php');
expect(is_file($routeFile))->toBeTrue();
$code = (string)file_get_contents($routeFile);
$normalized = preg_replace('/\s+/', ' ', $code);
expect($normalized)->toContain("Driver account already accepted the invitation.");
expect($normalized)->toContain("if (!\$subuser->requiresSetup()) {");
});
it('requires own subuser permissions for classic customer users in managed customer scope', function (): void {
$routeFile = app_path('routes/subusersRoute.php');
expect(is_file($routeFile))->toBeTrue();
$code = (string)file_get_contents($routeFile);
$normalized = preg_replace('/\s+/', ' ', $code);
expect($normalized)->toContain("\$user = \$auth->get_user(); if (\$user !== false) { \$customerNumber = (int)\$user->customer_number->value(); if (\$customerNumber <= 0) { \$response->error('Unauthorized', 401); } if (\$targetCustomerNumber !== null && \$customerNumber !== (int)\$targetCustomerNumber) { \$this->emitForbidden([\$permission]); } \$this->requirePermission(\$permission); return \$customerNumber;");
});