|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2025 LibreCode coop and contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\Libresign\Tests\Unit\Service; |
| 10 | + |
| 11 | +use OCA\Libresign\Exception\LibresignException; |
| 12 | +use OCA\Libresign\Service\Envelope\EnvelopeFileRelocator; |
| 13 | +use OCA\Libresign\Service\FolderService; |
| 14 | +use OCP\Files\Folder; |
| 15 | +use OCP\Files\Node; |
| 16 | +use OCP\IUser; |
| 17 | +use PHPUnit\Framework\MockObject\MockObject; |
| 18 | + |
| 19 | +class EnvelopeFileRelocatorTest extends \OCA\Libresign\Tests\Unit\TestCase { |
| 20 | + private FolderService&MockObject $folderService; |
| 21 | + private EnvelopeFileRelocator $relocator; |
| 22 | + |
| 23 | + public function setUp(): void { |
| 24 | + parent::setUp(); |
| 25 | + $this->folderService = $this->createMock(FolderService::class); |
| 26 | + $this->relocator = new EnvelopeFileRelocator($this->folderService); |
| 27 | + } |
| 28 | + |
| 29 | + public function testReturnsOriginalWhenAlreadyInside(): void { |
| 30 | + $sourceFile = $this->createMock(\OCP\Files\File::class); |
| 31 | + $sourceFile->method('getPath')->willReturn('/user/files/Envelope/doc.pdf'); |
| 32 | + |
| 33 | + $envelopeFolder = $this->createMock(Folder::class); |
| 34 | + $envelopeFolder->method('getPath')->willReturn('/user/files/Envelope'); |
| 35 | + |
| 36 | + $rootFolder = $this->createMock(Folder::class); |
| 37 | + $rootFolder->method('getFirstNodeById')->with(10)->willReturn($envelopeFolder); |
| 38 | + |
| 39 | + $this->folderService->expects($this->once())->method('setUserId')->with('u1'); |
| 40 | + $this->folderService->method('getUserRootFolder')->willReturn($rootFolder); |
| 41 | + |
| 42 | + $user = $this->createMock(IUser::class); |
| 43 | + $user->method('getUID')->willReturn('u1'); |
| 44 | + |
| 45 | + $result = $this->relocator->ensureFileInEnvelopeFolder($sourceFile, 10, $user); |
| 46 | + self::assertSame($sourceFile, $result); |
| 47 | + } |
| 48 | + |
| 49 | + public function testCopiesFileWhenOutside(): void { |
| 50 | + $sourceFile = $this->createMock(\OCP\Files\File::class); |
| 51 | + $sourceFile->method('getPath')->willReturn('/user/files/Other/doc.pdf'); |
| 52 | + $sourceFile->method('getName')->willReturn('doc.pdf'); |
| 53 | + $sourceFile->method('getContent')->willReturn('content'); |
| 54 | + |
| 55 | + $copiedFile = $this->createMock(\OCP\Files\File::class); |
| 56 | + |
| 57 | + $envelopeFolder = $this->createMock(Folder::class); |
| 58 | + $envelopeFolder->method('getPath')->willReturn('/user/files/Envelope'); |
| 59 | + $envelopeFolder->expects($this->once()) |
| 60 | + ->method('newFile') |
| 61 | + ->with('doc.pdf', 'content') |
| 62 | + ->willReturn($copiedFile); |
| 63 | + |
| 64 | + $rootFolder = $this->createMock(Folder::class); |
| 65 | + $rootFolder->method('getFirstNodeById')->with(10)->willReturn($envelopeFolder); |
| 66 | + |
| 67 | + $this->folderService->expects($this->once())->method('setUserId')->with('u1'); |
| 68 | + $this->folderService->method('getUserRootFolder')->willReturn($rootFolder); |
| 69 | + |
| 70 | + $user = $this->createMock(IUser::class); |
| 71 | + $user->method('getUID')->willReturn('u1'); |
| 72 | + |
| 73 | + $result = $this->relocator->ensureFileInEnvelopeFolder($sourceFile, 10, $user); |
| 74 | + self::assertSame($copiedFile, $result); |
| 75 | + } |
| 76 | + |
| 77 | + public function testThrowsWhenEnvelopeFolderNotFound(): void { |
| 78 | + $sourceFile = $this->createMock(\OCP\Files\File::class); |
| 79 | + $sourceFile->method('getPath')->willReturn('/user/files/doc.pdf'); |
| 80 | + |
| 81 | + $rootFolder = $this->createMock(Folder::class); |
| 82 | + $rootFolder->method('getFirstNodeById')->with(10)->willReturn($this->createMock(Node::class)); |
| 83 | + |
| 84 | + $this->folderService->method('setUserId'); |
| 85 | + $this->folderService->method('getUserRootFolder')->willReturn($rootFolder); |
| 86 | + |
| 87 | + $user = $this->createMock(IUser::class); |
| 88 | + $user->method('getUID')->willReturn('u1'); |
| 89 | + |
| 90 | + $this->expectException(LibresignException::class); |
| 91 | + $this->expectExceptionMessage('Envelope folder not found'); |
| 92 | + $this->relocator->ensureFileInEnvelopeFolder($sourceFile, 10, $user); |
| 93 | + } |
| 94 | + |
| 95 | + public function testThrowsWhenSourceIsNotFile(): void { |
| 96 | + $sourceNode = $this->createMock(Node::class); |
| 97 | + $sourceNode->method('getPath')->willReturn('/user/files/Other'); |
| 98 | + |
| 99 | + $envelopeFolder = $this->createMock(Folder::class); |
| 100 | + $envelopeFolder->method('getPath')->willReturn('/user/files/Envelope'); |
| 101 | + |
| 102 | + $rootFolder = $this->createMock(Folder::class); |
| 103 | + $rootFolder->method('getFirstNodeById')->with(10)->willReturn($envelopeFolder); |
| 104 | + |
| 105 | + $this->folderService->method('setUserId'); |
| 106 | + $this->folderService->method('getUserRootFolder')->willReturn($rootFolder); |
| 107 | + |
| 108 | + $user = $this->createMock(IUser::class); |
| 109 | + $user->method('getUID')->willReturn('u1'); |
| 110 | + |
| 111 | + $this->expectException(LibresignException::class); |
| 112 | + $this->expectExceptionMessage('Invalid file type for envelope'); |
| 113 | + $this->relocator->ensureFileInEnvelopeFolder($sourceNode, 10, $user); |
| 114 | + } |
| 115 | +} |
0 commit comments