From 31ced3f57268f4a7a4d00f24065852c9bc1f2534 Mon Sep 17 00:00:00 2001 From: Mike Koch Date: Tue, 14 Feb 2017 22:03:46 -0500 Subject: [PATCH] Add somre more fields --- .../Tickets/CreateTicketByCustomerModel.php | 10 ++++++ .../CustomFields/CustomFieldValidator.php | 4 +-- .../Tickets/NewTicketValidator.php | 5 +++ api/BusinessLogic/Tickets/TicketCreator.php | 2 ++ api/Controllers/Tickets/TicketController.php | 2 ++ api/DataAccess/Tickets/TicketGateway.php | 12 +++---- .../Tickets/NewTicketValidatorTest.php | 32 +++++++++++++++++++ .../CreateTicketForCustomerTest.php | 4 +++ 8 files changed, 61 insertions(+), 10 deletions(-) diff --git a/api/BusinessLogic/Tickets/CreateTicketByCustomerModel.php b/api/BusinessLogic/Tickets/CreateTicketByCustomerModel.php index 30980251..25a577f0 100644 --- a/api/BusinessLogic/Tickets/CreateTicketByCustomerModel.php +++ b/api/BusinessLogic/Tickets/CreateTicketByCustomerModel.php @@ -62,4 +62,14 @@ class CreateTicketByCustomerModel { * @var int[]|null */ public $screenResolution; + + /** + * @var int|null + */ + public $ipAddress; + + /** + * @var string + */ + public $language; } \ No newline at end of file diff --git a/api/BusinessLogic/Tickets/CustomFields/CustomFieldValidator.php b/api/BusinessLogic/Tickets/CustomFields/CustomFieldValidator.php index 77bbd336..037d828e 100644 --- a/api/BusinessLogic/Tickets/CustomFields/CustomFieldValidator.php +++ b/api/BusinessLogic/Tickets/CustomFields/CustomFieldValidator.php @@ -18,7 +18,7 @@ class CustomFieldValidator { return false; } - return count($customField['Categories']) === 0 || - in_array($categoryId, $customField['Categories']); + return count($customField['category']) === 0 || + in_array($categoryId, $customField['category']); } } \ No newline at end of file diff --git a/api/BusinessLogic/Tickets/NewTicketValidator.php b/api/BusinessLogic/Tickets/NewTicketValidator.php index 61af85e8..bb80a1f8 100644 --- a/api/BusinessLogic/Tickets/NewTicketValidator.php +++ b/api/BusinessLogic/Tickets/NewTicketValidator.php @@ -122,6 +122,11 @@ class NewTicketValidator { $validationModel->errorKeys[] = 'EMAIL_AT_MAX_OPEN_TICKETS'; } + if ($ticketRequest->language === null || + $ticketRequest->language === '') { + $validationModel->errorKeys[] = 'MISSING_LANGUAGE'; + } + return $validationModel; } } \ No newline at end of file diff --git a/api/BusinessLogic/Tickets/TicketCreator.php b/api/BusinessLogic/Tickets/TicketCreator.php index 4897a8f4..c723fafd 100644 --- a/api/BusinessLogic/Tickets/TicketCreator.php +++ b/api/BusinessLogic/Tickets/TicketCreator.php @@ -73,6 +73,8 @@ class TicketCreator { $ticket->suggestedArticles = $ticketRequest->suggestedKnowledgebaseArticleIds; $ticket->userAgent = $ticketRequest->userAgent; $ticket->screenResolution = $ticketRequest->screenResolution; + $ticket->ipAddress = $ticketRequest->ipAddress; + $ticket->language = $ticketRequest->language; $ticketGatewayGeneratedFields = $this->ticketGateway->createTicket($ticket, $heskSettings); diff --git a/api/Controllers/Tickets/TicketController.php b/api/Controllers/Tickets/TicketController.php index 294882c5..5b67fd2c 100644 --- a/api/Controllers/Tickets/TicketController.php +++ b/api/Controllers/Tickets/TicketController.php @@ -49,6 +49,8 @@ class TicketController { $ticketRequest->suggestedKnowledgebaseArticleIds = Helpers::safeArrayGet($json, 'suggestedArticles'); $ticketRequest->userAgent = Helpers::safeArrayGet($json, 'userAgent'); $ticketRequest->screenResolution = Helpers::safeArrayGet($json, 'screenResolution'); + $ticketRequest->ipAddress = Helpers::safeArrayGet($json, 'ip'); + $ticketRequest->language = Helpers::safeArrayGet($json, 'language'); $ticketRequest->customFields = array(); $jsonCustomFields = Helpers::safeArrayGet($json, 'customFields'); diff --git a/api/DataAccess/Tickets/TicketGateway.php b/api/DataAccess/Tickets/TicketGateway.php index 5c6d7cb0..ae5c2b4d 100644 --- a/api/DataAccess/Tickets/TicketGateway.php +++ b/api/DataAccess/Tickets/TicketGateway.php @@ -92,13 +92,6 @@ class TicketGateway extends CommonDao { function createTicket($ticket, $heskSettings) { global $hesklang; - // If language is not set or default, set it to NULL. - if ($ticket->language === null || empty($ticket->language)) { - $language = (!$heskSettings['can_sel_lang']) ? HESK_DEFAULT_LANGUAGE : hesk_dbEscape($hesklang['LANGUAGE']); - } else { - $language = $ticket->language; - } - $dueDate = $ticket->dueDate ? "'{$ticket->dueDate}'" : "NULL"; // Prepare SQL for custom fields $customWhere = ''; @@ -129,6 +122,9 @@ class TicketGateway extends CommonDao { && isset($ticket->screenResolution[1]) && $ticket->screenResolution[1] !== null ? intval($ticket->screenResolution[1]) : 'NULL'; + $ipAddress = $ticket->ipAddress !== null + && $ticket->ipAddress !== '' ? $ticket->ipAddress : ''; + $sql = "INSERT INTO `" . hesk_dbEscape($heskSettings['db_pfix']) . "tickets` ( `trackid`, @@ -171,7 +167,7 @@ class TicketGateway extends CommonDao { NOW(), '" . $suggestedArticles . "', '" . hesk_dbEscape($ticket->ipAddress) . "', - '" . hesk_dbEscape($language) . "', + '" . hesk_dbEscape($ticket->language) . "', '" . intval($ticket->openedBy) . "', '" . intval($ticket->ownerId) . "', '" . hesk_dbEscape($ticket->getAttachmentsForDatabase()) . "', diff --git a/api/Tests/BusinessLogic/Tickets/NewTicketValidatorTest.php b/api/Tests/BusinessLogic/Tickets/NewTicketValidatorTest.php index de978568..05f25c9d 100644 --- a/api/Tests/BusinessLogic/Tickets/NewTicketValidatorTest.php +++ b/api/Tests/BusinessLogic/Tickets/NewTicketValidatorTest.php @@ -429,4 +429,36 @@ class NewTicketValidatorTest extends TestCase { //-- Assert $this->assertArraySubset(['EMAIL_AT_MAX_OPEN_TICKETS'], $validationModel->errorKeys); } + + function testItAddsTheProperValidationErrorWhenTheCustomerSubmitsTicketWithLanguageNull() { + //-- Arrange + $this->ticketRequest->language = null; + $this->ticketValidators->method('isCustomerAtMaxTickets') + ->with($this->ticketRequest->email, $this->heskSettings) + ->willReturn(false); + + //-- Act + $validationModel = $this->newTicketValidator->validateNewTicketForCustomer($this->ticketRequest, + $this->heskSettings, + $this->userContext); + + //-- Assert + $this->assertArraySubset(['MISSING_LANGUAGE'], $validationModel->errorKeys); + } + + function testItAddsTheProperValidationErrorWhenTheCustomerSubmitsTicketWithLanguageBlank() { + //-- Arrange + $this->ticketRequest->language = ''; + $this->ticketValidators->method('isCustomerAtMaxTickets') + ->with($this->ticketRequest->email, $this->heskSettings) + ->willReturn(false); + + //-- Act + $validationModel = $this->newTicketValidator->validateNewTicketForCustomer($this->ticketRequest, + $this->heskSettings, + $this->userContext); + + //-- Assert + $this->assertArraySubset(['MISSING_LANGUAGE'], $validationModel->errorKeys); + } } diff --git a/api/Tests/BusinessLogic/Tickets/TicketCreatorTests/CreateTicketForCustomerTest.php b/api/Tests/BusinessLogic/Tickets/TicketCreatorTests/CreateTicketForCustomerTest.php index 46f16dd2..93f1c8c1 100644 --- a/api/Tests/BusinessLogic/Tickets/TicketCreatorTests/CreateTicketForCustomerTest.php +++ b/api/Tests/BusinessLogic/Tickets/TicketCreatorTests/CreateTicketForCustomerTest.php @@ -158,6 +158,8 @@ class CreateTicketTest extends TestCase { $this->ticketRequest->suggestedKnowledgebaseArticleIds = [1, 2, 3]; $this->ticketRequest->userAgent = 'UserAgent'; $this->ticketRequest->screenResolution = [1400, 900]; + $this->ticketRequest->ipAddress = ip2long('127.0.0.1'); + $this->ticketRequest->language = 'English'; //-- Act $ticket = $this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->modsForHeskSettings, $this->userContext); @@ -175,6 +177,8 @@ class CreateTicketTest extends TestCase { self::assertThat($ticket->suggestedArticles, self::equalTo($this->ticketRequest->suggestedKnowledgebaseArticleIds)); self::assertThat($ticket->userAgent, self::equalTo($this->ticketRequest->userAgent)); self::assertThat($ticket->screenResolution, self::equalTo($this->ticketRequest->screenResolution)); + self::assertThat($ticket->ipAddress, self::equalTo($this->ticketRequest->ipAddress)); + self::assertThat($ticket->language, self::equalTo($this->ticketRequest->language)); } function testItReturnsTheGeneratedPropertiesOnTheTicket() {