From ccbe2cd5801d39da746e51c149818bea1810a326 Mon Sep 17 00:00:00 2001 From: Mike Koch Date: Sun, 12 Mar 2017 00:44:50 -0500 Subject: [PATCH] MFH Settings being loaded, emails being sent out --- api/ApplicationContext.php | 9 ++- api/BusinessLogic/Categories/Category.php | 3 + .../Emails/EmailTemplateParser.php | 2 +- api/BusinessLogic/Tickets/TicketCreator.php | 16 +++-- api/Controllers/Tickets/TicketController.php | 5 +- api/DataAccess/Categories/CategoryGateway.php | 1 + api/DataAccess/Security/UserGateway.php | 2 + .../Settings/ModsForHeskSettingsGateway.php | 23 ++++++ .../CreateTicketForCustomerTest.php | 71 +++++++++++++------ api/autoload.php | 3 +- 10 files changed, 100 insertions(+), 35 deletions(-) create mode 100644 api/DataAccess/Settings/ModsForHeskSettingsGateway.php diff --git a/api/ApplicationContext.php b/api/ApplicationContext.php index 3f13facf..0fc14d58 100644 --- a/api/ApplicationContext.php +++ b/api/ApplicationContext.php @@ -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]); } } \ No newline at end of file diff --git a/api/BusinessLogic/Categories/Category.php b/api/BusinessLogic/Categories/Category.php index 5711f33d..ab9bc1af 100644 --- a/api/BusinessLogic/Categories/Category.php +++ b/api/BusinessLogic/Categories/Category.php @@ -8,6 +8,9 @@ class Category { */ public $id; + /* @var $name string */ + public $name; + /** * @var int Categories order number */ diff --git a/api/BusinessLogic/Emails/EmailTemplateParser.php b/api/BusinessLogic/Emails/EmailTemplateParser.php index 0d3974ca..8a1601ab 100644 --- a/api/BusinessLogic/Emails/EmailTemplateParser.php +++ b/api/BusinessLogic/Emails/EmailTemplateParser.php @@ -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) { diff --git a/api/BusinessLogic/Tickets/TicketCreator.php b/api/BusinessLogic/Tickets/TicketCreator.php index a6d1225f..822ee513 100644 --- a/api/BusinessLogic/Tickets/TicketCreator.php +++ b/api/BusinessLogic/Tickets/TicketCreator.php @@ -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 diff --git a/api/Controllers/Tickets/TicketController.php b/api/Controllers/Tickets/TicketController.php index 01a58e13..017e6819 100644 --- a/api/Controllers/Tickets/TicketController.php +++ b/api/Controllers/Tickets/TicketController.php @@ -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'); diff --git a/api/DataAccess/Categories/CategoryGateway.php b/api/DataAccess/Categories/CategoryGateway.php index 68f605e4..abda07b9 100644 --- a/api/DataAccess/Categories/CategoryGateway.php +++ b/api/DataAccess/Categories/CategoryGateway.php @@ -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']); diff --git a/api/DataAccess/Security/UserGateway.php b/api/DataAccess/Security/UserGateway.php index 341f3e31..d9a24f54 100644 --- a/api/DataAccess/Security/UserGateway.php +++ b/api/DataAccess/Security/UserGateway.php @@ -89,6 +89,8 @@ class UserGateway extends CommonDao { $users[] = $user; } + $this->close(); + return $users; } } \ No newline at end of file diff --git a/api/DataAccess/Settings/ModsForHeskSettingsGateway.php b/api/DataAccess/Settings/ModsForHeskSettingsGateway.php new file mode 100644 index 00000000..aa56b229 --- /dev/null +++ b/api/DataAccess/Settings/ModsForHeskSettingsGateway.php @@ -0,0 +1,23 @@ +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; + } +} \ No newline at end of file diff --git a/api/Tests/BusinessLogic/Tickets/TicketCreatorTests/CreateTicketForCustomerTest.php b/api/Tests/BusinessLogic/Tickets/TicketCreatorTests/CreateTicketForCustomerTest.php index f3072816..5f8383e0 100644 --- a/api/Tests/BusinessLogic/Tickets/TicketCreatorTests/CreateTicketForCustomerTest.php +++ b/api/Tests/BusinessLogic/Tickets/TicketCreatorTests/CreateTicketForCustomerTest.php @@ -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); } } diff --git a/api/autoload.php b/api/autoload.php index e06961f9..c12b9647 100644 --- a/api/autoload.php +++ b/api/autoload.php @@ -20,5 +20,4 @@ global $hesk_settings; require_once(__DIR__ . '/../inc/custom_fields.inc.php'); // Load the ApplicationContext -$applicationContext = new \ApplicationContext(); -//$modsForHeskSettings = mfh_getSettings(); \ No newline at end of file +$applicationContext = new \ApplicationContext(); \ No newline at end of file