diff --git a/README.md b/README.md index 43f1a4d..07dfecc 100644 --- a/README.md +++ b/README.md @@ -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`. + +

+ Setup checks +

+ ## Usage You can configure the OCR processing via Nextcloud's workflow engine. Therefore configure a new flow via `Settings` → `Flow` → `Add new flow` (if you don't see `OCR file` here the app isn't installed properly or you forgot to activate it). diff --git a/doc/img/setup_checks.jpg b/doc/img/setup_checks.jpg new file mode 100644 index 0000000..bd257de Binary files /dev/null and b/doc/img/setup_checks.jpg differ diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index a8a459d..b8d7f69 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -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; @@ -99,6 +100,7 @@ public function register(IRegistrationContext $context): void { $context->registerEventListener(RegisterOperationsEvent::class, RegisterFlowOperationsListener::class); $context->registerNotifierService(Notifier::class); + $context->registerSetupCheck(OcrMyPdfCheck::class); } /** diff --git a/lib/SetupChecks/OcrMyPdfCheck.php b/lib/SetupChecks/OcrMyPdfCheck.php new file mode 100644 index 0000000..c8d8d73 --- /dev/null +++ b/lib/SetupChecks/OcrMyPdfCheck.php @@ -0,0 +1,60 @@ + + * + * @author Robin Windey + * + * @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 . + * + */ + +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])); + } +} diff --git a/tests/Unit/SetupChecks/OcrMyPdfCheckTest.php b/tests/Unit/SetupChecks/OcrMyPdfCheckTest.php new file mode 100644 index 0000000..9125ad1 --- /dev/null +++ b/tests/Unit/SetupChecks/OcrMyPdfCheckTest.php @@ -0,0 +1,103 @@ + + * + * @author Robin Windey + * + * @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 . + * + */ + +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()); + } +}