|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileLicenseText: 2023 T-Systems International |
| 7 | + * SPDX-FileLicenseText: 2024 STRATO AG |
| 8 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 9 | + */ |
| 10 | + |
| 11 | +namespace OCA\NcTheming\Tests\Search; |
| 12 | + |
| 13 | +use OC\AppFramework\Bootstrap\Coordinator; |
| 14 | +use OC\Search\SearchComposer; |
| 15 | +use OCA\NcTheming\AppInfo\Application; |
| 16 | +use OCA\NcTheming\Search\SearchComposerDecorator; |
| 17 | +use OCP\Collaboration\Collaborators\ISearchResult; |
| 18 | +use OCP\IURLGenerator; |
| 19 | +use OCP\IUser; |
| 20 | +use OCP\Search\ISearchQuery; |
| 21 | +use OCP\Search\SearchResult; |
| 22 | +use PHPUnit\Framework\TestCase; |
| 23 | +use Psr\Container\ContainerInterface; |
| 24 | +use Psr\Log\LoggerInterface; |
| 25 | + |
| 26 | + |
| 27 | +class SearchComposerDecoratorTest extends TestCase { |
| 28 | + |
| 29 | + protected \OCP\AppFramework\App $app; |
| 30 | + protected IUser|\PHPUnit\Framework\MockObject\MockObject $user; |
| 31 | + protected ISearchQuery|\PHPUnit\Framework\MockObject\MockObject $query; |
| 32 | + protected SearchComposer|\PHPUnit\Framework\MockObject\MockObject $decoratedSearchComposer; |
| 33 | + protected SearchComposerDecorator $composerService; |
| 34 | + |
| 35 | + protected function setUp(): void { |
| 36 | + parent::setUp(); |
| 37 | + |
| 38 | + // test behavior in combination with the core apps |
| 39 | + $this->app = new \OCP\AppFramework\App(Application::APP_ID); |
| 40 | + } |
| 41 | + |
| 42 | + public function testGetProvidersDefault() { |
| 43 | + $this->composerService = |
| 44 | + new SearchComposerDecorator( |
| 45 | + new SearchComposer( |
| 46 | + $this->app->getContainer()->get(Coordinator::class), |
| 47 | + $this->app->getContainer()->get(ContainerInterface::class), |
| 48 | + $this->app->getContainer()->get(IURLGenerator::class), |
| 49 | + $this->app->getContainer()->get(LoggerInterface::class) |
| 50 | + ), |
| 51 | + [ |
| 52 | + ] |
| 53 | + ); |
| 54 | + $providers = array_values($this->composerService->getProviders('/', [])); |
| 55 | + $providerIds = array_map(function ($p) { |
| 56 | + return $p['id']; |
| 57 | + }, $providers); |
| 58 | + |
| 59 | + $this->assertContains('files', $providerIds); |
| 60 | + $this->assertContains('systemtags', $providerIds); |
| 61 | + $this->assertContains('comments', $providerIds); |
| 62 | + $this->assertContains('settings_apps', $providerIds); |
| 63 | + $this->assertContains('settings', $providerIds); |
| 64 | + } |
| 65 | + |
| 66 | + public function testGetProvidersAllowed() { |
| 67 | + $this->composerService = |
| 68 | + new SearchComposerDecorator( |
| 69 | + new SearchComposer( |
| 70 | + $this->app->getContainer()->get(Coordinator::class), |
| 71 | + $this->app->getContainer()->get(ContainerInterface::class), |
| 72 | + $this->app->getContainer()->get(IURLGenerator::class), |
| 73 | + $this->app->getContainer()->get(LoggerInterface::class) |
| 74 | + ), |
| 75 | + [ |
| 76 | + 'files', |
| 77 | + 'settings', |
| 78 | + ] |
| 79 | + ); |
| 80 | + $providers = array_values($this->composerService->getProviders('/', [])); |
| 81 | + $providerIds = array_map(function ($p) { |
| 82 | + return $p['id']; |
| 83 | + }, $providers); |
| 84 | + $this->assertContains('settings', $providerIds); |
| 85 | + $this->assertContains('files', $providerIds); |
| 86 | + $this->assertNotContains('settings_apps', $providerIds); |
| 87 | + $this->assertNotContains('systemtags', $providerIds); |
| 88 | + } |
| 89 | + |
| 90 | + protected function createSearchMock(): void { |
| 91 | + $this->decoratedSearchComposer = $this->createMock(SearchComposer::class); |
| 92 | + $this->composerService = |
| 93 | + new SearchComposerDecorator( |
| 94 | + $this->decoratedSearchComposer, |
| 95 | + [ |
| 96 | + 'files', |
| 97 | + 'settings', |
| 98 | + ] |
| 99 | + ); |
| 100 | + |
| 101 | + $this->user = $this->createMock(IUser::class); |
| 102 | + $this->query = $this->createMock(ISearchQuery::class); |
| 103 | + } |
| 104 | + |
| 105 | + public function testSearchDisabledProvider() { |
| 106 | + $this->createSearchMock(); |
| 107 | + /** @var ISearchResult $searchResult */ |
| 108 | + $searchResult = SearchResult::complete('files', []); |
| 109 | + $this->decoratedSearchComposer->expects(self::once()) |
| 110 | + ->method('search') |
| 111 | + ->with($this->user, 'files', $this->query) |
| 112 | + ->willReturn($searchResult); |
| 113 | + |
| 114 | + $result = $this->composerService->search($this->user, 'files', $this->query)->jsonSerialize(); |
| 115 | + |
| 116 | + $this->assertEquals('files', $result['name']); |
| 117 | + $this->assertFalse($result['isPaginated']); |
| 118 | + $this->assertEmpty($result['entries']); |
| 119 | + } |
| 120 | + |
| 121 | + public function testSearch() { |
| 122 | + $this->createSearchMock(); |
| 123 | + $this->decoratedSearchComposer->expects(self::once()) |
| 124 | + ->method("search") |
| 125 | + ->with($this->equalTo($this->user), $this->equalTo('files'), $this->equalTo($this->query)) |
| 126 | + ->willReturn(SearchResult::complete('files', [])); |
| 127 | + $this->composerService->search($this->user, 'files', $this->query)->jsonSerialize(); |
| 128 | + } |
| 129 | +} |
0 commit comments