Skip to content

Commit

Permalink
#5885 test added for queue job
Browse files Browse the repository at this point in the history
  • Loading branch information
touhidurabir committed Apr 24, 2024
1 parent ca28c9a commit 2936c39
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 8 deletions.
2 changes: 1 addition & 1 deletion classes/invitation/invitations/ReviewerAccessInvite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion classes/task/ReviewReminder.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function executeActions()
}

if ($mailable) {
ReviewReminderJob::dispatch($reviewAssignment->getId(), $mailable);
ReviewReminderJob::dispatch($context->getId(), $reviewAssignment->getId(), $mailable);
}
}

Expand Down
12 changes: 9 additions & 3 deletions jobs/email/ReviewReminder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -41,15 +45,15 @@ public function handle(): void
{
$reviewAssignment = Repo::reviewAssignment()->get($this->reviewAssignmentId);
$reviewer = Repo::user()->get($reviewAssignment->getReviewerId());

if (!isset($reviewer)) {
return;
}

$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);
Expand All @@ -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'))
Expand All @@ -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(),
Expand Down
5 changes: 2 additions & 3 deletions tests/PKPTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand All @@ -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;
}
Expand Down
168 changes: 168 additions & 0 deletions tests/jobs/email/ReviewReminderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php

/**
* @file tests/jobs/email/ReviewReminderTest.php
*
* Copyright (c) 2014-2024 Simon Fraser University
* Copyright (c) 2000-2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @brief Tests for review response and submit due reminder job.
*/

namespace PKP\tests\classes\core;

use Mockery;
use APP\core\Services;
use PKP\tests\PKPTestCase;
use PKP\jobs\email\ReviewReminder;
use Illuminate\Support\Facades\Mail;
use PKP\submission\reviewAssignment\ReviewAssignment;
use APP\submission\Repository as SubmissionRepository;
use PKP\emailTemplate\Repository as EmailTemplateRepository;
use PKP\invitation\repositories\Invitation as InvitationRepository;
use PKP\submission\reviewAssignment\Repository as ReviewAssignmentRepository;
use PKP\log\event\Repository as EventRepository;

/**
* @runTestsInSeparateProcesses
* @see https://docs.phpunit.de/en/9.6/annotations.html#runtestsinseparateprocesses
*/
class ReviewReminderTest extends PKPTestCase
{
/**
* Serializion from OJS 3.5.0
*/
protected string $serializedJobData = 'O:29:"PKP\jobs\email\ReviewReminder":5:{s:9:"contextId";i:1;s:18:"reviewAssignmentId";i:57;s:13:"mailableClass";s:43:"PKP\mail\mailables\ReviewResponseRemindAuto";s:10:"connection";s:8:"database";s:5:"queue";s:5:"queue";}';

/**
* Test job is a proper instance
*/
public function testUnserializationGetProperReviewReminderJobInstance(): void
{
$this->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());
}
}

0 comments on commit 2936c39

Please sign in to comment.