diff --git a/phpunit.xml b/phpunit.xml index dd5342b99bedd598439089395caae7c2b7206b3f..650bbf7f9f813dba8c037d5f5577fc69f06ea1a8 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -46,6 +46,7 @@ <file>test/unitTests/app/search/SearchControllerTest.php</file> <file>test/unitTests/app/template/DatasourceControllerTest.php</file> <file>test/unitTests/app/acknowledgementReceipt/AcknowledgementReceiptControllerTest.php</file> + <file>test/unitTests/app/notification/DiffusionTypesControllerTest.php</file> <!-- The last one should be history --> <file>test/unitTests/app/history/HistoryControllerTest.php</file> </testsuite> diff --git a/src/app/notification/controllers/DiffusionTypesController.php b/src/app/notification/controllers/DiffusionTypesController.php index 539fb1ec5f3f18f42bd17c26d4d9337a1cd099ff..2b8082058c767db187d0305c03c486a56596115b 100644 --- a/src/app/notification/controllers/DiffusionTypesController.php +++ b/src/app/notification/controllers/DiffusionTypesController.php @@ -7,7 +7,7 @@ */ /** - * @brief Notifications Controller + * @brief DiffusionTypes Controller * * @author dev@maarch.org * @ingroup notifications @@ -210,10 +210,14 @@ class DiffusionTypesController $where[] = "listinstance_id = :recordid"; } - if ($args['notification']['diffusion_properties'] != '') { + if (!empty($args['notification']['diffusion_properties'])) { $aStatus = explode(',', $args['notification']['diffusion_properties']); - $where[] = 'lb.status in (:statustab)'; - $arrayPDO[] = [':statustab' => $aStatus]; + foreach ($aStatus as $key => $status) { + $inQuestion[] = ':statustab'.$key; + $arrayPDO[':statustab'.$key] = $status; + } + $inQuestion = implode(', ', $inQuestion); + $where[] = 'lb.status in ('.$inQuestion.')'; } // Main document resource from view @@ -261,10 +265,14 @@ class DiffusionTypesController $where[] = "listinstance_id = :recordid"; } - if ($args['notification']['diffusion_properties'] != '') { + if (!empty($args['notification']['diffusion_properties'])) { $aStatus = explode(',', $args['notification']['diffusion_properties']); - $where[] = 'lb.status in (:statustab)'; - $arrayPDO[] = [':statustab' => $aStatus]; + foreach ($aStatus as $key => $status) { + $inQuestion[] = ':statustab'.$key; + $arrayPDO[':statustab'.$key] = $status; + } + $inQuestion = implode(', ', $inQuestion); + $where[] = 'lb.status in ('.$inQuestion.')'; } // Main document resource from view @@ -306,10 +314,14 @@ class DiffusionTypesController $where[] = "listinstance_id = :recordid"; } - if ($args['notification']['diffusion_properties'] != '') { + if (!empty($args['notification']['diffusion_properties'])) { $aStatus = explode(',', $args['notification']['diffusion_properties']); - $where[] = 'lb.status in (:statustab)'; - $arrayPDO[] = [':statustab' => $aStatus]; + foreach ($aStatus as $key => $status) { + $inQuestion[] = ':statustab'.$key; + $arrayPDO[':statustab'.$key] = $status; + } + $inQuestion = implode(', ', $inQuestion); + $where[] = 'lb.status in ('.$inQuestion.')'; } // Main document resource from view @@ -357,10 +369,14 @@ class DiffusionTypesController $where[] = "listinstance_id = :recordid"; } - if ($args['notification']['diffusion_properties'] != '') { + if (!empty($args['notification']['diffusion_properties'])) { $aStatus = explode(',', $args['notification']['diffusion_properties']); - $where[] = 'lb.status in (:statustab)'; - $arrayPDO[] = [':statustab' => $aStatus]; + foreach ($aStatus as $key => $status) { + $inQuestion[] = ':statustab'.$key; + $arrayPDO[':statustab'.$key] = $status; + } + $inQuestion = implode(', ', $inQuestion); + $where[] = 'lb.status in ('.$inQuestion.')'; } // Main document resource from view @@ -402,10 +418,14 @@ class DiffusionTypesController $where[] = "listinstance_id = :recordid"; } - if ($args['notification']['diffusion_properties'] != '') { + if (!empty($args['notification']['diffusion_properties'])) { $aStatus = explode(',', $args['notification']['diffusion_properties']); - $where[] = 'lb.status in (:statustab)'; - $arrayPDO[] = [':statustab' => $aStatus]; + foreach ($aStatus as $key => $status) { + $inQuestion[] = ':statustab'.$key; + $arrayPDO[':statustab'.$key] = $status; + } + $inQuestion = implode(', ', $inQuestion); + $where[] = 'lb.status in ('.$inQuestion.')'; } // Main document resource from view @@ -453,10 +473,14 @@ class DiffusionTypesController $where[] = "listinstance_id = :recordid"; } - if ($args['notification']['diffusion_properties'] != '') { + if (!empty($args['notification']['diffusion_properties'])) { $aStatus = explode(',', $args['notification']['diffusion_properties']); - $where[] = 'lb.status in (:statustab)'; - $arrayPDO[] = [':statustab' => $aStatus]; + foreach ($aStatus as $key => $status) { + $inQuestion[] = ':statustab'.$key; + $arrayPDO[':statustab'.$key] = $status; + } + $inQuestion = implode(', ', $inQuestion); + $where[] = 'lb.status in ('.$inQuestion.')'; } // Main document resource from view @@ -498,10 +522,14 @@ class DiffusionTypesController $where[] = "listinstance_id = :recordid"; } - if ($args['notification']['diffusion_properties'] != '') { + if (!empty($args['notification']['diffusion_properties'])) { $aStatus = explode(',', $args['notification']['diffusion_properties']); - $where[] = 'lb.status in (:statustab)'; - $arrayPDO[] = [':statustab' => $aStatus]; + foreach ($aStatus as $key => $status) { + $inQuestion[] = ':statustab'.$key; + $arrayPDO[':statustab'.$key] = $status; + } + $inQuestion = implode(', ', $inQuestion); + $where[] = 'lb.status in ('.$inQuestion.')'; } // Main document resource from view diff --git a/test/unitTests/app/notification/DiffusionTypesControllerTest.php b/test/unitTests/app/notification/DiffusionTypesControllerTest.php new file mode 100755 index 0000000000000000000000000000000000000000..a6b6b80bd9cd2e7c594f11f596dd61286b9ee05d --- /dev/null +++ b/test/unitTests/app/notification/DiffusionTypesControllerTest.php @@ -0,0 +1,195 @@ +<?php +/** +* Copyright Maarch since 2008 under licence GPLv3. +* See LICENCE.txt file at the root folder for more details. +* This file is part of Maarch software. + +* +* @brief DiffusionTypesControllerTest +* +* @author dev <dev@maarch.org> +* @ingroup notifications +*/ +use PHPUnit\Framework\TestCase; + +class DiffusionTypesControllerTest extends TestCase +{ + private static $id = null; + + public function testGetRecipientsByContact() + { + $diffusionTypesController = new \Notification\controllers\DiffusionTypesController(); + + foreach ($GLOBALS['resources'] as $resId) { + $args = [ + 'notification' => [ + 'diffusion_type' => 'contact' + ], + 'request' => 'recipients', + 'event' => [ + 'record_id' => $resId + ] + ]; + + $response = $diffusionTypesController->getItemsToNotify($args); + foreach ($response as $contact) { + $this->assertNotEmpty($contact['user_id']); + $this->assertIsInt($contact['user_id']); + $this->assertNotEmpty($contact['mail']); + $this->assertIsString($contact['mail']); + } + + $args['request'] = 'others'; + $response = $diffusionTypesController->getItemsToNotify($args); + $this->assertIsArray($response); + } + } + + public function testGetRecipientsByCopie() + { + $diffusionTypesController = new \Notification\controllers\DiffusionTypesController(); + + foreach ($GLOBALS['resources'] as $resId) { + $args = [ + 'notification' => [ + 'diffusion_type' => 'copy_list' + ], + 'request' => 'recipients', + 'event' => [ + 'record_id' => $resId, + 'table_name' => 'res_letterbox' + ] + ]; + + $response = $diffusionTypesController->getItemsToNotify($args); + foreach ($response as $user) { + $this->assertSame(20, $user['id']); + $this->assertSame('jjonasz', $user['user_id']); + $this->assertSame('Jean', $user['firstname']); + $this->assertSame('JONASZ', $user['lastname']); + $this->assertEmpty($user['phone']); + $this->assertSame('support@maarch.fr', $user['mail']); + $this->assertSame('OK', $user['status']); + } + + $args['event']['table_name'] = 'notes'; + $args['event']['record_id'] = 1; + $response = $diffusionTypesController->getItemsToNotify($args); + $this->assertIsArray($response); + + $args['event']['table_name'] = 'listinstance'; + $args['event']['user_id'] = 19; + $response = $diffusionTypesController->getItemsToNotify($args); + $this->assertIsArray($response); + } + + foreach ($GLOBALS['resources'] as $resId) { + $args = [ + 'notification' => [ + 'diffusion_type' => 'copy_list' + ], + 'request' => 'res_id', + 'event' => [ + 'record_id' => $resId, + 'table_name' => 'res_letterbox' + ] + ]; + + $response = $diffusionTypesController->getItemsToNotify($args); + $this->assertSame($resId, $response); + + $args['event']['table_name'] = 'notes'; + $args['event']['record_id'] = 1; + $response = $diffusionTypesController->getItemsToNotify($args); + $this->assertIsNumeric($response); + + $args['event']['table_name'] = 'listinstance'; + $args['event']['user_id'] = 19; + $response = $diffusionTypesController->getItemsToNotify($args); + $this->assertIsNumeric($response); + } + } + + public function testGetRecipientsByDestEntity() + { + $diffusionTypesController = new \Notification\controllers\DiffusionTypesController(); + + foreach ($GLOBALS['resources'] as $resId) { + $args = [ + 'notification' => [ + 'diffusion_type' => 'dest_user', + 'diffusion_properties' => 'NEW,COU,CLO,END,ATT,VAL,INIT' + ], + 'request' => 'recipients', + 'event' => [ + 'record_id' => $resId, + 'table_name' => 'res_letterbox' + ] + ]; + + $response = $diffusionTypesController->getItemsToNotify($args); + foreach ($response as $user) { + $this->assertSame(19, $user['id']); + $this->assertSame('bbain', $user['user_id']); + $this->assertSame('Barbara', $user['firstname']); + $this->assertSame('BAIN', $user['lastname']); + $this->assertEmpty($user['phone']); + $this->assertSame('support@maarch.fr', $user['mail']); + $this->assertSame('OK', $user['status']); + } + + $args['event']['table_name'] = 'notes'; + $args['event']['record_id'] = 1; + $response = $diffusionTypesController->getItemsToNotify($args); + foreach ($response as $user) { + $this->assertSame(19, $user['id']); + $this->assertSame('bbain', $user['user_id']); + $this->assertSame('Barbara', $user['firstname']); + $this->assertSame('BAIN', $user['lastname']); + $this->assertEmpty($user['phone']); + $this->assertSame('support@maarch.fr', $user['mail']); + $this->assertSame('OK', $user['status']); + } + + $args['event']['table_name'] = 'listinstance'; + $args['event']['user_id'] = 19; + $response = $diffusionTypesController->getItemsToNotify($args); + foreach ($response as $user) { + $this->assertSame(11, $user['id']); + $this->assertSame('aackermann', $user['user_id']); + $this->assertSame('Amanda', $user['firstname']); + $this->assertSame('ACKERMANN', $user['lastname']); + $this->assertEmpty($user['phone']); + $this->assertSame('support@maarch.fr', $user['mail']); + $this->assertSame('OK', $user['status']); + } + } + + foreach ($GLOBALS['resources'] as $resId) { + $args = [ + 'notification' => [ + 'diffusion_type' => 'dest_user', + 'diffusion_properties' => 'NEW,COU,CLO,END,ATT,VAL,INIT' + ], + 'request' => 'res_id', + 'event' => [ + 'record_id' => $resId, + 'table_name' => 'res_letterbox' + ] + ]; + + $response = $diffusionTypesController->getItemsToNotify($args); + $this->assertSame($resId, $response); + + $args['event']['table_name'] = 'notes'; + $args['event']['record_id'] = 1; + $response = $diffusionTypesController->getItemsToNotify($args); + $this->assertEmpty($response); + + $args['event']['table_name'] = 'listinstance'; + $args['event']['user_id'] = 19; + $response = $diffusionTypesController->getItemsToNotify($args); + $this->assertEmpty($response); + } + } +}