Skip to content

Commit

Permalink
Introduce Setup Check (#289)
Browse files Browse the repository at this point in the history
* Do some basic check if OCRMyPDF CLI is working
* Fixes #83
  • Loading branch information
R0Wi authored Dec 29, 2024
1 parent 8216649 commit 7bddb53
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ apt-get install tesseract-ocr-deu
apt-get install tesseract-ocr-chi-sim
```

### Setup Checks

The app will perform some [Setup Checks](https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/security_setup_warnings.html) to verify your installation. If there is any problem with your backend setup, you'll see an error printed in Nextcloud under `Administration Settings` → `Overview` → `Security & setup warnings`.

<p align="center">
<img width="50%" src="doc/img/setup_checks.jpg" alt="Setup checks">
</p>

## Usage
You can configure the OCR processing via Nextcloud's workflow engine. Therefore configure a new flow via `Settings` &#8594; `Flow` &#8594; `Add new flow` (if you don't see `OCR file` here the app isn't installed properly or you forgot to activate it).

Expand Down
Binary file added doc/img/setup_checks.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use OCA\WorkflowOcr\Service\NotificationService;
use OCA\WorkflowOcr\Service\OcrBackendInfoService;
use OCA\WorkflowOcr\Service\OcrService;
use OCA\WorkflowOcr\SetupChecks\OcrMyPdfCheck;
use OCA\WorkflowOcr\Wrapper\CommandWrapper;
use OCA\WorkflowOcr\Wrapper\Filesystem;
use OCA\WorkflowOcr\Wrapper\ICommand;
Expand Down Expand Up @@ -99,6 +100,7 @@ public function register(IRegistrationContext $context): void {

$context->registerEventListener(RegisterOperationsEvent::class, RegisterFlowOperationsListener::class);
$context->registerNotifierService(Notifier::class);
$context->registerSetupCheck(OcrMyPdfCheck::class);
}

/**
Expand Down
60 changes: 60 additions & 0 deletions lib/SetupChecks/OcrMyPdfCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2024 Robin Windey <[email protected]>
*
* @author Robin Windey <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\WorkflowOcr\SetupChecks;

use OCA\WorkflowOcr\Wrapper\ICommand;
use OCP\IL10N;
use OCP\SetupCheck\ISetupCheck;
use OCP\SetupCheck\SetupResult;

class OcrMyPdfCheck implements ISetupCheck {
public function __construct(
private IL10N $l10n,
private ICommand $command,
) {
}

public function getCategory(): string {
return 'system';
}

public function getName(): string {
return $this->l10n->t('Is OCRmyPDF installed');
}

public function run(): SetupResult {
$this->command->setCommand('ocrmypdf --version')->execute();
if ($this->command->getExitCode() === 127) {
return SetupResult::error($this->l10n->t('OCRmyPDF CLI is not installed.'), 'https://github.com/R0Wi-DEV/workflow_ocr?tab=readme-ov-file#backend');
}
if ($this->command->getExitCode() !== 0) {
return SetupResult::error($this->l10n->t('OCRmyPDF CLI is not working correctly. Error was: %1$s', [$this->command->getError()]));
}
$versionOutput = $this->command->getOutput();
return SetupResult::success($this->l10n->t('OCRmyPDF is installed and has version %1$s.', [$versionOutput]));
}
}
103 changes: 103 additions & 0 deletions tests/Unit/SetupChecks/OcrMyPdfCheckTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2024 Robin Windey <[email protected]>
*
* @author Robin Windey <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\WorkflowOcr\Tests\Unit\SetupChecks;

use OCA\WorkflowOcr\SetupChecks\OcrMyPdfCheck;
use OCA\WorkflowOcr\Wrapper\ICommand;
use OCP\IL10N;
use OCP\SetupCheck\SetupResult;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class OcrMyPdfCheckTest extends TestCase {
/** @var IL10N|MockObject */
private $l10n;
/** @var ICommand|MockObject */
private $command;
/** @var OcrMyPdfCheck */
private $ocrMyPdfCheck;

protected function setUp(): void {
$this->l10n = $this->createMock(IL10N::class);
$this->command = $this->createMock(ICommand::class);
$this->ocrMyPdfCheck = new OcrMyPdfCheck($this->l10n, $this->command);
}

public function testGetCategory(): void {
$this->assertEquals('system', $this->ocrMyPdfCheck->getCategory());
}

public function testGetName(): void {
$this->l10n->method('t')->willReturn('Is OCRmyPDF installed');
$this->assertEquals('Is OCRmyPDF installed', $this->ocrMyPdfCheck->getName());
}

public function testRunOcrMyPdfNotInstalled(): void {
$this->command->method('setCommand')->willReturnSelf();
$this->command->method('execute')->willReturn(true);
$this->command->method('getExitCode')->willReturn(127);

$this->l10n->method('t')->willReturn('OCRmyPDF CLI is not installed.');

$result = $this->ocrMyPdfCheck->run();
$this->assertInstanceOf(SetupResult::class, $result);
$this->assertEquals(SetupResult::ERROR, $result->getSeverity());
$this->assertEquals('OCRmyPDF CLI is not installed.', $result->getDescription());
}

public function testRunOcrMyPdfNotWorkingCorrectly(): void {
$this->command->method('setCommand')->willReturnSelf();
$this->command->method('execute')->willReturn(true);
$this->command->method('getExitCode')->willReturn(1);
$this->command->method('getError')->willReturn('Some error');

$this->l10n->expects($this->once())->method('t')
->with('OCRmyPDF CLI is not working correctly. Error was: %1$s', ['Some error'])
->willReturn('OCRmyPDF CLI is not working correctly. Error was: Some error');

$result = $this->ocrMyPdfCheck->run();
$this->assertInstanceOf(SetupResult::class, $result);
$this->assertEquals(SetupResult::ERROR, $result->getSeverity());
$this->assertEquals('OCRmyPDF CLI is not working correctly. Error was: Some error', $result->getDescription());
}

public function testRunOcrMyPdfInstalled(): void {
$this->command->method('setCommand')->willReturnSelf();
$this->command->method('execute')->willReturn(true);
$this->command->method('getExitCode')->willReturn(0);
$this->command->method('getOutput')->willReturn('12.0.0');

$this->l10n->expects($this->once())->method('t')
->with('OCRmyPDF is installed and has version %1$s.', ['12.0.0'])
->willReturn('OCRmyPDF is installed and has version 12.0.0.');

$result = $this->ocrMyPdfCheck->run();
$this->assertInstanceOf(SetupResult::class, $result);
$this->assertEquals(SetupResult::SUCCESS, $result->getSeverity());
$this->assertEquals('OCRmyPDF is installed and has version 12.0.0.', $result->getDescription());
}
}

0 comments on commit 7bddb53

Please sign in to comment.