fix: stop offering unbookable services in the booking edit flow
This commit is contained in:
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace App\Tests\Service;
|
||||
|
||||
use App\BusProNet\Constants;
|
||||
use App\BusProNet\Model\Booking;
|
||||
use App\BusProNet\Model\Service;
|
||||
use App\BusProNet\Model\Travel;
|
||||
use App\Form\Model\BookingDto;
|
||||
@@ -85,6 +86,95 @@ class ServiceAvailabilityCalculatorTest extends TestCase
|
||||
$this->assertSame([1, 4], array_keys($filtered));
|
||||
}
|
||||
|
||||
public function testOnRequestServiceStaysAvailableInCreateMode(): void
|
||||
{
|
||||
$skiPass = $this->createSkiPass(id: 1, available: 10, status: Constants::STATUS_ON_REQUEST);
|
||||
$bookingDto = $this->createBookingDtoWithServices([$skiPass]);
|
||||
|
||||
// Create may still move the whole booking to status 'A', so on request is bookable there
|
||||
$this->assertFalse($this->calculator->isServiceUnavailable(1, $bookingDto, 0));
|
||||
}
|
||||
|
||||
public function testOnRequestServiceIsUnavailableInEditMode(): void
|
||||
{
|
||||
$skiPass = $this->createSkiPass(id: 1, available: 10, status: Constants::STATUS_ON_REQUEST);
|
||||
$bookingDto = $this->createEditBookingDtoWithServices([$skiPass]);
|
||||
|
||||
$this->assertTrue($this->calculator->isServiceUnavailable(1, $bookingDto, 0));
|
||||
}
|
||||
|
||||
public function testOnRequestServiceAlreadyHeldStaysAvailableInEditMode(): void
|
||||
{
|
||||
$skiPass = $this->createSkiPass(id: 1, available: 0, status: Constants::STATUS_ON_REQUEST);
|
||||
$bookingDto = $this->createEditBookingDtoWithServices([$skiPass], heldServiceIds: [1]);
|
||||
|
||||
// Withdrawing a booked service breaks the Leistung/Teilnehmer counts, so it must be keepable
|
||||
$this->assertFalse($this->calculator->isServiceUnavailable(1, $bookingDto, 0));
|
||||
}
|
||||
|
||||
public function testAutoBookedOnRequestServiceStaysAvailableInEditMode(): void
|
||||
{
|
||||
$service = $this->createSkiPass(id: 1, available: 10, status: Constants::STATUS_ON_REQUEST);
|
||||
$service->autoBook = true;
|
||||
$bookingDto = $this->createEditBookingDtoWithServices([$service]);
|
||||
|
||||
$this->assertFalse($this->calculator->isServiceUnavailable(1, $bookingDto, 0));
|
||||
}
|
||||
|
||||
public function testMandatoryNonTransportOnRequestServiceStaysAvailableInEditMode(): void
|
||||
{
|
||||
$service = $this->createSkiPass(id: 1, available: 10, status: Constants::STATUS_ON_REQUEST);
|
||||
$service->mandatory = true;
|
||||
$service->category = Constants::CATEGORY_ADDITIONAL;
|
||||
$bookingDto = $this->createEditBookingDtoWithServices([$service]);
|
||||
|
||||
// Blocking a service the mandatory-services validator requires makes the form unsatisfiable
|
||||
$this->assertFalse($this->calculator->isServiceUnavailable(1, $bookingDto, 0));
|
||||
}
|
||||
|
||||
public function testMandatoryTransportOnRequestServiceIsStillUnavailableInEditMode(): void
|
||||
{
|
||||
$service = $this->createSkiPass(id: 1, available: 10, status: Constants::STATUS_ON_REQUEST);
|
||||
$service->mandatory = true;
|
||||
$service->category = Constants::CATEGORY_TRANSPORTATION;
|
||||
$bookingDto = $this->createEditBookingDtoWithServices([$service]);
|
||||
|
||||
// pflicht on a transport leg means "pick one of this group", not "compulsory"
|
||||
$this->assertTrue($this->calculator->isServiceUnavailable(1, $bookingDto, 0));
|
||||
}
|
||||
|
||||
public function testFilterAvailableServicesNeverEmptiesAGroupOnTheOnRequestRuleAlone(): void
|
||||
{
|
||||
$first = $this->createSkiPass(id: 1, available: 10, status: Constants::STATUS_ON_REQUEST);
|
||||
$second = $this->createSkiPass(id: 2, available: 10, status: Constants::STATUS_ON_REQUEST);
|
||||
|
||||
$bookingDto = $this->createEditBookingDtoWithServices([$first, $second]);
|
||||
|
||||
$filtered = $this->calculator->filterAvailableServices(
|
||||
$bookingDto->travel->additionalServices,
|
||||
$bookingDto,
|
||||
0
|
||||
);
|
||||
|
||||
$this->assertSame([1, 2], array_keys($filtered));
|
||||
}
|
||||
|
||||
public function testFilterAvailableServicesStillEmptiesAGroupThatIsSoldOutOrBlocked(): void
|
||||
{
|
||||
$soldOut = $this->createSkiPass(id: 1, available: 0, status: Constants::STATUS_ON_REQUEST);
|
||||
$blocked = $this->createSkiPass(id: 2, available: null, status: Constants::STATUS_BLOCKED);
|
||||
|
||||
$bookingDto = $this->createEditBookingDtoWithServices([$soldOut, $blocked]);
|
||||
|
||||
$filtered = $this->calculator->filterAvailableServices(
|
||||
$bookingDto->travel->additionalServices,
|
||||
$bookingDto,
|
||||
0
|
||||
);
|
||||
|
||||
$this->assertSame([], array_keys($filtered));
|
||||
}
|
||||
|
||||
private function createParkingService(int $id, int $available): Service
|
||||
{
|
||||
$service = new Service();
|
||||
@@ -123,4 +213,27 @@ class ServiceAvailabilityCalculatorTest extends TestCase
|
||||
|
||||
return $bookingDto;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, Service> $services
|
||||
* @param list<int> $heldServiceIds IDs participant 0 already holds in the booking
|
||||
*/
|
||||
private function createEditBookingDtoWithServices(array $services, array $heldServiceIds = []): BookingDto
|
||||
{
|
||||
$bookingDto = $this->createBookingDtoWithServices($services);
|
||||
|
||||
$heldServices = [];
|
||||
foreach ($heldServiceIds as $serviceId) {
|
||||
$held = clone $bookingDto->travel->additionalServices[$serviceId];
|
||||
$held->mapping = [0];
|
||||
$heldServices[$serviceId] = $held;
|
||||
}
|
||||
|
||||
// A non-null booking is what puts the DTO into edit mode
|
||||
$booking = new Booking();
|
||||
$booking->additionalServices = $heldServices;
|
||||
$bookingDto->booking = $booking;
|
||||
|
||||
return $bookingDto;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user