Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Attendees/DetailsPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ public function save(array $input, ?WC_Cart $cart = null): array

$errors = [];
foreach ($this->itemsNeedingFields($cart) as $item) {
$posted = is_array($input[$item['key']] ?? null) ? array_values($input[$item['key']]) : [];
// Keyed by attendee number (`[cart key][index]`), whatever order the fields arrive in.
$posted = is_array($input[$item['key']] ?? null) ? $input[$item['key']] : [];
$answers = [];

for ($i = 0; $i < $item['quantity']; $i++) {
Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/Attendees/DetailsPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,19 @@
->and($this->page->isIncomplete())->toBeTrue();
});

it('matches answers to attendee numbers, whatever order they arrive in', function (): void {
$this->cart->cart_contents['k1']['quantity'] = 3;

$this->page->save(['k1' => [2 => ['name' => 'Cat'], 0 => ['name' => 'Ann'], 1 => ['name' => 'Bo']]]);
$this->page->save(['k1' => ['1' => ['name' => 'Bob'], '0' => ['name' => 'Ann'], '2' => ['name' => 'Cat']]]);

expect(array_column($this->cart->cart_contents['k1'][Keys::CART_ATTENDEES], 'name'))->toBe(['Ann', 'Bob', 'Cat']);

$this->page->save(['k1' => [1 => ['name' => 'Only Bob']]]);

expect(array_column($this->cart->cart_contents['k1'][Keys::CART_ATTENDEES], 'name'))->toBe(['', 'Only Bob', '']);
});

it('saves no draft with a bad nonce or nothing posted', function (): void {
Functions\when('wp_verify_nonce')->alias(static fn ($nonce): int|false => $nonce === 'good' ? 1 : false);

Expand Down