From 536c3588dacbcc3d7f5d5c5677f3d016986a6310 Mon Sep 17 00:00:00 2001 From: Roly Rudy Gutierrez Pinto Date: Thu, 11 Jun 2020 11:49:54 -0400 Subject: [PATCH] PMCORE-1596 The files attached in the cases notes needs to send in the email notification. --- .../engine/classes/model/AppNotesTest.php | 136 +++++++++++++++++- .../src/ProcessMaker/Model/DocumentsTest.php | 20 +++ workflow/engine/classes/model/AppNotes.php | 41 ++++-- .../translations/english/processmaker.en.po | 6 + workflow/engine/data/mysql/insert.sql | 1 + .../src/ProcessMaker/Model/Documents.php | 16 +++ 6 files changed, 204 insertions(+), 16 deletions(-) diff --git a/tests/unit/workflow/engine/classes/model/AppNotesTest.php b/tests/unit/workflow/engine/classes/model/AppNotesTest.php index e6529b4f4..4d8b3feda 100644 --- a/tests/unit/workflow/engine/classes/model/AppNotesTest.php +++ b/tests/unit/workflow/engine/classes/model/AppNotesTest.php @@ -3,10 +3,14 @@ namespace Tests\unit\workflow\engine\classes\model; use AppNotes as ModelAppNotes; -use ProcessMaker\Model\Delegation; -use ProcessMaker\Model\AppMessage; +use Exception; +use Faker\Factory; use ProcessMaker\Model\Application; +use ProcessMaker\Model\AppMessage; use ProcessMaker\Model\AppNotes; +use ProcessMaker\Model\Delegation; +use ProcessMaker\Model\Documents; +use ProcessMaker\Model\EmailServerModel; use ProcessMaker\Model\User; use Tests\TestCase; @@ -17,12 +21,23 @@ use Tests\TestCase; */ class AppNotesTest extends TestCase { + private $faker; + + /** + * Set up method + */ + public function setUp() + { + parent::setUp(); + $this->faker = Factory::create(); + } + /** * It test the cases notes creation * * @test */ - public function it_test_case_notes_creation() + public function it_test_case_notes_creation() { $application = factory(Application::class)->create(); $user = factory(User::class)->create(); @@ -37,7 +52,7 @@ class AppNotesTest extends TestCase $query = AppNotes::query(); $query->select()->where('APP_UID', $application->APP_UID)->where('USR_UID', $user->USR_UID); $result = $query->get()->values()->toArray(); - $this->assertNotEmpty($result); + $this->assertNotEmpty($result); } /** @@ -45,7 +60,7 @@ class AppNotesTest extends TestCase * * @test */ - public function it_test_case_notes_creation_and_send_email_to_user() + public function it_test_case_notes_creation_and_send_email_to_user() { $application = factory(Application::class)->create(); $user = factory(User::class)->create(); @@ -74,7 +89,7 @@ class AppNotesTest extends TestCase * * @test */ - public function it_test_case_notes_creation_and_send_email() + public function it_test_case_notes_creation_and_send_email() { $application = factory(Application::class)->create(); $user = factory(User::class)->create(); @@ -101,4 +116,111 @@ class AppNotesTest extends TestCase $result = $query->get()->values()->toArray(); $this->assertNotEmpty($result); } -} \ No newline at end of file + + /** + * This test verifies the sending of the notification note with Exception. + * @test + * @covers \AppNotes::sendNoteNotification + */ + public function it_should_test_send_note_notification_with_exception() + { + //assert + $this->expectException(Exception::class); + + $appNotes = new ModelAppNotes(); + $appNotes->sendNoteNotification(null, null, null, null, null, null); + } + + /** + * This test verifies the sending of the notification note. + * @test + * @covers \AppNotes::sendNoteNotification + */ + public function it_should_test_send_note_notification_without_user() + { + $user = User::where('USR_UID', '=', '00000000000000000000000000000001') + ->get() + ->first(); + $application = factory(Application::class)->create(); + $delegation = factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID, + 'USR_UID' => $user->USR_UID + ]); + + $params = [ + $application->APP_UID, + '', + '', + $user->USR_UID, + $this->faker->email, + $delegation->DEL_INDEX + ]; + $appNotes = new ModelAppNotes(); + $appNotes->sendNoteNotification(...$params); + + //assert + $appMessage = AppMessage::where('APP_UID', '=', $application->APP_UID)->get()->first()->toArray(); + $this->assertArrayHasKey('APP_UID', $appMessage); + $this->assertEquals($appMessage['APP_UID'], $application->APP_UID); + } + + /** + * This test verifies the sending of the notification note with attach files. + * @test + * @covers \AppNotes::sendNoteNotification + */ + public function it_should_test_send_note_notification_with_attach_files() + { + $user = User::where('USR_UID', '=', '00000000000000000000000000000001') + ->get() + ->first(); + $application = factory(Application::class)->create(); + $delegation = factory(Delegation::class)->create([ + 'APP_UID' => $application->APP_UID, + 'USR_UID' => $user->USR_UID + ]); + $appNote = factory(AppNotes::class)->create(); + $appDocument = factory(Documents::class)->create([ + 'APP_UID' => $application->APP_UID, + 'DOC_ID' => $appNote->NOTE_ID + ]); + factory(EmailServerModel::class)->create([ + 'MESS_DEFAULT' => 1 + ]); + + $params = [ + $application->APP_UID, + $user->USR_UID, + '', + $user->USR_UID, + $this->faker->email, + $delegation->DEL_INDEX + ]; + $appNotes = new ModelAppNotes(); + $appNotes->sendNoteNotification(...$params); + + //assert + $appMessage = AppMessage::where('APP_UID', '=', $application->APP_UID)->get()->first()->toArray(); + $this->assertArrayHasKey('APP_UID', $appMessage); + $this->assertEquals($appMessage['APP_UID'], $application->APP_UID); + } + + /** + * This test verify if exists attachment files. + * @test + * @covers \AppNotes::getAttachedFilesFromTheCaseNote + */ + public function it_should_test_get_attached_files_from_the_casenote() + { + $appNote = factory(AppNotes::class)->create(); + $appDocument = factory(Documents::class)->create([ + 'DOC_ID' => $appNote->NOTE_ID + ]); + + $appUid = $appDocument->APP_UID; + $appNotes = new ModelAppNotes(); + $result = $appNotes->getAttachedFilesFromTheCaseNote($appUid); + + $this->assertNotEmpty($result); + } +} diff --git a/tests/unit/workflow/engine/src/ProcessMaker/Model/DocumentsTest.php b/tests/unit/workflow/engine/src/ProcessMaker/Model/DocumentsTest.php index bc882b79d..b1fe83257 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/Model/DocumentsTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/Model/DocumentsTest.php @@ -1,7 +1,9 @@ getAppFiles($appDoc->APP_UID, Documents::DOC_TYPE_CASE_NOTE); $this->assertNotEmpty($res); } + + /** + * This test verify if exists attachment files. + * @test + * @covers Documents::getAttachedFilesFromTheCaseNote + */ + public function it_should_test_get_attached_files_from_the_casenote() + { + $appNote = factory(AppNotes::class)->create(); + $appDocument = factory(Documents::class)->create([ + 'DOC_ID' => $appNote->NOTE_ID + ]); + + $appUid = $appDocument->APP_UID; + $result = Documents::getAttachedFilesFromTheCaseNote($appUid); + + $this->assertNotEmpty($result); + } } \ No newline at end of file diff --git a/workflow/engine/classes/model/AppNotes.php b/workflow/engine/classes/model/AppNotes.php index 6e99dc408..17c885359 100644 --- a/workflow/engine/classes/model/AppNotes.php +++ b/workflow/engine/classes/model/AppNotes.php @@ -1,6 +1,7 @@ addCaseNote() * @see AppNotes->postNewNote() * @see workflow/engine/src/ProcessMaker/Util/helpers.php::postNote() - */ - public function sendNoteNotification ($appUid, $usrUid, $noteContent, $noteRecipients, $from = '', $delIndex = 0) + */ + public function sendNoteNotification($appUid, $usrUid, $noteContent, $noteRecipients, $from = '', $delIndex = 0) { try { - $configuration = System::getEmailConfiguration(); $msgError = ""; - if (! isset( $configuration['MESS_ENABLED'] ) || $configuration['MESS_ENABLED'] != '1') { + if (!isset($configuration['MESS_ENABLED']) || $configuration['MESS_ENABLED'] != '1') { $msgError = "The default configuration wasn't defined"; $configuration['MESS_ENGINE'] = ''; } @@ -211,20 +211,27 @@ class AppNotes extends BaseAppNotes $cases = new Cases(); $fieldCase = $cases->loadCase($appUid, $delIndex); $configNoteNotification['subject'] = G::LoadTranslation('ID_MESSAGE_SUBJECT_NOTE_NOTIFICATION') . " @#APP_TITLE "; + //Define the body for the notification $configNoteNotification['body'] = $this->getBodyCaseNote($authorName, $noteContent); $body = nl2br(G::replaceDataField($configNoteNotification['body'], $fieldCase, 'mysql', false)); + $attachFileLinks = $this->getAttachedFilesFromTheCaseNote($appUid); + if (!empty($attachFileLinks)) { + $body = $body . "
" . G::LoadTranslation('ID_ATTACHED_FILES') . ": " . implode("
", $attachFileLinks) . "."; + } $users = new Users(); $recipientsArray = explode(",", $noteRecipients); foreach ($recipientsArray as $recipientUid) { $userInfo = $users->load($recipientUid); - $to = ((($userInfo['USR_FIRSTNAME'] != '') || ($userInfo['USR_LASTNAME'] != '')) ? $userInfo['USR_FIRSTNAME'] . ' ' . $userInfo['USR_LASTNAME'] . ' ' : '') . '<' . $userInfo['USR_EMAIL'] . '>'; + $ifUserNameDefined = $userInfo['USR_FIRSTNAME'] != '' || $userInfo['USR_LASTNAME'] != ''; + $to = ($ifUserNameDefined ? $userInfo['USR_FIRSTNAME'] . ' ' . $userInfo['USR_LASTNAME'] . ' ' : '') . '<' . $userInfo['USR_EMAIL'] . '>'; $spool = new SpoolRun(); $spool->setConfig($configuration); - $messageArray = AppMessage::buildMessageRow( + + $parameters = [ '', $appUid, $delIndex, @@ -244,7 +251,8 @@ class AppNotes extends BaseAppNotes (isset($fieldCase['APP_NUMBER'])) ? $fieldCase['APP_NUMBER'] : 0, (isset($fieldCase['PRO_ID'])) ? $fieldCase['PRO_ID'] : 0, (isset($fieldCase['TAS_ID'])) ? $fieldCase['TAS_ID'] : 0 - ); + ]; + $messageArray = AppMessage::buildMessageRow(...$parameters); $spool->create($messageArray); if ($msgError == '') { @@ -252,14 +260,29 @@ class AppNotes extends BaseAppNotes $spool->sendMail(); } } - } - //Send derivation notification - End } catch (Exception $exception) { throw $exception; } } + /** + * Get attached files from the case note, this require appUid. + * @param string $appUid + * @return array + */ + public function getAttachedFilesFromTheCaseNote(string $appUid): array + { + $attachFileLinks = []; + $url = System::getServerMainPath(); + $result = Documents::getAttachedFilesFromTheCaseNote($appUid); + $result->each(function($item) use($url, &$attachFileLinks) { + $href = $url . "/cases/casesShowCaseNotes?a={$item->APP_DOC_UID}=&v={$item->DOC_VERSION}"; + $attachFileLinks[] = "{$item->APP_DOC_FILENAME}"; + }); + return $attachFileLinks; + } + public function addCaseNote($applicationUid, $userUid, $note, $sendMail) { $response = $this->postNewNote($applicationUid, $userUid, $note, false); diff --git a/workflow/engine/content/translations/english/processmaker.en.po b/workflow/engine/content/translations/english/processmaker.en.po index bcdea0a8f..be838ea9b 100644 --- a/workflow/engine/content/translations/english/processmaker.en.po +++ b/workflow/engine/content/translations/english/processmaker.en.po @@ -2575,6 +2575,12 @@ msgstr "Attach" msgid "Attached" msgstr "Attached" +# TRANSLATION +# LABEL/ID_ATTACHED_FILES +#: LABEL/ID_ATTACHED_FILES +msgid "Attached files" +msgstr "Attached files" + # TRANSLATION # LABEL/ID_ATTRIBUTES #: LABEL/ID_ATTRIBUTES diff --git a/workflow/engine/data/mysql/insert.sql b/workflow/engine/data/mysql/insert.sql index df1b025bb..f0e2571f9 100644 --- a/workflow/engine/data/mysql/insert.sql +++ b/workflow/engine/data/mysql/insert.sql @@ -57232,6 +57232,7 @@ INSERT INTO TRANSLATION (TRN_CATEGORY,TRN_ID,TRN_LANG,TRN_VALUE,TRN_UPDATE_DATE ( 'LABEL','ID_ASSIGN_VARIABLES_OUT','en','Assign Variables Out','2014-01-15') , ( 'LABEL','ID_ATTACH','en','Attach','2014-01-15') , ( 'LABEL','ID_ATTACHED_DB','en','Attached','2014-10-08') , +( 'LABEL','ID_ATTACHED_FILES','en','Attached files','2020-06-10') , ( 'LABEL','ID_ATTRIBUTES','en','Attributes','2014-01-15') , ( 'LABEL','ID_ATTRIBUTE_HAS_INVALID_ELEMENT_KEY','en','The attribute {0}, has an invalid element (incorrect keys).','2014-05-20') , ( 'LABEL','ID_AT_RISK','en','At Risk','2014-01-15') , diff --git a/workflow/engine/src/ProcessMaker/Model/Documents.php b/workflow/engine/src/ProcessMaker/Model/Documents.php index 740e7d689..85939198d 100644 --- a/workflow/engine/src/ProcessMaker/Model/Documents.php +++ b/workflow/engine/src/ProcessMaker/Model/Documents.php @@ -93,4 +93,20 @@ class Documents extends Model return $documentList; } + + /** + * Get attached files from the case note. + * @param string $appUid + * @return object + */ + public static function getAttachedFilesFromTheCaseNote(string $appUid) + { + $result = Documents::select('APP_DOCUMENT.APP_DOC_UID', 'APP_DOCUMENT.DOC_VERSION', 'APP_DOCUMENT.APP_DOC_FILENAME') + ->join('APP_NOTES', function($join) use($appUid) { + $join->on('APP_NOTES.NOTE_ID', '=', 'APP_DOCUMENT.DOC_ID') + ->where('APP_DOCUMENT.APP_UID', '=', $appUid); + }) + ->get(); + return $result; + } }