diff --git a/api/BusinessLogic/Tickets/TicketCreator.php b/api/BusinessLogic/Tickets/TicketCreator.php index ee510b69..df177387 100644 --- a/api/BusinessLogic/Tickets/TicketCreator.php +++ b/api/BusinessLogic/Tickets/TicketCreator.php @@ -9,6 +9,7 @@ use BusinessLogic\Security\BanRetriever; use BusinessLogic\Tickets\CustomFields\CustomFieldValidator; use BusinessLogic\ValidationModel; use BusinessLogic\Validators; +use Core\Constants\CustomField; class TicketCreator { /** @@ -95,10 +96,10 @@ class TicketCreator { $validationModel->errorKeys[] = "CUSTOM_FIELD_{$customFieldNumber}_INVALID::NO_VALUE"; continue; } - /*switch($value['type']) { - case 'date': + switch($value['type']) { + case CustomField::DATE: if (!preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $custom_field_value)) { - $validationModel->errorKeys[] = 'CUSTOM_FIELD_' . $key . '_INVALID::INVALID_DATE'; + $validationModel->errorKeys[] = 'CUSTOM_FIELD_' . $customFieldNumber . '_INVALID::INVALID_DATE'; } else { // Actually validate based on range $date = strtotime($custom_field_value . ' t00:00:00'); @@ -106,18 +107,18 @@ class TicketCreator { $dmax = strlen($value['value']['dmax']) ? strtotime($value['value']['dmax'] . ' t00:00:00') : false; if ($dmin && $dmin > $date) { - $validationModel->errorKeys[] = 'CUSTOM_FIELD_' . $key . '_INVALID::DATE_BEFORE_MIN::MIN-' . $dmin . '::ENTERED-' . $date; + $validationModel->errorKeys[] = 'CUSTOM_FIELD_' . $customFieldNumber . '_INVALID::DATE_BEFORE_MIN::MIN:' . date('Y-m-d', $dmin) . '::ENTERED:' . date('Y-m-d', $date); } elseif ($dmax && $dmax < $date) { - $validationModel->errorKeys[] = 'CUSTOM_FIELD_' . $key . '_INVALID::DATE_AFTER_MAX::MAX-' . $dmax . '::ENTERED-' . $date; + $validationModel->errorKeys[] = 'CUSTOM_FIELD_' . $customFieldNumber . '_INVALID::DATE_AFTER_MAX::MAX:' . date('Y-m-d', $dmax) . '::ENTERED:' . date('Y-m-d', $date); } } break; - case 'email': + /*case 'email': if (!hesk_validateEmail($custom_field_value, $value['value']['multiple'], false)) { $validationModel->errorKeys[] = 'CUSTOM_FIELD_' . $key . '_INVALID::INVALID_OR_MISSING_EMAIL'; } - break; - }*/ + break;*/ + } } } diff --git a/api/Core/Constants/CustomField.php b/api/Core/Constants/CustomField.php new file mode 100644 index 00000000..566e1c76 --- /dev/null +++ b/api/Core/Constants/CustomField.php @@ -0,0 +1,22 @@ +heskSettings['custom_fields']['custom1'] = $customField; @@ -402,4 +403,120 @@ class TicketCreatorTest extends TestCase { //-- Assert (2/2) $this->assertThat($exceptionThrown, $this->equalTo(true)); } + + function testItAddsTheProperValidationErrorWhenTheCustomerSubmitsTicketWithBlankRequiredCustomField() { + //-- Arrange + $customField = array(); + $customField['req'] = 1; + $customField['type'] = CustomField::TEXT; + $customField['use'] = 1; + $customField['category'] = array(); + $this->heskSettings['custom_fields']['custom1'] = $customField; + $this->ticketRequest->customFields[1] = ''; + + //-- Act + $exceptionThrown = false; + try { + $this->ticketCreator->createTicketByCustomer($this->ticketRequest, + $this->heskSettings, + $this->modsForHeskSettings, + $this->userContext); + } catch (ValidationException $e) { + //-- Assert (1/2) + $exceptionThrown = true; + $this->assertArraySubset(['CUSTOM_FIELD_1_INVALID::NO_VALUE'], $e->validationModel->errorKeys); + } + + //-- Assert (2/2) + $this->assertThat($exceptionThrown, $this->equalTo(true)); + } + + function testItAddsTheProperValidationErrorWhenTheCustomerSubmitsTicketWithDateCustomFieldThatIsInvalid() { + //-- Arrange + $customField = array(); + $customField['req'] = 1; + $customField['type'] = CustomField::DATE; + $customField['use'] = 1; + $customField['category'] = array(); + $this->heskSettings['custom_fields']['custom1'] = $customField; + $this->ticketRequest->customFields[1] = '2017-30-00'; + + //-- Act + $exceptionThrown = false; + try { + $this->ticketCreator->createTicketByCustomer($this->ticketRequest, + $this->heskSettings, + $this->modsForHeskSettings, + $this->userContext); + } catch (ValidationException $e) { + //-- Assert (1/2) + $exceptionThrown = true; + $this->assertArraySubset(['CUSTOM_FIELD_1_INVALID::INVALID_DATE'], $e->validationModel->errorKeys); + } + + //-- Assert (2/2) + $this->assertThat($exceptionThrown, $this->equalTo(true)); + } + + function testItAddsTheProperValidationErrorWhenTheCustomerSubmitsTicketWithDateThatIsBeforeMinDate() { + //-- Arrange + $customField = array(); + $customField['req'] = 1; + $customField['type'] = CustomField::DATE; + $customField['use'] = 1; + $customField['category'] = array(); + $customField['value'] = array( + 'dmin' => '2017-01-01', + 'dmax' => '' + ); + $this->heskSettings['custom_fields']['custom1'] = $customField; + $this->ticketRequest->customFields[1] = '2016-12-31'; + + //-- Act + $exceptionThrown = false; + try { + $this->ticketCreator->createTicketByCustomer($this->ticketRequest, + $this->heskSettings, + $this->modsForHeskSettings, + $this->userContext); + } catch (ValidationException $e) { + //-- Assert (1/2) + $exceptionThrown = true; + $this->assertArraySubset(['CUSTOM_FIELD_1_INVALID::DATE_BEFORE_MIN::MIN:2017-01-01::ENTERED:2016-12-31'], $e->validationModel->errorKeys); + } + + //-- Assert (2/2) + $this->assertThat($exceptionThrown, $this->equalTo(true)); + } + + function testItAddsTheProperValidationErrorWhenTheCustomerSubmitsTicketWithDateThatIsAfterMaxDate() { + //-- Arrange + $customField = array(); + $customField['req'] = 1; + $customField['type'] = CustomField::DATE; + $customField['use'] = 1; + $customField['category'] = array(); + $customField['value'] = array( + 'dmin' => '', + 'dmax' => '2017-01-01' + ); + $this->heskSettings['custom_fields']['custom1'] = $customField; + $this->ticketRequest->customFields[1] = '2017-01-02'; + + //-- Act + $exceptionThrown = false; + try { + $this->ticketCreator->createTicketByCustomer($this->ticketRequest, + $this->heskSettings, + $this->modsForHeskSettings, + $this->userContext); + } catch (ValidationException $e) { + //-- Assert (1/2) + $exceptionThrown = true; + $this->assertArraySubset(['CUSTOM_FIELD_1_INVALID::DATE_AFTER_MAX::MAX:2017-01-01::ENTERED:2017-01-02'], $e->validationModel->errorKeys); + } + + //-- Assert (2/2) + $this->assertThat($exceptionThrown, $this->equalTo(true)); + } }