Repository navigation
Description
Which release line(s) does this affect?
2.x
Lunar version
2.0.0-alpha.9, and 2.x at 6fbff92
PHP version
8.5.13
Laravel version
13.44.1
Database driver & version
SQLite (the monorepo's test default). Not database-specific.
Expected behaviour
When a user signs in with no cart in the session, Lunar picks up the user's own cart the way CartSessionManager::current() does: the latest cart that is active and not merged (CartSessionManager.php#L167: ->unmerged()->active()->latest('id')).
Actual behaviour
The listener's fallback uses ->active()->first() (CartSessionAuthListener.php#L45). This causes two problems:
-
It can pick a merged cart. Both auth policies leave a merged cart that still has the user's
user_id, and that cart staysactive()because it has no order.- Under
merge, the guest cart is merged into the user's cart and then given the user's id. - Under
override, the user's previous cart gets amerged_idand keeps itsuser_id.
Once the user's newer cart is paid for, the merged cart is the only "active" one. The next sign-in from a fresh session puts it back in the session, with whatever lines it held.
- Under
-
It picks an arbitrary cart. With two active carts,
first()has no order, so the user can get an old one instead of the latest.
Minimal reproduction steps
Add this as tests/core/Unit/Listeners/CartSessionAuthLoginTest.php and run it. Both tests fail on 2.x: the first with "Expecting 2 not to be 2", the second with "Failed asserting that 1 is identical to 2".
<?php
use Illuminate\Auth\Events\Login;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Session;
use Lunar\Core\Models\Cart;
use Lunar\Core\Models\Channel;
use Lunar\Core\Models\Currency;
use Lunar\Core\Models\Order;
use Lunar\Tests\Core\Stubs\User;
use Lunar\Tests\Core\TestCase;
uses(TestCase::class)->group('cart_session');
uses(RefreshDatabase::class);
test('signing in never puts a merged cart back in the session', function () {
Currency::factory()->create(['default' => true]);
Channel::factory()->create(['default' => true]);
$user = User::factory()->create();
// The cart the user's guest cart was merged into at an earlier sign-in, since paid for.
$paid = Cart::factory()->create(['user_id' => $user->id]);
Order::factory()->create(['cart_id' => $paid->id, 'placed_at' => now()]);
// The guest cart that merge retired. It has no order, so active() still matches it.
$merged = Cart::factory()->create(['user_id' => $user->id, 'merged_id' => $paid->id]);
event(new Login('web', $user, false));
expect(Session::get(config('lunar.cart_session.session_key')))->not->toBe($merged->id);
});
test('signing in picks the latest active cart, as CartSessionManager does', function () {
Currency::factory()->create(['default' => true]);
Channel::factory()->create(['default' => true]);
$user = User::factory()->create();
Cart::factory()->create(['user_id' => $user->id]);
$latest = Cart::factory()->create(['user_id' => $user->id]);
event(new Login('web', $user, false));
expect(Session::get(config('lunar.cart_session.session_key')))->toBe($latest->id);
});Where the problem is
CartSessionAuthListener.php#L45. Matching CartSessionManager's query passes both tests, and the core suite still passes:
- $userCart = Cart::whereUserId($event->user->getKey())->active()->first();
+ $userCart = Cart::whereUserId($event->user->getKey())->unmerged()->active()->latest('id')->first();
Activity