From d476f86c8c041fe08a80a002848c2ba25e4fdb6a Mon Sep 17 00:00:00 2001 From: Mike Koch Date: Fri, 10 Feb 2017 22:09:46 -0500 Subject: [PATCH] Finished validation tests for create ticket --- api/BusinessLogic/Tickets/TicketCreator.php | 23 +++-- .../Tickets/TicketCreatorTest.php | 86 ++++++++++++++++++- 2 files changed, 101 insertions(+), 8 deletions(-) diff --git a/api/BusinessLogic/Tickets/TicketCreator.php b/api/BusinessLogic/Tickets/TicketCreator.php index df177387..0ace14ea 100644 --- a/api/BusinessLogic/Tickets/TicketCreator.php +++ b/api/BusinessLogic/Tickets/TicketCreator.php @@ -20,10 +20,15 @@ class TicketCreator { * @var $banRetriever BanRetriever */ private $banRetriever; + /** + * @var $ticketValidators TicketValidators + */ + private $ticketValidators; - function __construct($categoryRetriever, $banRetriever) { + function __construct($categoryRetriever, $banRetriever, $ticketValidators) { $this->categoryRetriever = $categoryRetriever; $this->banRetriever = $banRetriever; + $this->ticketValidators = $ticketValidators; } /** @@ -113,18 +118,22 @@ class TicketCreator { } } break; - /*case 'email': - if (!hesk_validateEmail($custom_field_value, $value['value']['multiple'], false)) { - $validationModel->errorKeys[] = 'CUSTOM_FIELD_' . $key . '_INVALID::INVALID_OR_MISSING_EMAIL'; + case CustomField::EMAIL: + if (!Validators::validateEmail($custom_field_value, $value['value']['multiple'], false)) { + $validationModel->errorKeys[] = "CUSTOM_FIELD_{$customFieldNumber}_INVALID::INVALID_EMAIL"; } - break;*/ + break; } } } - /*if ($banRetriever->isEmailBanned($ticketRequest->email, $heskSettings)) { + if ($this->banRetriever->isEmailBanned($ticketRequest->email, $heskSettings)) { $validationModel->errorKeys[] = 'EMAIL_BANNED'; - }*/ + } + + if ($this->ticketValidators->isCustomerAtMaxTickets($ticketRequest->email, $heskSettings)) { + $validationModel->errorKeys[] = 'EMAIL_AT_MAX_OPEN_TICKETS'; + } // TODO Check if we're at the max number of tickets // TODO submit_ticket.php:325-334 diff --git a/api/Tests/BusinessLogic/Tickets/TicketCreatorTest.php b/api/Tests/BusinessLogic/Tickets/TicketCreatorTest.php index cd54c8a8..2098bee6 100644 --- a/api/Tests/BusinessLogic/Tickets/TicketCreatorTest.php +++ b/api/Tests/BusinessLogic/Tickets/TicketCreatorTest.php @@ -34,6 +34,11 @@ class TicketCreatorTest extends TestCase { */ private $categoryRetriever; + /** + * @var $ticketValidators \PHPUnit_Framework_MockObject_MockObject + */ + private $ticketValidators; + /** * @var $ticketRequest CreateTicketByCustomerModel */ @@ -50,7 +55,8 @@ class TicketCreatorTest extends TestCase { function setUp() { $this->banRetriever = $this->createMock(BanRetriever::class); $this->categoryRetriever = $this->createMock(CategoryRetriever::class); - $this->ticketCreator = new TicketCreator($this->categoryRetriever, $this->banRetriever); + $this->ticketValidators = $this->createMock(TicketValidators::class); + $this->ticketCreator = new TicketCreator($this->categoryRetriever, $this->banRetriever, $this->ticketValidators); $this->userContext = new UserContext(); $this->ticketRequest = new CreateTicketByCustomerModel(); @@ -519,4 +525,82 @@ class TicketCreatorTest extends TestCase { //-- Assert (2/2) $this->assertThat($exceptionThrown, $this->equalTo(true)); } + + function testItAddsTheProperValidationErrorWhenTheCustomerSubmitsTicketWithEmailThatIsInvalid() { + //-- Arrange + $customField = array(); + $customField['req'] = 1; + $customField['type'] = CustomField::EMAIL; + $customField['use'] = 1; + $customField['category'] = array(); + $customField['value'] = array( + 'multiple' => 0 + ); + $this->heskSettings['custom_fields']['custom1'] = $customField; + $this->ticketRequest->customFields[1] = 'invalid@'; + + //-- 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_EMAIL'], $e->validationModel->errorKeys); + } + + //-- Assert (2/2) + $this->assertThat($exceptionThrown, $this->equalTo(true)); + } + + function testItAddsTheProperValidationErrorWhenTheCustomerSubmitsTicketWithABannedEmail() { + //-- Arrange + $this->ticketRequest->email = 'some@banned.email'; + $this->banRetriever->method('isEmailBanned') + ->with($this->ticketRequest->email, $this->heskSettings) + ->willReturn(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(['EMAIL_BANNED'], $e->validationModel->errorKeys); + } + + //-- Assert (2/2) + $this->assertThat($exceptionThrown, $this->equalTo(true)); + } + + function testItAddsTheProperValidationErrorWhenTheCustomerSubmitsTicketWhenTheyAreMaxedOut() { + //-- Arrange + $this->ticketRequest->email = 'some@maxedout.email'; + $this->ticketValidators->method('isCustomerAtMaxTickets') + ->with($this->ticketRequest->email, $this->heskSettings) + ->willReturn(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(['EMAIL_AT_MAX_OPEN_TICKETS'], $e->validationModel->errorKeys); + } + + //-- Assert (2/2) + $this->assertThat($exceptionThrown, $this->equalTo(true)); + } }