Add some more validation tests for dates

remotes/upstream/api-rewrite
Mike Koch 7 years ago
parent e44baa99c2
commit bc9bb698c5

@ -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;*/
}
}
}

@ -0,0 +1,22 @@
<?php
/**
* Created by PhpStorm.
* User: cokoch
* Date: 2/10/2017
* Time: 12:53 PM
*/
namespace Core\Constants;
class CustomField {
const RADIO = 'radio';
const SELECT = 'select';
const CHECKBOX = 'checkbox';
const TEXTAREA = 'textarea';
const DATE = 'date';
const EMAIL = 'email';
const HIDDEN = 'hidden';
const READONLY = 'readonly';
const TEXT = 'text';
}

@ -14,6 +14,7 @@ use BusinessLogic\Categories\CategoryRetriever;
use BusinessLogic\Exceptions\ValidationException;
use BusinessLogic\Security\BanRetriever;
use BusinessLogic\Security\UserContext;
use Core\Constants\CustomField;
use Core\Constants\Priority;
use PHPUnit\Framework\TestCase;
@ -380,7 +381,7 @@ class TicketCreatorTest extends TestCase {
//-- Arrange
$customField = array();
$customField['req'] = 1;
$customField['type'] = 'text';
$customField['type'] = CustomField::TEXT;
$customField['use'] = 1;
$customField['category'] = array();
$this->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));
}
}

Loading…
Cancel
Save