From ab61296881675cb5523fcc579e2aaad51a55ddd7 Mon Sep 17 00:00:00 2001 From: Robin Windey Date: Sun, 27 Aug 2023 21:53:33 +0200 Subject: [PATCH] Process OCR files by file id (#225) Closing #112 Signed-off-by: Robin Windey --- lib/BackgroundJobs/ProcessFileJob.php | 111 ++++------------- .../OcrProcessorNotFoundException.php | 3 + lib/OcrProcessors/OcrMyPdfBasedProcessor.php | 4 +- lib/Operation.php | 13 +- .../Notification/NotificationTest.php | 10 +- .../BackgroundJobs/ProcessFileJobTest.php | 112 ++++++++++-------- .../OcrProcessors/PdfOcrProcessorTest.php | 5 +- tests/Unit/OperationTest.php | 7 +- 8 files changed, 109 insertions(+), 156 deletions(-) diff --git a/lib/BackgroundJobs/ProcessFileJob.php b/lib/BackgroundJobs/ProcessFileJob.php index 1a4e06a..fb3dab3 100644 --- a/lib/BackgroundJobs/ProcessFileJob.php +++ b/lib/BackgroundJobs/ProcessFileJob.php @@ -28,8 +28,6 @@ use \OCP\Files\File; use OC\User\NoUserException; -use OCA\WorkflowOcr\Exception\OcrNotPossibleException; -use OCA\WorkflowOcr\Exception\OcrProcessorNotFoundException; use OCA\WorkflowOcr\Helper\IProcessingFileAccessor; use OCA\WorkflowOcr\Model\WorkflowSettings; use OCA\WorkflowOcr\Service\IEventService; @@ -103,19 +101,14 @@ public function __construct( */ protected function run($argument) : void { $this->logger->debug('STARTED -- Run ' . self::class . ' job. Argument: {argument}.', ['argument' => $argument]); - - [$success, $filePath, $uid, $settings] = $this->tryParseArguments($argument); - if (!$success) { - $this->notificationService->createErrorNotification($uid, 'Failed to parse arguments inside the OCR process. Please have a look at your servers logfile for more details.'); - return; - } try { + [$fileId, $uid, $settings] = $this->parseArguments($argument); $this->initUserEnvironment($uid); - $this->processFile($filePath, $settings, $uid); + $this->processFile($fileId, $settings); } catch (\Throwable $ex) { $this->logger->error($ex->getMessage(), ['exception' => $ex]); - $this->notificationService->createErrorNotification($uid, 'An error occured while executing the OCR process. Please have a look at your servers logfile for more details.'); + $this->notificationService->createErrorNotification($uid, 'An error occured while executing the OCR process ('.$ex->getMessage().'). Please have a look at your servers logfile for more details.'); } finally { $this->shutdownUserEnvironment(); } @@ -126,80 +119,33 @@ protected function run($argument) : void { /** * @param mixed $argument */ - private function tryParseArguments($argument) : array { + private function parseArguments($argument) : array { if (!is_array($argument)) { - $this->logger->warning('Argument is no array in ' . self::class . ' method \'tryParseArguments\'.'); - return [ - false - ]; - } - - $filePath = null; - $uid = null; - $filePathKey = 'filePath'; - if (array_key_exists($filePathKey, $argument)) { - $filePath = $argument[$filePathKey]; - // '', admin, 'files', 'path/to/file.pdf' - $splitted = explode('/', $filePath, 4); - if (count($splitted) < 4) { - $this->logger->warning('File path "' . $filePath . '" is not valid in ' . self::class . ' method \'tryParseArguments\'.'); - return [ - false - ]; - } - $uid = $splitted[1]; - } else { - $this->logVariableKeyNotSet($filePathKey, 'tryParseArguments'); + throw new \InvalidArgumentException('Argument is no array in ' . self::class . ' method \'tryParseArguments\'.'); } - $settings = null; - $settingsKey = 'settings'; - if (array_key_exists($settingsKey, $argument)) { - $jsonSettings = $argument[$settingsKey]; - $settings = new WorkflowSettings($jsonSettings); - } else { - $this->logVariableKeyNotSet($settingsKey, 'tryParseArguments'); - } + $jsonSettings = $argument['settings']; + $settings = new WorkflowSettings($jsonSettings); + $uid = $argument['uid']; + $fileId = intval($argument['fileId']); return [ - $filePath !== null && $uid !== null && $settings !== null, - $filePath, + $fileId, $uid, $settings ]; } /** - * @param string $filePath The file to be processed + * @param int $fileId The id of the file to be processed * @param WorkflowSettings $settings The settings to be used for processing - * @param string $userId The user who triggered the processing */ - private function processFile(string $filePath, WorkflowSettings $settings, string $userId) : void { - $node = $this->getNode($filePath, $userId); - - if ($node === null) { - return; - } + private function processFile(int $fileId, WorkflowSettings $settings) : void { + $node = $this->getNode($fileId); - $nodeId = $node->getId(); - - try { - $ocrFile = $this->ocrService->ocrFile($node, $settings); - } catch(\Throwable $throwable) { - if ($throwable instanceof(OcrNotPossibleException::class)) { - $msg = 'OCR for file ' . $node->getPath() . ' not possible. Message: ' . $throwable->getMessage(); - } elseif ($throwable instanceof(OcrProcessorNotFoundException::class)) { - $msg = 'OCR processor not found for mimetype ' . $node->getMimeType(); - } else { - throw $throwable; - } - - $this->logger->error($msg); - $this->notificationService->createErrorNotification($userId, $msg, $nodeId); - - return; - } + $ocrFile = $this->ocrService->ocrFile($node, $settings); + $filePath = $node->getPath(); $fileContent = $ocrFile->getFileContent(); $originalFileExtension = $node->getExtension(); $newFileExtension = $ocrFile->getFileExtension(); @@ -210,28 +156,23 @@ private function processFile(string $filePath, WorkflowSettings $settings, strin $filePath : $filePath . ".pdf"; - $this->createNewFileVersion($newFilePath, $fileContent, $nodeId); + $this->createNewFileVersion($newFilePath, $fileContent, $fileId); } $this->eventService->textRecognized($ocrFile, $node); } - private function getNode(string $filePath, string $userId) : ?Node { - try { - /** @var File */ - $node = $this->rootFolder->get($filePath); - } catch (NotFoundException $nfEx) { - $msg = 'Could not process file \'' . $filePath . '\'. File was not found'; - $this->logger->warning($msg); - $this->notificationService->createErrorNotification($userId, $msg); - return null; + private function getNode(int $fileId) : ?Node { + /** @var File[] */ + $nodeArr = $this->rootFolder->getById($fileId); + if (count($nodeArr) === 0) { + throw new NotFoundException('Could not process file with id \'' . $fileId . '\'. File was not found'); } + + $node = array_shift($nodeArr); if (!$node instanceof Node || $node->getType() !== FileInfo::TYPE_FILE) { - $msg = 'Skipping process for \'' . $filePath . '\'. It is not a file'; - $this->logger->warning($msg); - $this->notificationService->createErrorNotification($userId, $msg); - return null; + throw new \InvalidArgumentException('Skipping process for file with id \'' . $fileId . '\'. It is not a file'); } return $node; @@ -277,8 +218,4 @@ private function createNewFileVersion(string $filePath, string $ocrContent, int $this->processingFileAccessor->setCurrentlyProcessedFileId(null); } } - - private function logVariableKeyNotSet(string $key, string $method) : void { - $this->logger->warning("Variable '" . $key . "' not set in " . self::class . " method '" . $method . "'."); - } } diff --git a/lib/Exception/OcrProcessorNotFoundException.php b/lib/Exception/OcrProcessorNotFoundException.php index cb4bbdd..a370336 100644 --- a/lib/Exception/OcrProcessorNotFoundException.php +++ b/lib/Exception/OcrProcessorNotFoundException.php @@ -26,4 +26,7 @@ use Exception; class OcrProcessorNotFoundException extends Exception { + public function __construct(string $mimeType) { + $this->message = 'OCR processor for mime type ' . $mimeType . ' not found'; + } } diff --git a/lib/OcrProcessors/OcrMyPdfBasedProcessor.php b/lib/OcrProcessors/OcrMyPdfBasedProcessor.php index 37eaa44..740f42a 100644 --- a/lib/OcrProcessors/OcrMyPdfBasedProcessor.php +++ b/lib/OcrProcessors/OcrMyPdfBasedProcessor.php @@ -72,7 +72,7 @@ public function ocrFile(File $file, WorkflowSettings $settings, GlobalSettings $ $exitCode = $this->command->getExitCode(); if (!$success) { - throw new OcrNotPossibleException('OCRmyPDF exited abnormally with exit-code ' . $exitCode . '. Message: ' . $errorOutput . ' ' . $stdErr); + throw new OcrNotPossibleException('OCRmyPDF exited abnormally with exit-code ' . $exitCode . ' for file ' . $file->getPath() . '. Message: ' . $errorOutput . ' ' . $stdErr); } if ($stdErr !== '' || $errorOutput !== '') { @@ -86,7 +86,7 @@ public function ocrFile(File $file, WorkflowSettings $settings, GlobalSettings $ $ocrFileContent = $this->command->getOutput(); if (!$ocrFileContent) { - throw new OcrNotPossibleException('OCRmyPDF did not produce any output'); + throw new OcrNotPossibleException('OCRmyPDF did not produce any output for file ' . $file->getPath()); } $recognizedText = $this->sidecarFileAccessor->getSidecarFileContent(); diff --git a/lib/Operation.php b/lib/Operation.php index ec9cc43..eec7950 100644 --- a/lib/Operation.php +++ b/lib/Operation.php @@ -107,7 +107,7 @@ public function onEvent(string $eventName, Event $event, IRuleMatcher $ruleMatch if (!($match = $this->getMatch($ruleMatcher)) || !$this->tryGetFile($eventName, $event, $node) || $this->eventTriggeredByOcrProcess($node) || - !$this->tryGetJobArgs($node, $match, $argsArray)) { + !$this->tryGetJobArgs($node, $match['operation'], $argsArray)) { return; } @@ -192,14 +192,14 @@ private function getMatch(IRuleMatcher $ruleMatcher) : array { /** * @param Node $node - * @param array $match + * @param string $operation * @param array $argsArray */ - private function tryGetJobArgs(Node $node, $match, & $argsArray) : bool { + private function tryGetJobArgs(Node $node, $operation, & $argsArray) : bool { // Check path has valid structure $filePath = $node->getPath(); // '', admin, 'files', 'path/to/file.pdf' - [,, $folder,] = explode('/', $filePath, 4); + [, $user, $folder,] = explode('/', $filePath, 4); if ($folder !== 'files') { $this->logger->debug('Not processing event because path \'{path}\' seems to be invalid.', ['path' => $filePath]); @@ -207,8 +207,9 @@ private function tryGetJobArgs(Node $node, $match, & $argsArray) : bool { } $argsArray = [ - 'filePath' => $filePath, - 'settings' => $match['operation'] + 'uid' => $user, + 'fileId' => $node->getId(), + 'settings' => $operation ]; return true; diff --git a/tests/Integration/Notification/NotificationTest.php b/tests/Integration/Notification/NotificationTest.php index 226d097..70e277a 100644 --- a/tests/Integration/Notification/NotificationTest.php +++ b/tests/Integration/Notification/NotificationTest.php @@ -151,7 +151,7 @@ protected function setUp() : void { $this->processFileJob->setId(111); $this->processFileJob->setArgument([ - 'filePath' => '/admin/files/somefile.pdf', + 'fileId' => 42, 'uid' => 'someuser', 'settings' => '{}' ]); @@ -159,9 +159,9 @@ protected function setUp() : void { public function testBackgroundJobCreatesErrorNotificationIfOcrFailed() { $fileMock = $this->createValidFileMock(); - $this->rootFolder->method('get') - ->with('/admin/files/somefile.pdf') - ->willReturn($fileMock); + $this->rootFolder->method('getById') + ->with(42) + ->willReturn([$fileMock]); $this->ocrService->expects($this->once()) ->method('ocrFile') @@ -177,7 +177,7 @@ public function testBackgroundJobCreatesErrorNotificationIfOcrFailed() { $notification = $notifications[0]; $this->assertEquals('workflow_ocr', $notification->getApp()); $this->assertEquals('ocr_error', $notification->getSubject()); - $this->assertEquals('OCR for file /admin/files/somefile.pdf not possible. Message: Some error', $notification->getSubjectParameters()['message']); + $this->assertEquals('An error occured while executing the OCR process (Some error). Please have a look at your servers logfile for more details.', $notification->getSubjectParameters()['message']); } /** diff --git a/tests/Unit/BackgroundJobs/ProcessFileJobTest.php b/tests/Unit/BackgroundJobs/ProcessFileJobTest.php index 07f78f9..acf20c0 100644 --- a/tests/Unit/BackgroundJobs/ProcessFileJobTest.php +++ b/tests/Unit/BackgroundJobs/ProcessFileJobTest.php @@ -44,7 +44,6 @@ use OCP\Files\FileInfo; use OCP\Files\IRootFolder; use OCP\Files\Node; -use OCP\Files\NotFoundException; use OCP\IConfig; use OCP\IDBConnection; use OCP\IUser; @@ -81,12 +80,13 @@ class ProcessFileJobTest extends TestCase { private $jobList; /** @var ProcessFileJob */ private $processFileJob; + /** @var File[] */ + private $rootFolderGetById42ReturnValue; public function setUp() : void { parent::setUp(); $this->logger = $this->createMock(LoggerInterface::class); - $this->rootFolder = $this->createMock(IRootFolder::class); $this->ocrService = $this->createMock(IOcrService::class); $this->eventService = $this->createMock(IEventService::class); $this->viewFactory = $this->createMock(IViewFactory::class); @@ -95,6 +95,16 @@ public function setUp() : void { $this->processingFileAccessor = $this->createMock(IProcessingFileAccessor::class); $this->notificationService = $this->createMock(INotificationService::class); + /** @var MockObject|IRootFolder */ + $this->rootFolder = $this->createMock(IRootFolder::class); + $this->rootFolderGetById42ReturnValue = [$this->createValidFileMock()]; + $this->rootFolder->expects($this->any()) + ->method('getById') + ->with(42) + ->willReturnCallback(function () { + return $this->rootFolderGetById42ReturnValue; + }); + /** @var MockObject|IUserManager */ $userManager = $this->createMock(IUserManager::class); $user = $this->createMock(IUser::class); @@ -154,8 +164,8 @@ public function setUp() : void { ); $this->processFileJob->setArgument([ - 'filePath' => '/admin/files/somefile.pdf', - 'uid' => 'someuser', + 'fileId' => 42, + 'uid' => 'admin', 'settings' => '{}' ]); } @@ -182,7 +192,7 @@ public function testCatchesExceptionAndResetsUserEnvironment() { /** * @dataProvider dataProvider_InvalidArguments */ - public function testDoesNothingOnInvalidArguments($argument, $invalidCount) { + public function testLogsErrorAndDoesNothingOnInvalidArguments($argument, $errorMessagePart) { $this->processFileJob->setArgument($argument); $this->filesystem->expects($this->never()) ->method('init') @@ -193,8 +203,11 @@ public function testDoesNothingOnInvalidArguments($argument, $invalidCount) { $this->viewFactory->expects($this->never()) ->method('create') ->withAnyParameters(); - $this->logger->expects($this->exactly($invalidCount)) - ->method('warning'); + $this->logger->expects($this->once()) + ->method('error') + ->with($this->stringContains($errorMessagePart), $this->callback(function ($loggerArgs) { + return is_array($loggerArgs) && ($loggerArgs['exception'] instanceof \Exception); + })); $this->processFileJob->start($this->jobList); } @@ -217,8 +230,8 @@ public function testCallsInitFilesystem(array $arguments, string $user, string $ public function testCallsGetOnRootFolder(array $arguments, string $user, string $rootFolderPath, string $originalFileExtension, string $expectedOcrFilename) { $this->processFileJob->setArgument($arguments); $this->rootFolder->expects($this->once()) - ->method('get') - ->with($arguments['filePath']); + ->method('getById') + ->with(42); $this->processFileJob->start($this->jobList); } @@ -232,9 +245,7 @@ public function testCallsOcr_IfIsFile(array $arguments, string $user, string $ro $mimeType = 'application/pdf'; $content = 'someFileContent'; $fileMock = $this->createValidFileMock($mimeType, $content); - $this->rootFolder->method('get') - ->with($arguments['filePath']) - ->willReturn($fileMock); + $this->rootFolderGetById42ReturnValue = [$fileMock]; $this->ocrService->expects($this->once()) ->method('ocrFile') @@ -246,19 +257,15 @@ public function testCallsOcr_IfIsFile(array $arguments, string $user, string $ro /** * @dataProvider dataProvider_ValidArguments */ - public function testCreatesNewFileVersionAndEmitsTextRecognizedEvent(array $arguments, string $user, string $rootFolderPath, string $originalFileExtension, string $expectedOcrFilename) { + public function testCreatesNewFileVersionAndEmitsTextRecognizedEvent(array $arguments, string $user, string $rootFolderPath, string $originalFileName, string $expectedOcrFilename) { $this->processFileJob->setArgument($arguments); $mimeType = 'application/pdf'; $content = 'someFileContent'; $ocrContent = 'someOcrProcessedFile'; $ocrResult = new OcrProcessorResult($ocrContent, "pdf", $ocrContent); // Extend this cases if we add new OCR processors - $filePath = $arguments['filePath']; - $dirPath = dirname($filePath); - - $fileMock = $this->createValidFileMock($mimeType, $content, $originalFileExtension); - $this->rootFolder->method('get') - ->with($arguments['filePath']) - ->willReturn($fileMock); + $originalFileMock = $this->createValidFileMock($mimeType, $content, $rootFolderPath, $originalFileName); + + $this->rootFolderGetById42ReturnValue = [$originalFileMock]; $this->ocrService->expects($this->once()) ->method('ocrFile') @@ -271,23 +278,24 @@ public function testCreatesNewFileVersionAndEmitsTextRecognizedEvent(array $argu ->with($expectedOcrFilename, $ocrContent); $this->viewFactory->expects($this->once()) ->method('create') - ->with($dirPath) + ->with($rootFolderPath) ->willReturn($viewMock); $this->eventService->expects($this->once()) ->method('textRecognized') - ->with($ocrResult, $fileMock); + ->with($ocrResult, $originalFileMock); $this->processFileJob->start($this->jobList); } - public function testNotFoundLogsWarning_AndDoesNothingAfterwards() { - $this->rootFolder->expects($this->once()) - ->method('get') - ->willThrowException(new NotFoundException()); + public function testNotFoundLogsErrorAndSendsNotification_AndDoesNothingAfterwards() { + $this->rootFolderGetById42ReturnValue = []; $this->logger->expects($this->once()) - ->method('warning') + ->method('error') ->with($this->stringContains('not found')); + $this->notificationService->expects($this->once()) + ->method('createErrorNotification') + ->with($this->stringContains('An error occured while executing the OCR process (') && $this->stringContains('File was not found')); $this->ocrService->expects($this->never()) ->method('ocrFile'); @@ -298,9 +306,7 @@ public function testNotFoundLogsWarning_AndDoesNothingAfterwards() { * @dataProvider dataProvider_InvalidNodes */ public function testDoesNotCallOcr_OnNonFile($invalidNode) { - $this->rootFolder->method('get') - ->with('/admin/files/somefile.pdf') - ->willReturn($invalidNode); + $this->rootFolderGetById42ReturnValue = [$invalidNode]; $this->ocrService->expects($this->never()) ->method('ocrFile'); @@ -358,7 +364,7 @@ public function testThrowsNoUserException_OnNonExistingUser() { $this->createMock(ITimeFactory::class) ); $processFileJob->setId(111); - $arguments = ['filePath' => '/nonexistinguser/files/someInvalidStuff', 'settings' => '{}']; + $arguments = ['fileId' => 42, 'uid' => 'nonexistinguser', 'settings' => '{}']; $processFileJob->setArgument($arguments); $processFileJob->start($this->jobList); @@ -374,10 +380,7 @@ public function testCallsProcessingFileAccessor(array $arguments, string $user, $ocrContent = 'someOcrProcessedFile'; $ocrResult = new OcrProcessorResult($ocrContent, "pdf", $ocrContent); // Extend this cases if we add new OCR processors - $fileMock = $this->createValidFileMock($mimeType, $content); - $this->rootFolder->method('get') - ->with($arguments['filePath']) - ->willReturn($fileMock); + $this->rootFolderGetById42ReturnValue = [$this->createValidFileMock($mimeType, $content)]; $this->ocrService->expects($this->once()) ->method('ocrFile') @@ -388,14 +391,16 @@ public function testCallsProcessingFileAccessor(array $arguments, string $user, ->method('create') ->willReturn($viewMock); - $calledWith42 = 0; + $calledWithFileId42 = 0; $calledWithNull = 0; + $withIdCalledFirst = false; $this->processingFileAccessor->expects($this->exactly(2)) ->method('setCurrentlyProcessedFileId') - ->with($this->callback(function ($id) use (&$calledWith42, &$calledWithNull) { + ->with($this->callback(function ($id) use (&$calledWithFileId42, &$calledWithNull, &$withIdCalledFirst) { if ($id === 42) { - $calledWith42++; + $calledWithFileId42++; + $withIdCalledFirst = $calledWithNull === 0; } elseif ($id === null) { $calledWithNull++; } @@ -405,8 +410,9 @@ public function testCallsProcessingFileAccessor(array $arguments, string $user, $this->processFileJob->start($this->jobList); - $this->assertEquals(1, $calledWith42); + $this->assertEquals(1, $calledWithFileId42); $this->assertEquals(1, $calledWithNull); + $this->assertTrue($withIdCalledFirst); } /** @@ -418,10 +424,12 @@ public function testDoesNotCreateNewFileVersionIfOcrContentWasEmpty(array $argum $content = 'someFileContent'; $ocrContent = ''; $ocrResult = new OcrProcessorResult($ocrContent, "pdf", $ocrContent); + $fileId = $arguments['fileId']; - $fileMock = $this->createValidFileMock($mimeType, $content); - $this->rootFolder->method('get') - ->willReturn($fileMock); + $this->rootFolder->expects($this->once()) + ->method('getById') + ->with($fileId) + ->willReturn([$this->createValidFileMock($mimeType, $content)]); $this->ocrService->expects($this->once()) ->method('ocrFile') @@ -442,7 +450,6 @@ public function testDoesNotCreateNewFileVersionIfOcrContentWasEmpty(array $argum } public function testLogsNonOcrExceptionsFromOcrService() { - $this->processFileJob->setArgument(['filePath' => '/admin/files/somefile.pdf', 'settings' => '{}']); $mimeType = 'application/pdf'; $content = 'someFileContent'; $exception = new \Exception('someException'); @@ -466,18 +473,16 @@ public function testLogsNonOcrExceptionsFromOcrService() { public function dataProvider_InvalidArguments() { $arr = [ - [null, 1], - [['mykey' => 'myvalue'], 2], - [['someotherkey' => 'someothervalue', 'k2' => 'v2'], 2], - [['filePath' => 'someInvalidPath'], 1] + [null, "Argument is no array"], + [['mykey' => 'myvalue'], "Undefined array key"] ]; return $arr; } public function dataProvider_ValidArguments() { $arr = [ - [['filePath' => '/admin/files/somefile.pdf', 'uid' => 'admin', 'settings' => '{}'], 'admin', '/admin/files', 'pdf', 'somefile.pdf'], - [['filePath' => '/myuser/files/subfolder/someotherfile.jpg', 'uid' => 'myuser', 'settings' => '{}'], 'myuser', '/myuser/files', 'jpg', 'someotherfile.jpg.pdf'] + [['fileId' => 42, 'uid' => 'admin', 'settings' => '{}'], 'admin', '/admin/files', 'somefile.pdf', 'somefile.pdf'], + [['fileId' => 42, 'uid' => 'myuser', 'settings' => '{}'], 'myuser', '/myuser/files', 'someotherfile.jpg', 'someotherfile.jpg.pdf'] ]; return $arr; } @@ -500,14 +505,14 @@ public function dataProvider_InvalidNodes() { public function dataProvider_OcrExceptions() { return [ [new OcrNotPossibleException('Ocr not possible')], - [new OcrProcessorNotFoundException()] + [new OcrProcessorNotFoundException('audio/mpeg')] ]; } /** * @return File|MockObject */ - private function createValidFileMock(string $mimeType = 'application/pdf', string $content = 'someFileContent', string $fileExtension = "pdf") { + private function createValidFileMock(string $mimeType = 'application/pdf', string $content = 'someFileContent', string $rootFolderPath = '/admin/files', string $fileName = 'somefile.pdf') { /** @var MockObject|File */ $fileMock = $this->createMock(File::class); $fileMock->method('getType') @@ -518,8 +523,11 @@ private function createValidFileMock(string $mimeType = 'application/pdf', strin ->willReturn($content); $fileMock->method('getId') ->willReturn(42); + $fileMock->method('getPath') + ->willReturn("$rootFolderPath/$fileName"); + #get extension from filename $fileMock->method('getExtension') - ->willReturn($fileExtension); + ->willReturn(pathinfo($fileName, PATHINFO_EXTENSION)); return $fileMock; } } diff --git a/tests/Unit/OcrProcessors/PdfOcrProcessorTest.php b/tests/Unit/OcrProcessors/PdfOcrProcessorTest.php index c2c32fa..99d1e18 100644 --- a/tests/Unit/OcrProcessors/PdfOcrProcessorTest.php +++ b/tests/Unit/OcrProcessors/PdfOcrProcessorTest.php @@ -165,6 +165,9 @@ public function testThrowsErrorIfOcrFileWasEmpty() { ->method('getStdErr') ->willReturn('stdErr'); $this->ocrMyPdfOutput = ""; + $this->fileBefore->expects($this->once()) + ->method('getPath') + ->willReturn('/admin/files/somefile.pdf'); $thrown = false; $processor = new PdfOcrProcessor($this->command, $this->logger, $this->sidecarFileAccessor); @@ -174,7 +177,7 @@ public function testThrowsErrorIfOcrFileWasEmpty() { } catch (\Throwable $t) { $thrown = true; $this->assertInstanceOf(OcrNotPossibleException::class, $t); - $this->assertEquals('OCRmyPDF did not produce any output', $t->getMessage()); + $this->assertEquals('OCRmyPDF did not produce any output for file /admin/files/somefile.pdf', $t->getMessage()); } $this->assertTrue($thrown); diff --git a/tests/Unit/OperationTest.php b/tests/Unit/OperationTest.php index b234f93..bc0dbb8 100644 --- a/tests/Unit/OperationTest.php +++ b/tests/Unit/OperationTest.php @@ -210,10 +210,11 @@ public function testDoesNothingOnFileWithoutOwner() { public function testAddWithCorrectFilePathAndUser() { $filePath = "/admin/files/path/to/file.pdf"; + $fileId = 42; $uid = 'admin'; $this->jobList->expects($this->once()) ->method('add') - ->with(ProcessFileJob::class, ['filePath' => $filePath, 'settings' => self::SETTINGS]); + ->with(ProcessFileJob::class, ['fileId' => $fileId, 'uid' => $uid, 'settings' => self::SETTINGS]); $operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $this->processingFileAccessor, $this->rootFolder); @@ -231,7 +232,7 @@ public function testAddWithCorrectFilePathAndUser() { $fileMock->method('getOwner') ->willReturn($userMock); $fileMock->method('getId') - ->willReturn(42); + ->willReturn($fileId); $event = new GenericEvent($fileMock); $eventName = '\OCP\Files::postCreate'; @@ -403,7 +404,7 @@ public function testFileAddedToQueueOnTagAssignedEvent() { $this->jobList->expects($this->once()) ->method('add') - ->with(ProcessFileJob::class, ['filePath' => $filePath, 'settings' => self::SETTINGS]); + ->with(ProcessFileJob::class, ['fileId' => $fileId, 'uid' => $uid, 'settings' => self::SETTINGS]); /** @var MockObject|IUser */ $userMock = $this->createMock(IUser::class);