diff --git a/src/bb-modules/Paidsupport/Service.php b/src/bb-modules/Paidsupport/Service.php index 5c1745fb7..e230a9047 100644 --- a/src/bb-modules/Paidsupport/Service.php +++ b/src/bb-modules/Paidsupport/Service.php @@ -54,8 +54,9 @@ class Service implements InjectionAwareInterface $paidSupportService = $di['mod_service']('Paidsupport'); $paidSupportService->setDi($di); - $paidSupportService->enoughInBalanceToOpenTicket($client); - + if ($paidSupportService->hasHelpdeskPaidSupport($params['support_helpdesk_id'])) { + $paidSupportService->enoughInBalanceToOpenTicket($client); + } return true; } @@ -73,6 +74,10 @@ class Service implements InjectionAwareInterface $paidSupportService = $di['mod_service']('Paidsupport'); $paidSupportService->setDi($di); + if (!$paidSupportService->hasHelpdeskPaidSupport($supportTicket->support_helpdesk_id)) { + return true; + } + $paidSupportService->enoughInBalanceToOpenTicket($client); $clientBalanceService = $di['mod_service']('Client', 'Balance'); @@ -110,6 +115,12 @@ class Service implements InjectionAwareInterface return isset($config['error_msg']) ? $config['error_msg'] : 'Configure paid support module!'; } + public function getPaidHelpdeskConfig() + { + $config = $this->di['mod_config']('Paidsupport'); + return isset($config['helpdesk']) ? $config['helpdesk'] : array(); + } + public function enoughInBalanceToOpenTicket(\Model_Client $client) { $clientBalanceService = $this->di['mod_service']('Client', 'Balance'); @@ -122,5 +133,22 @@ class Service implements InjectionAwareInterface return true; } + /** + * @param $id + * @return bool + */ + public function hasHelpdeskPaidSupport($id) + { + $helpdeskConfig = $this->getPaidHelpdeskConfig(); + + if (empty ($helpdeskConfig)) { + return true; + } + + if (isset($helpdeskConfig[$id]) && $helpdeskConfig[$id] == 1){ + return true; + } + return false; + } } \ No newline at end of file diff --git a/src/bb-modules/Paidsupport/html_admin/mod_paidsupport_settings.phtml b/src/bb-modules/Paidsupport/html_admin/mod_paidsupport_settings.phtml index 7906b7994..186d0e66d 100644 --- a/src/bb-modules/Paidsupport/html_admin/mod_paidsupport_settings.phtml +++ b/src/bb-modules/Paidsupport/html_admin/mod_paidsupport_settings.phtml @@ -35,6 +35,38 @@
+
+
+
{% trans 'Set paid supoprt for helpdesks: ' %}
+

{% trans 'Paid support applied for all of them if nothing is selected' %}

+
+ + + {% for id, title in admin.support_helpdesk_get_pairs %} + + + + + + {% else %} + + + + {% endfor %} + + + + + + +
+ + +
{% trans 'The list is empty' %}
+ {% trans 'Create new helpdesk' %} +
+
+
diff --git a/tests/bb-modules/Paidsupport/ServiceTest.php b/tests/bb-modules/Paidsupport/ServiceTest.php index fa8b04930..45ee272dd 100644 --- a/tests/bb-modules/Paidsupport/ServiceTest.php +++ b/tests/bb-modules/Paidsupport/ServiceTest.php @@ -160,7 +160,7 @@ class ServiceTest extends \PHPUnit_Framework_TestCase { $this->assertTrue($result); } - public function testonBeforeClientOpenTicket() + public function testonBeforeClientOpenTicket_PaidSupportForHelpdeskEnabled() { $di = new \Box_Di(); @@ -175,6 +175,9 @@ class ServiceTest extends \PHPUnit_Framework_TestCase { $di['db'] = $dbMock; $paidSupportMock = $this->getMockBuilder('\Box\Mod\Paidsupport\Service')->getMock(); + $paidSupportMock->expects($this->atLeastOnce()) + ->method('hasHelpdeskPaidSupport') + ->willReturn(true); $paidSupportMock->expects($this->atLeastOnce()) ->method('enoughInBalanceToOpenTicket') ->with($clientModel); @@ -186,6 +189,51 @@ class ServiceTest extends \PHPUnit_Framework_TestCase { $params = array( 'client_id' => 1, + 'support_helpdesk_id' => 1, + ); + + $boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor()->getMock(); + $boxEventMock->expects($this->atLeastOnce()) + ->method('getDi') + ->willReturn($di); + $boxEventMock->expects($this->atLeastOnce()) + ->method('getParameters') + ->willReturn($params); + + $result = $this->service->onBeforeClientOpenTicket($boxEventMock); + $this->assertTrue($result); + } + + public function testonBeforeClientOpenTicket_PaidSupportForHelpdeskDisabled() + { + $di = new \Box_Di(); + + $clientModel = new \Model_Client(); + $clientModel->loadBean(new \RedBeanPHP\OODBBean()); + + $dbMock = $this->getMockBuilder('\Box_Database')->getMock(); + $dbMock->expects($this->atLeastOnce()) + ->method('load') + ->with('Client') + ->willReturn($clientModel); + $di['db'] = $dbMock; + + $paidSupportMock = $this->getMockBuilder('\Box\Mod\Paidsupport\Service')->getMock(); + $paidSupportMock->expects($this->atLeastOnce()) + ->method('hasHelpdeskPaidSupport') + ->willReturn(false); + $paidSupportMock->expects($this->never()) + ->method('enoughInBalanceToOpenTicket') + ->with($clientModel); + $di['mod_service'] = $di->protect(function ($serviceName) use($paidSupportMock){ + if ($serviceName == 'Paidsupport'){ + return $paidSupportMock; + } + }); + + $params = array( + 'client_id' => 1, + 'support_helpdesk_id' => 1, ); $boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor()->getMock(); @@ -288,6 +336,9 @@ class ServiceTest extends \PHPUnit_Framework_TestCase { $di['db'] = $dbMock; $paidSupportMock = $this->getMockBuilder('\Box\Mod\Paidsupport\Service')->getMock(); + $paidSupportMock->expects($this->atLeastOnce()) + ->method('hasHelpdeskPaidSupport') + ->willReturn(true); $paidSupportMock->expects($this->atLeastOnce()) ->method('enoughInBalanceToOpenTicket') ->with($clientModel); @@ -307,6 +358,7 @@ class ServiceTest extends \PHPUnit_Framework_TestCase { $params = array( 'id' => 1, + 'support_helpdesk_id' => 1, ); $boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor()->getMock(); @@ -320,5 +372,145 @@ class ServiceTest extends \PHPUnit_Framework_TestCase { $result = $this->service->onAfterClientOpenTicket($boxEventMock); $this->assertTrue($result); } + + public function testonAfterClientOpenTicket_PaidSupportDisabledForHelpdesk() + { + $di = new \Box_Di(); + + $supportTicketModel = new \Model_SupportTicket(); + $supportTicketModel->loadBean(new \RedBeanPHP\OODBBean()); + + $clientModel = new \Model_Client(); + $clientModel->loadBean(new \RedBeanPHP\OODBBean()); + + $dbMock = $this->getMockBuilder('\Box_Database')->getMock(); + $dbMock->expects($this->atLeastOnce()) + ->method('load') + ->withConsecutive(array('SupportTicket'), array('Client')) + ->willReturnOnConsecutiveCalls($supportTicketModel, $clientModel); + $di['db'] = $dbMock; + + $paidSupportMock = $this->getMockBuilder('\Box\Mod\Paidsupport\Service')->getMock(); + $paidSupportMock->expects($this->atLeastOnce()) + ->method('hasHelpdeskPaidSupport') + ->willReturn(false); + $paidSupportMock->expects($this->never()) + ->method('enoughInBalanceToOpenTicket') + ->with($clientModel); + + $di['mod_service'] = $di->protect(function ($serviceName, $sub ='') use($paidSupportMock){ + if ($serviceName == 'Paidsupport'){ + return $paidSupportMock; + } + }); + + $params = array( + 'id' => 1, + 'support_helpdesk_id' => 1, + ); + + $boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor()->getMock(); + $boxEventMock->expects($this->atLeastOnce()) + ->method('getDi') + ->willReturn($di); + $boxEventMock->expects($this->atLeastOnce()) + ->method('getParameters') + ->willReturn($params); + + $result = $this->service->onAfterClientOpenTicket($boxEventMock); + $this->assertTrue($result); + } + + public function testgetPaidHelpdeskConfig() + { + $di = new \Box_Di(); + $helpdeskId = 2; + $helpdeskConfig = array( + $helpdeskId => 0 + ); + $paidSupportConfig = array( + 'helpdesk' => $helpdeskConfig, + ); + + $di['mod_config'] = $di->protect(function($serviceName) use ($paidSupportConfig){ + if ($serviceName== 'Paidsupport'){ + return $paidSupportConfig; + } + }); + + $this->service->setDi($di); + $result = $this->service->getPaidHelpdeskConfig(); + $this->assertInternalType('array', $result); + $this->assertNotEmpty($result); + $this->assertEquals($helpdeskConfig, $result); + + } + + public function testgetPaidHelpdeskConfig_IsNotSet() + { + $di = new \Box_Di(); + $paidSupportConfig = array(); + + $di['mod_config'] = $di->protect(function($serviceName) use ($paidSupportConfig){ + if ($serviceName== 'Paidsupport'){ + return $paidSupportConfig; + } + }); + + $this->service->setDi($di); + $result = $this->service->getPaidHelpdeskConfig(); + $this->assertInternalType('array', $result); + $this->assertEmpty($result); + } + + public function testhasHelpdeskPaidSupport_turnedOff() + { + $helpdeskId = 1; + $helpdeskConfig = array( + $helpdeskId => 0 + ); + $paidSupportServiceMock = $this->getMockBuilder('\Box\Mod\Paidsupport\Service') + ->setMethods(array('getPaidHelpdeskConfig')) + ->getMock(); + $paidSupportServiceMock->expects($this->atLeastOnce()) + ->method('getPaidHelpdeskConfig') + ->willReturn($helpdeskConfig); + + $result = $paidSupportServiceMock->hasHelpdeskPaidSupport($helpdeskId); + $this->assertFalse($result); + } + + public function testhasHelpdeskPaidSupport_turnedOn() + { + $helpdeskId = 1; + $helpdeskConfig = array( + $helpdeskId => 1 + ); + $paidSupportServiceMock = $this->getMockBuilder('\Box\Mod\Paidsupport\Service') + ->setMethods(array('getPaidHelpdeskConfig')) + ->getMock(); + $paidSupportServiceMock->expects($this->atLeastOnce()) + ->method('getPaidHelpdeskConfig') + ->willReturn($helpdeskConfig); + + $result = $paidSupportServiceMock->hasHelpdeskPaidSupport($helpdeskId); + $this->assertTrue($result); + } + + public function testhasHelpdeskPaidSupport_ConfigNotConfigured() + { + $helpdeskId = 1; + $helpdeskConfig = array(); + + $paidSupportServiceMock = $this->getMockBuilder('\Box\Mod\Paidsupport\Service') + ->setMethods(array('getPaidHelpdeskConfig')) + ->getMock(); + $paidSupportServiceMock->expects($this->atLeastOnce()) + ->method('getPaidHelpdeskConfig') + ->willReturn($helpdeskConfig); + + $result = $paidSupportServiceMock->hasHelpdeskPaidSupport($helpdeskId); + $this->assertTrue($result); + } } \ No newline at end of file diff --git a/tests/integration/bb-modules/mod_paidsupport/ServiceTest.php b/tests/integration/bb-modules/mod_paidsupport/ServiceTest.php index 3199fa150..95a6e1dd1 100644 --- a/tests/integration/bb-modules/mod_paidsupport/ServiceTest.php +++ b/tests/integration/bb-modules/mod_paidsupport/ServiceTest.php @@ -2,13 +2,24 @@ class Box_Mod_Paidsupport_ServiceTest extends ApiTestCase { - public function testCreatePaidSupportTicket() + + public function setup() { + parent::setUp(); $data = array( 'id' => 'paidsupport', 'type' => 'mod', ); $this->api_admin->extension_activate($data); + + $hookService = $this->di['mod_service']('hook'); + $hookService->batchConnect('paidsupport'); + + + } + + public function testCreatePaidSupportTicket() + { $ticketPrice = 5.5; $balance = $this->api_client->client_balance_get_total(); @@ -18,7 +29,6 @@ class Box_Mod_Paidsupport_ServiceTest extends ApiTestCase } $beforeBalance = $this->api_client->client_balance_get_total(); - $data = array( 'ext' => 'mod_paidsupport', 'ticket_price' => $ticketPrice, @@ -46,14 +56,89 @@ class Box_Mod_Paidsupport_ServiceTest extends ApiTestCase $this->assertEquals(-$ticketPrice, $clientBalanceModel->amount); } + public function testCreatePaidSupportTicket_HelpdeskhasPaidSupport() + { + $ticketPrice = 5.5; + + $balance = $this->api_client->client_balance_get_total(); + + if ($balance < $ticketPrice){ + $this->api_admin->client_balance_add_funds(array('id' => 1, 'amount' => $ticketPrice, 'description' => 'Added from PHPUnit')); + } + + $beforeBalance = $this->api_client->client_balance_get_total(); + + $helpdeskId = 1; + + $data = array( + 'ext' => 'mod_paidsupport', + 'ticket_price' => $ticketPrice, + 'error_msg' => 'Insufficient amount in balance', + 'helpdesk' => array($helpdeskId => 1), + ); + + $this->api_admin->extension_config_save($data); + $data = array( + 'subject' => 'Subject', + 'content' => 'content', + 'support_helpdesk_id' => $helpdeskId, + + 'order_id' => '1', + 'task' => 'cancel', + ); + $id = $this->api_client->support_ticket_create($data); + + $supportTicket = $this->di['db']->load('SupportTicket', $id); + $this->assertInstanceOf('\Model_SupportTicket', $supportTicket); + + $balance = $this->api_client->client_balance_get_total(); + $this->assertEquals($beforeBalance - $ticketPrice, $balance); + + $clientBalanceModel = $this->di['db']->findOne('ClientBalance', 'ORDER BY id desc'); + $this->assertEquals(-$ticketPrice, $clientBalanceModel->amount); + } + + public function testCreatePaidSupportTicket_HelpdesHasNotPaidSupport() + { + $ticketPrice = 5.5; + + $balance = $this->api_client->client_balance_get_total(); + + if ($balance <= $ticketPrice){ + $this->api_admin->client_balance_add_funds(array('id' => 1, 'amount' => $ticketPrice, 'description' => 'Added from PHPUnit')); + } + + $beforeBalance = $this->api_client->client_balance_get_total(); + + $helpdeskId = 1; + + $data = array( + 'ext' => 'mod_paidsupport', + 'ticket_price' => $ticketPrice, + 'error_msg' => 'Insufficient amount in balance', + 'helpdesk' => array($helpdeskId => 0), + ); + + $this->api_admin->extension_config_save($data); + $data = array( + 'subject' => 'Subject', + 'content' => 'content', + 'support_helpdesk_id' => $helpdeskId, + + 'order_id' => '1', + 'task' => 'cancel', + ); + $id = $this->api_client->support_ticket_create($data); + + $supportTicket = $this->di['db']->load('SupportTicket', $id); + $this->assertInstanceOf('\Model_SupportTicket', $supportTicket); + + $balance = $this->api_client->client_balance_get_total(); + $this->assertEquals($beforeBalance, $balance); + } + public function testCreatePaidSupportTicket_insufficientFunds() { - $data = array( - 'id' => 'paidsupport', - 'type' => 'mod', - ); - $this->api_admin->extension_activate($data); - $balance = $this->api_client->client_balance_get_total(); $ticketPrice = 10 + $balance;