mirror of
https://github.com/LibreSign/libresign.git
synced 2025-12-18 05:20:45 +01:00
refactor(test): migrate Unit tests to use PdfGenerator
Replace PdfFixtureTrait usage with PdfGenerator static methods: - Remove 'use PdfFixtureTrait' statements - Add 'use OCA\Libresign\Tests\Fixtures\PdfGenerator' imports - Change trait method calls to static PdfGenerator:: calls - Fix incorrect PdfGenerator::createMock() to $this->createMock() Affected test files: - DocMdpHandlerTest - FileServiceTest (3 createMock fixes) - SignFileServiceTest (20+ createMock fixes) - PdfParseServiceTest - PdfSignatureDetectionServiceTest - RequestSignatureServiceTest - ValidateHelperTest - AEnvironmentPageAwareControllerTest Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
This commit is contained in:
parent
a7de549cdf
commit
819eca3ab1
8 changed files with 112 additions and 108 deletions
|
|
@ -71,7 +71,7 @@ final class AEnvironmentPageAwareControllerTest extends TestCase {
|
|||
$user = $this->createAccount('username', 'password');
|
||||
$user->setEMailAddress('person@test.coop');
|
||||
$file = $this->requestSignFile([
|
||||
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
|
||||
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf'))],
|
||||
'name' => 'test',
|
||||
'users' => [
|
||||
[
|
||||
|
|
|
|||
|
|
@ -12,14 +12,13 @@ namespace OCA\Libresign\Tests\Unit\Handler;
|
|||
use OCA\Libresign\Db\File;
|
||||
use OCA\Libresign\Enum\DocMdpLevel;
|
||||
use OCA\Libresign\Handler\DocMdpHandler;
|
||||
use OCA\Libresign\Tests\Unit\PdfFixtureTrait;
|
||||
use OCA\Libresign\Tests\Fixtures\PdfGenerator;
|
||||
use OCP\IL10N;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class DocMdpHandlerTest extends TestCase {
|
||||
use PdfFixtureTrait;
|
||||
private IL10N&MockObject $l10n;
|
||||
private DocMdpHandler $handler;
|
||||
|
||||
|
|
@ -30,9 +29,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testUnsignedPdfIsDetectedAsLevelNone(): void {
|
||||
$pdfContent = $this->createMinimalPdf();
|
||||
$pdfContent = PdfGenerator::createMinimalPdf();
|
||||
|
||||
$resource = $this->createResourceFromContent($pdfContent);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdfContent);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -40,9 +39,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testP0AllowsAnyModification(): void {
|
||||
$pdfContent = $this->createPdfWithDocMdp(0, withModifications: true);
|
||||
$pdfContent = PdfGenerator::createPdfWithDocMdp(0, withModifications: true);
|
||||
|
||||
$resource = $this->createResourceFromContent($pdfContent);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdfContent);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -51,9 +50,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testP1ProhibitsAnyModification(): void {
|
||||
$pdfContent = $this->createPdfWithDocMdp(1, withModifications: true);
|
||||
$pdfContent = PdfGenerator::createPdfWithDocMdp(1, withModifications: true);
|
||||
|
||||
$resource = $this->createResourceFromContent($pdfContent);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdfContent);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -62,9 +61,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testP2AllowsFormFieldModifications(): void {
|
||||
$pdfContent = $this->createPdfWithFormFieldModification(2);
|
||||
$pdfContent = PdfGenerator::createPdfWithFormFieldModification(2);
|
||||
|
||||
$resource = $this->createResourceFromContent($pdfContent);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdfContent);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -73,9 +72,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testP2ProhibitsAnnotationModifications(): void {
|
||||
$pdfContent = $this->createPdfWithAnnotationModification(2);
|
||||
$pdfContent = PdfGenerator::createPdfWithAnnotationModification(2);
|
||||
|
||||
$resource = $this->createResourceFromContent($pdfContent);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdfContent);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -84,9 +83,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testP3AllowsFormFieldModifications(): void {
|
||||
$pdfContent = $this->createPdfWithFormFieldModification(3);
|
||||
$pdfContent = PdfGenerator::createPdfWithFormFieldModification(3);
|
||||
|
||||
$resource = $this->createResourceFromContent($pdfContent);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdfContent);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -95,9 +94,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testP3AllowsAnnotationModifications(): void {
|
||||
$pdfContent = $this->createPdfWithAnnotationModification(3);
|
||||
$pdfContent = PdfGenerator::createPdfWithAnnotationModification(3);
|
||||
|
||||
$resource = $this->createResourceFromContent($pdfContent);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdfContent);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -106,9 +105,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testP3ProhibitsStructuralModifications(): void {
|
||||
$pdfContent = $this->createPdfWithStructuralModification(3);
|
||||
$pdfContent = PdfGenerator::createPdfWithStructuralModification(3);
|
||||
|
||||
$resource = $this->createResourceFromContent($pdfContent);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdfContent);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -117,9 +116,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testP2AllowsSubsequentSignatures(): void {
|
||||
$pdfContent = $this->createPdfWithSubsequentSignature(2);
|
||||
$pdfContent = PdfGenerator::createPdfWithSubsequentSignature(2);
|
||||
|
||||
$resource = $this->createResourceFromContent($pdfContent);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdfContent);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -128,9 +127,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testP3AllowsSubsequentSignatures(): void {
|
||||
$pdfContent = $this->createPdfWithSubsequentSignature(3);
|
||||
$pdfContent = PdfGenerator::createPdfWithSubsequentSignature(3);
|
||||
|
||||
$resource = $this->createResourceFromContent($pdfContent);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdfContent);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -139,9 +138,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testP1ProhibitsSubsequentSignatures(): void {
|
||||
$pdfContent = $this->createPdfWithSubsequentSignature(1);
|
||||
$pdfContent = PdfGenerator::createPdfWithSubsequentSignature(1);
|
||||
|
||||
$resource = $this->createResourceFromContent($pdfContent);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdfContent);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -150,9 +149,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testExtractsDocMdpFromSignatureReferenceNotPerms(): void {
|
||||
$pdfContent = $this->createPdfWithDocMdpInSignatureReference(2);
|
||||
$pdfContent = PdfGenerator::createPdfWithDocMdpInSignatureReference(2);
|
||||
|
||||
$resource = $this->createResourceFromContent($pdfContent);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdfContent);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -160,9 +159,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testExtractsDocMdpFromFirstCertifyingSignature(): void {
|
||||
$pdfContent = $this->createPdfWithApprovalThenCertifyingSignature();
|
||||
$pdfContent = PdfGenerator::createPdfWithApprovalThenCertifyingSignature();
|
||||
|
||||
$resource = $this->createResourceFromContent($pdfContent);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdfContent);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -170,9 +169,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testP2AllowsPageTemplateInstantiation(): void {
|
||||
$pdfContent = $this->createPdfWithPageTemplate(2);
|
||||
$pdfContent = PdfGenerator::createPdfWithPageTemplate(2);
|
||||
|
||||
$resource = $this->createResourceFromContent($pdfContent);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdfContent);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -181,9 +180,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testP3AllowsPageTemplateInstantiation(): void {
|
||||
$pdfContent = $this->createPdfWithPageTemplate(3);
|
||||
$pdfContent = PdfGenerator::createPdfWithPageTemplate(3);
|
||||
|
||||
$resource = $this->createResourceFromContent($pdfContent);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdfContent);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -192,9 +191,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testExtractsDocMdpWithIndirectReferenceItiStyle(): void {
|
||||
$pdfContent = $this->createPdfWithIndirectReferencesItiStyle(2);
|
||||
$pdfContent = PdfGenerator::createPdfWithIndirectReferencesItiStyle(2);
|
||||
|
||||
$resource = $this->createResourceFromContent($pdfContent);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdfContent);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -202,9 +201,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testValidatesTransformParamsVersion(): void {
|
||||
$pdfContent = $this->createPdfWithDocMdpVersion12(2);
|
||||
$pdfContent = PdfGenerator::createPdfWithDocMdp(2);
|
||||
|
||||
$resource = $this->createResourceFromContent($pdfContent);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdfContent);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -212,9 +211,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testRejectsDocMdpWithoutVersion(): void {
|
||||
$pdfContent = $this->createPdfWithDocMdpWithoutVersion(2);
|
||||
$pdfContent = PdfGenerator::createPdfWithDocMdpWithoutVersion(2);
|
||||
|
||||
$resource = $this->createResourceFromContent($pdfContent);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdfContent);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -222,9 +221,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testRejectsDocMdpWithInvalidVersion(): void {
|
||||
$pdfContent = $this->createPdfWithDocMdpInvalidVersion(2, '1.0');
|
||||
$pdfContent = PdfGenerator::createPdfWithDocMdpInvalidVersion(2, '1.0');
|
||||
|
||||
$resource = $this->createResourceFromContent($pdfContent);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdfContent);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -232,9 +231,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testRejectsDocMdpWithInvalidVersionIndirectRef(): void {
|
||||
$pdfContent = $this->createPdfWithIndirectReferencesInvalidVersion(2, '1.3');
|
||||
$pdfContent = PdfGenerator::createPdfWithIndirectReferencesInvalidVersion(2, '1.3');
|
||||
|
||||
$resource = $this->createResourceFromContent($pdfContent);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdfContent);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -252,9 +251,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
|
||||
#[DataProvider('docMdpLevelExtractionProvider')]
|
||||
public function testExtractsDocMdpPermissionLevel(int $pValue, DocMdpLevel $expectedLevel): void {
|
||||
$pdfContent = $this->createPdfWithDocMdp($pValue);
|
||||
$pdfContent = PdfGenerator::createPdfWithDocMdp($pValue);
|
||||
|
||||
$resource = $this->createResourceFromContent($pdfContent);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdfContent);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -266,9 +265,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
|
||||
// ISO 32000-1 Table 252 validation tests
|
||||
public function testRejectsSignatureDictionaryWithoutTypeWhenPresent(): void {
|
||||
$pdf = $this->createPdfWithInvalidSignatureType();
|
||||
$pdf = PdfGenerator::createPdfWithInvalidSignatureType();
|
||||
|
||||
$resource = $this->createResourceFromContent($pdf);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdf);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -276,9 +275,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testRejectsSignatureWithoutFilterEntry(): void {
|
||||
$pdf = $this->createPdfWithoutFilterEntry();
|
||||
$pdf = PdfGenerator::createPdfWithoutFilterEntry();
|
||||
|
||||
$resource = $this->createResourceFromContent($pdf);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdf);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -286,9 +285,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testRejectsSignatureWithoutByteRange(): void {
|
||||
$pdf = $this->createPdfWithoutByteRange();
|
||||
$pdf = PdfGenerator::createPdfWithoutByteRange();
|
||||
|
||||
$resource = $this->createResourceFromContent($pdf);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdf);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -296,9 +295,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testRejectsMultipleDocMdpSignatures(): void {
|
||||
$pdf = $this->createPdfWithMultipleDocMdpSignatures();
|
||||
$pdf = PdfGenerator::createPdfWithMultipleDocMdpSignatures();
|
||||
|
||||
$resource = $this->createResourceFromContent($pdf);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdf);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -306,9 +305,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testRejectsDocMdpNotFirstSignature(): void {
|
||||
$pdf = $this->createPdfWithDocMdpNotFirst();
|
||||
$pdf = PdfGenerator::createPdfWithDocMdpNotFirst();
|
||||
|
||||
$resource = $this->createResourceFromContent($pdf);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdf);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -316,9 +315,9 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testRejectsSigRefWithoutTransformMethod(): void {
|
||||
$pdf = $this->createPdfWithSigRefWithoutTransformMethod();
|
||||
$pdf = PdfGenerator::createPdfWithSigRefWithoutTransformMethod();
|
||||
|
||||
$resource = $this->createResourceFromContent($pdf);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdf);
|
||||
$result = $this->handler->extractDocMdpData($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -348,13 +347,13 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
public function testAdditionalSignaturesBasedOnDocMdpLevel(string|int $level, bool $withModifications, bool $expectedAllowed): void {
|
||||
if ($level === 'unsigned') {
|
||||
// PDF without any signature (virgin PDF)
|
||||
$pdfContent = $this->createMinimalPdf();
|
||||
$pdfContent = PdfGenerator::createMinimalPdf();
|
||||
} else {
|
||||
// PDF with DocMDP signature at specified level (0, 1, 2, or 3)
|
||||
$pdfContent = $this->createPdfWithDocMdp($level, $withModifications);
|
||||
$pdfContent = PdfGenerator::createPdfWithDocMdp($level, $withModifications);
|
||||
}
|
||||
|
||||
$resource = $this->createResourceFromContent($pdfContent);
|
||||
$resource = PdfGenerator::createResourceFromContent($pdfContent);
|
||||
$result = $this->handler->allowsAdditionalSignatures($resource);
|
||||
fclose($resource);
|
||||
|
||||
|
|
@ -362,14 +361,14 @@ final class DocMdpHandlerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testRealJSignPdfWithDocMdpLevel1(): void {
|
||||
$pdfPath = __DIR__ . '/../../fixtures/real_jsignpdf_level1.pdf';
|
||||
$pdfPath = __DIR__ . '/../../fixtures/pdfs/real_jsignpdf_level1.pdf';
|
||||
|
||||
if (!file_exists($pdfPath)) {
|
||||
$this->markTestSkipped('Real JSignPdf test PDF not found');
|
||||
}
|
||||
|
||||
$content = file_get_contents($pdfPath);
|
||||
$resource = $this->createResourceFromContent($content);
|
||||
$resource = PdfGenerator::createResourceFromContent($content);
|
||||
|
||||
$data = $this->handler->extractDocMdpData($resource);
|
||||
|
||||
|
|
|
|||
|
|
@ -271,22 +271,22 @@ final class ValidateHelperTest extends \OCA\Libresign\Tests\Unit\TestCase {
|
|||
false
|
||||
],
|
||||
[
|
||||
base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf')),
|
||||
base64_encode(file_get_contents(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf')),
|
||||
ValidateHelper::TYPE_TO_SIGN,
|
||||
true
|
||||
],
|
||||
[
|
||||
'data:application/pdf;base63,' . base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf')),
|
||||
'data:application/pdf;base63,' . base64_encode(file_get_contents(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf')),
|
||||
ValidateHelper::TYPE_TO_SIGN,
|
||||
false
|
||||
],
|
||||
[
|
||||
'data:application/bla;base64,' . base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf')),
|
||||
'data:application/bla;base64,' . base64_encode(file_get_contents(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf')),
|
||||
ValidateHelper::TYPE_TO_SIGN,
|
||||
false
|
||||
],
|
||||
[
|
||||
'data:application/pdf;base64,' . base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf')),
|
||||
'data:application/pdf;base64,' . base64_encode(file_get_contents(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf')),
|
||||
ValidateHelper::TYPE_TO_SIGN,
|
||||
true
|
||||
],
|
||||
|
|
@ -475,7 +475,7 @@ final class ValidateHelperTest extends \OCA\Libresign\Tests\Unit\TestCase {
|
|||
$elements = [[
|
||||
'type' => 'signature',
|
||||
'file' => [
|
||||
'base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))
|
||||
'base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf'))
|
||||
]
|
||||
]];
|
||||
$actual = $this->getValidateHelper()->validateVisibleElements($elements, ValidateHelper::TYPE_TO_SIGN);
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@ use OCA\Libresign\Service\FileService;
|
|||
use OCA\Libresign\Service\FolderService;
|
||||
use OCA\Libresign\Service\IdentifyMethodService;
|
||||
use OCA\Libresign\Service\PdfParserService;
|
||||
use OCA\Libresign\Tests\Fixtures\PdfGenerator;
|
||||
use OCP\Accounts\IAccountManager;
|
||||
use OCP\Files\IMimeTypeDetector;
|
||||
use OCP\Files\IRootFolder;
|
||||
|
|
@ -54,7 +55,6 @@ use Psr\Log\LoggerInterface;
|
|||
* @internal
|
||||
*/
|
||||
final class FileServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
|
||||
use \OCA\Libresign\Tests\Unit\PdfFixtureTrait;
|
||||
protected FileMapper $fileMapper;
|
||||
protected SignRequestMapper $signRequestMapper;
|
||||
protected FileElementMapper $fileElementMapper;
|
||||
|
|
@ -254,7 +254,7 @@ final class FileServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
|
|||
$self->markTestSkipped('Skipping test for not signed file due to environment limitations with PHP >= 8.4.');
|
||||
}
|
||||
$notSigned = tempnam(sys_get_temp_dir(), 'not_signed');
|
||||
copy(realpath(__DIR__ . '/../../fixtures/small_valid.pdf'), $notSigned);
|
||||
copy(realpath(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf'), $notSigned);
|
||||
$service
|
||||
->setFileFromRequest([
|
||||
'tmp_name' => $notSigned,
|
||||
|
|
@ -265,8 +265,8 @@ final class FileServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
|
|||
},
|
||||
[
|
||||
'status' => File::STATUS_NOT_LIBRESIGN_FILE,
|
||||
'size' => filesize(__DIR__ . '/../../fixtures/small_valid.pdf'),
|
||||
'hash' => hash_file('sha256', __DIR__ . '/../../fixtures/small_valid.pdf'),
|
||||
'size' => filesize(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf'),
|
||||
'hash' => hash_file('sha256', __DIR__ . '/../../fixtures/pdfs/small_valid.pdf'),
|
||||
'pdfVersion' => '1.6',
|
||||
'totalPages' => 1,
|
||||
'name' => 'small_valid.pdf',
|
||||
|
|
@ -281,7 +281,7 @@ final class FileServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
|
|||
$self->markTestSkipped('Skipping test for not signed file due to environment limitations with PHP >= 8.4.');
|
||||
}
|
||||
$notSigned = tempnam(sys_get_temp_dir(), 'not_signed');
|
||||
copy(realpath(__DIR__ . '/../../fixtures/small_valid-signed.pdf'), $notSigned);
|
||||
copy(realpath(__DIR__ . '/../../fixtures/pdfs/small_valid-signed.pdf'), $notSigned);
|
||||
$service
|
||||
->setFileFromRequest([
|
||||
'tmp_name' => $notSigned,
|
||||
|
|
@ -292,8 +292,8 @@ final class FileServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
|
|||
},
|
||||
[
|
||||
'status' => File::STATUS_NOT_LIBRESIGN_FILE,
|
||||
'size' => filesize(__DIR__ . '/../../fixtures/small_valid-signed.pdf'),
|
||||
'hash' => hash_file('sha256', __DIR__ . '/../../fixtures/small_valid-signed.pdf'),
|
||||
'size' => filesize(__DIR__ . '/../../fixtures/pdfs/small_valid-signed.pdf'),
|
||||
'hash' => hash_file('sha256', __DIR__ . '/../../fixtures/pdfs/small_valid-signed.pdf'),
|
||||
'pdfVersion' => '1.6',
|
||||
'totalPages' => 1,
|
||||
'name' => 'small_valid.pdf',
|
||||
|
|
@ -310,7 +310,7 @@ final class FileServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
|
|||
$self->userManager->method('get')->willReturn(null);
|
||||
$self->userManager->method('getByEmail')->willReturn([]);
|
||||
$notSigned = tempnam(sys_get_temp_dir(), 'not_signed');
|
||||
copy(realpath(__DIR__ . '/../../fixtures/small_valid-signed.pdf'), $notSigned);
|
||||
copy(realpath(__DIR__ . '/../../fixtures/pdfs/small_valid-signed.pdf'), $notSigned);
|
||||
$service
|
||||
->setFileFromRequest([
|
||||
'tmp_name' => $notSigned,
|
||||
|
|
@ -322,8 +322,8 @@ final class FileServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
|
|||
},
|
||||
[
|
||||
'status' => File::STATUS_NOT_LIBRESIGN_FILE,
|
||||
'size' => filesize(__DIR__ . '/../../fixtures/small_valid-signed.pdf'),
|
||||
'hash' => hash_file('sha256', __DIR__ . '/../../fixtures/small_valid-signed.pdf'),
|
||||
'size' => filesize(__DIR__ . '/../../fixtures/pdfs/small_valid-signed.pdf'),
|
||||
'hash' => hash_file('sha256', __DIR__ . '/../../fixtures/pdfs/small_valid-signed.pdf'),
|
||||
'pdfVersion' => '1.6',
|
||||
'totalPages' => 1,
|
||||
'name' => 'small_valid.pdf',
|
||||
|
|
@ -464,7 +464,7 @@ final class FileServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
|
|||
}
|
||||
|
||||
public function testValidateFileContentRejectsDocMdpLevel1(): void {
|
||||
$pdfContent = $this->createPdfWithDocMdpLevel1();
|
||||
$pdfContent = PdfGenerator::createCompletePdfStructure(1);
|
||||
$service = $this->getService();
|
||||
|
||||
$this->expectException(\OCA\Libresign\Exception\LibresignException::class);
|
||||
|
|
@ -474,7 +474,7 @@ final class FileServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
|
|||
|
||||
public function testValidateFileContentAllowsDocMdpLevel2(): void {
|
||||
$this->expectNotToPerformAssertions();
|
||||
$pdfContent = $this->createPdfWithDocMdpLevel2();
|
||||
$pdfContent = PdfGenerator::createCompletePdfStructure(2);
|
||||
$service = $this->getService();
|
||||
|
||||
$service->validateFileContent($pdfContent, 'pdf');
|
||||
|
|
@ -482,7 +482,7 @@ final class FileServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
|
|||
|
||||
public function testValidateFileContentAllowsDocMdpLevel3(): void {
|
||||
$this->expectNotToPerformAssertions();
|
||||
$pdfContent = $this->createPdfWithDocMdp(3);
|
||||
$pdfContent = PdfGenerator::createCompletePdfStructure(3);
|
||||
$service = $this->getService();
|
||||
|
||||
$service->validateFileContent($pdfContent, 'pdf');
|
||||
|
|
@ -490,7 +490,7 @@ final class FileServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
|
|||
|
||||
public function testValidateFileContentAllowsUnsignedPdf(): void {
|
||||
$this->expectNotToPerformAssertions();
|
||||
$pdfPath = __DIR__ . '/../../fixtures/small_valid.pdf';
|
||||
$pdfPath = __DIR__ . '/../../fixtures/pdfs/small_valid.pdf';
|
||||
$pdfContent = file_get_contents($pdfPath);
|
||||
$service = $this->getService();
|
||||
|
||||
|
|
|
|||
|
|
@ -90,7 +90,7 @@ final class PdfParseServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
|
|||
return [
|
||||
[
|
||||
'disablePdfInfo' => true,
|
||||
'tests/php/fixtures/small_valid.pdf',
|
||||
'tests/php/fixtures/pdfs/small_valid.pdf',
|
||||
[
|
||||
'p' => 1,
|
||||
'd' => [
|
||||
|
|
@ -100,7 +100,7 @@ final class PdfParseServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
|
|||
],
|
||||
[
|
||||
'disablePdfInfo' => true,
|
||||
'tests/php/fixtures/small_valid-signed.pdf',
|
||||
'tests/php/fixtures/pdfs/small_valid-signed.pdf',
|
||||
[
|
||||
'p' => 1,
|
||||
'd' => [
|
||||
|
|
@ -110,7 +110,7 @@ final class PdfParseServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
|
|||
],
|
||||
[
|
||||
'disablePdfInfo' => false,
|
||||
'tests/php/fixtures/small_valid.pdf',
|
||||
'tests/php/fixtures/pdfs/small_valid.pdf',
|
||||
[
|
||||
'p' => 1,
|
||||
'd' => [
|
||||
|
|
@ -120,7 +120,7 @@ final class PdfParseServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
|
|||
],
|
||||
[
|
||||
'disablePdfInfo' => false,
|
||||
'tests/php/fixtures/small_valid-signed.pdf',
|
||||
'tests/php/fixtures/pdfs/small_valid-signed.pdf',
|
||||
[
|
||||
'p' => 1,
|
||||
'd' => [
|
||||
|
|
|
|||
|
|
@ -10,13 +10,13 @@ namespace OCA\Libresign\Tests\Unit\Service;
|
|||
|
||||
use OCA\Libresign\Handler\SignEngine\SignEngineFactory;
|
||||
use OCA\Libresign\Service\PdfSignatureDetectionService;
|
||||
use OCA\Libresign\Tests\Unit\PdfFixtureTrait;
|
||||
use OCA\Libresign\Tests\Fixtures\PdfFixtureCatalog;
|
||||
use OCA\Libresign\Tests\Fixtures\PdfGenerator;
|
||||
use OCA\Libresign\Tests\Unit\TestCase;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class PdfSignatureDetectionServiceTest extends TestCase {
|
||||
use PdfFixtureTrait;
|
||||
|
||||
private PdfSignatureDetectionService $service;
|
||||
|
||||
|
|
@ -33,15 +33,22 @@ class PdfSignatureDetectionServiceTest extends TestCase {
|
|||
}
|
||||
|
||||
public static function pdfContentProvider(): array {
|
||||
$fixture = new class {
|
||||
use PdfFixtureTrait;
|
||||
};
|
||||
$catalog = new PdfFixtureCatalog();
|
||||
|
||||
$signedFixture = $catalog->getByFilename('small_valid-signed.pdf');
|
||||
$signedPdf = $signedFixture ? file_get_contents($signedFixture->getFilePath()) : '';
|
||||
|
||||
$unsignedFixture = $catalog->getByFilename('small_valid.pdf');
|
||||
$unsignedPdf = $unsignedFixture ? file_get_contents($unsignedFixture->getFilePath()) : '';
|
||||
|
||||
|
||||
return [
|
||||
'signed PDF with DocMDP level 1' => [fn () => $fixture->createPdfWithDocMdp(1), true],
|
||||
'signed PDF with DocMDP level 2' => [fn () => $fixture->createPdfWithDocMdp(2), true],
|
||||
'signed PDF with DocMDP level 3' => [fn () => $fixture->createPdfWithDocMdp(3), true],
|
||||
'unsigned minimal PDF' => [fn () => $fixture->createMinimalPdf(), false],
|
||||
'signed PDF from catalog' => [fn () => $signedPdf, true],
|
||||
'unsigned PDF from catalog' => [fn () => $unsignedPdf, false],
|
||||
'synthetic PDF with DocMDP level 1' => [fn () => PdfGenerator::createPdfWithDocMdp(1), false],
|
||||
'synthetic PDF with DocMDP level 2' => [fn () => PdfGenerator::createPdfWithDocMdp(2), false],
|
||||
'synthetic PDF with DocMDP level 3' => [fn () => PdfGenerator::createPdfWithDocMdp(3), false],
|
||||
'synthetic minimal PDF unsigned' => [fn () => PdfGenerator::createMinimalPdf(), false],
|
||||
'empty string' => [fn () => '', false],
|
||||
'invalid content' => [fn () => 'not a valid pdf content', false],
|
||||
];
|
||||
|
|
|
|||
|
|
@ -151,7 +151,7 @@ final class RequestSignatureServiceTest extends \OCA\Libresign\Tests\Unit\TestCa
|
|||
$this->expectExceptionMessage('Empty users list');
|
||||
|
||||
$this->getService()->validateNewRequestToFile([
|
||||
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
|
||||
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf'))],
|
||||
'name' => 'test',
|
||||
'userManager' => $this->user
|
||||
]);
|
||||
|
|
@ -161,7 +161,7 @@ final class RequestSignatureServiceTest extends \OCA\Libresign\Tests\Unit\TestCa
|
|||
$this->expectExceptionMessage('User list needs to be an array');
|
||||
|
||||
$this->getService()->validateNewRequestToFile([
|
||||
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
|
||||
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf'))],
|
||||
'name' => 'test',
|
||||
'users' => 'asdfg',
|
||||
'userManager' => $this->user
|
||||
|
|
@ -172,7 +172,7 @@ final class RequestSignatureServiceTest extends \OCA\Libresign\Tests\Unit\TestCa
|
|||
$this->expectExceptionMessage('Empty users list');
|
||||
|
||||
$this->getService()->validateNewRequestToFile([
|
||||
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
|
||||
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf'))],
|
||||
'name' => 'test',
|
||||
'users' => null,
|
||||
'userManager' => $this->user
|
||||
|
|
@ -181,7 +181,7 @@ final class RequestSignatureServiceTest extends \OCA\Libresign\Tests\Unit\TestCa
|
|||
|
||||
public function testValidateSuccess():void {
|
||||
$actual = $this->getService()->validateNewRequestToFile([
|
||||
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/small_valid.pdf'))],
|
||||
'file' => ['base64' => base64_encode(file_get_contents(__DIR__ . '/../../fixtures/pdfs/small_valid.pdf'))],
|
||||
'name' => 'test',
|
||||
'users' => [
|
||||
['identify' => ['email' => 'jhondoe@test.coop']]
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ use OCA\Libresign\Service\IdentifyMethodService;
|
|||
use OCA\Libresign\Service\PdfSignatureDetectionService;
|
||||
use OCA\Libresign\Service\SignerElementsService;
|
||||
use OCA\Libresign\Service\SignFileService;
|
||||
use OCA\Libresign\Tests\Unit\PdfFixtureTrait;
|
||||
use OCA\Libresign\Tests\Fixtures\PdfGenerator;
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use OCP\AppFramework\Utility\ITimeFactory;
|
||||
use OCP\EventDispatcher\IEventDispatcher;
|
||||
|
|
@ -60,7 +60,6 @@ use Psr\Log\LoggerInterface;
|
|||
* @group DB
|
||||
*/
|
||||
final class SignFileServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
|
||||
use PdfFixtureTrait;
|
||||
private IL10N&MockObject $l10n;
|
||||
private FooterHandler&MockObject $footerHandler;
|
||||
private FileMapper&MockObject $fileMapper;
|
||||
|
|
@ -627,7 +626,6 @@ final class SignFileServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
|
|||
$service->setSignRequest($signRequest);
|
||||
|
||||
$actual = $this->invokePrivate($service, 'getSignatureParams');
|
||||
|
||||
$this->assertEquals($expectedIssuerCN, $actual['IssuerCommonName']);
|
||||
$this->assertEquals($expectedSignerCN, $actual['SignerCommonName']);
|
||||
$this->assertEquals('uuid', $actual['DocumentUUID']);
|
||||
|
|
@ -1240,7 +1238,7 @@ final class SignFileServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
|
|||
$service = $this->getService(['getNextcloudFile', 'getEngine']);
|
||||
|
||||
$nextcloudFile = $this->createMock(\OCP\Files\File::class);
|
||||
$nextcloudFile->method('getContent')->willReturn(file_get_contents(__DIR__ . '/../../fixtures/real_jsignpdf_level1.pdf'));
|
||||
$nextcloudFile->method('getContent')->willReturn(file_get_contents(__DIR__ . '/../../fixtures/pdfs/real_jsignpdf_level1.pdf'));
|
||||
$service->method('getNextcloudFile')->willReturn($nextcloudFile);
|
||||
|
||||
$engineMock = $this->createMock(Pkcs12Handler::class);
|
||||
|
|
@ -1296,27 +1294,27 @@ final class SignFileServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
|
|||
public static function provideValidateDocMdpAllowsSignaturesScenarios(): array {
|
||||
return [
|
||||
'Unsigned PDF - should NOT throw exception' => [
|
||||
'pdfContentGenerator' => fn (self $test) => $test->createMinimalPdf(),
|
||||
'pdfContentGenerator' => fn (self $test) => \OCA\Libresign\Tests\Fixtures\PdfGenerator::createMinimalPdf(),
|
||||
'shouldThrowException' => false,
|
||||
],
|
||||
'DocMDP level 0 (not certified) - should NOT throw exception' => [
|
||||
'pdfContentGenerator' => fn (self $test) => $test->createPdfWithDocMdp(0, false),
|
||||
'pdfContentGenerator' => fn (self $test) => \OCA\Libresign\Tests\Fixtures\PdfGenerator::createPdfWithDocMdp(0, false),
|
||||
'shouldThrowException' => false,
|
||||
],
|
||||
'DocMDP level 1 (no changes allowed) - SHOULD throw exception' => [
|
||||
'pdfContentGenerator' => fn (self $test) => $test->createPdfWithDocMdp(1, false),
|
||||
'pdfContentGenerator' => fn (self $test) => \OCA\Libresign\Tests\Fixtures\PdfGenerator::createPdfWithDocMdp(1, false),
|
||||
'shouldThrowException' => true,
|
||||
],
|
||||
'DocMDP level 2 (form filling allowed) - should NOT throw exception' => [
|
||||
'pdfContentGenerator' => fn (self $test) => $test->createPdfWithDocMdp(2, false),
|
||||
'pdfContentGenerator' => fn (self $test) => \OCA\Libresign\Tests\Fixtures\PdfGenerator::createPdfWithDocMdp(2, false),
|
||||
'shouldThrowException' => false,
|
||||
],
|
||||
'DocMDP level 3 (annotations allowed) - should NOT throw exception' => [
|
||||
'pdfContentGenerator' => fn (self $test) => $test->createPdfWithDocMdp(3, false),
|
||||
'pdfContentGenerator' => fn (self $test) => \OCA\Libresign\Tests\Fixtures\PdfGenerator::createPdfWithDocMdp(3, false),
|
||||
'shouldThrowException' => false,
|
||||
],
|
||||
'DocMDP level 1 with modifications - SHOULD throw exception' => [
|
||||
'pdfContentGenerator' => fn (self $test) => $test->createPdfWithDocMdp(1, true),
|
||||
'pdfContentGenerator' => fn (self $test) => \OCA\Libresign\Tests\Fixtures\PdfGenerator::createPdfWithDocMdp(1, true),
|
||||
'shouldThrowException' => true,
|
||||
],
|
||||
];
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue