libresign/tests/php/Unit/Handler/PdfTest.php
Vitor Mattos 07a231aea2
fix: isolate PHP-pdftk dependency
I identified some issues with the follow log:

```
[PHP] Warning: include(): Failed opening '<redacted>/apps/richdocuments/vendor/composer/../mikehaertl/php-pdftk/src/Pdf.php' for inclusion (include_path='<redacted>/3rdparty/pear/archive_tar:/<redacted>/3rdparty/pear/console_getopt:<redacted>/3rdparty/pear/pear-core-minimal/src:/<redacted>/3rdparty/pear/pear_exception:/<redacted>/apps') at /<redacted>/lib/composer/composer/ClassLoader.php#576
	GET /ocs/v2.php/apps/libresign/api/v1/file/validate/file_id/2556
```

It's only occurr when use the app richdocuments, I saw that the package
mikehaertl/php-pdftk also is used by this app and because this the
autoload was mixed.

To solve this issue I used the package humbug/php-scoper that isolate
the dependency into a different namespace.

I also followed this article:

https://arthur-schiwon.de/isolating-nextcloud-app-dependencies-php-scoper

And the follow search results to see implementation examples:

https://github.com/search?q=OCA+path%3Ascoper.inc.php&type=code
https://github.com/search?q=org%3Anextcloud+path%3Ascoper.inc.php&type=code

Considering that we already have tests to cover the usage of pdftk,
isn't necessary to add new tests, if the test pass, this change worked
fine because loaded the isolated packages and the isolated packages made
the necessary.

Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
2025-09-03 09:55:55 -03:00

94 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Handler\PdfTk\Pdf;
use OCA\Libresign\Helper\JavaHelper;
use OCP\IAppConfig;
use OCP\IL10N;
use OCP\L10N\IFactory as IL10NFactory;
use PHPUnit\Framework\MockObject\MockObject;
/**
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
final class PdfTest extends \OCA\Libresign\Tests\Unit\TestCase {
private JavaHelper&MockObject $javaHelper;
private IAppConfig $appConfig;
private IL10N $l10n;
public function setUp(): void {
parent::setUp();
$this->javaHelper = $this->createMock(JavaHelper::class);
$this->appConfig = $this->getMockAppConfig();
$this->l10n = \OCP\Server::get(IL10NFactory::class)->get(Application::APP_ID);
}
private function getInstance(array $methods = []): Pdf|MockObject {
if ($methods) {
return $this->getMockBuilder(Pdf::class)
->setConstructorArgs([
$this->javaHelper,
$this->appConfig,
$this->l10n,
])
->onlyMethods($methods)
->getMock();
}
return new Pdf(
$this->javaHelper,
$this->appConfig,
$this->l10n,
);
}
public function testApplyStampReturnsBufferWhenSuccess(): void {
$pdf = $this->getInstance(['configureCommand', 'multiStamp']);
$mock = $this->createMock(\OCA\Libresign\Vendor\mikehaertl\pdftk\Pdf::class);
$mock->method('toString')->willReturn('%PDF-1.4 fake');
$pdf->method('multiStamp')->willReturn($mock);
$result = $pdf->applyStamp('/tmp/test.pdf', '/tmp/stamp.pdf');
$this->assertSame('%PDF-1.4 fake', $result);
}
public function testApplyStampThrowsWhenHaventJavaPath(): void {
$this->javaHelper->method('getJavaPath')->willReturn('');
$this->appConfig->setValueString(Application::APP_ID, 'pdftk_path', '/fake/path');
$pdf = $this->getInstance();
$this->expectException(RuntimeException::class);
$this->expectExceptionMessageMatches('/Java/');
$pdf->applyStamp('/tmp/input.pdf', '/tmp/stamp.pdf');
}
public function testApplyStampThrowsWhenHaventPdftk(): void {
$this->javaHelper->method('getJavaPath')->willReturn('/fake/path');
$this->appConfig->setValueString(Application::APP_ID, 'pdftk_path', '');
$pdf = $this->getInstance();
$this->expectException(RuntimeException::class);
$this->expectExceptionMessageMatches('/PDFtk/');
$pdf->applyStamp('/tmp/input.pdf', '/tmp/stamp.pdf');
}
public function testInvalidDependenciesPath(): void {
$this->javaHelper->method('getJavaPath')->willReturn('/fake/path');
$this->appConfig->setValueString(Application::APP_ID, 'pdftk_path', '/fake/path');
$pdf = $this->getInstance();
$this->expectException(LibresignException::class);
$this->expectExceptionMessageMatches('/set up/');
$pdf->applyStamp('/tmp/input.pdf', '/tmp/stamp.pdf');
}
}