diff --git a/tests/unit/workflow/engine/src/ProcessMaker/TaskScheduler/TaskTest.php b/tests/unit/workflow/engine/src/ProcessMaker/TaskScheduler/TaskTest.php index b493b3a22..0634e1856 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/TaskScheduler/TaskTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/TaskScheduler/TaskTest.php @@ -308,4 +308,34 @@ class TaskTest extends TestCase Queue::assertPushed(TaskScheduler::class); } } + + /** + * This test verify the fillReportByUser activity method for synchronous and asynchronous execution. + * @test + * @covers ProcessMaker\TaskScheduler\Task::runTask() + * @covers ProcessMaker\TaskScheduler\Task::fillReportByUser() + * @dataProvider asynchronousCases + */ + public function it_should_test_fillReportByUser_method($asynchronous) + { + $task = new Task($asynchronous, ''); + $dateInit = $this->faker->dateTime; + $dateFinish = $this->faker->dateTime; + + //assert synchronous for cron file + if ($asynchronous === false) { + ob_start(); + $task->fillReportByUser($dateInit, $dateFinish); + $printing = ob_get_clean(); + $this->assertRegExp("/User Reporting/", $printing); + } + + //assert asynchronous for job process + if ($asynchronous === true) { + Queue::fake(); + Queue::assertNothingPushed(); + $task->fillReportByUser($dateInit, $dateFinish); + Queue::assertPushed(TaskScheduler::class); + } + } } diff --git a/workflow/engine/bin/cron_single.php b/workflow/engine/bin/cron_single.php index 0bd6532f7..505e1724e 100644 --- a/workflow/engine/bin/cron_single.php +++ b/workflow/engine/bin/cron_single.php @@ -317,7 +317,9 @@ try { $task->executePlugins(); } /*----------------------------------********---------------------------------*/ - fillReportByUser(); + if (empty($argvx) || strpos($argvx, "report_by_user") !== false) { + $task->fillReportByUser($dateInit, $dateFinish); + } fillReportByProcess(); synchronizeDrive(); synchronizeGmailLabels(); @@ -587,36 +589,6 @@ function setExecutionResultMessage($m, $t = '') } /*----------------------------------********---------------------------------*/ -function fillReportByUser() -{ - try { - global $argvx; - global $dateInit; - global $dateFinish; - - if (strpos($argvx, "report_by_user") === false) { - return false; - } - if ($dateInit == null) { - eprintln("You must enter the starting date.", "red"); - eprintln('Example: +init-date"YYYY-MM-DD HH:MM:SS" +finish-date"YYYY-MM-DD HH:MM:SS"', "red"); - return false; - } - - $dateFinish = ($dateFinish != null) ? $dateFinish : date("Y-m-d H:i:s"); - - $appcv = new AppCacheView(); - $appcv->setPathToAppCacheFiles(PATH_METHODS . 'setup' . PATH_SEP . 'setupSchemas' . PATH_SEP); - setExecutionMessage("Calculating data to fill the 'User Reporting'..."); - $appcv->fillReportByUser($dateInit, $dateFinish); - setExecutionResultMessage("DONE"); - } catch (Exception $e) { - setExecutionResultMessage("WITH ERRORS", "error"); - eprintln(" '-" . $e->getMessage(), "red"); - saveLog("fillReportByUser", "error", "Error in fill report by user: " . $e->getMessage()); - } -} - function fillReportByProcess() { try { diff --git a/workflow/engine/src/ProcessMaker/TaskScheduler/Task.php b/workflow/engine/src/ProcessMaker/TaskScheduler/Task.php index bd3033931..0d36bde95 100644 --- a/workflow/engine/src/ProcessMaker/TaskScheduler/Task.php +++ b/workflow/engine/src/ProcessMaker/TaskScheduler/Task.php @@ -5,6 +5,7 @@ namespace ProcessMaker\TaskScheduler; use Application; use AppAssignSelfServiceValueGroupPeer; use AppAssignSelfServiceValuePeer; +use AppCacheView; use AppDelegation; use App\Jobs\TaskScheduler; use Bootstrap; @@ -392,4 +393,43 @@ class Task } } } + + /** + * This fills the report by user. + * @param datetime $dateInit + * @param datetime $dateFinish + */ + public function fillReportByUser($dateInit, $dateFinish) + { + $job = function() use($dateInit, $dateFinish) { + try { + if ($dateInit == null) { + if ($this->asynchronous === false) { + eprintln("You must enter the starting date.", "red"); + eprintln('Example: +init-date"YYYY-MM-DD HH:MM:SS" +finish-date"YYYY-MM-DD HH:MM:SS"', "red"); + } + if ($this->asynchronous === true) { + $message = 'You must enter the starting date. Example: +init-date"YYYY-MM-DD HH:MM:SS" +finish-date"YYYY-MM-DD HH:MM:SS"'; + Log::channel('taskScheduler:taskScheduler')->info($message, Bootstrap::context($context)); + } + return false; + } + + $dateFinish = ($dateFinish != null) ? $dateFinish : date("Y-m-d H:i:s"); + + $appCacheView = new AppCacheView(); + $appCacheView->setPathToAppCacheFiles(PATH_METHODS . 'setup' . PATH_SEP . 'setupSchemas' . PATH_SEP); + $this->setExecutionMessage("Calculating data to fill the 'User Reporting'..."); + $appCacheView->fillReportByUser($dateInit, $dateFinish); + setExecutionResultMessage("DONE"); + } catch (Exception $e) { + $this->setExecutionResultMessage("WITH ERRORS", "error"); + if ($this->asynchronous === false) { + eprintln(" '-" . $e->getMessage(), "red"); + } + $this->saveLog("fillReportByUser", "error", "Error in fill report by user: " . $e->getMessage()); + } + }; + $this->runTask($job); + } }