Skip to content

Commit

Permalink
Implement #130 (#160) (#161)
Browse files Browse the repository at this point in the history
Only create new file version if OCR result was not empty

Signed-off-by: Robin Windey <ro.windey@gmail.com>

Signed-off-by: Robin Windey <ro.windey@gmail.com>

Signed-off-by: Robin Windey <ro.windey@gmail.com>
  • Loading branch information
R0Wi authored Nov 1, 2022
1 parent 221f3f2 commit ffce79d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/BackgroundJobs/ProcessFileJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions tests/Unit/BackgroundJobs/ProcessFileJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down

0 comments on commit ffce79d

Please sign in to comment.