Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Db/FileMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public function neutralizeDeletedUser(string $userId, string $displayName): void
*/
public function getChildrenFiles(int $parentId): array {
$cached = array_filter($this->file, fn ($f) => $f->getParentFileId() === $parentId);
if (!empty($cached)) {
if (!empty($cached) && count($cached) > 1) {
return array_values($cached);
}

Expand Down
13 changes: 11 additions & 2 deletions lib/Service/IdentifyMethod/IdentifyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,25 @@ private function propagateIdentifiedDateToEnvelopeChildren(IdentifyMethod $ident
$signRequest = $this->signRequestMapper->getById($identifyMethod->getSignRequestId());
$fileEntity = $this->fileMapper->getById($signRequest->getFileId());

if (method_exists($fileEntity, 'getNodeType') && $fileEntity->getNodeType() !== 'envelope') {
if ($fileEntity->getNodeType() === 'envelope') {
$envelopeId = $fileEntity->getId();
} elseif ($fileEntity->hasParent()) {
$envelopeId = $fileEntity->getParentFileId();
} else {
return;
}

$children = $this->signRequestMapper->getByEnvelopeChildrenAndIdentifyMethod(
$fileEntity->getId(),
$envelopeId,
$signRequest->getId(),
);

foreach ($children as $childSignRequest) {
// Skip the current sign request to avoid updating it twice
if ($childSignRequest->getId() === $signRequest->getId()) {
continue;
}

$childMethods = $this->identifyMethodMapper->getIdentifyMethodsFromSignRequestId($childSignRequest->getId());

foreach ($childMethods as $childEntity) {
Expand Down
11 changes: 8 additions & 3 deletions lib/Service/SignFileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,21 +388,26 @@ public function sign(): void {
* @return array Array of ['file' => FileEntity, 'signRequest' => SignRequestEntity]
*/
private function getSignRequestsToSign(): array {
if (!$this->libreSignFile->isEnvelope()) {
if (!$this->libreSignFile->isEnvelope()
&& !$this->libreSignFile->hasParent()
) {
return [[
'file' => $this->libreSignFile,
'signRequest' => $this->signRequest,
]];
}

$childFiles = $this->fileMapper->getChildrenFiles($this->libreSignFile->getId());
$envelopeId = $this->libreSignFile->isEnvelope()
? $this->libreSignFile->getId()
: $this->libreSignFile->getParentFileId();

$childFiles = $this->fileMapper->getChildrenFiles($envelopeId);
if (empty($childFiles)) {
throw new LibresignException('No files found in envelope');
}

$childSignRequests = $this->signRequestMapper->getByEnvelopeChildrenAndIdentifyMethod(
$this->libreSignFile->getId(),
$envelopeId,
$this->signRequest->getId()
);

Expand Down
152 changes: 152 additions & 0 deletions tests/php/Unit/Service/IdentifyMethod/IdentifyServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Libresign\Tests\Unit\Service\IdentifyMethod;

use OCA\Libresign\Db\File;
use OCA\Libresign\Db\FileMapper;
use OCA\Libresign\Db\IdentifyMethod;
use OCA\Libresign\Db\IdentifyMethodMapper;
use OCA\Libresign\Db\SignRequest;
use OCA\Libresign\Db\SignRequestMapper;
use OCA\Libresign\Service\IdentifyMethod\IdentifyService;
use OCA\Libresign\Service\SessionService;
use OCA\Libresign\Tests\Unit\TestCase;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\IRootFolder;
use OCP\IAppConfig;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\Security\IHasher;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;

final class IdentifyServiceTest extends TestCase {
private IdentifyMethodMapper&MockObject $identifyMethodMapper;
private SessionService&MockObject $sessionService;
private ITimeFactory&MockObject $timeFactory;
private IEventDispatcher&MockObject $eventDispatcher;
private IRootFolder&MockObject $rootFolder;
private IAppConfig&MockObject $appConfig;
private SignRequestMapper&MockObject $signRequestMapper;
private IL10N&MockObject $l10n;
private FileMapper&MockObject $fileMapper;
private IHasher&MockObject $hasher;
private IUserManager&MockObject $userManager;
private IURLGenerator&MockObject $urlGenerator;
private LoggerInterface&MockObject $logger;
private IdentifyService $service;

public function setUp(): void {
parent::setUp();

$this->identifyMethodMapper = $this->createMock(IdentifyMethodMapper::class);
$this->sessionService = $this->createMock(SessionService::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->rootFolder = $this->createMock(IRootFolder::class);
$this->appConfig = $this->createMock(IAppConfig::class);
$this->signRequestMapper = $this->createMock(SignRequestMapper::class);
$this->l10n = $this->createMock(IL10N::class);
$this->fileMapper = $this->createMock(FileMapper::class);
$this->hasher = $this->createMock(IHasher::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->logger = $this->createMock(LoggerInterface::class);

$this->service = new IdentifyService(
$this->identifyMethodMapper,
$this->sessionService,
$this->timeFactory,
$this->eventDispatcher,
$this->rootFolder,
$this->appConfig,
$this->signRequestMapper,
$this->l10n,
$this->fileMapper,
$this->hasher,
$this->userManager,
$this->urlGenerator,
$this->logger,
);
}

public function testPropagateIdentifiedDateSkipsCurrentRequestAndUpdatesSiblings(): void {
$identifyMethod = new IdentifyMethod();
$identifyMethod->setSignRequestId(1);
$identifyMethod->setIdentifierKey('email');
$identifyMethod->setIdentifierValue('[email protected]');
$identifyMethod->setIdentifiedAtDate('2024-01-01T00:00:00Z');

$parentEnvelopeId = 99;

$currentFile = new File();
$currentFile->setId(10);
$currentFile->setParentFileId($parentEnvelopeId);

$currentSignRequest = new SignRequest();
$currentSignRequest->setId(1);
$currentSignRequest->setFileId($currentFile->getId());

$siblingFile = new File();
$siblingFile->setId(11);
$siblingFile->setParentFileId($parentEnvelopeId);

$siblingSignRequest = new SignRequest();
$siblingSignRequest->setId(2);
$siblingSignRequest->setFileId($siblingFile->getId());

$this->signRequestMapper
->expects($this->once())
->method('getById')
->with($identifyMethod->getSignRequestId())
->willReturn($currentSignRequest);

$this->fileMapper
->expects($this->once())
->method('getById')
->with($currentFile->getId())
->willReturn($currentFile);

$this->signRequestMapper
->expects($this->once())
->method('getByEnvelopeChildrenAndIdentifyMethod')
->with($parentEnvelopeId, $currentSignRequest->getId())
->willReturn([$currentSignRequest, $siblingSignRequest]);

$siblingIdentifyMethod = new IdentifyMethod();
$siblingIdentifyMethod->setSignRequestId($siblingSignRequest->getId());
$siblingIdentifyMethod->setIdentifierKey($identifyMethod->getIdentifierKey());
$siblingIdentifyMethod->setIdentifierValue($identifyMethod->getIdentifierValue());

$this->identifyMethodMapper
->expects($this->exactly(2))
->method('getIdentifyMethodsFromSignRequestId')
->willReturnMap([
[$identifyMethod->getSignRequestId(), []],
[$siblingSignRequest->getId(), [$siblingIdentifyMethod]],
]);

$this->identifyMethodMapper
->expects($this->once())
->method('insertOrUpdate')
->with($identifyMethod);

$this->identifyMethodMapper
->expects($this->once())
->method('update')
->with($this->callback(function (IdentifyMethod $updated) use ($siblingIdentifyMethod, $identifyMethod) {
return $updated->getSignRequestId() === $siblingIdentifyMethod->getSignRequestId()
&& $updated->getIdentifiedAtDate() == $identifyMethod->getIdentifiedAtDate();
}));

$this->service->save($identifyMethod);
}
}
46 changes: 46 additions & 0 deletions tests/php/Unit/Service/SignFileServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,52 @@ public static function providerGetOrGeneratePfxContent(): array {
];
}

public function testGetSignRequestsToSignWhenFileHasParentEnvelope(): void {
$service = $this->getService();

$envelopeId = 99;
$childFile = new File();
$childFile->setId(10);
$childFile->setParentFileId($envelopeId);

$siblingFile = new File();
$siblingFile->setId(11);
$siblingFile->setParentFileId($envelopeId);

$signRequest = new SignRequest();
$signRequest->setId(200);
$signRequest->setFileId($childFile->getId());

$siblingSignRequest = new SignRequest();
$siblingSignRequest->setId(201);
$siblingSignRequest->setFileId($siblingFile->getId());

$this->fileMapper
->expects($this->once())
->method('getChildrenFiles')
->with($envelopeId)
->willReturn([$childFile, $siblingFile]);

$this->signRequestMapper
->expects($this->once())
->method('getByEnvelopeChildrenAndIdentifyMethod')
->with($envelopeId, $signRequest->getId())
->willReturn([$signRequest, $siblingSignRequest]);

$result = self::invokePrivate(
$service
->setLibreSignFile($childFile)
->setSignRequest($signRequest),
'getSignRequestsToSign'
);

$this->assertCount(2, $result);
$this->assertSame($childFile, $result[0]['file']);
$this->assertSame($signRequest, $result[0]['signRequest']);
$this->assertSame($siblingFile, $result[1]['file']);
$this->assertSame($siblingSignRequest, $result[1]['signRequest']);
}

#[DataProvider('providerStoreUserMetadata')]
public function testStoreUserMetadata(bool $collectMetadata, ?array $previous, array $new, ?array $expected): void {
$signRequest = new \OCA\Libresign\Db\SignRequest();
Expand Down
Loading