Skip to content

Commit

Permalink
Improve test coverate to PdfParser
Browse files Browse the repository at this point in the history
  • Loading branch information
vitormattos committed May 6, 2022
1 parent 24ce51d commit da35e08
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 41 deletions.
2 changes: 1 addition & 1 deletion lib/Command/Configure/Cfssl.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$configPath = $this->getFullPath() . DIRECTORY_SEPARATOR . 'cfssl_config' . DIRECTORY_SEPARATOR;
$cfsslUri = 'http://127.0.0.1:8888/api/v1/cfssl/';
} else {
$output->writeln('CFSSL binary not found!');
$output->writeln('<info>CFSSL binary not found! run libresign:istall --cfssl first.</info>');
if (!$configPath = $input->getOption('config-path')) {
throw new InvalidArgumentException('Invalid config path');
}
Expand Down
42 changes: 3 additions & 39 deletions lib/Migration/Version2040Date20211027183759.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
use OCP\DB\ISchemaWrapper;
use OCP\Files\File;
use OCP\Files\IRootFolder;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\NullOutput;

class Version2040Date20211027183759 extends SimpleMigrationStep {
/** @var IDBConnection */
private $connection;
/** @var IRootFolder*/
private $root;
/** @var PdfParserService */
Expand All @@ -28,10 +27,6 @@ public function __construct(IDBConnection $connection,
IRootFolder $root,
PdfParserService $PdfParserService) {
$this->connection = $connection;
$this->install = $install;
$this->config = $config;
$this->systemConfig = $systemConfig;
$this->rootFolder = $rootfolder;
$this->root = $root;
$this->PdfParserService = $PdfParserService;
}
Expand All @@ -40,7 +35,7 @@ public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array
$query = $this->connection->getQueryBuilder();
$query->select('id', 'node_id', 'user_id')
->from('libresign_file', 'f');
$this->rows = $query->execute()->fetchAll();
$this->rows = $query->executeQuery()->fetchAll();
}

/**
Expand All @@ -65,7 +60,6 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
}

public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options): void {
$cli = $this->getLibesignCli();
foreach ($this->rows as $row) {
$userFolder = $this->root->getUserFolder($row['user_id']);
/** @var File[] */
Expand All @@ -83,34 +77,4 @@ public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array
}
}
}

private function getMetadataFromCli(string $cli, string $filePath): array {
$fullPath = $this->getDataDir() . $filePath;
$json = shell_exec($cli . ' info ' . $fullPath);
$array = json_decode($json, true);
$output = [
'p' => count($array['pages']),
'extension' => 'pdf',
];
foreach ($array['pages'] as $page) {
$output['d'][] = [
'w' => $page['width'],
'h' => $page['height'],
];
}
return $output;
}

private function getDataDir(): string {
return $this->systemConfig->getValue('datadirectory', \OC::$SERVERROOT . '/data/');
}

private function getLibesignCli(): string {
$path = $this->config->getAppValue(Application::APP_ID, 'libresign_cli_path');
if (!file_exists($path)) {
$this->install->run(new StringInput('--cli'), new NullOutput());
$path = $this->config->getAppValue(Application::APP_ID, 'libresign_cli_path');
}
return $path;
}
}
9 changes: 9 additions & 0 deletions lib/Service/PdfParserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use OC\SystemConfig;
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Exception\LibresignException;
use OCP\IConfig;

class PdfParserService {
Expand Down Expand Up @@ -31,10 +32,18 @@ private function getDataDir(): string {
return $this->systemConfig->getValue('datadirectory', \OC::$SERVERROOT . '/data/');
}

/**
* @param string $filePath
* @return array
* @throws LibresignException
*/
public function getMetadata(string $filePath): array {
$fullPath = $this->getDataDir() . $filePath;
$json = shell_exec($this->cliPath . ' info ' . $fullPath);
$array = json_decode($json, true);
if (!is_array($array)) {
throw new LibresignException('Impossible get metadata from this file. Check if you installed correctly the libresign-cli.');
}
$output = [
'p' => count($array['pages']),
];
Expand Down
1 change: 0 additions & 1 deletion lib/Service/SignFileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Handler\Pkcs7Handler;
use OCA\Libresign\Handler\Pkcs12Handler;
use OCA\Libresign\Handler\TCPDILibresign;
use OCA\Libresign\Helper\JSActions;
use OCA\Libresign\Helper\ValidateHelper;
use OCP\Accounts\IAccountManager;
Expand Down
90 changes: 90 additions & 0 deletions tests/Unit/Service/PdfParseServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace OCA\Libresign\Tests\Unit\Service;

use OC\SystemConfig;
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Service\InstallService;
use OCA\Libresign\Service\PdfParserService;
use OCP\IConfig;
use PHPUnit\Framework\MockObject\MockObject;

/**
* @internal
*/
final class PdfParseServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
/** @var IConfig|MockObject */
private $config;
/** @var SystemConfig|MockObject */
private $systemConfig;
/** @var InstallService|MockObject */
private $installService;

public function setUp(): void {
$this->config = $this->createMock(IConfig::class);
$this->systemConfig = $this->createMock(SystemConfig::class);
$this->installService = $this->createMock(InstallService::class);
}

private function getService(): PdfParserService {
return new PdfParserService(
$this->config,
$this->systemConfig,
$this->installService
);
}

public function testGetMetadataWithFail(): void {
$this->expectException(LibresignException::class);
$this->config
->method('getAppValue')
->willReturnCallback(function ($appid, $key, $default) {
switch ($key) {
case 'libresign_cli_path': return '/fake_path/';
}
});
$this->systemConfig
->method('getValue')
->willReturnCallback(function ($key, $default) {
switch ($key) {
case 'datadirectory': return $default;
}
});
$path = '/fake';
$this->getService()->getMetadata($path);
}

/**
* @dataProvider providerGetMetadataWIthSuccess
*/
public function testGetMetadataWIthSuccess(string $path, array $expected): void {
$this->installService = \OC::$server->get(InstallService::class);
$this->systemConfig = \OC::$server->get(SystemConfig::class);
$this->config = \OC::$server->get(IConfig::class);
$actual = $this->getService()->getMetadata($path);
$this->assertEquals($expected, $actual);
}

public function providerGetMetadataWIthSuccess(): array {
return [
[
'/../apps/libresign/tests/fixtures/small_valid.pdf',
[
'p' => 1,
'd' => [
['w' => 595.276, 'h' => 841.89],
],
]
],
[
'/../apps/libresign/tests/fixtures/small_valid-signed.pdf',
[
'p' => 1,
'd' => [
['w' => 595.276, 'h' => 841.89],
],
]
],
];
}
}

0 comments on commit da35e08

Please sign in to comment.