Skip to content

Commit ac41bfc

Browse files
test: files_sharing add test for LoadAdditionalListener
Signed-off-by: Misha M.-Kupriyanov <kupriyanov@strato.de>
1 parent ba86ee8 commit ac41bfc

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
/**
4+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-License-Identifier: AGPL-3.0-only
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace OCA\Files_Sharing\Tests\Listener;
10+
11+
use OC\InitialStateService;
12+
use OCA\Files\Event\LoadAdditionalScriptsEvent;
13+
use OCA\Files_Sharing\Listener\LoadAdditionalListener;
14+
use OCP\EventDispatcher\Event;
15+
use OCP\IConfig;
16+
use OCP\L10N\IFactory;
17+
use OCP\Share\IManager;
18+
use Psr\Log\LoggerInterface;
19+
use Test\TestCase;
20+
21+
class LoadAdditionalListenerTest extends TestCase {
22+
/** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
23+
protected $logger;
24+
/** @var LoadAdditionalScriptsEvent|\PHPUnit\Framework\MockObject\MockObject */
25+
protected $event;
26+
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
27+
protected $shareManager;
28+
/** @var IFactory|\PHPUnit\Framework\MockObject\MockObject */
29+
protected $factory;
30+
/** @var InitialStateService|\PHPUnit\Framework\MockObject\MockObject */
31+
protected $initialStateService;
32+
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
33+
protected $config;
34+
35+
protected function setUp(): void {
36+
parent::setUp();
37+
38+
$this->logger = $this->getMockBuilder(LoggerInterface::class)
39+
->disableOriginalConstructor()
40+
->getMock();
41+
42+
$this->event = $this->createMock(LoadAdditionalScriptsEvent::class);
43+
$this->shareManager = $this->createMock(IManager::class);
44+
$this->factory = $this->createMock(IFactory::class);
45+
$this->initialStateService = $this->createMock(InitialStateService::class);
46+
$this->config = $this->createMock(IConfig::class);
47+
}
48+
49+
public function testHandleIgnoresNonMatchingEvent(): void {
50+
$listener = new LoadAdditionalListener();
51+
$event = $this->createMock(Event::class);
52+
53+
// Should not throw or call anything
54+
$listener->handle($event);
55+
56+
$this->assertTrue(true); // No exception means pass
57+
}
58+
59+
public function testHandleWithLoadAdditionalScriptsEvent(): void {
60+
$listener = new LoadAdditionalListener();
61+
62+
$this->shareManager->method('shareApiEnabled')->willReturn(false);
63+
$this->factory->method('findLanguage')->willReturn('language_mock');
64+
$this->config->method('getSystemValueBool')->willReturn(true);
65+
66+
$this->overwriteService(IManager::class, $this->shareManager);
67+
$this->overwriteService(IFactory::class, $this->factory);
68+
$this->overwriteService(InitialStateService::class, $this->initialStateService);
69+
$this->overwriteService(IConfig::class, $this->config);
70+
71+
$scriptsBefore = \OCP\Util::getScripts();
72+
$this->assertNotContains('files_sharing/l10n/language_mock', $scriptsBefore);
73+
$this->assertNotContains('files_sharing/js/additionalScripts', $scriptsBefore);
74+
$this->assertNotContains('files_sharing/js/init', $scriptsBefore);
75+
$this->assertNotContains('files_sharing/css/icons', \OC_Util::$styles);
76+
77+
// Util static methods can't be easily mocked, so just ensure no exceptions
78+
$listener->handle($this->event);
79+
80+
// assert array $scripts contains the expected scripts
81+
$scriptsAfter = \OCP\Util::getScripts();
82+
$this->assertContains('files_sharing/l10n/language_mock', $scriptsAfter);
83+
$this->assertContains('files_sharing/js/additionalScripts', $scriptsAfter);
84+
$this->assertNotContains('files_sharing/js/init', $scriptsAfter);
85+
86+
$this->assertContains('files_sharing/css/icons', \OC_Util::$styles);
87+
88+
$this->assertTrue(true);
89+
}
90+
91+
public function testHandleWithLoadAdditionalScriptsEventWithShareApiEnabled(): void {
92+
$listener = new LoadAdditionalListener();
93+
94+
$this->shareManager->method('shareApiEnabled')->willReturn(true);
95+
$this->config->method('getSystemValueBool')->willReturn(true);
96+
97+
$this->overwriteService(IManager::class, $this->shareManager);
98+
$this->overwriteService(InitialStateService::class, $this->initialStateService);
99+
$this->overwriteService(IConfig::class, $this->config);
100+
$this->overwriteService(IFactory::class, $this->factory);
101+
102+
$scriptsBefore = \OCP\Util::getScripts();
103+
$this->assertNotContains('files_sharing/js/init', $scriptsBefore);
104+
105+
// Util static methods can't be easily mocked, so just ensure no exceptions
106+
$listener->handle($this->event);
107+
108+
$scriptsAfter = \OCP\Util::getScripts();
109+
110+
// assert array $scripts contains the expected scripts
111+
$this->assertContains('files_sharing/js/init', $scriptsAfter);
112+
113+
$this->assertTrue(true);
114+
}
115+
}

0 commit comments

Comments
 (0)