From e22d318b9244329d9f1497d1e83741e6bb94308d Mon Sep 17 00:00:00 2001 From: Mike Koch Date: Tue, 7 Feb 2017 22:16:15 -0500 Subject: [PATCH] More tests for TicketCreator. Need to refactor some custom field logic --- api/BusinessLogic/Tickets/TicketCreator.php | 15 +- api/Core/Constants/Priority.php | 11 ++ .../Tickets/TicketCreatorTest.php | 152 +++++++++++++++++- 3 files changed, 170 insertions(+), 8 deletions(-) create mode 100644 api/Core/Constants/Priority.php diff --git a/api/BusinessLogic/Tickets/TicketCreator.php b/api/BusinessLogic/Tickets/TicketCreator.php index de42e351..005db7e2 100644 --- a/api/BusinessLogic/Tickets/TicketCreator.php +++ b/api/BusinessLogic/Tickets/TicketCreator.php @@ -73,7 +73,7 @@ class TicketCreator { } // Don't allow critical priority tickets - /*if ($heskSettings['cust_urgency'] && intval($ticketRequest->priority) === $TICKET_PRIORITY_CRITICAL) { + if ($heskSettings['cust_urgency'] && intval($ticketRequest->priority) === $TICKET_PRIORITY_CRITICAL) { $validationModel->errorKeys[] = 'CRITICAL_PRIORITY_FORBIDDEN'; } @@ -88,13 +88,14 @@ class TicketCreator { } foreach ($heskSettings['custom_fields'] as $key => $value) { - if ($value['use'] == 1 && hesk_is_custom_field_in_category($key, intval($ticketRequest->category))) { - $custom_field_value = $ticketRequest->customFields[$key]; + $customFieldNumber = intval(str_replace('custom', '', $key)); + if ($value['use'] == 1 && hesk_is_custom_field_in_category($customFieldNumber, intval($ticketRequest->category))) { + $custom_field_value = $ticketRequest->customFields[$customFieldNumber]; if (empty($custom_field_value)) { - $validationModel->errorKeys[] = 'CUSTOM_FIELD_' . $key . '_INVALID::NO_VALUE'; + $validationModel->errorKeys[] = "CUSTOM_FIELD_{$customFieldNumber}_INVALID::NO_VALUE"; continue; } - switch($value['type']) { + /*switch($value['type']) { case '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'; @@ -116,11 +117,11 @@ class TicketCreator { $validationModel->errorKeys[] = 'CUSTOM_FIELD_' . $key . '_INVALID::INVALID_OR_MISSING_EMAIL'; } break; - } + }*/ } } - if ($banRetriever->isEmailBanned($ticketRequest->email, $heskSettings)) { + /*if ($banRetriever->isEmailBanned($ticketRequest->email, $heskSettings)) { $validationModel->errorKeys[] = 'EMAIL_BANNED'; }*/ diff --git a/api/Core/Constants/Priority.php b/api/Core/Constants/Priority.php new file mode 100644 index 00000000..88f98cc3 --- /dev/null +++ b/api/Core/Constants/Priority.php @@ -0,0 +1,11 @@ +ticketRequest->name = 'Name'; $this->ticketRequest->email = 'some@e.mail'; $this->ticketRequest->category = 1; + $this->ticketRequest->priority = Priority::HIGH; + $this->ticketRequest->subject = 'Subject'; + $this->ticketRequest->message = 'Message'; + $this->ticketRequest->customFields = array(); $this->heskSettings = array( - 'multi_eml' => false + 'multi_eml' => false, + 'cust_urgency' => false, + 'require_subject' => 1, + 'require_message' => 1, + 'custom_fields' => array(), ); $category = new Category(); @@ -256,4 +265,145 @@ class TicketCreatorTest extends TestCase { //-- Assert (2/2) $this->assertThat($exceptionThrown, $this->equalTo(true)); } + + function testItAddsTheProperValidationErrorWhenTheCustomerSubmitsTicketWithPriorityCritical() { + //-- Arrange + $this->ticketRequest->priority = Priority::CRITICAL; + $this->heskSettings['cust_urgency'] = true; + + //-- 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(['CRITICAL_PRIORITY_FORBIDDEN'], $e->validationModel->errorKeys); + } + + //-- Assert (2/2) + $this->assertThat($exceptionThrown, $this->equalTo(true)); + } + + function testItAddsTheProperValidationErrorWhenTheCustomerSubmitsTicketWithNullSubjectAndItIsRequired() { + //-- Arrange + $this->ticketRequest->subject = null; + $this->heskSettings['require_subject'] = 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(['SUBJECT_REQUIRED'], $e->validationModel->errorKeys); + } + + //-- Assert (2/2) + $this->assertThat($exceptionThrown, $this->equalTo(true)); + } + + function testItAddsTheProperValidationErrorWhenTheCustomerSubmitsTicketWithBlankSubjectAndItIsRequired() { + //-- Arrange + $this->ticketRequest->subject = ''; + $this->heskSettings['require_subject'] = 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(['SUBJECT_REQUIRED'], $e->validationModel->errorKeys); + } + + //-- Assert (2/2) + $this->assertThat($exceptionThrown, $this->equalTo(true)); + } + + function testItAddsTheProperValidationErrorWhenTheCustomerSubmitsTicketWithNullMessageAndItIsRequired() { + //-- Arrange + $this->ticketRequest->message = null; + $this->heskSettings['require_message'] = 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(['MESSAGE_REQUIRED'], $e->validationModel->errorKeys); + } + + //-- Assert (2/2) + $this->assertThat($exceptionThrown, $this->equalTo(true)); + } + + function testItAddsTheProperValidationErrorWhenTheCustomerSubmitsTicketWithBlankMessageAndItIsRequired() { + //-- Arrange + $this->ticketRequest->message = ''; + $this->heskSettings['require_message'] = 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(['MESSAGE_REQUIRED'], $e->validationModel->errorKeys); + } + + //-- Assert (2/2) + $this->assertThat($exceptionThrown, $this->equalTo(true)); + } + + function testItAddsTheProperValidationErrorWhenTheCustomerSubmitsTicketWithNullRequiredCustomField() { + $this->markTestIncomplete( + 'Not complete; need to refactor custom field in category' + ); + + //-- Arrange + $customField = array(); + $customField['req'] = 1; + $customField['type'] = 'text'; + $customField['use'] = 1; + $customField['category'] = array(); + $this->heskSettings['custom_fields']['custom1'] = $customField; + $this->ticketRequest->customFields[1] = null; + + //-- 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)); + } }