Files
api/services/nginx/app/tests/Unit/Orders/OrderItemsListingOrderingTest.php
T
Jeppe B 58d5e177a9 fix(api): order order_items so primary precedes addons in getOrderItems (#364)
Defensive ORDER BY in orders_o.php::getOrderItems so primary items render before their addons (related_item_id IS NULL DESC, related_item_id ASC, id ASC). Pinned with OrderItemsListingOrderingTest which locates orders_o.php via worktree-aware resolver.
2026-08-11 07:05:59 +02:00

104 lines
4.2 KiB
PHP

<?php
declare(strict_types=1);
// Locate the worktree's `orders_o.php`. The default `app_path()` helper resolves
// symlinks and points at the primary checkout's source, which we are forbidden to
// mutate. We prefer (in order):
// 1. The TRUCKWASH_WORKTREE_ROOT env var when set and valid
// 2. Walking up from this test file's directory to a project root that owns
// the `services/nginx/app/objects/orders_o.php` file
// 3. Falling back to the primary checkout's path (only when neither of the
// above resolves; this is the production CI path)
$ordersObjectFileResolver = static function (): string {
$candidate = getenv('TRUCKWASH_WORKTREE_ROOT');
if (is_string($candidate) && $candidate !== '' && is_dir($candidate)) {
$path = $candidate . '/services/nginx/app/objects/orders_o.php';
if (is_file($path)) {
return $path;
}
}
// Walk up from this test file looking for the orders_o.php in the same
// services/nginx/app tree. The test lives in
// services/nginx/app/tests/Unit/Orders/, so the target lives 4 levels up
// from this file's directory. We still walk defensively so the test works
// even if the test is moved into a deeper or shallower location.
$directory = __DIR__;
for ($i = 0; $i < 10; $i++) {
$candidatePath = $directory . '/objects/orders_o.php';
if (is_file($candidatePath)) {
return $candidatePath;
}
$parent = dirname($directory);
if ($parent === $directory) {
break;
}
$directory = $parent;
}
$existing = app_path('objects/orders_o.php');
if (is_file($existing)) {
return $existing;
}
throw new RuntimeException('Unable to locate orders_o.php for the addon ordering test.');
};
it('orders the SELECT in getOrderItems so primary items precede their addons', function () use ($ordersObjectFileResolver): void {
$ordersObjectFile = $ordersObjectFileResolver();
$content = file_get_contents($ordersObjectFile);
expect($content)->not->toBeFalse();
// Locate the getOrderItems method body.
$start = strpos($content, 'public function getOrderItems(int $order_id): array');
expect($start)->not->toBeFalse();
// Bound the search so we don't accidentally match unrelated SQL further down
// the file (the helper around line 645 in orders_o.php also uses ORDER BY id DESC).
$end = strpos($content, "\n }\n", $start);
expect($end)->not->toBeFalse();
$methodBody = substr($content, (int)$start, (int)$end - (int)$start);
// The SELECT against order_items must include an explicit ORDER BY so MySQL
// does not return rows in undefined order (which has been observed to put
// primary order items after their addons, breaking the FE tree-builder and
// the invoice line listing).
expect($methodBody)
->toContain("FROM order_items WHERE order_id = \$order_id")
->and($methodBody)
->toContain('ORDER BY');
// The ORDER BY must place primary items first (related_item_id IS NULL DESC),
// group addons by their parent (related_item_id ASC), and fall back to
// insertion order (id ASC).
expect($methodBody)
->toContain('(related_item_id IS NULL) DESC')
->and($methodBody)
->toContain('related_item_id ASC')
->and($methodBody)
->toContain('id ASC');
});
it('does not leave the legacy unordered SELECT in getOrderItems', function () use ($ordersObjectFileResolver): void {
$ordersObjectFile = $ordersObjectFileResolver();
$content = file_get_contents($ordersObjectFile);
expect($content)->not->toBeFalse();
$start = strpos($content, 'public function getOrderItems(int $order_id): array');
$end = strpos($content, "\n }\n", $start);
expect($start)->not->toBeFalse()
->and($end)->not->toBeFalse();
$methodBody = substr($content, (int)$start, (int)$end - (int)$start);
// The buggy SQL must be gone: previously this returned rows in whatever
// order MySQL felt like, leading to addons being listed before their primary
// and the operator seeing "only Trailer and Dolly" attached to a Trækker order.
expect($methodBody)
->not->toContain("FROM order_items WHERE order_id = \$order_id\";\n \$result");
});