Refactored custom field validator, re-enabled test
This commit is contained in:
parent
e22d318b92
commit
e44baa99c2
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: cokoch
|
||||
* Date: 2/9/2017
|
||||
* Time: 12:28 PM
|
||||
*/
|
||||
|
||||
namespace BusinessLogic\Tickets\CustomFields;
|
||||
|
||||
|
||||
class CustomFieldValidator {
|
||||
static function isCustomFieldInCategory($customFieldId, $categoryId, $staff, $heskSettings) {
|
||||
$customField = $heskSettings['custom_fields']["custom{$customFieldId}"];
|
||||
|
||||
if (!$customField['use'] ||
|
||||
(!$staff && $customField['use'] === 2)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return count($customField['category']) === 0 ||
|
||||
in_array($categoryId, $customField['category']);
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@ namespace BusinessLogic\Tickets;
|
||||
use BusinessLogic\Categories\CategoryRetriever;
|
||||
use BusinessLogic\Exceptions\ValidationException;
|
||||
use BusinessLogic\Security\BanRetriever;
|
||||
use BusinessLogic\Tickets\CustomFields\CustomFieldValidator;
|
||||
use BusinessLogic\ValidationModel;
|
||||
use BusinessLogic\Validators;
|
||||
|
||||
@ -72,7 +73,6 @@ class TicketCreator {
|
||||
}
|
||||
}
|
||||
|
||||
// Don't allow critical priority tickets
|
||||
if ($heskSettings['cust_urgency'] && intval($ticketRequest->priority) === $TICKET_PRIORITY_CRITICAL) {
|
||||
$validationModel->errorKeys[] = 'CRITICAL_PRIORITY_FORBIDDEN';
|
||||
}
|
||||
@ -89,7 +89,7 @@ class TicketCreator {
|
||||
|
||||
foreach ($heskSettings['custom_fields'] as $key => $value) {
|
||||
$customFieldNumber = intval(str_replace('custom', '', $key));
|
||||
if ($value['use'] == 1 && hesk_is_custom_field_in_category($customFieldNumber, intval($ticketRequest->category))) {
|
||||
if ($value['use'] == 1 && CustomFieldValidator::isCustomFieldInCategory($customFieldNumber, intval($ticketRequest->category), false, $heskSettings)) {
|
||||
$custom_field_value = $ticketRequest->customFields[$customFieldNumber];
|
||||
if (empty($custom_field_value)) {
|
||||
$validationModel->errorKeys[] = "CUSTOM_FIELD_{$customFieldNumber}_INVALID::NO_VALUE";
|
||||
|
@ -0,0 +1,86 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: cokoch
|
||||
* Date: 2/9/2017
|
||||
* Time: 12:29 PM
|
||||
*/
|
||||
|
||||
namespace BusinessLogic\Tickets\CustomFields;
|
||||
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class CustomFieldValidatorTest extends TestCase {
|
||||
function testItReturnsTrueWhenTheCustomFieldIsInTheCategory() {
|
||||
//-- Arrange
|
||||
$heskSettings = array(
|
||||
'custom_fields' => array(
|
||||
'custom1' => array(
|
||||
'use' => 1,
|
||||
'category' => array(1, 2)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
//-- Act
|
||||
$result = CustomFieldValidator::isCustomFieldInCategory(1, 1, false, $heskSettings);
|
||||
|
||||
//-- Assert
|
||||
$this->assertThat($result, $this->isTrue());
|
||||
}
|
||||
|
||||
function testItReturnsTrueWhenTheCustomFieldIsForAllCategories() {
|
||||
//-- Arrange
|
||||
$heskSettings = array(
|
||||
'custom_fields' => array(
|
||||
'custom1' => array(
|
||||
'use' => 1,
|
||||
'category' => []
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
//-- Act
|
||||
$result = CustomFieldValidator::isCustomFieldInCategory(1, 1, false, $heskSettings);
|
||||
|
||||
//-- Assert
|
||||
$this->assertThat($result, $this->isTrue());
|
||||
}
|
||||
|
||||
function testItReturnsFalseWhenTheCustomFieldIsNotInTheCategory() {
|
||||
//-- Arrange
|
||||
$heskSettings = array(
|
||||
'custom_fields' => array(
|
||||
'custom1' => array(
|
||||
'use' => 1,
|
||||
'category' => array(1, 2)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
//-- Act
|
||||
$result = CustomFieldValidator::isCustomFieldInCategory(1, 50, false, $heskSettings);
|
||||
|
||||
//-- Assert
|
||||
$this->assertThat($result, $this->isFalse());
|
||||
}
|
||||
|
||||
function testItReturnsFalseWhenTheCustomFieldIsForStaffOnly() {
|
||||
//-- Arrange
|
||||
$heskSettings = array(
|
||||
'custom_fields' => array(
|
||||
'custom1' => array(
|
||||
'use' => 2,
|
||||
'category' => array(1, 2)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
//-- Act
|
||||
$result = CustomFieldValidator::isCustomFieldInCategory(1, 1, false, $heskSettings);
|
||||
|
||||
//-- Assert
|
||||
$this->assertThat($result, $this->isFalse());
|
||||
}
|
||||
}
|
@ -377,10 +377,6 @@ class TicketCreatorTest extends TestCase {
|
||||
}
|
||||
|
||||
function testItAddsTheProperValidationErrorWhenTheCustomerSubmitsTicketWithNullRequiredCustomField() {
|
||||
$this->markTestIncomplete(
|
||||
'Not complete; need to refactor custom field in category'
|
||||
);
|
||||
|
||||
//-- Arrange
|
||||
$customField = array();
|
||||
$customField['req'] = 1;
|
||||
|
Loading…
x
Reference in New Issue
Block a user