|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace lib\Files\Template; |
| 11 | + |
| 12 | +use OC\AppFramework\Bootstrap\Coordinator; |
| 13 | +use OC\AppFramework\Bootstrap\RegistrationContext; |
| 14 | +use OC\Files\FilenameValidator; |
| 15 | +use OC\Files\Template\TemplateManager; |
| 16 | +use OCP\Files\Folder; |
| 17 | +use OCP\Files\GenericFileException; |
| 18 | +use OCP\Files\IRootFolder; |
| 19 | +use OCP\Files\NotFoundException; |
| 20 | +use OCP\IConfig; |
| 21 | +use OCP\IDBConnection; |
| 22 | +use OCP\IL10N; |
| 23 | +use OCP\IPreview; |
| 24 | +use OCP\L10N\IFactory; |
| 25 | +use Psr\Log\NullLogger; |
| 26 | +use Test\TestCase; |
| 27 | + |
| 28 | +class TemplateManagerTest extends TestCase { |
| 29 | + |
| 30 | + private IRootFolder $rootFolder; |
| 31 | + private Coordinator $bootstrapCoordinator; |
| 32 | + |
| 33 | + private TemplateManager $templateManager; |
| 34 | + |
| 35 | + protected function setUp(): void { |
| 36 | + parent::setUp(); |
| 37 | + |
| 38 | + $l10n = $this->createMock(IL10N::class); |
| 39 | + $l10n->method('t') |
| 40 | + ->willReturnCallback(fn ($string, $params) => sprintf($string, ...$params)); |
| 41 | + $l10nFactory = $this->createMock(IFactory::class); |
| 42 | + $l10nFactory->method('get') |
| 43 | + ->willReturn($l10n); |
| 44 | + $database = $this->createMock(IDBConnection::class); |
| 45 | + $database->method('supports4ByteText')->willReturn(true); |
| 46 | + $config = $this->createMock(IConfig::class); |
| 47 | + $logger = new NullLogger(); |
| 48 | + |
| 49 | + $filenameValidator = new FilenameValidator( |
| 50 | + $l10nFactory, |
| 51 | + $database, |
| 52 | + $config, |
| 53 | + $logger, |
| 54 | + ); |
| 55 | + |
| 56 | + $serverContainer = $this->createMock(\OCP\IServerContainer::class); |
| 57 | + $eventDispatcher = $this->createMock(\OCP\EventDispatcher\IEventDispatcher::class); |
| 58 | + $this->bootstrapCoordinator = $this->createMock(Coordinator::class); |
| 59 | + $this->bootstrapCoordinator->method('getRegistrationContext') |
| 60 | + ->willReturn(new RegistrationContext($logger)); |
| 61 | + $this->rootFolder = $this->createMock(IRootFolder::class); |
| 62 | + $userSession = $this->createMock(\OCP\IUserSession::class); |
| 63 | + $userManager = $this->createMock(\OCP\IUserManager::class); |
| 64 | + $previewManager = $this->createMock(IPreview::class); |
| 65 | + |
| 66 | + $this->templateManager = new TemplateManager( |
| 67 | + $serverContainer, |
| 68 | + $eventDispatcher, |
| 69 | + $this->bootstrapCoordinator, |
| 70 | + $this->rootFolder, |
| 71 | + $userSession, |
| 72 | + $userManager, |
| 73 | + $previewManager, |
| 74 | + $config, |
| 75 | + $l10nFactory, |
| 76 | + $logger, |
| 77 | + $filenameValidator |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + public function testCreateFromTemplateShoudValidateFilename(): void { |
| 82 | + $this->expectException(GenericFileException::class); |
| 83 | + |
| 84 | + $fileDirectory = '/'; |
| 85 | + $filePath = $fileDirectory . str_repeat('a', 251); |
| 86 | + |
| 87 | + $userFolder = $this->createMock(Folder::class); |
| 88 | + $userFolder->method('get') |
| 89 | + ->willReturnCallback(function ($path) use ($filePath, $fileDirectory) { |
| 90 | + if ($path === $filePath) { |
| 91 | + throw new NotFoundException(); |
| 92 | + } |
| 93 | + return $this->createMock(Folder::class); |
| 94 | + }); |
| 95 | + $userFolder->method('nodeExists') |
| 96 | + ->willReturnCallback(function ($path) use ($filePath, $fileDirectory) { |
| 97 | + return $path === $fileDirectory; |
| 98 | + }); |
| 99 | + $this->rootFolder->method('getUserFolder') |
| 100 | + ->willReturn($userFolder); |
| 101 | + |
| 102 | + $this->templateManager->createFromTemplate($filePath); |
| 103 | + } |
| 104 | +} |
0 commit comments