diff --git a/tests/unit/workflow/engine/methods/emailServer/EmailServerGmailOAuthTest.php b/tests/unit/workflow/engine/methods/emailServer/EmailServerGmailOAuthTest.php index 82b7c674f..98184d4ae 100644 --- a/tests/unit/workflow/engine/methods/emailServer/EmailServerGmailOAuthTest.php +++ b/tests/unit/workflow/engine/methods/emailServer/EmailServerGmailOAuthTest.php @@ -4,6 +4,7 @@ namespace Tests\unit\workflow\engine\methods\emailServer; use Faker\Factory; use Google_Auth_Exception; +use Illuminate\Support\Facades\Cache; use ProcessMaker\GmailOAuth\GmailOAuth; use ProcessMaker\Model\User; use RBAC; @@ -12,7 +13,7 @@ use Tests\TestCase; class EmailServerGmailOAuthTest extends TestCase { /** - * This test expects an exception from the Google client. + * This test expects an error message stored in the cache. * The Google client requires valid codes to obtain the clientId from a request, * otherwise it will throw an exception. * @test @@ -40,7 +41,7 @@ class EmailServerGmailOAuthTest extends TestCase */ $_GET['code'] = $faker->regexify("/[1-9]\/[a-zA-Z]{25}-[a-zA-Z]{16}_[a-zA-Z]{19}-[a-zA-Z]{24}/"); - $this->expectException(Google_Auth_Exception::class); require_once PATH_METHODS . 'emailServer/emailServerGmailOAuth.php'; + $this->assertTrue(Cache::has('errorMessageIfNotAuthenticate')); } } diff --git a/tests/unit/workflow/engine/src/ProcessMaker/GmailOAuth/GmailOAuthTest.php b/tests/unit/workflow/engine/src/ProcessMaker/GmailOAuth/GmailOAuthTest.php index 79739cebf..5f819c2a7 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/GmailOAuth/GmailOAuthTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/GmailOAuth/GmailOAuthTest.php @@ -210,6 +210,10 @@ class GmailOAuthTest extends TestCase $gmailOauth->setSendTestMail(0); $result = $gmailOauth->sendTestEmailWithGoogleServiceGmail(); $this->assertTrue($result instanceof Google_Service_Gmail_Message); + + $this->expectException(Exception::class); + $gmailOauth->setSendTestMail(1); + $result = $gmailOauth->sendTestEmailWithGoogleServiceGmail(); } /** @@ -249,6 +253,12 @@ class GmailOAuthTest extends TestCase $gmailOauth->setSendTestMail(0); $result = $gmailOauth->sendTestMailWithPHPMailerOAuth(); $this->assertTrue($result instanceof PHPMailerOAuth); + + $this->expectException(Exception::class); + $gmailOauth->setSenderEmail(""); + $gmailOauth->setMailTo($faker->email); + $gmailOauth->setSendTestMail(1); + $result = $gmailOauth->sendTestMailWithPHPMailerOAuth(); } /** @@ -262,4 +272,62 @@ class GmailOAuthTest extends TestCase $result = $gmailOauth->getMessageBody(); $this->assertTrue(is_string($result)); } + + /** + * This ensures that it is saved in the APP_MESSAGE table. + * @test + * @covers \ProcessMaker\GmailOAuth\GmailOAuth::saveIntoAppMessage() + */ + public function it_should_save_into_app_message_table() + { + $faker = $this->faker; + $gmailOauth = new GmailOAuth(); + + $gmailOauth->setFromAccount($faker->email); + $gmailOauth->setSenderEmail($faker->email); + $gmailOauth->setMailTo($faker->email); + + try { + $gmailOauth->saveIntoAppMessage("pending"); + } catch (Exception $e) { + $this->fail($e->getMessage()); + } + $this->assertTrue(true); + + try { + $gmailOauth->saveIntoAppMessage("sent"); + } catch (Exception $e) { + $this->fail($e->getMessage()); + } + $this->assertTrue(true); + } + + /** + * This ensures that it is saved in the Standard Log table. + * @test + * @covers \ProcessMaker\GmailOAuth\GmailOAuth::saveIntoStandardLogs() + */ + public function it_should_save_into_standard_log() + { + $faker = $this->faker; + $gmailOauth = new GmailOAuth(); + + $gmailOauth->setFromAccount($faker->email); + $gmailOauth->setSenderEmail($faker->email); + $gmailOauth->setMailTo($faker->email); + + try { + $gmailOauth->saveIntoStandardLogs("pending"); + } catch (Exception $e) { + $this->fail($e->getMessage()); + } + $this->assertTrue(true); + + try { + $gmailOauth->saveIntoStandardLogs("sent"); + } catch (Exception $e) { + $this->fail($e->getMessage()); + } + $this->assertTrue(true); + } } diff --git a/workflow/engine/methods/emailServer/emailServerGmailOAuth.php b/workflow/engine/methods/emailServer/emailServerGmailOAuth.php index 10861d501..b2ab32d32 100644 --- a/workflow/engine/methods/emailServer/emailServerGmailOAuth.php +++ b/workflow/engine/methods/emailServer/emailServerGmailOAuth.php @@ -11,7 +11,7 @@ try { $validInput = empty($_GET['code']) || empty($_SESSION['gmailOAuth']) || !is_object($_SESSION['gmailOAuth']); if ($validInput) { G::header($header); - exit(); + return; } $RBAC->allows(basename(__FILE__), "code"); @@ -22,7 +22,7 @@ try { if (isset($result["error"])) { Cache::put('errorMessageIfNotAuthenticate', G::json_decode($result["error"]), 2); G::header($header); - exit(); + return; } $gmailOAuth->setRefreshToken($googleClient->getRefreshToken()); @@ -38,4 +38,4 @@ try { } G::header($header); -exit(); +return; diff --git a/workflow/engine/src/ProcessMaker/GmailOAuth/GmailOAuth.php b/workflow/engine/src/ProcessMaker/GmailOAuth/GmailOAuth.php index 36d0000a0..cf3313480 100644 --- a/workflow/engine/src/ProcessMaker/GmailOAuth/GmailOAuth.php +++ b/workflow/engine/src/ProcessMaker/GmailOAuth/GmailOAuth.php @@ -2,6 +2,8 @@ namespace ProcessMaker\GmailOAuth; +use AppMessage; +use Bootstrap; use G; use Google_Client; use Google_Service_Gmail; @@ -10,6 +12,7 @@ use PHPMailerOAuth; use ProcessMaker\BusinessModel\EmailServer; use ProcessMaker\Core\System; use TemplatePower; +use WsBase; class GmailOAuth { @@ -415,7 +418,65 @@ class GmailOAuth $phpMailerOAuth->Subject = G::LoadTranslation("ID_MESS_TEST_SUBJECT"); $phpMailerOAuth->Body = utf8_encode($this->getMessageBody()); $phpMailerOAuth->AddAddress($this->mailTo); - $phpMailerOAuth->Send(); + $status = $phpMailerOAuth->Send(); + $this->saveIntoStandardLogs($status ? "sent" : "pending"); + $this->saveIntoAppMessage($status ? "sent" : "pending"); return $phpMailerOAuth; } + + /** + * Register into APP_MESSAGE table. + * @param string $status + */ + public function saveIntoAppMessage(string $status = "") + { + $appMsgUid = G::generateUniqueID(); + $spool = new AppMessage(); + $spool->setAppMsgUid($appMsgUid); + $spool->setMsgUid(""); + $spool->setAppUid(""); + $spool->setDelIndex(0); + $spool->setAppMsgType(WsBase::MESSAGE_TYPE_TEST_EMAIL); + $spool->setAppMsgTypeId(isset(AppMessage::$app_msg_type_values[WsBase::MESSAGE_TYPE_TEST_EMAIL]) ? AppMessage::$app_msg_type_values[WsBase::MESSAGE_TYPE_TEST_EMAIL] : 0); + $spool->setAppMsgSubject(G::LoadTranslation("ID_MESS_TEST_SUBJECT")); + $spool->setAppMsgFrom($this->fromAccount); + $spool->setAppMsgTo($this->mailTo); + $spool->setAppMsgBody(utf8_encode($this->getMessageBody())); + $spool->setAppMsgDate(date('Y-m-d H:i:s')); + $spool->setAppMsgCc(""); + $spool->setAppMsgBcc(""); + $spool->setappMsgAttach(serialize([""])); + $spool->setAppMsgTemplate(""); + $spool->setAppMsgStatus($status); + $spool->setAppMsgStatusId(AppMessage::$app_msg_status_values[$status] ? AppMessage::$app_msg_status_values[$status] : 0); + $spool->setAppMsgSendDate(date('Y-m-d H:i:s')); + $spool->setAppMsgShowMessage(1); + $spool->setAppMsgError(""); + $spool->setAppNumber(0); + $spool->setTasId(0); + $spool->setProId(0); + $spool->save(); + } + + /** + * Register into standard logs. + * @param string $status + */ + public function saveIntoStandardLogs(string $status = "") + { + $channel = "Test Email Servers Configuration"; + $severity = 200; //INFO + $message = "Email Server test has been sent"; + $context = [ + "emailServerUid" => $this->emailServerUid, + "emailEngine" => $this->emailEngine, + "from" => $this->fromAccount, + "senderAccount" => $this->mailTo, + "senderEmail" => $this->senderEmail, + "senderName" => $this->senderName, + "status" => $status + ]; + $workspace = config("system.workspace"); + Bootstrap::registerMonolog($channel, $severity, $message, $context, $workspace); + } }