Skip to content

Commit

Permalink
pkp#9899 Job unit tests patch (pkp#10368)
Browse files Browse the repository at this point in the history
* pkp#9899 job unit tests patch fix

* pkp#9899 Updated StatisticsReportNotifyTest to have app specific serialized data

* pkp#9899 Updated StatisticsReportNotifyTest to have OPS specific serialized data

* pkp#9899 job mocking for submission deposition test

* pkp#9899 updated mock class implementation for doi registration

* pkp#9899 Added test skipping for OMP

* pkp#9899 unused class import cleanup

* pkp#9899 removed direct mocking of job class

* pkp#9899 fixed PHPUnit deprecation warning

* pkp#9899 notification mocks added

* pkp#9899 added backup/restore for dao and container keys

* pkp#9899 fixed usage stats file create/update issue for tests
  • Loading branch information
touhidurabir authored and Hafsa-Naeem committed Dec 11, 2024
1 parent 3421592 commit 7122bbc
Show file tree
Hide file tree
Showing 21 changed files with 604 additions and 41 deletions.
13 changes: 12 additions & 1 deletion tests/PKPTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

use APP\core\Application;
use Illuminate\Support\Facades\Mail;

use APP\core\PageRouter;
use APP\core\Request;
use Mockery;
Expand Down Expand Up @@ -257,6 +256,18 @@ protected function localeToRegExp(string $translation): string
return '/^' . implode('.*?', $escapedPieces) . '$/u';
}

/**
* Get the app specific search dao based on application name
*/
protected function getAppSearchDaoKey(): string
{
return match(Application::get()->getName()) {
'ojs2' => 'ArticleSearchDAO',
'omp' => 'MonographSearchDAO',
'ops' => 'PreprintSearchDAO',
};
}

/**
* Mock the mail facade
* @see https://laravel.com/docs/10.x/mocking
Expand Down
6 changes: 4 additions & 2 deletions tests/classes/filter/FilterDAOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace PKP\tests\classes\filter;

use PKP\db\DAORegistry;
use PKP\filter\PersistableFilter;
use PKP\filter\FilterDAO;
use PKP\filter\FilterGroup;
use PKP\filter\FilterGroupDAO;
Expand Down Expand Up @@ -48,7 +49,8 @@ protected function setUp(): void
$someGroup->setInputType('primitive::string');
$someGroup->setOutputType('primitive::string');
$filterGroupDao = DAORegistry::getDAO('FilterGroupDAO'); /** @var FilterGroupDAO $filterGroupDao */
self::assertTrue(is_integer($filterGroupId = $filterGroupDao->insertObject($someGroup)));
$filterGroupId = $filterGroupDao->insertObject($someGroup);
self::assertTrue(is_integer($filterGroupId));
}

/**
Expand All @@ -61,7 +63,7 @@ public function testFilterCrud()
// Install a test filter object.
$settings = ['seq' => '1', 'some-key' => 'some-value'];
$testFilter = $filterDao->configureObject(PersistableTestFilter::class, 'test-filter-group', $settings, false, 1);
self::assertInstanceOf('PersistableFilter', $testFilter);
self::assertInstanceOf(PersistableFilter::class, $testFilter);
$filterId = $testFilter->getId();
self::assertTrue(is_integer($filterId));

Expand Down
14 changes: 13 additions & 1 deletion tests/jobs/bulk/BulkEmailSenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use PKP\tests\PKPTestCase;
use PKP\jobs\bulk\BulkEmailSender;
use PKP\user\Collector as UserCollector;
use APP\user\Repository as UserRepository;
use PKP\user\Repository as UserRepository;

/**
* @runTestsInSeparateProcesses
Expand All @@ -32,6 +32,18 @@ class BulkEmailSenderTest extends PKPTestCase
O:29:"PKP\\jobs\\bulk\BulkEmailSender":9:{s:10:"\0*\0userIds";a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}s:12:"\0*\0contextId";i:1;s:10:"\0*\0subject";s:12:"Test subject";s:7:"\0*\0body";s:16:"<p>Test body</p>";s:12:"\0*\0fromEmail";s:20:"rvaca@mailinator.com";s:11:"\0*\0fromName";s:11:"Ramiro Vaca";s:10:"connection";s:8:"database";s:5:"queue";s:5:"queue";s:7:"batchId";s:36:"9c1cbc05-017b-4a02-bd5a-b113c92a7735";}
END;

/**
* @see PKPTestCase::getMockedContainerKeys()
*/
protected function getMockedContainerKeys(): array
{
return [
...parent::getMockedContainerKeys(),
UserCollector::class,
UserRepository::class,
];
}

/**
* Test job is a proper instance
*/
Expand Down
29 changes: 29 additions & 0 deletions tests/jobs/doi/DepositContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ class DepositContextTest extends PKPTestCase
O:27:"PKP\\jobs\\doi\\DepositContext":3:{s:12:"\0*\0contextId";i:1;s:10:"connection";s:8:"database";s:5:"queue";s:5:"queue";}
END;

/**
* @see PKPTestCase::getMockedDAOs()
*/
protected function getMockedDAOs(): array
{
return array_filter([
...parent::getMockedDAOs(),
substr(strrchr(get_class(Application::getContextDAO()), '\\'), 1),
]);
}

/**
* @see PKPTestCase::getMockedContainerKeys()
*/
protected function getMockedContainerKeys(): array
{
return [
...parent::getMockedContainerKeys(),
DoiRepository::class,
];
}

/**
* Test job is a proper instance
*/
Expand All @@ -55,11 +77,18 @@ public function testRunSerializedJob(): void

$contextDaoClass = get_class(Application::getContextDAO());

/**
* @disregard P1013 PHP Intelephense error suppression
* @see https://github.com/bmewburn/vscode-intelephense/issues/568
*/
$contextMock = Mockery::mock(get_class(Application::getContextDAO()->newDataObject()))
->makePartial()
->shouldReceive('getData')
->with(Context::SETTING_DOI_AUTOMATIC_DEPOSIT)
->andReturn(true)
->shouldReceive('getLocalizedData')
->withAnyArgs()
->andReturn('')
->getMock();

$contextDaoMock = Mockery::mock($contextDaoClass)
Expand Down
72 changes: 61 additions & 11 deletions tests/jobs/doi/DepositSubmissionTest.php

Large diffs are not rendered by default.

60 changes: 58 additions & 2 deletions tests/jobs/email/EditorialReminderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use PKP\facades\Locale;
use PKP\tests\PKPTestCase;
use PKP\jobs\email\EditorialReminder;
use APP\user\Repository as UserRepository;
use PKP\user\Repository as UserRepository;
use PKP\submission\reviewRound\ReviewRound;
use APP\submission\Collector as SubmissionCollector;
use APP\submission\Repository as SubmissionRepository;
Expand All @@ -37,6 +37,33 @@ class EditorialReminderTest extends PKPTestCase
O:32:"PKP\\jobs\\email\\EditorialReminder":4:{s:11:"\0*\0editorId";i:2;s:12:"\0*\0contextId";i:1;s:10:"connection";s:8:"database";s:5:"queue";s:5:"queue";}
END;

/**
* @see PKPTestCase::getMockedDAOs()
*/
protected function getMockedDAOs(): array
{
return [
...parent::getMockedDAOs(),
'NotificationSubscriptionSettingsDAO',
'ReviewRoundDAO',
'NotificationDAO',
'NotificationSettingsDAO',
];
}

/**
* @see PKPTestCase::getMockedContainerKeys()
*/
protected function getMockedContainerKeys(): array
{
return [
...parent::getMockedContainerKeys(),
UserRepository::class,
SubmissionCollector::class,
SubmissionRepository::class,
];
}

/**
* Test job is a proper instance
*/
Expand Down Expand Up @@ -76,7 +103,7 @@ public function testRunSerializedJob(): void

// Need to replace the container binding of `context` with a mock object
\APP\core\Services::register(
new class extends \APP\services\OJSServiceProvider
new class implements \Pimple\ServiceProviderInterface
{
public function register(\Pimple\Container $pimple)
{
Expand Down Expand Up @@ -217,6 +244,35 @@ public function register(\Pimple\Container $pimple)

app()->instance(EmailTemplateRepository::class, $emailTemplateRepoMock);

$notificationMock = Mockery::mock(\APP\notification\Notification::class)
->makePartial()
->shouldReceive([
'setData' => null,
'getContextId' => 0,
])
->withAnyArgs()
->getMock();

$notifiactionDaoMock = Mockery::mock(\PKP\notification\NotificationDAO::class)
->makePartial()
->shouldReceive([
'newDataObject' => $notificationMock,
'insertObject' => 0,
])
->withAnyArgs()
->getMock();

DAORegistry::registerDAO('NotificationDAO', $notifiactionDaoMock);

$notificationSettingsDaoMock = Mockery::mock(\PKP\notification\NotificationSettingsDAO::class)
->makePartial()
->shouldReceive('updateNotificationSetting')
->withAnyArgs()
->andReturn(null)
->getMock();

DAORegistry::registerDAO('NotificationSettingsDAO', $notificationSettingsDaoMock);

$this->assertNull($editorialReminderJob->handle());
}
}
27 changes: 24 additions & 3 deletions tests/jobs/metadata/BatchMetadataChangedJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use Mockery;
use PKP\db\DAORegistry;
use APP\core\Application;
use PKP\tests\PKPTestCase;
use PKP\jobs\metadata\BatchMetadataChangedJob;
use APP\submission\Repository as SubmissionRepository;
Expand All @@ -32,6 +33,28 @@ class BatchMetadataChangedJobTest extends PKPTestCase
O:41:"PKP\\jobs\\metadata\\BatchMetadataChangedJob":3:{s:13:"submissionIds";a:2:{i:0;i:1;i:1;i:2;}s:10:"connection";s:8:"database";s:5:"queue";s:5:"queue";}
END;

/**
* @see PKPTestCase::getMockedDAOs()
*/
protected function getMockedDAOs(): array
{
return [
...parent::getMockedDAOs(),
$this->getAppSearchDaoKey(),
];
}

/**
* @see PKPTestCase::getMockedContainerKeys()
*/
protected function getMockedContainerKeys(): array
{
return [
...parent::getMockedContainerKeys(),
SubmissionRepository::class,
];
}

/**
* Test job is a proper instance
*/
Expand Down Expand Up @@ -99,9 +122,7 @@ public function testRunSerializedJob(): void
->withAnyArgs()
->getMock();

DAORegistry::registerDAO('ArticleSearchDAO', $submissionSearchDAOMock); // for OJS
DAORegistry::registerDAO('MonographSearchDAO', $submissionSearchDAOMock); // for OMP
DAORegistry::registerDAO('PreprintSearchDAO', $submissionSearchDAOMock); // for OPS
DAORegistry::registerDAO($this->getAppSearchDaoKey(), $submissionSearchDAOMock);

$this->assertNull($batchMetadataChangedJob->handle());
}
Expand Down
27 changes: 24 additions & 3 deletions tests/jobs/metadata/MetadataChangedJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use Mockery;
use PKP\db\DAORegistry;
use APP\core\Application;
use PKP\tests\PKPTestCase;
use PKP\jobs\metadata\MetadataChangedJob;
use APP\submission\Repository as SubmissionRepository;
Expand All @@ -32,6 +33,28 @@ class MetadataChangedJobTest extends PKPTestCase
O:36:"PKP\\jobs\\metadata\\MetadataChangedJob":3:{s:15:"\0*\0submissionId";i:24;s:10:"connection";s:8:"database";s:5:"queue";s:5:"queue";}
END;

/**
* @see PKPTestCase::getMockedDAOs()
*/
protected function getMockedDAOs(): array
{
return [
...parent::getMockedDAOs(),
$this->getAppSearchDaoKey(),
];
}

/**
* @see PKPTestCase::getMockedContainerKeys()
*/
protected function getMockedContainerKeys(): array
{
return [
...parent::getMockedContainerKeys(),
SubmissionRepository::class,
];
}

/**
* Test job is a proper instance
*/
Expand Down Expand Up @@ -99,9 +122,7 @@ public function testRunSerializedJob(): void
->withAnyArgs()
->getMock();

DAORegistry::registerDAO('ArticleSearchDAO', $submissionSearchDAOMock); // for OJS
DAORegistry::registerDAO('MonographSearchDAO', $submissionSearchDAOMock); // for OMP
DAORegistry::registerDAO('PreprintSearchDAO', $submissionSearchDAOMock); // for OPS
DAORegistry::registerDAO($this->getAppSearchDaoKey(), $submissionSearchDAOMock);

$this->assertNull($metadataChangedJob->handle());
}
Expand Down
58 changes: 57 additions & 1 deletion tests/jobs/notifications/NewAnnouncementNotifyUsersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use PKP\db\DAORegistry;
use APP\core\Application;
use PKP\tests\PKPTestCase;
use APP\user\Repository as UserRepository;
use PKP\user\Repository as UserRepository;
use PKP\jobs\notifications\NewAnnouncementNotifyUsers;
use PKP\announcement\Repository as AnnouncementRepository;
use PKP\emailTemplate\Repository as EmailTemplateRepository;
Expand All @@ -35,6 +35,32 @@ class NewAnnouncementNotifyUsersTest extends PKPTestCase
O:49:"PKP\\jobs\\notifications\\NewAnnouncementNotifyUsers":7:{s:15:"\0*\0recipientIds";O:29:"Illuminate\Support\Collection":2:{s:8:"\0*\0items";a:3:{i:0;i:2;i:1;i:3;i:2;i:4;}s:28:"\0*\0escapeWhenCastingToString";b:0;}s:12:"\0*\0contextId";i:1;s:17:"\0*\0announcementId";i:1;s:9:"\0*\0locale";s:2:"en";s:9:"\0*\0sender";O:13:"PKP\user\User":7:{s:5:"_data";a:22:{s:2:"id";i:1;s:8:"userName";s:5:"admin";s:8:"password";s:60:"$2y$10\$uFmYXg8/Ufa0HbskyW57Be22stFGY5qtxJZmTOae3PfDB86V3x7BW";s:5:"email";s:23:"pkpadmin@mailinator.com";s:3:"url";N;s:5:"phone";N;s:14:"mailingAddress";N;s:14:"billingAddress";N;s:7:"country";N;s:7:"locales";a:0:{}s:6:"gossip";N;s:13:"dateLastEmail";N;s:14:"dateRegistered";s:19:"2023-02-28 20:19:07";s:13:"dateValidated";N;s:13:"dateLastLogin";s:19:"2024-05-22 19:05:03";s:18:"mustChangePassword";N;s:7:"authStr";N;s:8:"disabled";b:0;s:14:"disabledReason";N;s:10:"inlineHelp";b:1;s:10:"familyName";a:1:{s:2:"en";s:5:"admin";}s:9:"givenName";a:1:{s:2:"en";s:5:"admin";}}s:20:"_hasLoadableAdapters";b:0;s:27:"_metadataExtractionAdapters";a:0:{}s:25:"_extractionAdaptersLoaded";b:0;s:26:"_metadataInjectionAdapters";a:0:{}s:24:"_injectionAdaptersLoaded";b:0;s:9:"\0*\0_roles";a:0:{}}s:10:"connection";s:8:"database";s:5:"queue";s:5:"queue";}
END;

/**
* @see PKPTestCase::getMockedDAOs()
*/
protected function getMockedDAOs(): array
{
return array_filter([
...parent::getMockedDAOs(),
substr(strrchr(get_class(Application::getContextDAO()), '\\'), 1),
'NotificationDAO',
'NotificationSettingsDAO',
]);
}

/**
* @see PKPTestCase::getMockedContainerKeys()
*/
protected function getMockedContainerKeys(): array
{
return [
...parent::getMockedContainerKeys(),
AnnouncementRepository::class,
EmailTemplateRepository::class,
UserRepository::class,
];
}

/**
* Test job is a proper instance
*/
Expand Down Expand Up @@ -82,6 +108,7 @@ public function testRunSerializedJob(): void
->makePartial()
->shouldReceive([
'getId' => 0,
'getLocalizedData' => '',
])
->withAnyArgs()
->getMock();
Expand Down Expand Up @@ -122,6 +149,35 @@ public function testRunSerializedJob(): void

app()->instance(UserRepository::class, $userRepoMock);

$notificationMock = Mockery::mock(\APP\notification\Notification::class)
->makePartial()
->shouldReceive([
'setData' => null,
'getContextId' => 0,
])
->withAnyArgs()
->getMock();

$notifiactionDaoMock = Mockery::mock(\PKP\notification\NotificationDAO::class)
->makePartial()
->shouldReceive([
'newDataObject' => $notificationMock,
'insertObject' => 0,
])
->withAnyArgs()
->getMock();

DAORegistry::registerDAO('NotificationDAO', $notifiactionDaoMock);

$notificationSettingsDaoMock = Mockery::mock(\PKP\notification\NotificationSettingsDAO::class)
->makePartial()
->shouldReceive('updateNotificationSetting')
->withAnyArgs()
->andReturn(null)
->getMock();

DAORegistry::registerDAO('NotificationSettingsDAO', $notificationSettingsDaoMock);

$this->assertNull($newAnnouncementNotifyUsersJob->handle());
}
}
Loading

0 comments on commit 7122bbc

Please sign in to comment.