From 920b79160172686ed2b566c02ceef958db78e52d Mon Sep 17 00:00:00 2001 From: Roly Rudy Gutierrez Pinto Date: Thu, 12 Dec 2019 11:32:20 -0400 Subject: [PATCH] PMC-1378 Create/Fix unit tests for the feature "GMail API email server" --- database/factories/AppMessageFactory.php | 37 ++ database/factories/EmailServerFactory.php | 93 ++++- gulliver/system/class.g.php | 2 +- phpunit.xml | 1 + tests/unit/gulliver/system/rbacTest.php | 31 ++ .../workflow/engine/classes/SpoolRunTest.php | 336 +++++++++++++++++- .../workflow/engine/classes/WsBaseTest.php | 145 +++++--- .../emailServer/EmailServerAjaxTest.php | 208 +++++++++++ .../emailServer/EmailServerGmailOAuthTest.php | 46 +++ .../methods/emailServer/EmailServerTest.php | 37 ++ .../BusinessModel/EmailServerTest.php | 253 +++++++++++++ .../src/ProcessMaker/Core/SystemTest.php | 43 ++- .../Model/EmailServerModelTest.php | 2 +- .../src/ProcessMaker/Model/AppMessage.php | 12 + 14 files changed, 1160 insertions(+), 86 deletions(-) create mode 100644 database/factories/AppMessageFactory.php create mode 100644 tests/unit/gulliver/system/rbacTest.php create mode 100644 tests/unit/workflow/engine/methods/emailServer/EmailServerAjaxTest.php create mode 100644 tests/unit/workflow/engine/methods/emailServer/EmailServerGmailOAuthTest.php create mode 100644 tests/unit/workflow/engine/methods/emailServer/EmailServerTest.php create mode 100644 tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/EmailServerTest.php create mode 100644 workflow/engine/src/ProcessMaker/Model/AppMessage.php diff --git a/database/factories/AppMessageFactory.php b/database/factories/AppMessageFactory.php new file mode 100644 index 000000000..3c7a1a997 --- /dev/null +++ b/database/factories/AppMessageFactory.php @@ -0,0 +1,37 @@ +define(\ProcessMaker\Model\AppMessage::class, function(Faker $faker) { + return [ + 'APP_MSG_UID' => G::generateUniqueID(), + 'MSG_UID' => '', + 'APP_UID' => function() { + return factory(\ProcessMaker\Model\Application::class)->create()->APP_UID; + }, + 'DEL_INDEX' => 1, + 'APP_MSG_TYPE' => 'ROUTING', + 'APP_MSG_TYPE_ID' => 0, + 'APP_MSG_SUBJECT' => $faker->title, + 'APP_MSG_FROM' => $faker->email, + 'APP_MSG_TO' => $faker->email, + 'APP_MSG_BODY' => $faker->text, + 'APP_MSG_DATE' => $faker->dateTime(), + 'APP_MSG_CC' => '', + 'APP_MSG_BCC' => '', + 'APP_MSG_TEMPLATE' => '', + 'APP_MSG_STATUS' => 'pending', + 'APP_MSG_STATUS_ID' => 1, + 'APP_MSG_ATTACH' => '', + 'APP_MSG_SEND_DATE' => $faker->dateTime(), + 'APP_MSG_SHOW_MESSAGE' => 1, + 'APP_MSG_ERROR' => '', + 'PRO_ID' => function() { + return factory(\ProcessMaker\Model\Process::class)->create()->PRO_ID; + }, + 'TAS_ID' => function() { + return factory(\ProcessMaker\Model\Task::class)->create()->TAS_ID; + }, + 'APP_NUMBER' => 1 + ]; +}); diff --git a/database/factories/EmailServerFactory.php b/database/factories/EmailServerFactory.php index 63d464e15..d99b3e185 100755 --- a/database/factories/EmailServerFactory.php +++ b/database/factories/EmailServerFactory.php @@ -5,7 +5,7 @@ use Faker\Generator as Faker; $factory->define(\ProcessMaker\Model\EmailServerModel::class, function(Faker $faker) { return [ 'MESS_UID' => G::generateUniqueID(), - 'MESS_ENGINE' => '', + 'MESS_ENGINE' => 'MAIL', 'MESS_SERVER' => '', 'MESS_PORT' => 0, 'MESS_INCOMING_SERVER' => '', @@ -19,5 +19,96 @@ $factory->define(\ProcessMaker\Model\EmailServerModel::class, function(Faker $fa 'MESS_TRY_SEND_INMEDIATLY' => 0, 'MAIL_TO' => '', 'MESS_DEFAULT' => 0, + 'OAUTH_CLIENT_ID' => '', + 'OAUTH_CLIENT_SECRET' => '', + 'OAUTH_REFRESH_TOKEN' => '' + ]; +}); + +$factory->state(\ProcessMaker\Model\EmailServerModel::class, 'PHPMAILER', function ($faker) { + return [ + 'MESS_UID' => G::generateUniqueID(), + 'MESS_ENGINE' => 'PHPMAILER', + 'MESS_PORT' => $faker->numberBetween(400, 500), + 'MESS_INCOMING_SERVER' => '', + 'MESS_INCOMING_PORT' => 0, + 'MESS_RAUTH' => 1, + 'MESS_ACCOUNT' => $faker->email, + 'MESS_PASSWORD' => $faker->password, + 'MESS_FROM_MAIL' => $faker->email, + 'MESS_FROM_NAME' => $faker->name, + 'SMTPSECURE' => 'ssl', + 'MESS_TRY_SEND_INMEDIATLY' => 0, + 'MAIL_TO' => $faker->email, + 'MESS_DEFAULT' => 0, + 'OAUTH_CLIENT_ID' => '', + 'OAUTH_CLIENT_SECRET' => '', + 'OAUTH_REFRESH_TOKEN' => '' + ]; +}); + +$factory->state(\ProcessMaker\Model\EmailServerModel::class, 'IMAP', function ($faker) { + return [ + 'MESS_UID' => G::generateUniqueID(), + 'MESS_ENGINE' => 'IMAP', + 'MESS_PORT' => $faker->numberBetween(400, 500), + 'MESS_INCOMING_SERVER' => 'imap.' . $faker->domainName, + 'MESS_INCOMING_PORT' => $faker->numberBetween(400, 500), + 'MESS_RAUTH' => 1, + 'MESS_ACCOUNT' => $faker->email, + 'MESS_PASSWORD' => $faker->password, + 'MESS_FROM_MAIL' => $faker->email, + 'MESS_FROM_NAME' => $faker->name, + 'SMTPSECURE' => 'ssl', + 'MESS_TRY_SEND_INMEDIATLY' => 0, + 'MAIL_TO' => $faker->email, + 'MESS_DEFAULT' => 0, + 'OAUTH_CLIENT_ID' => '', + 'OAUTH_CLIENT_SECRET' => '', + 'OAUTH_REFRESH_TOKEN' => '' + ]; +}); + +$factory->state(\ProcessMaker\Model\EmailServerModel::class, 'GMAILAPI', function ($faker) { + return [ + 'MESS_UID' => G::generateUniqueID(), + 'MESS_ENGINE' => 'GMAILAPI', + 'MESS_PORT' => 0, + 'MESS_INCOMING_SERVER' => '', + 'MESS_INCOMING_PORT' => 0, + 'MESS_RAUTH' => 1, + 'MESS_ACCOUNT' => $faker->email, + 'MESS_PASSWORD' => '', + 'MESS_FROM_MAIL' => $faker->email, + 'MESS_FROM_NAME' => $faker->name, + 'SMTPSECURE' => 'No', + 'MESS_TRY_SEND_INMEDIATLY' => 0, + 'MAIL_TO' => $faker->email, + 'MESS_DEFAULT' => 0, + 'OAUTH_CLIENT_ID' => $faker->regexify("/[0-9]{12}-[a-z]{32}\.apps\.googleusercontent\.com/"), + 'OAUTH_CLIENT_SECRET' => $faker->regexify("/[a-z]{24}/"), + 'OAUTH_REFRESH_TOKEN' => $faker->regexify("/[a-z]{7}[a-zA-Z0-9]{355}==/") + ]; +}); + +$factory->state(\ProcessMaker\Model\EmailServerModel::class, 'OPENMAIL', function ($faker) { + return [ + 'MESS_UID' => G::generateUniqueID(), + 'MESS_ENGINE' => 'OPENMAIL', + 'MESS_PORT' => 0, + 'MESS_INCOMING_SERVER' => '', + 'MESS_INCOMING_PORT' => 0, + 'MESS_RAUTH' => 1, + 'MESS_ACCOUNT' => $faker->email, + 'MESS_PASSWORD' => $faker->password, + 'MESS_FROM_MAIL' => $faker->email, + 'MESS_FROM_NAME' => $faker->name, + 'SMTPSECURE' => 'ssl', + 'MESS_TRY_SEND_INMEDIATLY' => 0, + 'MAIL_TO' => $faker->email, + 'MESS_DEFAULT' => 0, + 'OAUTH_CLIENT_ID' => '', + 'OAUTH_CLIENT_SECRET' => '', + 'OAUTH_REFRESH_TOKEN' => '' ]; }); diff --git a/gulliver/system/class.g.php b/gulliver/system/class.g.php index 0aceb2881..b1538d0df 100644 --- a/gulliver/system/class.g.php +++ b/gulliver/system/class.g.php @@ -633,7 +633,7 @@ class G try { $file = G::ExpandPath('skinEngine') . 'skinEngine.php'; - include $file; + include_once $file; $skinEngine = new SkinEngine($G_TEMPLATE, $G_SKIN, $G_CONTENT); $skinEngine->setLayout($layout); $skinEngine->dispatch(); diff --git a/phpunit.xml b/phpunit.xml index 43268ce99..2a6ef8061 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -31,6 +31,7 @@ ./gulliver ./workflow/engine/classes ./workflow/engine/src + ./workflow/engine/methods/emailServer/ ./workflow/engine/classes/model/map diff --git a/tests/unit/gulliver/system/rbacTest.php b/tests/unit/gulliver/system/rbacTest.php new file mode 100644 index 000000000..e2337b6c5 --- /dev/null +++ b/tests/unit/gulliver/system/rbacTest.php @@ -0,0 +1,31 @@ +authorizedActions; + + $this->assertArrayHasKey("emailServerGmailOAuth.php", $authorizedActions); + + $subset = [ + 'code' => ['PM_SETUP'] + ]; + $this->assertContains($subset, $authorizedActions); + } +} diff --git a/tests/unit/workflow/engine/classes/SpoolRunTest.php b/tests/unit/workflow/engine/classes/SpoolRunTest.php index 86563df0c..e0351f8d3 100644 --- a/tests/unit/workflow/engine/classes/SpoolRunTest.php +++ b/tests/unit/workflow/engine/classes/SpoolRunTest.php @@ -1,10 +1,14 @@ setData( - G::generateUniqueID(), - $faker->words(3, true), - $faker->companyEmail, - $faker->freeEmail, - $faker->text(), - $faker->dateTime()->format('Y-m-d H:i:s'), - $faker->companyEmail, - $faker->freeEmail + G::generateUniqueID(), + $faker->words(3, true), + $faker->companyEmail, + $faker->freeEmail, + $faker->text(), + $faker->dateTime()->format('Y-m-d H:i:s'), + $faker->companyEmail, + $faker->freeEmail ); // Build the "to", "cc" an "bcc" values @@ -45,14 +49,14 @@ class SpoolRunTest extends TestCase // Set a second set of data $spoolRun->setData( - G::generateUniqueID(), - $faker->words(3, true), - $faker->companyEmail, - $faker->freeEmail, - $faker->text(), - $faker->dateTime()->format('Y-m-d H:i:s'), - $faker->companyEmail, - $faker->freeEmail + G::generateUniqueID(), + $faker->words(3, true), + $faker->companyEmail, + $faker->freeEmail, + $faker->text(), + $faker->dateTime()->format('Y-m-d H:i:s'), + $faker->companyEmail, + $faker->freeEmail ); // Build the "to", "cc" an "bcc" values @@ -66,4 +70,302 @@ class SpoolRunTest extends TestCase $this->assertCount(1, $fileData['envelope_cc']); $this->assertCount(1, $fileData['envelope_bcc']); } + + /** + * This test uses the GMAILAPI option in a simple way. + * @test + * @covers \SpoolRun::__construct() + * @covers \SpoolRun::setData() + * @covers \SpoolRun::setConfig() + * @covers \SpoolRun::sendMail() + * @covers \SpoolRun::handleMail() + */ + public function it_should_handle_gmail_oauth_option() + { + $appMsgUid = G::generateUniqueID(); + factory(AppMessage::class)->create([ + 'APP_MSG_UID' => $appMsgUid + ]); + + $emailServer = factory(EmailServerModel::class)->states('GMAILAPI')->make(); + + $config = $emailServer->toArray(); + $config['SMTPSecure'] = 'ssl'; + + $faker = Factory::create(); + $spoolRun = new SpoolRun(); + $spoolRun->setData( + $appMsgUid, + $faker->title, + $faker->companyEmail, + $faker->freeEmail, + $faker->text(), + $faker->dateTime()->format('Y-m-d H:i:s'), + $faker->companyEmail, + $faker->freeEmail + ); + $spoolRun->setConfig($config); + + $expected = $spoolRun->sendMail(); + + $this->assertTrue($expected); + } + + /** + * This test uses the MAIL option in a simple way. + * @test + * @covers \SpoolRun::__construct() + * @covers \SpoolRun::setData() + * @covers \SpoolRun::setConfig() + * @covers \SpoolRun::sendMail() + * @covers \SpoolRun::handleMail() + */ + public function it_should_handle_mail_option() + { + $appMsgUid = G::generateUniqueID(); + factory(AppMessage::class)->create([ + 'APP_MSG_UID' => $appMsgUid + ]); + + $emailServer = factory(EmailServerModel::class)->create(); + + $config = $emailServer->toArray(); + + $faker = Factory::create(); + $spoolRun = new SpoolRun(); + $spoolRun->setData( + $appMsgUid, + $faker->title, + $faker->companyEmail, + $faker->freeEmail, + $faker->text(), + $faker->dateTime()->format('Y-m-d H:i:s'), + $faker->companyEmail, + $faker->freeEmail + ); + $spoolRun->setConfig($config); + + $expected = $spoolRun->sendMail(); + + $this->assertTrue($expected); + } + + /** + * This test uses the PHPMAILER option in a simple way. + * @test + * @covers \SpoolRun::__construct() + * @covers \SpoolRun::setData() + * @covers \SpoolRun::setConfig() + * @covers \SpoolRun::sendMail() + * @covers \SpoolRun::handleMail() + */ + public function it_should_handle_php_mailer_option() + { + $appMsgUid = G::generateUniqueID(); + factory(AppMessage::class)->create([ + 'APP_MSG_UID' => $appMsgUid + ]); + + $emailServer = factory(EmailServerModel::class)->states('PHPMAILER')->make(); + + $config = $emailServer->toArray(); + $config['SMTPSecure'] = 'ssl'; + + $faker = Factory::create(); + $spoolRun = new SpoolRun(); + $spoolRun->setData( + $appMsgUid, + $faker->title, + $faker->companyEmail, + $faker->freeEmail, + $faker->text(), + $faker->dateTime()->format('Y-m-d H:i:s'), + $faker->companyEmail, + $faker->freeEmail + ); + $spoolRun->setConfig($config); + + $expected = $spoolRun->sendMail(); + + $this->assertTrue($expected); + } + + /** + * This test uses the OPENMAIL option in a simple way. + * @test + * @covers \SpoolRun::__construct() + * @covers \SpoolRun::setData() + * @covers \SpoolRun::setConfig() + * @covers \SpoolRun::sendMail() + * @covers \SpoolRun::handleMail() + */ + public function it_should_handle_open_mail_option() + { + $this->markTestIncomplete("The OPENMAIL depends on the package class but this is not found in the environment."); + + $appMsgUid = G::generateUniqueID(); + factory(AppMessage::class)->create([ + 'APP_MSG_UID' => $appMsgUid + ]); + + $emailServer = factory(EmailServerModel::class)->states('OPENMAIL')->make(); + + $config = $emailServer->toArray(); + + $faker = Factory::create(); + $spoolRun = new SpoolRun(); + $spoolRun->setData( + $appMsgUid, + $faker->title, + $faker->companyEmail, + $faker->freeEmail, + $faker->text(), + $faker->dateTime()->format('Y-m-d H:i:s'), + $faker->companyEmail, + $faker->freeEmail + ); + $spoolRun->setConfig($config); + + $expected = $spoolRun->sendMail(); + + $this->assertTrue($expected); + } + + /** + * This test ensures that characters that are not utf8 are converted properly, + * for subject and body fields. + * @test + * @covers \SpoolRun::__construct() + * @covers \SpoolRun::setData() + * @covers \SpoolRun::setConfig() + * @covers \SpoolRun::sendMail() + * @covers \SpoolRun::handleMail() + */ + public function it_should_handle_utf8_characters() + { + $appMsgUid = G::generateUniqueID(); + factory(AppMessage::class)->create([ + 'APP_MSG_UID' => $appMsgUid + ]); + + $emailServer = factory(EmailServerModel::class)->states('PHPMAILER')->make(); + + $config = $emailServer->toArray(); + $config['SMTPSecure'] = 'ssl'; + + $faker = Factory::create(); + $subject = "\xf8foo"; + $body = "\xf8foo\xf8foo"; + + $spoolRun = new SpoolRun(); + $spoolRun->setData( + $appMsgUid, + $subject, + $faker->companyEmail, + $faker->freeEmail, + $body, + $faker->dateTime()->format('Y-m-d H:i:s'), + $faker->companyEmail, + $faker->freeEmail + ); + $spoolRun->setConfig($config); + + $expected = $spoolRun->sendMail(); + + $this->assertTrue($expected); + } + + /** + * This test verifies the sending of attachments to the email. + * @test + * @covers \SpoolRun::__construct() + * @covers \SpoolRun::setData() + * @covers \SpoolRun::setConfig() + * @covers \SpoolRun::sendMail() + * @covers \SpoolRun::handleMail() + */ + public function it_should_handle_attachment_files() + { + $appMsgUid = G::generateUniqueID(); + factory(AppMessage::class)->create([ + 'APP_MSG_UID' => $appMsgUid + ]); + + $emailServer = factory(EmailServerModel::class)->states('PHPMAILER')->make(); + + $config = $emailServer->toArray(); + $config['SMTPSecure'] = 'ssl'; + + $faker = Factory::create(); + + $file1 = UploadedFile::fake()->image('avatar.jpg', 400, 300); + $file2 = UploadedFile::fake()->create('document.pdf', 200); + + $files = [ + $file1->path(), + $file2->path() + ]; + + $spoolRun = new SpoolRun(); + $spoolRun->setData( + $appMsgUid, + $faker->title, + $faker->companyEmail, + $faker->freeEmail, + $faker->text(), + $faker->dateTime()->format('Y-m-d H:i:s'), + $faker->companyEmail, + $faker->freeEmail, + '', + $files + ); + $spoolRun->setConfig($config); + + $expected = $spoolRun->sendMail(); + + $this->assertTrue($expected); + } + + /** + * This test ensures that the EnvelopeTo field process is working. + * @test + * @covers \SpoolRun::__construct() + * @covers \SpoolRun::setData() + * @covers \SpoolRun::runHandleEnvelopeTo() + * @covers \SpoolRun::handleEnvelopeTo() + * @covers \SpoolRun::setConfig() + * @covers \SpoolRun::sendMail() + * @covers \SpoolRun::handleMail() + */ + public function it_should_handle_envelope_to() + { + $appMsgUid = G::generateUniqueID(); + factory(AppMessage::class)->create([ + 'APP_MSG_UID' => $appMsgUid + ]); + + $emailServer = factory(EmailServerModel::class)->states('PHPMAILER')->make(); + + $config = $emailServer->toArray(); + $config['SMTPSecure'] = 'ssl'; + + $faker = Factory::create(); + $spoolRun = new SpoolRun(); + $spoolRun->setData( + $appMsgUid, + $faker->title, + $faker->name . "<" . $faker->companyEmail . "," . $faker->companyEmail . ">", + $faker->name . "<" . $faker->freeEmail . "," . $faker->freeEmail . ">", + $faker->text(), + $faker->dateTime()->format('Y-m-d H:i:s'), + $faker->name . "<" . $faker->companyEmail . "," . $faker->companyEmail . ">", + $faker->name . "<" . $faker->freeEmail . "," . $faker->freeEmail . ">" + ); + $spoolRun->runHandleEnvelopeTo(); + $spoolRun->setConfig($config); + + $expected = $spoolRun->sendMail(); + + $this->assertTrue($expected); + } } diff --git a/tests/unit/workflow/engine/classes/WsBaseTest.php b/tests/unit/workflow/engine/classes/WsBaseTest.php index be3acf033..ffb16875b 100755 --- a/tests/unit/workflow/engine/classes/WsBaseTest.php +++ b/tests/unit/workflow/engine/classes/WsBaseTest.php @@ -10,6 +10,7 @@ use ProcessMaker\Model\EmailServerModel; use ProcessMaker\Model\Process; use ProcessMaker\Model\Task; use ProcessMaker\Model\User; +use ProcessMaker\Util\WsMessageResponse; use Tests\TestCase; class WsBaseTest extends TestCase @@ -484,48 +485,48 @@ class WsBaseTest extends TestCase //Create the application factory $application1 = factory(Application::class)->create( - [ - 'APP_STATUS' => 'TO_DO', - 'APP_TITLE' => 'Title1' - ] + [ + 'APP_STATUS' => 'TO_DO', + 'APP_TITLE' => 'Title1' + ] ); $application2 = factory(Application::class)->create( - [ - 'APP_STATUS' => 'DRAFT', - 'APP_TITLE' => 'Title2' - ] + [ + 'APP_STATUS' => 'DRAFT', + 'APP_TITLE' => 'Title2' + ] ); //Create the delegation factory $delegation1 = factory(Delegation::class)->create( - [ - 'USR_UID' => $user->USR_UID, - 'DEL_THREAD_STATUS' => 'OPEN', - 'DEL_FINISH_DATE' => null, - 'APP_NUMBER' => $application1->APP_NUMBER - ] + [ + 'USR_UID' => $user->USR_UID, + 'DEL_THREAD_STATUS' => 'OPEN', + 'DEL_FINISH_DATE' => null, + 'APP_NUMBER' => $application1->APP_NUMBER + ] ); $delegation2 = factory(Delegation::class)->create( - [ - 'USR_UID' => $user->USR_UID, - 'DEL_THREAD_STATUS' => 'OPEN', - 'DEL_FINISH_DATE' => null, - 'APP_NUMBER' => $application2->APP_NUMBER - ] + [ + 'USR_UID' => $user->USR_UID, + 'DEL_THREAD_STATUS' => 'OPEN', + 'DEL_FINISH_DATE' => null, + 'APP_NUMBER' => $application2->APP_NUMBER + ] ); //Create app thread factory factory(AppThread::class)->create( - [ - 'APP_THREAD_STATUS' => 'OPEN', - 'APP_UID' => $delegation1->APP_UID - ] + [ + 'APP_THREAD_STATUS' => 'OPEN', + 'APP_UID' => $delegation1->APP_UID + ] ); factory(AppThread::class)->create( - [ - 'APP_THREAD_STATUS' => 'OPEN', - 'APP_UID' => $delegation2->APP_UID - ] + [ + 'APP_THREAD_STATUS' => 'OPEN', + 'APP_UID' => $delegation2->APP_UID + ] ); //Instance the object @@ -559,48 +560,48 @@ class WsBaseTest extends TestCase //Create the application factory $application1 = factory(Application::class)->create( - [ - 'APP_STATUS' => 'TO_DO', - 'APP_TITLE' => 'Title1' - ] + [ + 'APP_STATUS' => 'TO_DO', + 'APP_TITLE' => 'Title1' + ] ); $application2 = factory(Application::class)->create( - [ - 'APP_STATUS' => 'DRAFT', - 'APP_TITLE' => 'Title2' - ] + [ + 'APP_STATUS' => 'DRAFT', + 'APP_TITLE' => 'Title2' + ] ); //Create the delegation factory $delegation1 = factory(Delegation::class)->create( - [ - 'USR_UID' => $user1->USR_UID, - 'DEL_THREAD_STATUS' => 'OPEN', - 'DEL_FINISH_DATE' => null, - 'APP_NUMBER' => $application1->APP_NUMBER - ] + [ + 'USR_UID' => $user1->USR_UID, + 'DEL_THREAD_STATUS' => 'OPEN', + 'DEL_FINISH_DATE' => null, + 'APP_NUMBER' => $application1->APP_NUMBER + ] ); $delegation2 = factory(Delegation::class)->create( - [ - 'USR_UID' => $user1->USR_UID, - 'DEL_THREAD_STATUS' => 'OPEN', - 'DEL_FINISH_DATE' => null, - 'APP_NUMBER' => $application2->APP_NUMBER - ] + [ + 'USR_UID' => $user1->USR_UID, + 'DEL_THREAD_STATUS' => 'OPEN', + 'DEL_FINISH_DATE' => null, + 'APP_NUMBER' => $application2->APP_NUMBER + ] ); //Create app thread factory factory(AppThread::class)->create( - [ - 'APP_THREAD_STATUS' => 'OPEN', - 'APP_UID' => $delegation1->APP_UID - ] + [ + 'APP_THREAD_STATUS' => 'OPEN', + 'APP_UID' => $delegation1->APP_UID + ] ); factory(AppThread::class)->create( - [ - 'APP_THREAD_STATUS' => 'OPEN', - 'APP_UID' => $delegation2->APP_UID - ] + [ + 'APP_THREAD_STATUS' => 'OPEN', + 'APP_UID' => $delegation2->APP_UID + ] ); //Instance the object @@ -612,4 +613,36 @@ class WsBaseTest extends TestCase //Assert the result his empty $this->assertEmpty($res); } + + /** + * This test ensures obtaining the email configuration with all fields. + * @test + * @covers \WsBase::sendMessage() + */ + public function it_should_get_email_configuration() + { + $faker = Factory::create(); + + //data + $case = $this->createNewCase(); + $template = $this->createTemplate($case->process->PRO_UID, $case->user->USR_UID); + + //parameters + $appUid = $case->applicationUid; + $from = $faker->email; + $to = ""; + $cc = ""; + $bcc = ""; + $subject = $faker->title; + $templateName = basename($template->PRF_PATH); + $appFields = [ + 'var1' => $faker->numberBetween(1, 100) + ]; + + $wsBase = new WsBase(); + $result = $wsBase->sendMessage($appUid, $from, $to, $cc, $bcc, $subject, $templateName, $appFields); + + //assertions + $this->assertInstanceOf(WsMessageResponse::class, $result); + } } diff --git a/tests/unit/workflow/engine/methods/emailServer/EmailServerAjaxTest.php b/tests/unit/workflow/engine/methods/emailServer/EmailServerAjaxTest.php new file mode 100644 index 000000000..abfb01505 --- /dev/null +++ b/tests/unit/workflow/engine/methods/emailServer/EmailServerAjaxTest.php @@ -0,0 +1,208 @@ +settingUserLogged(); + } + + /** + * This starts a valid user in session with the appropriate permissions. + * @global object $RBAC + */ + private function settingUserLogged() + { + global $RBAC; + + $user = User::where('USR_ID', '=', 1) + ->get() + ->first(); + + $_SESSION['USER_LOGGED'] = $user['USR_UID']; + + $RBAC = RBAC::getSingleton(PATH_DATA, session_id()); + $RBAC->initRBAC(); + $RBAC->loadUserRolePermission('PROCESSMAKER', $_SESSION['USER_LOGGED']); + } + /** + * This allows multiple calls to the require_once statement, use the original + * content by creating several documents in the temporary folder. + * @global object $RBAC + * @return string + */ + private function requireOnceForEmailServerAjax(): string + { + $fileName = PATH_METHODS . 'emailServer/emailServerAjax.php'; + $tempFile = tempnam(sys_get_temp_dir(), basename($fileName, '.php')); + if ($tempFile !== false) { + file_put_contents($tempFile, file_get_contents($fileName)); + + global $RBAC; + $RBAC->authorizedActions[basename($tempFile)] = [ + 'INS' => ['PM_SETUP'], + 'UPD' => ['PM_SETUP'], + 'DEL' => ['PM_SETUP'], + 'LST' => ['PM_SETUP'], + 'TEST' => ['PM_SETUP'], + 'createAuthUrl' => ['PM_SETUP'] + ]; + ob_start(); + require_once $tempFile; + return ob_get_clean(); + } + return ""; + } + + /** + * This tests the createAuthUrl option at the endpoint emailServerAjax.php. + * @test + */ + public function it_should_verify_the_option_create_auth_url() + { + $faker = Factory::create(); + $post = [ + 'option' => 'createAuthUrl', + 'clientID' => $faker->regexify('[0-9]{12}\-[a-zA-Z]{20}'), + 'clientSecret' => $faker->regexify('[a-zA-Z]{10}'), + 'emailEngine' => 'GMAILAPI', + 'fromAccount' => $faker->email, + 'senderEmail' => $faker->email, + 'senderName' => $faker->name, + 'sendTestMail' => 1, + 'mailTo' => $faker->email, + 'setDefaultConfiguration' => 1 + ]; + $_POST = array_merge($_POST, $post); + + $content = $this->requireOnceForEmailServerAjax(); + $data = json_decode($content, JSON_OBJECT_AS_ARRAY); + + $this->assertContains(200, $data); + } + + /** + * This tests the INS option at the endpoint emailServerAjax.php. + * @test + */ + public function it_should_verify_the_option_ins() + { + $faker = Factory::create(); + $post = [ + 'option' => 'INS', + 'cboEmailEngine' => 'PHPMAILER', + 'server' => 'smtp.gmail.com', + 'port' => '465', + 'incomingServer' => '', + 'incomingPort' => '', + 'reqAuthentication' => 1, + 'accountFrom' => $faker->email, + 'password' => $faker->password, + 'fromMail' => $faker->email, + 'fromName' => $faker->name, + 'smtpSecure' => 'ssl', + 'sendTestMail' => 1, + 'mailTo' => $faker->email, + 'emailServerDefault' => 1, + ]; + $_POST = array_merge($_POST, $post); + + $content = $this->requireOnceForEmailServerAjax(); + $data = json_decode($content, JSON_OBJECT_AS_ARRAY); + + $this->assertContains("OK", $data); + } + + /** + * This tests the UPD option at the endpoint emailServerAjax.php. + * @test + */ + public function it_should_verify_the_option_upd() + { + $faker = Factory::create(); + + $emailServer = factory(EmailServerModel::class)->create([ + 'MESS_ENGINE' => 'PHPMAILER', + ]); + + $post = [ + 'option' => 'UPD', + 'emailServerUid' => $emailServer->MESS_UID, + 'cboEmailEngine' => 'PHPMAILER', + 'server' => 'smtp.gmail.com', + 'port' => '465', + 'incomingServer' => '', + 'incomingPort' => '', + 'reqAuthentication' => 1, + 'accountFrom' => $faker->email, + 'password' => $faker->password, + 'fromMail' => $faker->email, + 'fromName' => $faker->name, + 'smtpSecure' => 'ssl', + 'sendTestMail' => 1, + 'mailTo' => $faker->email, + 'emailServerDefault' => 1, + ]; + $_POST = array_merge($_POST, $post); + + $content = $this->requireOnceForEmailServerAjax(); + $data = json_decode($content, JSON_OBJECT_AS_ARRAY); + + $this->assertContains("OK", $data); + } + + /** + * This tests the DEL option at the endpoint emailServerAjax.php. + * @test + */ + public function it_should_verify_the_option_del() + { + $emailServer = factory(EmailServerModel::class)->create([ + 'MESS_ENGINE' => 'PHPMAILER', + ]); + + $post = [ + 'option' => 'DEL', + 'emailServerUid' => $emailServer->MESS_UID + ]; + $_POST = array_merge($_POST, $post); + + $content = $this->requireOnceForEmailServerAjax(); + $data = json_decode($content, JSON_OBJECT_AS_ARRAY); + + $this->assertContains("OK", $data); + } + + /** + * This tests the LST option at the endpoint emailServerAjax.php. + * @test + */ + public function it_should_verify_the_option_lst() + { + $post = [ + 'option' => 'LST', + 'pageSize' => 25, + 'search' => '' + ]; + $_POST = array_merge($_POST, $post); + + $content = $this->requireOnceForEmailServerAjax(); + $data = json_decode($content, JSON_OBJECT_AS_ARRAY); + + $this->assertContains("OK", $data); + $this->assertTrue(isset($data["resultRoot"])); + $this->assertTrue(is_array($data["resultRoot"])); + } +} diff --git a/tests/unit/workflow/engine/methods/emailServer/EmailServerGmailOAuthTest.php b/tests/unit/workflow/engine/methods/emailServer/EmailServerGmailOAuthTest.php new file mode 100644 index 000000000..82b7c674f --- /dev/null +++ b/tests/unit/workflow/engine/methods/emailServer/EmailServerGmailOAuthTest.php @@ -0,0 +1,46 @@ +get() + ->first(); + + $_SESSION['USER_LOGGED'] = $user['USR_UID']; + $_POST['USR_UID'] = $user['USR_UID']; + + $RBAC = RBAC::getSingleton(PATH_DATA, session_id()); + $RBAC->initRBAC(); + $RBAC->loadUserRolePermission('PROCESSMAKER', $_SESSION['USER_LOGGED']); + + $_SESSION['gmailOAuth'] = new GmailOAuth(); + + /** + * This gets a fake code, according to the nomenclature. + */ + $_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'; + } +} diff --git a/tests/unit/workflow/engine/methods/emailServer/EmailServerTest.php b/tests/unit/workflow/engine/methods/emailServer/EmailServerTest.php new file mode 100644 index 000000000..5ea67190d --- /dev/null +++ b/tests/unit/workflow/engine/methods/emailServer/EmailServerTest.php @@ -0,0 +1,37 @@ +get() + ->first(); + + $_SESSION['USER_LOGGED'] = $user['USR_UID']; + $_POST['USR_UID'] = $user['USR_UID']; + + $RBAC = RBAC::getSingleton(PATH_DATA, session_id()); + $RBAC->initRBAC(); + $RBAC->loadUserRolePermission('PROCESSMAKER', $_SESSION['USER_LOGGED']); + + ob_start(); + require_once PATH_METHODS . 'emailServer/emailServer.php'; + $content = ob_get_clean(); + + $this->assertContains("EMAILSERVER_LICENSED", $content); + } +} diff --git a/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/EmailServerTest.php b/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/EmailServerTest.php new file mode 100644 index 000000000..f41d49188 --- /dev/null +++ b/tests/unit/workflow/engine/src/ProcessMaker/BusinessModel/EmailServerTest.php @@ -0,0 +1,253 @@ +emailServer = new EmailServer(); + $this->faker = Factory::create(); + } + + /** + * Get structure for registry the EMAIL_SERVER. + * @return array + */ + private function getDataForEmailServerRegistry(): array + { + $faker = $this->faker; + return [ + 'MESS_ENGINE' => 'PHPMAILER', + 'MESS_SERVER' => 'smtp.' . $faker->domainName, + 'MESS_PORT' => $faker->numberBetween(400, 500), + 'MESS_INCOMING_SERVER' => '', + 'MESS_INCOMING_PORT' => $faker->numberBetween(400, 500), + 'MESS_RAUTH' => 1, + 'MESS_ACCOUNT' => $faker->email, + 'MESS_PASSWORD' => $faker->password, + 'MESS_FROM_MAIL' => $faker->email, + 'MESS_FROM_NAME' => $faker->name, + 'SMTPSECURE' => 'ssl', + 'MESS_TRY_SEND_INMEDIATLY' => 1, + 'MAIL_TO' => $faker->email, + 'MESS_DEFAULT' => 1, + 'OAUTH_CLIENT_ID' => '', + 'OAUTH_CLIENT_SECRET' => '', + 'OAUTH_REFRESH_TOKEN' => '', + ]; + } + + /** + * This creates a record in the EMAIL_SERVER table. + * @test + * @covers \ProcessMaker\BusinessModel\EmailServer::create() + */ + public function it_should_create() + { + $faker = $this->faker; + $expected = $this->getDataForEmailServerRegistry(); + $this->emailServer->setContextLog([ + 'workspace' => 'workflow' + ]); + $actual = $this->emailServer->create($expected); + + $this->assertTrue(isset($actual['MESS_UID'])); + $this->assertTrue(is_string($actual['MESS_UID'])); + $this->assertEquals($expected['MESS_ENGINE'], $actual['MESS_ENGINE']); + $this->assertEquals($expected['MESS_ACCOUNT'], $actual['MESS_ACCOUNT']); + + $expected['MESS_PASSWORD'] = G::encrypt('hash:' . $faker->password, 'EMAILENCRYPT'); + $actual = $this->emailServer->create($expected); + + $expected['MESS_PASSWORD'] = G::encrypt('hash:' . $faker->password . 'hash:', 'EMAILENCRYPT'); + $actual = $this->emailServer->create($expected); + + $this->expectException(Exception::class); + $this->emailServer->create([]); + } + + /** + * This updates a record in the EMAIL_SERVER table. + * @test + * @covers \ProcessMaker\BusinessModel\EmailServer::update() + */ + public function it_should_update() + { + $faker = $this->faker; + $emailServer = factory(EmailServerModel::class)->create($this->getDataForEmailServerRegistry()); + $data = $emailServer->toArray(); + + $this->emailServer->setContextLog([ + 'workspace' => 'workflow' + ]); + + $expected = [ + 'MESS_ENGINE' => 'PHPMAILER', + 'MESS_SERVER' => 'smtp.' . $faker->domainName, + 'MESS_PORT' => $faker->numberBetween(400, 500), + 'MESS_INCOMING_SERVER' => '', + 'MESS_INCOMING_PORT' => $faker->numberBetween(400, 500), + 'MESS_RAUTH' => 1, + 'MESS_ACCOUNT' => $faker->email, + 'MESS_PASSWORD' => $faker->password, + 'MESS_FROM_MAIL' => $faker->email, + 'MESS_FROM_NAME' => $faker->name, + 'SMTPSECURE' => 'ssl', + 'MESS_TRY_SEND_INMEDIATLY' => 1, + 'MAIL_TO' => $faker->email, + 'MESS_DEFAULT' => 1, + ]; + $actual = $this->emailServer->update($data['MESS_UID'], $expected); + + $this->assertEquals($expected['MESS_ENGINE'], $actual['MESS_ENGINE']); + $this->assertEquals($expected['MESS_ACCOUNT'], $actual['MESS_ACCOUNT']); + + $expected['MESS_PASSWORD'] = G::encrypt('hash:' . $faker->password, 'EMAILENCRYPT'); + $actual = $this->emailServer->update($data['MESS_UID'], $expected); + + $expected['MESS_PASSWORD'] = G::encrypt('hash:' . $faker->password . 'hash:', 'EMAILENCRYPT'); + $actual = $this->emailServer->update($data['MESS_UID'], $expected); + + $this->emailServer->setFormatFieldNameInUppercase(false); + $this->expectException(Exception::class); + $actual = $this->emailServer->update($data['MESS_UID'], $expected); + } + + /** + * Get data of a from a record EMAIL_SERVER. + * @test + * @covers \ProcessMaker\BusinessModel\EmailServer::getEmailServerDataFromRecord() + */ + public function it_should_get_email_server_data_from_record() + { + $faker = $this->faker; + $this->emailServer->setContextLog([ + 'workspace' => 'workflow' + ]); + $expected = $this->getDataForEmailServerRegistry(); + $expected['MESS_UID'] = $faker->regexify("/[a-zA-Z]{32}/"); + + $actual = $this->emailServer->getEmailServerDataFromRecord($expected); + + $this->assertEquals($expected['MESS_ENGINE'], $actual['MESS_ENGINE']); + $this->assertEquals($expected['MESS_ACCOUNT'], $actual['MESS_ACCOUNT']); + + unset($expected['MESS_ENGINE']); + $this->expectException(Exception::class); + $actual = $this->emailServer->getEmailServerDataFromRecord($expected); + } + + /** + * This test obtains the configuration record that is marked by default. + * @test + * @covers \ProcessMaker\BusinessModel\EmailServer::getEmailServerDefault() + */ + public function it_should_get_email_server_default() + { + $this->emailServer->setContextLog([ + 'workspace' => 'workflow' + ]); + $actual = $this->emailServer->getEmailServerDefault(); + $this->assertNotEmpty($actual); + } + + /** + * This test gets the records from the "EMAIL_SERVER" table. + * @test + * @covers \ProcessMaker\BusinessModel\EmailServer::getEmailServers() + */ + public function it_should_get_email_servers() + { + $this->emailServer->setContextLog([ + 'workspace' => 'workflow' + ]); + $actual = $this->emailServer->getEmailServers(); + $this->assertNotEmpty($actual); + } + + /** + * This test gets the records from the "EMAIL_SERVER" table with parameters. + * @test + * @covers \ProcessMaker\BusinessModel\EmailServer::getEmailServers() + */ + public function it_should_get_email_servers_with_parameters() + { + $this->emailServer->setContextLog([ + 'workspace' => 'workflow' + ]); + + $actual = $this->emailServer->getEmailServers(null, null, null, null, 0); + $this->assertEmpty($actual); + + $faker = $this->faker; + $actual = $this->emailServer->getEmailServers(['filter' => $faker->text]); + $this->assertNotEmpty($actual); + + $actual = $this->emailServer->getEmailServers(null, $faker->text); + $this->assertNotEmpty($actual); + + $actual = $this->emailServer->getEmailServers(null, "MESS_SERVER"); + $this->assertNotEmpty($actual); + + $actual = $this->emailServer->getEmailServers(null, "MESS_SERVER", "DESC"); + $this->assertNotEmpty($actual); + + $actual = $this->emailServer->getEmailServers(null, "MESS_SERVER", "DESC", 0); + $this->assertNotEmpty($actual); + + $actual = $this->emailServer->getEmailServers(null, "MESS_SERVER", "DESC", 0, 10); + $this->assertNotEmpty($actual); + + $this->expectException(Exception::class); + $actual = $this->emailServer->getEmailServers(null, "MESS_SERVER", "DESC", -1, -10); + } + + /** + * This test gets a record of the EMAIL_SERVER table. + * @test + * @covers \ProcessMaker\BusinessModel\EmailServer::getEmailServer() + */ + public function it_should_get_email_server() + { + $this->emailServer->setContextLog([ + 'workspace' => 'workflow' + ]); + $emailServer = factory(EmailServerModel::class)->create($this->getDataForEmailServerRegistry()); + $emailServerUid = $emailServer->MESS_UID; + $actual = $this->emailServer->getEmailServer($emailServerUid); + $this->assertNotEmpty($actual); + } + + /** + * This test should throw an exception when a record is not found. + * @test + * @covers \ProcessMaker\BusinessModel\EmailServer::getEmailServer() + */ + public function it_should_get_email_server_when_not_exist_registry() + { + $faker = $this->faker; + $this->emailServer->setContextLog([ + 'workspace' => 'workflow' + ]); + $emailServer = factory(EmailServerModel::class)->create($this->getDataForEmailServerRegistry()); + $emailServerUid = $faker->regexify("/[a-zA-Z]{32}/"); + + $this->expectException(Exception::class); + $actual = $this->emailServer->getEmailServer($emailServerUid); + } +} diff --git a/tests/unit/workflow/engine/src/ProcessMaker/Core/SystemTest.php b/tests/unit/workflow/engine/src/ProcessMaker/Core/SystemTest.php index 19634a5a4..25ba7021d 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/Core/SystemTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/Core/SystemTest.php @@ -3,27 +3,24 @@ namespace Tests\unit\workflow\engine\src\ProcessMaker\Core; use ProcessMaker\Core\System; +use ProcessMaker\Model\EmailServerModel; use Tests\TestCase; class SystemTest extends TestCase { + /** * Define the required variables */ - protected function setUp() + public function setUp() { - $this->markTestIncomplete();//@todo: Please correct this unit test - $config = config('database.connections.testexternal'); - define('DB_HOST', $config['host']); - define('DB_NAME', $config['database']); - define('DB_USER', $config['username']); - define('DB_PASS', $config['password']); + parent::setUp(); } /** - * It tests the initLaravel method - * + * It tests the initLaravel method. * @test + * @covers \ProcessMaker\Core\System::initLaravel() */ public function it_should_init_laravel_configurations() { @@ -36,4 +33,30 @@ class SystemTest extends TestCase $this->assertEquals(DB_USER, config('database.connections.workflow.username')); $this->assertEquals(DB_PASS, config('database.connections.workflow.password')); } -} \ No newline at end of file + + /** + * This gets the settings for sending email. + * @test + * @covers \ProcessMaker\Core\System::getEmailConfiguration() + */ + public function it_should_get_email_configuration() + { + $system = new System(); + + //default values + EmailServerModel::truncate(); + $actual = $system->getEmailConfiguration(); + $this->assertEmpty($actual); + + //new instance + $emailServer = factory(EmailServerModel::class)->create([ + 'MESS_DEFAULT' => 1 + ]); + $actual = $system->getEmailConfiguration(); + $this->assertNotEmpty($actual); + $this->assertArrayHasKey('MESS_ENGINE', $actual); + $this->assertArrayHasKey('OAUTH_CLIENT_ID', $actual); + $this->assertArrayHasKey('OAUTH_CLIENT_SECRET', $actual); + $this->assertArrayHasKey('OAUTH_REFRESH_TOKEN', $actual); + } +} diff --git a/tests/unit/workflow/engine/src/ProcessMaker/Model/EmailServerModelTest.php b/tests/unit/workflow/engine/src/ProcessMaker/Model/EmailServerModelTest.php index 5d849a46d..9d50100e5 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/Model/EmailServerModelTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/Model/EmailServerModelTest.php @@ -86,4 +86,4 @@ class EmailServerModelTest extends TestCase //Assert the result es not empty $this->assertNotEmpty($res); } -} \ No newline at end of file +} diff --git a/workflow/engine/src/ProcessMaker/Model/AppMessage.php b/workflow/engine/src/ProcessMaker/Model/AppMessage.php new file mode 100644 index 000000000..1eef530c4 --- /dev/null +++ b/workflow/engine/src/ProcessMaker/Model/AppMessage.php @@ -0,0 +1,12 @@ +