From 2936c397aafc06c889f9dea411189f62f70dcbc8 Mon Sep 17 00:00:00 2001 From: Touhidur Rahman Date: Wed, 24 Apr 2024 17:47:48 +0600 Subject: [PATCH] pkp/pkp-lib#5885 test added for queue job --- .../invitations/ReviewerAccessInvite.php | 2 +- classes/task/ReviewReminder.php | 2 +- jobs/email/ReviewReminder.php | 12 +- tests/PKPTestCase.php | 5 +- tests/jobs/email/ReviewReminderTest.php | 168 ++++++++++++++++++ 5 files changed, 181 insertions(+), 8 deletions(-) create mode 100644 tests/jobs/email/ReviewReminderTest.php diff --git a/classes/invitation/invitations/ReviewerAccessInvite.php b/classes/invitation/invitations/ReviewerAccessInvite.php index 97c1a165dfb..3951f32fd03 100644 --- a/classes/invitation/invitations/ReviewerAccessInvite.php +++ b/classes/invitation/invitations/ReviewerAccessInvite.php @@ -23,7 +23,7 @@ use PKP\invitation\invitations\enums\InvitationStatus; use PKP\mail\variables\ReviewAssignmentEmailVariable; use PKP\security\Validation; -use ReviewAssignment; +use PKP\submission\reviewAssignment\ReviewAssignment; class ReviewerAccessInvite extends BaseInvitation { diff --git a/classes/task/ReviewReminder.php b/classes/task/ReviewReminder.php index 45151cfe190..9de333feeb0 100644 --- a/classes/task/ReviewReminder.php +++ b/classes/task/ReviewReminder.php @@ -146,7 +146,7 @@ public function executeActions() } if ($mailable) { - ReviewReminderJob::dispatch($reviewAssignment->getId(), $mailable); + ReviewReminderJob::dispatch($context->getId(), $reviewAssignment->getId(), $mailable); } } diff --git a/jobs/email/ReviewReminder.php b/jobs/email/ReviewReminder.php index 07a5499255f..dd861b6aff1 100644 --- a/jobs/email/ReviewReminder.php +++ b/jobs/email/ReviewReminder.php @@ -29,7 +29,11 @@ class ReviewReminder extends BaseJob { - public function __construct(public int $reviewAssignmentId, public string $mailableClass) + public function __construct( + public int $contextId, + public int $reviewAssignmentId, + public string $mailableClass + ) { parent::__construct(); } @@ -41,7 +45,7 @@ public function handle(): void { $reviewAssignment = Repo::reviewAssignment()->get($this->reviewAssignmentId); $reviewer = Repo::user()->get($reviewAssignment->getReviewerId()); - + if (!isset($reviewer)) { return; } @@ -49,7 +53,7 @@ public function handle(): void $submission = Repo::submission()->get($reviewAssignment->getData('submissionId')); $contextService = Services::get('context'); - $context = $contextService->get($submission->getData('contextId')); + $context = $contextService->get($this->contextId); /** @var ReviewRemindAuto|ReviewResponseRemindAuto $mailable */ $mailable = new $this->mailableClass($context, $submission, $reviewAssignment); @@ -59,6 +63,7 @@ public function handle(): void $context->getId(), $mailable::getEmailTemplateKey() ); + $mailable->subject($emailTemplate->getLocalizedData('subject', $primaryLocale)) ->body($emailTemplate->getLocalizedData('body', $primaryLocale)) ->from($context->getData('contactEmail'), $context->getData('contactName')) @@ -67,6 +72,7 @@ public function handle(): void $mailable->setData($primaryLocale); $reviewerAccessKeysEnabled = $context->getData('reviewerAccessKeysEnabled'); + if ($reviewerAccessKeysEnabled) { // Give one-click access if enabled $reviewInvitation = new ReviewerAccessInvite( $reviewAssignment->getReviewerId(), diff --git a/tests/PKPTestCase.php b/tests/PKPTestCase.php index daa08d13cb1..f17b6adc4d3 100644 --- a/tests/PKPTestCase.php +++ b/tests/PKPTestCase.php @@ -181,7 +181,7 @@ protected function setTestConfiguration($config, $configPath = 'config') * * @return Request */ - protected function mockRequest($path = 'index/test-page/test-op', $userId = null) + protected function mockRequest(string $path = 'index/test-page/test-op', int $userId = 0) { // Back up the default request. if (!isset($this->registryBackup['request'])) { @@ -205,8 +205,7 @@ protected function mockRequest($path = 'index/test-page/test-op', $userId = null $request->setRouter($router); // Test user. - $session = $request->getSession(); - $session->setUserId($userId); + $request->getSessionGuard()->setUserId($userId); return $request; } diff --git a/tests/jobs/email/ReviewReminderTest.php b/tests/jobs/email/ReviewReminderTest.php new file mode 100644 index 00000000000..8ec7f41e481 --- /dev/null +++ b/tests/jobs/email/ReviewReminderTest.php @@ -0,0 +1,168 @@ +assertInstanceOf(ReviewReminder::class, unserialize($this->serializedJobData)); + } + + /** + * Test job will not fail when no reviewer associated with review assignment + */ + public function testJobWillRunWithIfNoReviewerExists(): void + { + $reviewReminderJob = unserialize($this->serializedJobData); + + $reviewAssignmentMock = Mockery::mock(ReviewAssignment::class) + ->shouldReceive([ + 'getReviewerId' => 0, + 'getData' => 0, + 'getSubmissionId' => 0, + 'getRound' => 0, + 'getReviewMethod' => '', + 'getRecommendation' => '', + 'getReviewerFullName' => '', + 'getId' => 0, + 'getDateResponseDue' => \Carbon\Carbon::now()->format('Y-m-d H:i:s'), + 'getDateAssigned' => \Carbon\Carbon::now()->format('Y-m-d H:i:s'), + 'getDateDue' => \Carbon\Carbon::now()->format('Y-m-d H:i:s'), + ]) + ->withAnyArgs() + ->getMock(); + + app()->instance(ReviewAssignment::class, $reviewAssignmentMock); + + $reviewAssignmentRepoMock = Mockery::mock(app(ReviewAssignmentRepository::class)) + ->makePartial() + ->shouldReceive([ + 'get' => $reviewAssignmentMock, + 'edit' => null, + ]) + ->withAnyArgs() + ->getMock(); + + app()->instance(ReviewAssignmentRepository::class, $reviewAssignmentRepoMock); + + $this->assertNull($reviewReminderJob->handle()); + } + + /** + * Test job will not fail + */ + public function testRunSerializedJob(): void + { + Mail::fake(); + + $reviewReminderJob = unserialize($this->serializedJobData); + + // need to mock request so that a valid context information is set and can be retrived + $contextService = Services::get("context"); + $context = $contextService->get($reviewReminderJob->contextId); + $this->mockRequest($context->getPath() . '/test-page/test-op'); + + $publicationMock = Mockery::mock(\APP\publication\Publication::class) + ->makePartial() + ->shouldReceive('getData') + ->with('authors') + ->andReturn(\Illuminate\Support\LazyCollection::make([new \PKP\author\Author()])) + ->getMock(); + + $submissionMock = Mockery::mock(\APP\submission\Submission::class) + ->makePartial() + ->shouldReceive([ + 'getId' => 0, + 'getData' => 0, + 'getCurrentPublication' => $publicationMock + ]) + ->withAnyArgs() + ->getMock(); + + $submissionRepoMock = Mockery::mock(app(SubmissionRepository::class)) + ->makePartial() + ->shouldReceive('get') + ->withAnyArgs() + ->andReturn($submissionMock) + ->getMock(); + + app()->instance(SubmissionRepository::class, $submissionRepoMock); + + $emailTemplateMock = Mockery::mock(\PKP\emailTemplate\EmailTemplate::class) + ->makePartial() + ->shouldReceive([ + "getLocalizedData" => "" + ]) + ->withAnyArgs() + ->getMock(); + + $emailTemplateRepoMock = Mockery::mock(app(EmailTemplateRepository::class)) + ->makePartial() + ->shouldReceive([ + 'getByKey' => $emailTemplateMock, + ]) + ->withAnyArgs() + ->getMock(); + + app()->instance(EmailTemplateRepository::class, $emailTemplateRepoMock); + + $invitationRepoMock = Mockery::mock(app(InvitationRepository::class)) + ->makePartial() + ->shouldReceive([ + 'addInvitation' => 0, + 'getMailable' => null, + ]) + ->withAnyArgs() + ->getMock(); + + app()->instance(InvitationRepository::class, $invitationRepoMock); + + $eventRepoMock = Mockery::mock(app(EventRepository::class)) + ->makePartial() + ->shouldReceive([ + 'newDataObject' => new \PKP\log\event\EventLogEntry, + 'add' => 0, + ]) + ->withAnyArgs() + ->getMock(); + + app()->instance(EventRepository::class, $eventRepoMock); + + $this->assertNull($reviewReminderJob->handle()); + } +}