-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Do some basic check if OCRMyPDF CLI is working * Fixes #83
- Loading branch information
Showing
5 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |