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