refactor(departments-edit): migrate to actionEditContext (step 2)

Pilot migration of pages/admin/departments/edit($id).php onto the
actionEditContext aggregator introduced in step 1. The CONTEXT-stage
vorspiel (lookup → authorize → tenant-scope → can_view_page → viewAuth)
collapses into a single declarative call; the POST branch (CSRF →
SUBMIT-authorize → can_update gate → service call → PRG) stays
callsite-specific as planned.

Three deliberate touches beyond a 1:1 lift:

* Additive aggregator extension: actionEditContext gains an optional
  notFoundFlashScopeKey arg so the dedup scope-key 'department_not_found'
  is preserved without widening the frozen actionResolveModelOrFail
  building-block signature. Pattern is documented as the
  forward-compatibility mechanism for future cluster migrations.
* t() consistency: the not-found message now flows through t() via the
  aggregator. To avoid a partial-translation mix, Flash::success calls
  for 'Department updated' (×2) are also wrapped — German users now see
  fully translated messages instead of a German/English mix.
* Defensive scope consumption: the action now consults
  $tenantScope['scope'] before falling through to the strict-mode
  fallback. The Departments policy never emits can_manage_all_tenants
  today (so behavior is identical), but the action is now resilient to
  future policies that might.

New ActionContextCsrfPairingContractTest enforces actionRequireCsrf()
before any POST body access for actions that use the aggregators —
preventing CSRF-pairing regressions during the cluster-wide rollout
(step 3). The step-1 testNoProductionCallSitesYet guard is removed,
since departments-edit is now the first legitimate caller; the new
pairing test takes over its protective role with a more substantive
guarantee.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-25 23:52:27 +02:00
parent 3207d71244
commit 5378209fed
5 changed files with 408 additions and 78 deletions

View File

@@ -17,6 +17,12 @@ use ReflectionNamedType;
* This test also asserts that the Tenant-Scope and Fragment-Resolver helpers
* carry their PHPStan array-shape return docblocks. Acceptance check SC-004
* relies on the shape staying stable.
*
* Step 2 (Pilot-Migration Departments-Edit, Run
* 2026-04-25-action-context-rollout-step2-departments) is the first
* production caller. The previous testNoProductionCallSitesYet assertion
* has been removed; the CSRF↔Aggregator pairing is enforced by
* tests/Architecture/ActionContextCsrfPairingContractTest.php instead.
*/
class ActionContextHelperContractTest extends TestCase
{
@@ -190,49 +196,6 @@ class ActionContextHelperContractTest extends TestCase
$this->assertTrue(function_exists('actionFragmentContext'));
}
public function testNoProductionCallSitesYet(): void
{
$root = $this->projectRootPath();
$names = [
'actionResolveModelOrFail',
'actionAuthorizeAndExtractCapabilities',
'actionDeriveTenantScope',
'actionEnforceCanViewPage',
'actionBuildViewAuth',
'actionFragmentResolveOrStatus',
'actionEditContext',
'actionCreateContext',
'actionFragmentContext',
];
$hits = [];
foreach (['pages', 'modules'] as $dir) {
$base = $root . '/' . $dir;
if (!is_dir($base)) {
continue;
}
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($base, \FilesystemIterator::SKIP_DOTS));
/** @var \SplFileInfo $file */
foreach ($iterator as $file) {
if (!$file->isFile() || $file->getExtension() !== 'php') {
continue;
}
$content = (string) file_get_contents($file->getPathname());
foreach ($names as $name) {
if (preg_match('/\b' . preg_quote($name, '/') . '\s*\(/', $content)) {
$hits[] = $name . ' in ' . str_replace($root . '/', '', $file->getPathname());
}
}
}
}
$this->assertSame(
[],
$hits,
"Step 1 must not introduce production call-sites — defer to Step 2 (Departments-Edit pilot):\n" . implode("\n", $hits)
);
}
/**
* Ensure the `mixed` return type of building blocks is preserved where
* documented. Catches accidental tightening that would break Step 2.