diff --git a/lib/BackgroundJobs/ProcessFileJob.php b/lib/BackgroundJobs/ProcessFileJob.php index 3a59660..36e3d2c 100644 --- a/lib/BackgroundJobs/ProcessFileJob.php +++ b/lib/BackgroundJobs/ProcessFileJob.php @@ -184,19 +184,21 @@ private function processFile(string $filePath, WorkflowSettings $settings) : voi return; } - $fileContent = $ocrFile->getFileContent(); $nodeId = $node->getId(); $originalFileExtension = $node->getExtension(); $newFileExtension = $ocrFile->getFileExtension(); - if ($originalFileExtension === $newFileExtension) { - $this->createNewFileVersion($filePath, $fileContent, $nodeId); - $this->eventService->textRecognized($ocrFile, $node); - } else { - $this->createNewFileVersion($filePath.".pdf", $fileContent, $nodeId); - $this->eventService->textRecognized($ocrFile, $node); + // Only create a new file version if the file OCR result was not empty #130 + if ($ocrFile->getRecognizedText() !== '') { + $newFilePath = $originalFileExtension === $newFileExtension ? + $filePath : + $filePath . ".pdf"; + + $this->createNewFileVersion($newFilePath, $fileContent, $nodeId); } + + $this->eventService->textRecognized($ocrFile, $node); } private function getNode(string $filePath) : ?Node { diff --git a/tests/Unit/BackgroundJobs/ProcessFileJobTest.php b/tests/Unit/BackgroundJobs/ProcessFileJobTest.php index ef28dfa..ea64b25 100644 --- a/tests/Unit/BackgroundJobs/ProcessFileJobTest.php +++ b/tests/Unit/BackgroundJobs/ProcessFileJobTest.php @@ -403,6 +403,38 @@ public function testCallsProcessingFileAccessor(array $arguments, string $user, $this->assertEquals(1, $calledWithNull); } + /** + * @dataProvider dataProvider_ValidArguments + */ + public function testDoesNotCreateNewFileVersionIfOcrContentWasEmpty(array $arguments, string $user, string $rootFolderPath, string $originalFileExtension, string $expectedOcrFilename) { + $this->processFileJob->setArgument($arguments); + $mimeType = 'application/pdf'; + $content = 'someFileContent'; + $ocrContent = ''; + $ocrResult = new OcrProcessorResult($ocrContent, "pdf", $ocrContent); + + $fileMock = $this->createValidFileMock($mimeType, $content); + $this->rootFolder->method('get') + ->willReturn($fileMock); + + $this->ocrService->expects($this->once()) + ->method('ocrFile') + ->willReturn($ocrResult); + + $viewMock = $this->createMock(IView::class); + $this->viewFactory->expects($this->never()) + ->method('create') + ->willReturn($viewMock); + + $this->processingFileAccessor->expects($this->never()) + ->method('setCurrentlyProcessedFileId'); + + $this->eventService->expects($this->once()) + ->method('textRecognized'); + + $this->processFileJob->execute($this->jobList); + } + public function dataProvider_InvalidArguments() { $arr = [ [null, 1],