feat: use normalized checkbox values in field handlers

This commit is contained in:
Björn Fromme
2026-03-16 12:03:00 +01:00
parent 6abfa122e4
commit 40c0042158
6 changed files with 69 additions and 4 deletions
@@ -153,6 +153,21 @@ abstract class AbstractParticipantFieldHandler implements ParticipantFieldHandle
return is_numeric($value) ? (int) $value : null; return is_numeric($value) ? (int) $value : null;
} }
/**
* Normalizes a checkbox/form toggle value to a boolean.
*
* Form checkboxes may submit true, false, 1, 0, '1', '0', or be absent.
* This method treats only true, 1, and '1' as checked; everything else as unchecked.
*
* @param mixed $value The raw value from form submission
*
* @return bool True if the checkbox is considered checked, false otherwise
*/
protected function normalizeCheckboxValue(mixed $value): bool
{
return true === $value || 1 === $value || '1' === $value;
}
/** /**
* Returns field state modifications that should be applied after processing. * Returns field state modifications that should be applied after processing.
* *
@@ -68,6 +68,6 @@ class ParticipantBulkInsuranceFieldHandler extends AbstractParticipantFieldHandl
// Get checkbox value from submitted data and store it // Get checkbox value from submitted data and store it
$bulkInsuranceBooking = $this->getFieldValue($submittedData, $this->getFieldName()); $bulkInsuranceBooking = $this->getFieldValue($submittedData, $this->getFieldName());
$participant->bulkInsuranceBooking = (bool) $bulkInsuranceBooking; $participant->bulkInsuranceBooking = $this->normalizeCheckboxValue($bulkInsuranceBooking);
} }
} }
@@ -334,7 +334,7 @@ class ParticipantInsuranceFieldHandler extends AbstractParticipantFieldHandler
// Check if bulk insurance booking is enabled // Check if bulk insurance booking is enabled
$bulkInsuranceBooking = $applicantData['bulkInsuranceBooking'] ?? false; $bulkInsuranceBooking = $applicantData['bulkInsuranceBooking'] ?? false;
if (false === (bool) $bulkInsuranceBooking) { if (false === $this->normalizeCheckboxValue($bulkInsuranceBooking)) {
return false; return false;
} }
@@ -66,7 +66,7 @@ class ParticipantParkingFieldHandler extends AbstractParticipantFieldHandler
} }
$parkingSelected = $this->getFieldValue($submittedData, $this->getFieldName()); $parkingSelected = $this->getFieldValue($submittedData, $this->getFieldName());
$isParkingSelected = (bool) $parkingSelected; $isParkingSelected = $this->normalizeCheckboxValue($parkingSelected);
// Store boolean value for backward compatibility // Store boolean value for backward compatibility
$participant->parking = $isParkingSelected; $participant->parking = $isParkingSelected;
@@ -100,7 +100,7 @@ class ParticipantRentalInsuranceFieldHandler extends AbstractParticipantFieldHan
// Extract checkbox value from submitted data (this comes from the rentalInsuranceSelected property) // Extract checkbox value from submitted data (this comes from the rentalInsuranceSelected property)
$rentalInsuranceSelected = $this->getFieldValue($submittedData, $this->getFieldName()); $rentalInsuranceSelected = $this->getFieldValue($submittedData, $this->getFieldName());
$isRentalInsuranceSelected = (bool) $rentalInsuranceSelected; $isRentalInsuranceSelected = $this->normalizeCheckboxValue($rentalInsuranceSelected);
// Store boolean value // Store boolean value
$participant->rentalInsuranceSelected = $isRentalInsuranceSelected; $participant->rentalInsuranceSelected = $isRentalInsuranceSelected;
@@ -37,6 +37,22 @@ class ParticipantParkingFieldHandlerTest extends TestCase
$this->assertTrue($this->handler->shouldProcess(['some' => 'data'], BookingDto::MODE_EDIT, 5)); $this->assertTrue($this->handler->shouldProcess(['some' => 'data'], BookingDto::MODE_EDIT, 5));
} }
/**
* @dataProvider normalizeCheckboxValueCheckedProvider
*/
public function testNormalizeCheckboxValueReturnsTrueWhenChecked(mixed $value): void
{
$this->assertTrue($this->invokeNormalizeCheckboxValue($value));
}
/**
* @dataProvider normalizeCheckboxValueUncheckedProvider
*/
public function testNormalizeCheckboxValueReturnsFalseWhenUnchecked(mixed $value): void
{
$this->assertFalse($this->invokeNormalizeCheckboxValue($value));
}
public function testProcessFieldWithoutParticipant(): void public function testProcessFieldWithoutParticipant(): void
{ {
$travel = new Travel(); $travel = new Travel();
@@ -241,6 +257,40 @@ class ParticipantParkingFieldHandlerTest extends TestCase
$this->assertNull($participant->parkingService); $this->assertNull($participant->parkingService);
} }
/**
* @return array<string, array{mixed}>
*/
public static function normalizeCheckboxValueCheckedProvider(): array
{
return [
'true' => [true],
'int 1' => [1],
'string 1' => ['1'],
];
}
/**
* @return array<string, array{mixed}>
*/
public static function normalizeCheckboxValueUncheckedProvider(): array
{
return [
'false' => [false],
'int 0' => [0],
'string 0' => ['0'],
'empty string' => [''],
'null' => [null],
'non-numeric string' => ['off'],
];
}
private function invokeNormalizeCheckboxValue(mixed $value): bool
{
$reflection = new \ReflectionMethod($this->handler, 'normalizeCheckboxValue');
return $reflection->invoke($this->handler, $value);
}
private function createTransportationService(string $subType): Service private function createTransportationService(string $subType): Service
{ {
$service = new Service(); $service = new Service();