MFH Settings being loaded, emails being sent out

remotes/upstream/api-rewrite
Mike Koch 7 years ago
parent 06a38e9383
commit ccbe2cd580

@ -19,6 +19,7 @@ use BusinessLogic\Tickets\VerifiedEmailChecker;
use DataAccess\Categories\CategoryGateway;
use DataAccess\Security\BanGateway;
use DataAccess\Security\UserGateway;
use DataAccess\Settings\ModsForHeskSettingsGateway;
use DataAccess\Statuses\StatusGateway;
use DataAccess\Tickets\TicketGateway;
use DataAccess\Tickets\VerifiedEmailGateway;
@ -30,6 +31,9 @@ class ApplicationContext {
function __construct() {
$this->get = array();
// Settings
$this->get[ModsForHeskSettingsGateway::class] = new ModsForHeskSettingsGateway();
// Verified Email Checker
$this->get[VerifiedEmailGateway::class] = new VerifiedEmailGateway();
$this->get[VerifiedEmailChecker::class] = new VerifiedEmailChecker($this->get[VerifiedEmailGateway::class]);
@ -66,7 +70,7 @@ class ApplicationContext {
$this->get[TicketRetriever::class] = new TicketRetriever($this->get[TicketGateway::class]);
$this->get[TicketValidators::class] = new TicketValidators($this->get[TicketGateway::class]);
$this->get[TrackingIdGenerator::class] = new TrackingIdGenerator($this->get[TicketGateway::class]);
$this->get[Autoassigner::class] = new Autoassigner();
$this->get[Autoassigner::class] = new Autoassigner($this->get[CategoryGateway::class], $this->get[UserGateway::class]);
$this->get[NewTicketValidator::class] = new NewTicketValidator($this->get[CategoryRetriever::class],
$this->get[BanRetriever::class],
$this->get[TicketValidators::class]);
@ -77,6 +81,7 @@ class ApplicationContext {
$this->get[TicketGateway::class],
$this->get[VerifiedEmailChecker::class],
$this->get[EmailSenderHelper::class],
$this->get[UserGateway::class]);
$this->get[UserGateway::class],
$this->get[ModsForHeskSettingsGateway::class]);
}
}

@ -8,6 +8,9 @@ class Category {
*/
public $id;
/* @var $name string */
public $name;
/**
* @var int Categories order number
*/

@ -174,7 +174,7 @@ class EmailTemplateParser {
// Status name and category name
$defaultStatus = $this->statusGateway->getStatusForDefaultAction(DefaultStatusForAction::NEW_TICKET, $heskSettings);
$statusName = hesk_msgToPlain($defaultStatus->localizedNames[$language]->text);
$category = hesk_msgToPlain($this->categoryGateway->getAllCategories($heskSettings)[$ticket->categoryId]);
$category = hesk_msgToPlain($this->categoryGateway->getAllCategories($heskSettings)[$ticket->categoryId]->name);
$owner = hesk_msgToPlain($this->userGateway->getNameForId($ticket->ownerId, $heskSettings));
switch ($ticket->priorityId) {

@ -8,6 +8,7 @@ use BusinessLogic\Emails\EmailTemplateRetriever;
use BusinessLogic\Exceptions\ValidationException;
use BusinessLogic\Statuses\DefaultStatusForAction;
use DataAccess\Security\UserGateway;
use DataAccess\Settings\ModsForHeskSettingsGateway;
use DataAccess\Statuses\StatusGateway;
use DataAccess\Tickets\TicketGateway;
@ -52,8 +53,11 @@ class TicketCreator {
*/
private $userGateway;
function __construct($newTicketValidator, $trackingIdGenerator, $autoassigner,
$statusGateway, $ticketGateway, $verifiedEmailChecker, $emailSenderHelper, $userGateway) {
/* @var $modsForHeskSettingsGateway ModsForHeskSettingsGateway */
private $modsForHeskSettingsGateway;
function __construct($newTicketValidator, $trackingIdGenerator, $autoassigner, $statusGateway, $ticketGateway,
$verifiedEmailChecker, $emailSenderHelper, $userGateway, $modsForHeskSettingsGateway) {
$this->newTicketValidator = $newTicketValidator;
$this->trackingIdGenerator = $trackingIdGenerator;
$this->autoassigner = $autoassigner;
@ -62,6 +66,7 @@ class TicketCreator {
$this->verifiedEmailChecker = $verifiedEmailChecker;
$this->emailSenderHelper = $emailSenderHelper;
$this->userGateway = $userGateway;
$this->modsForHeskSettingsGateway = $modsForHeskSettingsGateway;
}
/**
@ -69,13 +74,14 @@ class TicketCreator {
*
* @param $ticketRequest CreateTicketByCustomerModel
* @param $heskSettings array HESK settings
* @param $modsForHeskSettings array Mods for HESK settings
* @param $userContext
* @return Ticket The newly created ticket
* @throws ValidationException When a required field in $ticket_request is missing
* @throws \Exception When the default status for new tickets is not found
*/
function createTicketByCustomer($ticketRequest, $heskSettings, $modsForHeskSettings, $userContext) {
function createTicketByCustomer($ticketRequest, $heskSettings, $userContext) {
$modsForHeskSettings = $this->modsForHeskSettingsGateway->getAllSettings($heskSettings);
$validationModel = $this->newTicketValidator->validateNewTicketForCustomer($ticketRequest, $heskSettings, $userContext);
if (count($validationModel->errorKeys) > 0) {
@ -96,7 +102,7 @@ class TicketCreator {
$ticket->trackingId = $this->trackingIdGenerator->generateTrackingId($heskSettings);
if ($heskSettings['autoassign']) {
$ticket->ownerId = $this->autoassigner->getNextUserForTicket($ticketRequest->category, $heskSettings);
$ticket->ownerId = $this->autoassigner->getNextUserForTicket($ticketRequest->category, $heskSettings)->id;
}
// Transform one-to-one properties

@ -20,14 +20,14 @@ class TicketController {
}
function post() {
global $applicationContext, $hesk_settings, $modsForHeskSettings, $userContext;
global $applicationContext, $hesk_settings, $userContext;
/* @var $ticketCreator TicketCreator */
$ticketCreator = $applicationContext->get[TicketCreator::class];
$jsonRequest = JsonRetriever::getJsonData();
$ticket = $ticketCreator->createTicketByCustomer($this->buildTicketRequestFromJson($jsonRequest), $hesk_settings, $modsForHeskSettings, $userContext);
$ticket = $ticketCreator->createTicketByCustomer($this->buildTicketRequestFromJson($jsonRequest), $hesk_settings, $userContext);
//if ticket is a stageTicket, email user
//else if assigned to owner, email new owner
@ -55,6 +55,7 @@ class TicketController {
$ticketRequest->screenResolution = Helpers::safeArrayGet($json, 'screenResolution');
$ticketRequest->ipAddress = Helpers::safeArrayGet($json, 'ip');
$ticketRequest->language = Helpers::safeArrayGet($json, 'language');
$ticketRequest->sendEmailToCustomer = Helpers::safeArrayGet($json, 'sendEmailToCustomer');
$ticketRequest->customFields = array();
$jsonCustomFields = Helpers::safeArrayGet($json, 'customFields');

@ -23,6 +23,7 @@ class CategoryGateway extends CommonDao {
$category = new Category();
$category->id = intval($row['id']);
$category->name = $row['name'];
$category->catOrder = intval($row['cat_order']);
$category->autoAssign = $row['autoassign'] == 1;
$category->type = intval($row['type']);

@ -89,6 +89,8 @@ class UserGateway extends CommonDao {
$users[] = $user;
}
$this->close();
return $users;
}
}

@ -0,0 +1,23 @@
<?php
namespace DataAccess\Settings;
use DataAccess\CommonDao;
class ModsForHeskSettingsGateway extends CommonDao {
function getAllSettings($heskSettings) {
$this->init();
$rs = hesk_dbQuery("SELECT `Key`, `Value` FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "settings` WHERE `Key` <> 'modsForHeskVersion'");
$settings = array();
while ($row = hesk_dbFetchAssoc($rs)) {
$settings[$row['Key']] = $row['Value'];
}
$this->close();
return $settings;
}
}

@ -5,10 +5,8 @@ namespace BusinessLogic\Tickets\TicketCreatorTests;
use BusinessLogic\Emails\Addressees;
use BusinessLogic\Emails\EmailSenderHelper;
use BusinessLogic\Emails\EmailTemplate;
use BusinessLogic\Emails\EmailTemplateRetriever;
use BusinessLogic\Security\UserContext;
use BusinessLogic\Statuses\DefaultStatusForAction;
use BusinessLogic\Statuses\Status;
use BusinessLogic\Tickets\Autoassigner;
use BusinessLogic\Tickets\CreateTicketByCustomerModel;
@ -20,6 +18,7 @@ use BusinessLogic\Tickets\VerifiedEmailChecker;
use BusinessLogic\ValidationModel;
use Core\Constants\Priority;
use DataAccess\Security\UserGateway;
use DataAccess\Settings\ModsForHeskSettingsGateway;
use DataAccess\Statuses\StatusGateway;
use DataAccess\Tickets\TicketGateway;
use PHPUnit\Framework\TestCase;
@ -66,11 +65,6 @@ class CreateTicketTest extends TestCase {
*/
private $heskSettings;
/**
* @var $modsForHeskSettings array
*/
private $modsForHeskSettings;
/**
* @var $userContext UserContext
*/
@ -96,6 +90,12 @@ class CreateTicketTest extends TestCase {
*/
private $userGateway;
/* @var $modsForHeskSettingsGateway \PHPUnit_Framework_MockObject_MockObject */
private $modsForHeskSettingsGateway;
/* @var $modsForHeskSettings array */
private $modsForHeskSettings;
protected function setUp() {
$this->ticketGateway = $this->createMock(TicketGateway::class);
$this->newTicketValidator = $this->createMock(NewTicketValidator::class);
@ -105,10 +105,11 @@ class CreateTicketTest extends TestCase {
$this->verifiedEmailChecker = $this->createMock(VerifiedEmailChecker::class);
$this->emailSenderHelper = $this->createMock(EmailSenderHelper::class);
$this->userGateway = $this->createMock(UserGateway::class);
$this->modsForHeskSettingsGateway = $this->createMock(ModsForHeskSettingsGateway::class);
$this->ticketCreator = new TicketCreator($this->newTicketValidator, $this->trackingIdGenerator,
$this->autoassigner, $this->statusGateway, $this->ticketGateway, $this->verifiedEmailChecker,
$this->emailSenderHelper, $this->userGateway);
$this->emailSenderHelper, $this->userGateway, $this->modsForHeskSettingsGateway);
$this->ticketRequest = new CreateTicketByCustomerModel();
$this->ticketRequest->name = 'Name';
@ -133,7 +134,6 @@ class CreateTicketTest extends TestCase {
$this->newTicketValidator->method('validateNewTicketForCustomer')->willReturn(new ValidationModel());
$this->trackingIdGenerator->method('generateTrackingId')->willReturn('123-456-7890');
$this->autoassigner->method('getNextUserForTicket')->willReturn(1);
$this->ticketGatewayGeneratedFields = new TicketGatewayGeneratedFields();
$this->ticketGateway->method('createTicket')->willReturn($this->ticketGatewayGeneratedFields);
@ -144,16 +144,22 @@ class CreateTicketTest extends TestCase {
}
function testItSavesTheTicketToTheDatabase() {
//-- Arrange
$this->modsForHeskSettingsGateway->method('getAllSettings')->willReturn($this->modsForHeskSettings);
//-- Assert
$this->ticketGateway->expects($this->once())->method('createTicket');
//-- Act
$this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->modsForHeskSettings, $this->userContext);
$this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->userContext);
}
function testItSetsTheTrackingIdOnTheTicket() {
//-- Arrange
$this->modsForHeskSettingsGateway->method('getAllSettings')->willReturn($this->modsForHeskSettings);
//-- Act
$ticket = $this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->modsForHeskSettings, $this->userContext);
$ticket = $this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->userContext);
//-- Assert
self::assertThat($ticket->trackingId, self::equalTo('123-456-7890'));
@ -162,17 +168,24 @@ class CreateTicketTest extends TestCase {
function testItSetsTheNextUserForAutoassign() {
//-- Arrange
$this->heskSettings['autoassign'] = 1;
$autoassignUser = new UserContext();
$autoassignUser->id = 1;
$this->autoassigner->method('getNextUserForTicket')->willReturn($autoassignUser);
$this->modsForHeskSettingsGateway->method('getAllSettings')->willReturn($this->modsForHeskSettings);
//-- Act
$ticket = $this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->modsForHeskSettings, $this->userContext);
$ticket = $this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->userContext);
//-- Assert
self::assertThat($ticket->ownerId, self::equalTo(1));
}
function testItDoesntCallTheAutoassignerWhenDisabledInHesk() {
//-- Arrange
$this->modsForHeskSettingsGateway->method('getAllSettings')->willReturn($this->modsForHeskSettings);
//-- Act
$ticket = $this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->modsForHeskSettings, $this->userContext);
$ticket = $this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->userContext);
//-- Assert
self::assertThat($ticket->ownerId, self::equalTo(null));
@ -196,9 +209,10 @@ class CreateTicketTest extends TestCase {
$this->ticketRequest->screenResolution = [1400, 900];
$this->ticketRequest->ipAddress = '127.0.0.1';
$this->ticketRequest->language = 'English';
$this->modsForHeskSettingsGateway->method('getAllSettings')->willReturn($this->modsForHeskSettings);
//-- Act
$ticket = $this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->modsForHeskSettings, $this->userContext);
$ticket = $this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->userContext);
//-- Assert
self::assertThat($ticket->name, self::equalTo($this->ticketRequest->name));
@ -222,9 +236,10 @@ class CreateTicketTest extends TestCase {
$this->ticketGatewayGeneratedFields->dateCreated = 'date created';
$this->ticketGatewayGeneratedFields->dateModified = 'date modified';
$this->ticketGatewayGeneratedFields->id = 50;
$this->modsForHeskSettingsGateway->method('getAllSettings')->willReturn($this->modsForHeskSettings);
//-- Act
$ticket = $this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->modsForHeskSettings, $this->userContext);
$ticket = $this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->userContext);
//-- Assert
self::assertThat($ticket->dateCreated, self::equalTo($this->ticketGatewayGeneratedFields->dateCreated));
@ -233,16 +248,22 @@ class CreateTicketTest extends TestCase {
}
function testItSetsTheDefaultStatus() {
//-- Arrange
$this->modsForHeskSettingsGateway->method('getAllSettings')->willReturn($this->modsForHeskSettings);
//-- Act
$ticket = $this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->modsForHeskSettings, $this->userContext);
$ticket = $this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->userContext);
//-- Assert
self::assertThat($ticket->statusId, self::equalTo(1));
}
function testItSetsTheDefaultProperties() {
//-- Arrange
$this->modsForHeskSettingsGateway->method('getAllSettings')->willReturn($this->modsForHeskSettings);
//-- Act
$ticket = $this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->modsForHeskSettings, $this->userContext);
$ticket = $this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->userContext);
//-- Assert
self::assertThat($ticket->archived, self::isFalse());
@ -257,12 +278,13 @@ class CreateTicketTest extends TestCase {
function testItChecksIfTheEmailIsVerified() {
//-- Arrange
$this->modsForHeskSettings['customer_email_verification_required'] = true;
$this->modsForHeskSettingsGateway->method('getAllSettings')->willReturn($this->modsForHeskSettings);
//-- Assert
$this->verifiedEmailChecker->expects($this->once())->method('isEmailVerified');
//-- Act
$this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->modsForHeskSettings, $this->userContext);
$this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->userContext);
}
function testItSendsAnEmailToTheCustomerWhenTheTicketIsCreated() {
@ -271,13 +293,14 @@ class CreateTicketTest extends TestCase {
$this->ticketRequest->language = 'English';
$expectedAddressees = new Addressees();
$expectedAddressees->to = array($this->ticketRequest->email);
$this->modsForHeskSettingsGateway->method('getAllSettings')->willReturn($this->modsForHeskSettings);
//-- Assert
$this->emailSenderHelper->expects($this->once())->method('sendEmailForTicket')
->with(EmailTemplateRetriever::NEW_TICKET, 'English', $expectedAddressees, $this->anything(), $this->heskSettings, $this->modsForHeskSettings);
->with(EmailTemplateRetriever::NEW_TICKET, 'English', $expectedAddressees, $this->anything(), $this->heskSettings, $this->anything());
//-- Act
$this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->modsForHeskSettings, $this->userContext);
$this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->userContext);
}
function testItDoesNotSendsAnEmailToTheCustomerWhenTheTicketIsCreatedAndSendToCustomerIsFalse() {
@ -286,12 +309,13 @@ class CreateTicketTest extends TestCase {
$this->ticketRequest->language = 'English';
$expectedAddressees = new Addressees();
$expectedAddressees->to = array($this->ticketRequest->email);
$this->modsForHeskSettingsGateway->method('getAllSettings')->willReturn($this->modsForHeskSettings);
//-- Assert
$this->emailSenderHelper->expects($this->never())->method('sendEmailForTicket');
//-- Act
$this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->modsForHeskSettings, $this->userContext);
$this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->userContext);
}
function testItSendsAnEmailToTheAssignedToOwnerWhenTheTicketIsCreated() {
@ -300,12 +324,13 @@ class CreateTicketTest extends TestCase {
$this->ticketRequest->language = 'English';
$expectedAddressees = new Addressees();
$expectedAddressees->to = array($this->ticketRequest->email);
$this->modsForHeskSettingsGateway->method('getAllSettings')->willReturn($this->modsForHeskSettings);
//-- Assert
$this->emailSenderHelper->expects($this->once())->method('sendEmailForTicket')
->with(EmailTemplateRetriever::NEW_TICKET, 'English', $expectedAddressees, $this->anything(), $this->heskSettings, $this->modsForHeskSettings);
->with(EmailTemplateRetriever::NEW_TICKET, 'English', $expectedAddressees, $this->anything(), $this->heskSettings, $this->anything());
//-- Act
$this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->modsForHeskSettings, $this->userContext);
$this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->userContext);
}
}

@ -20,5 +20,4 @@ global $hesk_settings;
require_once(__DIR__ . '/../inc/custom_fields.inc.php');
// Load the ApplicationContext
$applicationContext = new \ApplicationContext();
//$modsForHeskSettings = mfh_getSettings();
$applicationContext = new \ApplicationContext();
Loading…
Cancel
Save