Skip to content

Commit e2577de

Browse files
authored
Merge pull request #4820 from nextcloud/backport/4773/stable30
[stable30] chore(test): add unit tests for RegisterTemplateFileCreatorListener
2 parents ca63d4b + 379c035 commit e2577de

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
namespace OCA\Richdocuments\Tests\Listener;
9+
10+
use OCA\Richdocuments\Listener\RegisterTemplateFileCreatorListener;
11+
use OCA\Richdocuments\PermissionManager;
12+
use OCA\Richdocuments\Service\CapabilitiesService;
13+
use OCP\App\IAppManager;
14+
use OCP\EventDispatcher\Event;
15+
use OCP\Files\Template\ITemplateManager;
16+
use OCP\Files\Template\RegisterTemplateCreatorEvent;
17+
use OCP\IConfig;
18+
use OCP\IL10N;
19+
use PHPUnit\Framework\TestCase;
20+
21+
class RegisterTemplateFileCreatorListenerTest extends TestCase {
22+
private $l10n;
23+
private $config;
24+
private $appManager;
25+
private $capabilitiesService;
26+
private $permissionManager;
27+
private $templateManager;
28+
29+
protected function setUp(): void {
30+
parent::setUp();
31+
$this->l10n = $this->createMock(IL10N::class);
32+
$this->config = $this->createMock(IConfig::class);
33+
$this->appManager = $this->createMock(IAppManager::class);
34+
$this->capabilitiesService = $this->createMock(CapabilitiesService::class);
35+
$this->permissionManager = $this->createMock(PermissionManager::class);
36+
$this->templateManager = $this->createMock(ITemplateManager::class);
37+
}
38+
39+
public function testHandleDoesNotRegisterIfEventIsNotRegisterTemplateCreatorEvent() {
40+
$listener = new RegisterTemplateFileCreatorListener(
41+
$this->l10n,
42+
$this->config,
43+
$this->appManager,
44+
$this->capabilitiesService,
45+
$this->permissionManager
46+
);
47+
$event = $this->createMock(Event::class);
48+
$this->templateManager->expects($this->never())->method('registerTemplateFileCreator');
49+
$listener->handle($event);
50+
}
51+
52+
public function testHandleDoesNotRegisterIfPermissionOrCapabilitiesMissing() {
53+
$event = $this->createMock(RegisterTemplateCreatorEvent::class);
54+
$event->method('getTemplateManager')->willReturn($this->templateManager);
55+
$this->permissionManager->method('isEnabledForUser')->willReturn(false);
56+
$this->capabilitiesService->method('getCapabilities')->willReturn([]);
57+
58+
$listener = new RegisterTemplateFileCreatorListener(
59+
$this->l10n,
60+
$this->config,
61+
$this->appManager,
62+
$this->capabilitiesService,
63+
$this->permissionManager
64+
);
65+
$this->templateManager->expects($this->never())->method('registerTemplateFileCreator');
66+
$listener->handle($event);
67+
}
68+
69+
public function testHandleRegistersTemplateFileCreators() {
70+
$event = $this->createMock(RegisterTemplateCreatorEvent::class);
71+
$event->method('getTemplateManager')->willReturn($this->templateManager);
72+
$this->permissionManager->method('isEnabledForUser')->willReturn(true);
73+
$this->capabilitiesService->method('getCapabilities')->willReturn(['something']);
74+
$this->capabilitiesService->method('hasDrawSupport')->willReturn(true);
75+
$this->config->method('getAppValue')->willReturn('ooxml');
76+
$this->appManager->method('getAppPath')->willReturn('/tmp');
77+
78+
$this->templateManager->expects($this->exactly(4))->method('registerTemplateFileCreator');
79+
80+
$listener = new RegisterTemplateFileCreatorListener(
81+
$this->l10n,
82+
$this->config,
83+
$this->appManager,
84+
$this->capabilitiesService,
85+
$this->permissionManager
86+
);
87+
$listener->handle($event);
88+
}
89+
90+
public function testHandleRegistersWithoutDrawSupport() {
91+
$event = $this->createMock(RegisterTemplateCreatorEvent::class);
92+
$event->method('getTemplateManager')->willReturn($this->templateManager);
93+
$this->permissionManager->method('isEnabledForUser')->willReturn(true);
94+
$this->capabilitiesService->method('getCapabilities')->willReturn(['something']);
95+
$this->capabilitiesService->method('hasDrawSupport')->willReturn(false);
96+
$this->config->method('getAppValue')->willReturn('ooxml');
97+
$this->appManager->method('getAppPath')->willReturn('/tmp');
98+
99+
$this->templateManager->expects($this->exactly(3))->method('registerTemplateFileCreator');
100+
101+
$listener = new RegisterTemplateFileCreatorListener(
102+
$this->l10n,
103+
$this->config,
104+
$this->appManager,
105+
$this->capabilitiesService,
106+
$this->permissionManager
107+
);
108+
$listener->handle($event);
109+
}
110+
}

0 commit comments

Comments
 (0)