|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Tests\Richdocuments; |
| 11 | + |
| 12 | +use OCA\Richdocuments\AppConfig; |
| 13 | +use OCA\Richdocuments\Service\CapabilitiesService; |
| 14 | +use OCA\Richdocuments\Service\PdfService; |
| 15 | +use OCA\Richdocuments\Service\RemoteService; |
| 16 | +use OCA\Richdocuments\Service\TemplateFieldService; |
| 17 | +use OCP\Files\File; |
| 18 | +use OCP\Files\IRootFolder; |
| 19 | +use OCP\Files\Storage\IStorage; |
| 20 | +use OCP\ICache; |
| 21 | +use OCP\ICacheFactory; |
| 22 | +use OCP\ITempManager; |
| 23 | +use PHPUnit\Framework\MockObject\MockObject; |
| 24 | +use PHPUnit\Framework\TestCase; |
| 25 | +use Psr\Log\LoggerInterface; |
| 26 | + |
| 27 | +class TemplateFieldServiceTest extends TestCase { |
| 28 | + private IRootFolder&MockObject $rootFolder; |
| 29 | + private ICacheFactory&MockObject $cacheFactory; |
| 30 | + private ICache&MockObject $cache; |
| 31 | + private RemoteService&MockObject $remoteService; |
| 32 | + private LoggerInterface&MockObject $logger; |
| 33 | + private TemplateFieldService $service; |
| 34 | + |
| 35 | + protected function setUp(): void { |
| 36 | + parent::setUp(); |
| 37 | + $this->rootFolder = $this->createMock(IRootFolder::class); |
| 38 | + $this->cacheFactory = $this->createMock(ICacheFactory::class); |
| 39 | + $this->cache = $this->createMock(ICache::class); |
| 40 | + $this->remoteService = $this->createMock(RemoteService::class); |
| 41 | + $this->logger = $this->createMock(LoggerInterface::class); |
| 42 | + |
| 43 | + $this->cacheFactory->method('createLocal')->willReturn($this->cache); |
| 44 | + $this->cache->method('get')->willReturn(null); |
| 45 | + |
| 46 | + $this->service = new TemplateFieldService( |
| 47 | + $this->createMock(\OCP\Http\Client\IClientService::class), |
| 48 | + $this->createMock(CapabilitiesService::class), |
| 49 | + $this->createMock(AppConfig::class), |
| 50 | + $this->rootFolder, |
| 51 | + $this->logger, |
| 52 | + $this->cacheFactory, |
| 53 | + $this->remoteService, |
| 54 | + $this->createMock(ITempManager::class), |
| 55 | + $this->createMock(PdfService::class), |
| 56 | + 'testuser', |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + private function createFileMock(string $mimetype): File&MockObject { |
| 61 | + $storage = $this->createMock(IStorage::class); |
| 62 | + $storage->method('fopen')->willReturn(fopen('php://memory', 'r')); |
| 63 | + |
| 64 | + $file = $this->createMock(File::class); |
| 65 | + $file->method('getId')->willReturn(42); |
| 66 | + $file->method('getEtag')->willReturn('etag-42'); |
| 67 | + $file->method('getName')->willReturn('template.docx'); |
| 68 | + $file->method('getInternalPath')->willReturn('files/template.docx'); |
| 69 | + $file->method('getMimetype')->willReturn($mimetype); |
| 70 | + $file->method('getStorage')->willReturn($storage); |
| 71 | + |
| 72 | + return $file; |
| 73 | + } |
| 74 | + |
| 75 | + public function testExtractFieldsReturnsEmptyForUnsupportedMimetype(): void { |
| 76 | + $file = $this->createFileMock('application/octet-stream'); |
| 77 | + |
| 78 | + $this->remoteService->expects($this->never())->method('extractDocumentStructure'); |
| 79 | + |
| 80 | + $result = $this->service->extractFields($file); |
| 81 | + $this->assertEquals([], $result); |
| 82 | + } |
| 83 | + |
| 84 | + public function testExtractFieldsCallsRemoteServiceForOdfMimetype(): void { |
| 85 | + $file = $this->createFileMock('application/vnd.oasis.opendocument.text'); |
| 86 | + |
| 87 | + $this->remoteService->expects($this->once()) |
| 88 | + ->method('extractDocumentStructure') |
| 89 | + ->willReturn([]); |
| 90 | + |
| 91 | + $this->service->extractFields($file); |
| 92 | + } |
| 93 | + |
| 94 | + public function testExtractFieldsCallsRemoteServiceForOoxmlMimetype(): void { |
| 95 | + $file = $this->createFileMock('application/vnd.openxmlformats-officedocument.wordprocessingml.document'); |
| 96 | + |
| 97 | + $this->remoteService->expects($this->once()) |
| 98 | + ->method('extractDocumentStructure') |
| 99 | + ->willReturn([]); |
| 100 | + |
| 101 | + $this->service->extractFields($file); |
| 102 | + } |
| 103 | +} |
0 commit comments