diff --git a/lib/pkp b/lib/pkp index 72087f4dab..5bba13a0c6 160000 --- a/lib/pkp +++ b/lib/pkp @@ -1 +1 @@ -Subproject commit 72087f4dabb1e80629b33639e3568501fd309fe2 +Subproject commit 5bba13a0c60efa569e1ab6fb95e15998015a0a8c diff --git a/tests/jobs/.gitkeep b/tests/jobs/.gitkeep deleted file mode 100644 index ca99fa2374..0000000000 --- a/tests/jobs/.gitkeep +++ /dev/null @@ -1 +0,0 @@ -Placeholder while we have no tests here so git will check out the directory -- keeps phpunit from complaining diff --git a/tests/jobs/statistics/CompileCounterSubmissionDailyMetricsTest.php b/tests/jobs/statistics/CompileCounterSubmissionDailyMetricsTest.php new file mode 100644 index 0000000000..4c2405c152 --- /dev/null +++ b/tests/jobs/statistics/CompileCounterSubmissionDailyMetricsTest.php @@ -0,0 +1,88 @@ +assertInstanceOf( + CompileCounterSubmissionDailyMetrics::class, + unserialize($this->serializedJobData) + ); + } + + /** + * Ensure that a serialized job can be unserialized and executed + */ + public function testRunSerializedJob(): void + { + /** @var CompileCounterSubmissionDailyMetrics $compileCounterSubmissionDailyMetricsJob */ + $compileCounterSubmissionDailyMetricsJob = unserialize($this->serializedJobData); + + $temporaryTotalsDAOMock = Mockery::mock(\APP\statistics\TemporaryTotalsDAO::class) + ->makePartial() + ->shouldReceive([ + 'deleteCounterSubmissionDailyByLoadId' => null, + 'compileCounterSubmissionDailyMetrics' => null, + ]) + ->withAnyArgs() + ->getMock(); + + DAORegistry::registerDAO('TemporaryTotalsDAO', $temporaryTotalsDAOMock); + + $temporaryItemInvestigationsDAOMock = Mockery::mock(\APP\statistics\TemporaryItemInvestigationsDAO::class) + ->makePartial() + ->shouldReceive([ + 'compileCounterSubmissionDailyMetrics' => null, + ]) + ->withAnyArgs() + ->getMock(); + + DAORegistry::registerDAO('TemporaryItemInvestigationsDAO', $temporaryItemInvestigationsDAOMock); + + $temporaryItemRequestsDAOMock = Mockery::mock(\APP\statistics\TemporaryItemRequestsDAO::class) + ->makePartial() + ->shouldReceive([ + 'compileCounterSubmissionDailyMetrics' => null, + ]) + ->withAnyArgs() + ->getMock(); + + DAORegistry::registerDAO('TemporaryItemRequestsDAO', $temporaryItemRequestsDAOMock); + + + $compileCounterSubmissionDailyMetricsJob->handle(); + + $this->expectNotToPerformAssertions(); + } +} diff --git a/tests/jobs/statistics/CompileCounterSubmissionInstitutionDailyMetricsTest.php b/tests/jobs/statistics/CompileCounterSubmissionInstitutionDailyMetricsTest.php new file mode 100644 index 0000000000..304acb7d6b --- /dev/null +++ b/tests/jobs/statistics/CompileCounterSubmissionInstitutionDailyMetricsTest.php @@ -0,0 +1,88 @@ +assertInstanceOf( + CompileCounterSubmissionInstitutionDailyMetrics::class, + unserialize($this->serializedJobData) + ); + } + + /** + * Ensure that a serialized job can be unserialized and executed + */ + public function testRunSerializedJob(): void + { + /** @var CompileCounterSubmissionInstitutionDailyMetrics $compileCounterSubmissionInstitutionDailyMetricsJob */ + $compileCounterSubmissionInstitutionDailyMetricsJob = unserialize($this->serializedJobData); + + $temporaryTotalsDAOMock = Mockery::mock(\APP\statistics\TemporaryTotalsDAO::class) + ->makePartial() + ->shouldReceive([ + 'deleteCounterSubmissionInstitutionDailyByLoadId' => null, + 'compileCounterSubmissionDailyMetrics' => null, + ]) + ->withAnyArgs() + ->getMock(); + + DAORegistry::registerDAO('TemporaryTotalsDAO', $temporaryTotalsDAOMock); + + $temporaryItemInvestigationsDAOMock = Mockery::mock(\APP\statistics\TemporaryItemInvestigationsDAO::class) + ->makePartial() + ->shouldReceive([ + 'compileCounterSubmissionInstitutionDailyMetrics' => null, + ]) + ->withAnyArgs() + ->getMock(); + + DAORegistry::registerDAO('TemporaryItemInvestigationsDAO', $temporaryItemInvestigationsDAOMock); + + $temporaryItemRequestsDAOMock = Mockery::mock(\APP\statistics\TemporaryItemRequestsDAO::class) + ->makePartial() + ->shouldReceive([ + 'compileCounterSubmissionInstitutionDailyMetrics' => null, + ]) + ->withAnyArgs() + ->getMock(); + + DAORegistry::registerDAO('TemporaryItemRequestsDAO', $temporaryItemRequestsDAOMock); + + + $compileCounterSubmissionInstitutionDailyMetricsJob->handle(); + + $this->expectNotToPerformAssertions(); + } +} diff --git a/tests/jobs/statistics/CompileSubmissionGeoDailyMetricsTest.php b/tests/jobs/statistics/CompileSubmissionGeoDailyMetricsTest.php new file mode 100644 index 0000000000..496cd5c961 --- /dev/null +++ b/tests/jobs/statistics/CompileSubmissionGeoDailyMetricsTest.php @@ -0,0 +1,77 @@ +assertInstanceOf( + CompileSubmissionGeoDailyMetrics::class, + unserialize($this->serializedJobData) + ); + } + + /** + * Ensure that a serialized job can be unserialized and executed + */ + public function testRunSerializedJob(): void + { + /** @var CompileSubmissionGeoDailyMetrics $compileSubmissionGeoDailyMetricsJob */ + $compileSubmissionGeoDailyMetricsJob = unserialize($this->serializedJobData); + + $temporaryTotalsDAOMock = Mockery::mock(\APP\statistics\TemporaryTotalsDAO::class) + ->makePartial() + ->shouldReceive([ + 'deleteSubmissionGeoDailyByLoadId' => null, + 'compileSubmissionGeoDailyMetrics' => null, + ]) + ->withAnyArgs() + ->getMock(); + + DAORegistry::registerDAO('TemporaryTotalsDAO', $temporaryTotalsDAOMock); + + $temporaryItemInvestigationsDAOMock = Mockery::mock(\APP\statistics\TemporaryItemInvestigationsDAO::class) + ->makePartial() + ->shouldReceive([ + 'compileSubmissionGeoDailyMetrics' => null, + ]) + ->withAnyArgs() + ->getMock(); + + DAORegistry::registerDAO('TemporaryItemInvestigationsDAO', $temporaryItemInvestigationsDAOMock); + + $compileSubmissionGeoDailyMetricsJob->handle(); + + $this->expectNotToPerformAssertions(); + } +} diff --git a/tests/jobs/statistics/CompileUniqueInvestigationsTest.php b/tests/jobs/statistics/CompileUniqueInvestigationsTest.php new file mode 100644 index 0000000000..e2c252443f --- /dev/null +++ b/tests/jobs/statistics/CompileUniqueInvestigationsTest.php @@ -0,0 +1,66 @@ +assertInstanceOf( + CompileUniqueInvestigations::class, + unserialize($this->serializedJobData) + ); + } + + /** + * Ensure that a serialized job can be unserialized and executed + */ + public function testRunSerializedJob(): void + { + /** @var CompileUniqueInvestigations $compileUniqueInvestigationsJob */ + $compileUniqueInvestigationsJob = unserialize($this->serializedJobData); + + $temporaryItemInvestigationsDAOMock = Mockery::mock(\APP\statistics\TemporaryItemInvestigationsDAO::class) + ->makePartial() + ->shouldReceive([ + 'compileUniqueClicks' => null, + ]) + ->withAnyArgs() + ->getMock(); + + DAORegistry::registerDAO('TemporaryItemInvestigationsDAO', $temporaryItemInvestigationsDAOMock); + + $compileUniqueInvestigationsJob->handle(); + + $this->expectNotToPerformAssertions(); + } +} diff --git a/tests/jobs/statistics/CompileUniqueRequestsTest.php b/tests/jobs/statistics/CompileUniqueRequestsTest.php new file mode 100644 index 0000000000..4eee3c8db3 --- /dev/null +++ b/tests/jobs/statistics/CompileUniqueRequestsTest.php @@ -0,0 +1,66 @@ +assertInstanceOf( + CompileUniqueRequests::class, + unserialize($this->serializedJobData) + ); + } + + /** + * Ensure that a serialized job can be unserialized and executed + */ + public function testRunSerializedJob(): void + { + /** @var CompileUniqueRequests $compileUniqueRequestsJob */ + $compileUniqueRequestsJob = unserialize($this->serializedJobData); + + $temporaryItemRequestsDAOMock = Mockery::mock(\APP\statistics\TemporaryItemRequestsDAO::class) + ->makePartial() + ->shouldReceive([ + 'compileUniqueClicks' => null, + ]) + ->withAnyArgs() + ->getMock(); + + DAORegistry::registerDAO('TemporaryItemRequestsDAO', $temporaryItemRequestsDAOMock); + + $compileUniqueRequestsJob->handle(); + + $this->expectNotToPerformAssertions(); + } +} diff --git a/tests/jobs/statistics/DeleteUsageStatsTemporaryRecordsTest.php b/tests/jobs/statistics/DeleteUsageStatsTemporaryRecordsTest.php new file mode 100644 index 0000000000..400d4f3d93 --- /dev/null +++ b/tests/jobs/statistics/DeleteUsageStatsTemporaryRecordsTest.php @@ -0,0 +1,96 @@ +assertInstanceOf( + DeleteUsageStatsTemporaryRecords::class, + unserialize($this->serializedJobData) + ); + } + + /** + * Ensure that a serialized job can be unserialized and executed + */ + public function testRunSerializedJob(): void + { + /** @var DeleteUsageStatsTemporaryRecords $deleteUsageStatsTemporaryRecordsJob */ + $deleteUsageStatsTemporaryRecordsJob = unserialize($this->serializedJobData); + + $temporaryTotalsDAOMock = Mockery::mock(\APP\statistics\TemporaryTotalsDAO::class) + ->makePartial() + ->shouldReceive([ + 'deleteByLoadId' => null, + ]) + ->withAnyArgs() + ->getMock(); + + DAORegistry::registerDAO('TemporaryTotalsDAO', $temporaryTotalsDAOMock); + + $temporaryItemInvestigationsDAOMock = Mockery::mock(\APP\statistics\TemporaryItemInvestigationsDAO::class) + ->makePartial() + ->shouldReceive([ + 'deleteByLoadId' => null, + ]) + ->withAnyArgs() + ->getMock(); + + DAORegistry::registerDAO('TemporaryItemInvestigationsDAO', $temporaryItemInvestigationsDAOMock); + + $temporaryItemRequestsDAOMock = Mockery::mock(\APP\statistics\TemporaryItemRequestsDAO::class) + ->makePartial() + ->shouldReceive([ + 'deleteByLoadId' => null, + ]) + ->withAnyArgs() + ->getMock(); + + DAORegistry::registerDAO('TemporaryItemRequestsDAO', $temporaryItemRequestsDAOMock); + + $temporaryInstitutionsDAOMock = Mockery::mock(\PKP\statistics\TemporaryInstitutionsDAO::class) + ->makePartial() + ->shouldReceive([ + 'deleteByLoadId' => null, + ]) + ->withAnyArgs() + ->getMock(); + + DAORegistry::registerDAO('TemporaryInstitutionsDAO', $temporaryInstitutionsDAOMock); + + $deleteUsageStatsTemporaryRecordsJob->handle(); + + $this->expectNotToPerformAssertions(); + } +} diff --git a/tests/jobs/statistics/ProcessUsageStatsLogFileTest.php b/tests/jobs/statistics/ProcessUsageStatsLogFileTest.php new file mode 100644 index 0000000000..d008fc6266 --- /dev/null +++ b/tests/jobs/statistics/ProcessUsageStatsLogFileTest.php @@ -0,0 +1,171 @@ +assertInstanceOf( + ProcessUsageStatsLogFile::class, + unserialize($this->serializedJobData) + ); + } + + /** + * Ensure that a serialized job can be unserialized and executed + */ + public function testRunSerializedJob(): void + { + /** @var ProcessUsageStatsLogFile $processUsageStatsLogFileJob */ + $processUsageStatsLogFileJob = unserialize($this->serializedJobData); + + // we need to create a dummy file if not existed as to avoid mocking PHP's built in functions + $dummyFile = $this->createDummyFileIfNeeded($processUsageStatsLogFileJob, 'loadId'); + + $this->createArchiveDirectoryIfRequired(); + + $temporaryTotalsDAOMock = Mockery::mock(\APP\statistics\TemporaryTotalsDAO::class) + ->makePartial() + ->shouldReceive([ + 'deleteByLoadId' => null, + 'insert' => null, + ]) + ->withAnyArgs() + ->getMock(); + + DAORegistry::registerDAO('TemporaryTotalsDAO', $temporaryTotalsDAOMock); + + $temporaryItemInvestigationsDAOMock = Mockery::mock(\APP\statistics\TemporaryItemInvestigationsDAO::class) + ->makePartial() + ->shouldReceive([ + 'deleteByLoadId' => null, + 'insert' => null, + ]) + ->withAnyArgs() + ->getMock(); + + DAORegistry::registerDAO('TemporaryItemInvestigationsDAO', $temporaryItemInvestigationsDAOMock); + + $temporaryItemRequestsDAOMock = Mockery::mock(\APP\statistics\TemporaryItemRequestsDAO::class) + ->makePartial() + ->shouldReceive([ + 'deleteByLoadId' => null, + 'insert' => null, + ]) + ->withAnyArgs() + ->getMock(); + + DAORegistry::registerDAO('TemporaryItemRequestsDAO', $temporaryItemRequestsDAOMock); + + $temporaryInstitutionsDAOMock = Mockery::mock(\PKP\statistics\TemporaryInstitutionsDAO::class) + ->makePartial() + ->shouldReceive([ + 'deleteByLoadId' => null, + 'insert' => null, + ]) + ->withAnyArgs() + ->getMock(); + + DAORegistry::registerDAO('TemporaryInstitutionsDAO', $temporaryInstitutionsDAOMock); + + $processUsageStatsLogFileJob->handle(); + + if ($dummyFile) { + unlink($dummyFile); + } + + $this->expectNotToPerformAssertions(); + } + + /** + * Create the dummy file with dummy content if required + */ + protected function createDummyFileIfNeeded(ProcessUsageStatsLogFile $job, string $propertyName): ?string + { + $reflection = new ReflectionClass($job); + $property = $reflection->getProperty($propertyName); + $property->setAccessible(true); + $fileName = $property->getValue($job); + + $filePath = StatisticsHelper::getUsageStatsDirPath() + . DIRECTORY_SEPARATOR + . FileLoader::FILE_LOADER_PATH_DISPATCH + . DIRECTORY_SEPARATOR; + + if (!file_exists($filePath . $fileName)) { + + // create the 'FileLoader::FILE_LOADER_PATH_DISPATCH' directory if not exists + if (!file_exists($filePath)) { + $fileManager = new FileManager(); + $fileManager->mkdirtree($filePath); + } + + touch($filePath . $fileName); + + file_put_contents($filePath . $fileName, $this->dummyFileContent); + return $filePath . $fileName; + } + + return null; + } + + /** + * Create the archive path/directory as needed + */ + protected function createArchiveDirectoryIfRequired(): bool + { + $filePath = StatisticsHelper::getUsageStatsDirPath() + . DIRECTORY_SEPARATOR + . FileLoader::FILE_LOADER_PATH_ARCHIVE + . DIRECTORY_SEPARATOR; + + if (file_exists($filePath)) { + return true; + } + + // create the 'FileLoader::FILE_LOADER_PATH_ARCHIVE' directory if not exists + $fileManager = new FileManager(); + $fileManager->mkdirtree($filePath); + + return file_exists($filePath); + } +}