From 85b6f5357cd7f3b4856974374a8c2d4c4587cf26 Mon Sep 17 00:00:00 2001 From: 4Giedrius <4giedrius@gmail.com> Date: Fri, 5 Dec 2014 10:07:45 +0200 Subject: [PATCH 1/4] Added waiting time setting in configuration page of Support module --- .../Support/html_admin/mod_support_settings.phtml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/bb-modules/Support/html_admin/mod_support_settings.phtml b/src/bb-modules/Support/html_admin/mod_support_settings.phtml index ae9cf6487..6bfb80d1d 100644 --- a/src/bb-modules/Support/html_admin/mod_support_settings.phtml +++ b/src/bb-modules/Support/html_admin/mod_support_settings.phtml @@ -88,6 +88,15 @@ + {% trans 'Wait time - how many hours user needs to wait until consequent ticket submission' %} +
+ +
+ +
+
+
+ From 554f9a2458af47b1b1bfee07103c74a05dd8094a Mon Sep 17 00:00:00 2001 From: 4Giedrius <4giedrius@gmail.com> Date: Fri, 5 Dec 2014 10:22:57 +0200 Subject: [PATCH 2/4] Added function to check if client can submit ticket if waiting time is enabled --- src/bb-modules/Support/Service.php | 33 +++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/bb-modules/Support/Service.php b/src/bb-modules/Support/Service.php index ba67f1b97..d29b41970 100644 --- a/src/bb-modules/Support/Service.php +++ b/src/bb-modules/Support/Service.php @@ -843,6 +843,24 @@ class Service implements \Box\InjectionAwareInterface return $ticket->hash; } + public function canClientSubmitNewTicket(\Model_Client $client, array $config) + { + $hours = $config['wait_hours']; + + $lastTicket = $this->di['db']->findOne('SupportTicket', 'client_id = :client_id ORDER BY created_at DESC', array(':client_id' => $client->id)); + if (!$lastTicket instanceof \Model_SupportTicket) { + return true; + } + + $timeSinceLast = round(abs(strtotime($lastTicket->created_at) - strtotime(date('c'))) / 3600, 0); + + if ($timeSinceLast < $hours) { + throw new \Box_Exception(sprintf('You can submit one ticket per %s hours. %s hours left', $hours, $hours - $timeSinceLast)); + } + + return true; + } + public function ticketCreateForClient(\Model_Client $client, \Model_SupportHelpdesk $helpdesk, array $data) { //@todo validate task params @@ -857,6 +875,13 @@ class Service implements \Box\InjectionAwareInterface throw new \Box_Exception('We have already received this request.'); } + $mod = $this->di['mod']('support'); + $config = $mod->getConfig(); + + if (isset($config['wait_hours']) && is_numeric($config['wait_hours'])) { + $this->canClientSubmitNewTicket($client, $config); + } + $event_params = $data; $event_params['client_id'] = $client->id; $this->di['events_manager']->fire(array('event' => 'onBeforeClientOpenTicket', 'params' => $event_params)); @@ -882,19 +907,17 @@ class Service implements \Box\InjectionAwareInterface $this->di['events_manager']->fire(array('event' => 'onAfterClientOpenTicket', 'params' => array('id' => $ticket->id))); - $mod = $this->di['mod']('support'); - $config = $mod->getConfig(); if (isset($config['autorespond_enable']) && $config['autorespond_enable'] && isset($config['autorespond_message_id']) && !empty($config['autorespond_message_id']) ) { try { - $cannedObj = $this->di['db']->getExistingModelById('SupportPr', $config['autorespond_message_id'], 'Canned reply not found'); - $canned = $this->cannedToApiArray($cannedObj); + $cannedObj = $this->di['db']->getExistingModelById('SupportPr', $config['autorespond_message_id'], 'Canned reply not found'); + $canned = $this->cannedToApiArray($cannedObj); $staffService = $this->di['mod_service']('staff'); $admin = $staffService->getCronAdmin(); - if (isset($canned['content']) && $admin instanceof \Model_Admin){ + if (isset($canned['content']) && $admin instanceof \Model_Admin) { $this->ticketReply($ticket, $admin, $canned['content']); } From a0f43174e7935105f20263a1a44a30872c2eccca Mon Sep 17 00:00:00 2001 From: 4Giedrius <4giedrius@gmail.com> Date: Fri, 5 Dec 2014 10:43:35 +0200 Subject: [PATCH 3/4] Unit tests added --- tests/bb-modules/Support/ServiceTest.php | 48 ++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/bb-modules/Support/ServiceTest.php b/tests/bb-modules/Support/ServiceTest.php index 510ac25e9..e4a6a9d32 100644 --- a/tests/bb-modules/Support/ServiceTest.php +++ b/tests/bb-modules/Support/ServiceTest.php @@ -2545,5 +2545,53 @@ class ServiceTest extends \PHPUnit_Framework_TestCase $result = $this->service->ticketTaskComplete($model); $this->assertTrue($result); } + + public function testCanClientSubmitNewTicketProvider() + { + + $ticket = new \Model_SupportTicket(); + $ticket->loadBean(new \RedBeanPHP\OODBBean()); + $ticket->client_id = 5; + $ticket->created_at = date('c'); + + $ticket2 = new \Model_SupportTicket(); + $ticket2->loadBean(new \RedBeanPHP\OODBBean()); + $ticket2->client_id = 5; + $ticket2->created_at = date('c', strtotime("-2 days"));; + + return array( + array($ticket, 24, false), //Ticket is created today, exception should be thrown + array(null, 24, true), //No previously created tickets found, can submit a ticket + array($ticket2, 24, true) //Last ticket submitted 2 days ago, can submit a ticket + ); + } + + /** + * @dataProvider testCanClientSubmitNewTicketProvider + */ + public function testCanClientSubmitNewTicket($ticket, $hours, $expected) + { + if (!$expected) { + $this->setExpectedException('\Box_Exception'); + } + $dbMock = $this->getMockBuilder('\Box_Database')->disableOriginalConstructor()->getMock(); + $dbMock->expects($this->atLeastOnce()) + ->method('findOne') + ->will($this->returnValue($ticket)); + + $di = new \Box_Di(); + $di['db'] = $dbMock; + $this->service->setDi($di); + + $client = new \Model_Client(); + $client->loadBean(new \RedBeanPHP\OODBBean()); + $client->id = 5; + + $config = array('wait_hours' => $hours); + + $result = $this->service->canClientSubmitNewTicket($client, $config); + $this->assertTrue($result); + + } } \ No newline at end of file From 77b3cffa60fbdf3aeae42e204febdad17df9ac01 Mon Sep 17 00:00:00 2001 From: 4Giedrius <4giedrius@gmail.com> Date: Fri, 5 Dec 2014 10:43:52 +0200 Subject: [PATCH 4/4] Integration tests added --- .../bb-modules/mod_support/Api_ClientTest.php | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/integration/bb-modules/mod_support/Api_ClientTest.php b/tests/integration/bb-modules/mod_support/Api_ClientTest.php index bae64e82c..9b7089bdd 100644 --- a/tests/integration/bb-modules/mod_support/Api_ClientTest.php +++ b/tests/integration/bb-modules/mod_support/Api_ClientTest.php @@ -82,4 +82,41 @@ class Api_Client_SupportTest extends BBDbApiTestCase $id = $this->api_client->support_ticket_create($data); $this->assertInternalType('int', $id); } + + /** + * @expectedException \Box_Exception + */ + public function testCanSubmitTicketException() + { + $this->api_admin->extension_config_save( + array( + 'ext' => 'mod_support', + 'wait_hours' => 24, + )); + $data = array( + 'client_id' => 1, + 'support_helpdesk_id' => 1, + 'subject' => 'this is subject', + 'content' => 'this is content', + ); + $this->api_client->support_ticket_create($data); + $this->api_client->support_ticket_create($data); //should throw an exception because 1 ticket per 24 hours is allowed + } + + public function testCanSubmitTicket() + { + $this->api_admin->extension_config_save( + array( + 'ext' => 'mod_support', + 'wait_hours' => '', + )); + $data = array( + 'client_id' => 1, + 'support_helpdesk_id' => 1, + 'subject' => 'this is subject', + 'content' => 'this is content', + ); + $this->api_client->support_ticket_create($data); + $this->api_client->support_ticket_create($data); //should not throw an exception as delay time is not set + } } \ No newline at end of file