|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2026 LibreCode coop and LibreCode contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace OCA\ProfileFields\Tests\Unit\Search; |
| 11 | + |
| 12 | +use OCA\ProfileFields\Search\ProfileFieldDirectorySearchService; |
| 13 | +use OCA\ProfileFields\Search\ProfileFieldSearchProvider; |
| 14 | +use OCP\IL10N; |
| 15 | +use OCP\IURLGenerator; |
| 16 | +use OCP\IUser; |
| 17 | +use OCP\Search\ISearchQuery; |
| 18 | +use PHPUnit\Framework\MockObject\MockObject; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +class ProfileFieldSearchProviderTest extends TestCase { |
| 22 | + public function testSearchReturnsEmptyResultForShortTerms(): void { |
| 23 | + $provider = new ProfileFieldSearchProvider( |
| 24 | + $this->buildL10n(), |
| 25 | + $this->createMock(IURLGenerator::class), |
| 26 | + $this->createMock(ProfileFieldDirectorySearchService::class), |
| 27 | + ); |
| 28 | + $query = $this->createMock(ISearchQuery::class); |
| 29 | + $query->method('getTerm')->willReturn('a'); |
| 30 | + $query->method('getLimit')->willReturn(10); |
| 31 | + $query->method('getCursor')->willReturn(0); |
| 32 | + |
| 33 | + $result = $provider->search($this->createMock(IUser::class), $query); |
| 34 | + |
| 35 | + $this->assertSame([], $result->jsonSerialize()['entries']); |
| 36 | + } |
| 37 | + |
| 38 | + public function testSearchNormalizesInitialNullCursorToZero(): void { |
| 39 | + $l10n = $this->buildL10n(); |
| 40 | + $urlGenerator = $this->createMock(IURLGenerator::class); |
| 41 | + $searchService = $this->createMock(ProfileFieldDirectorySearchService::class); |
| 42 | + $user = $this->createMock(IUser::class); |
| 43 | + $query = $this->createMock(ISearchQuery::class); |
| 44 | + $query->method('getTerm')->willReturn('latam'); |
| 45 | + $query->method('getLimit')->willReturn(1); |
| 46 | + $query->method('getCursor')->willReturn(null); |
| 47 | + |
| 48 | + $searchService->expects($this->once()) |
| 49 | + ->method('search') |
| 50 | + ->with($user, 'latam', 1, 0) |
| 51 | + ->willReturn([ |
| 52 | + 'total' => 2, |
| 53 | + 'items' => [[ |
| 54 | + 'user_uid' => 'alice', |
| 55 | + 'display_name' => 'Alice Doe', |
| 56 | + 'matched_fields' => [[ |
| 57 | + 'field_key' => 'region', |
| 58 | + 'field_label' => 'Region', |
| 59 | + 'value' => 'LATAM', |
| 60 | + ]], |
| 61 | + ]], |
| 62 | + ]); |
| 63 | + |
| 64 | + $urlGenerator->expects($this->exactly(2)) |
| 65 | + ->method('linkToRouteAbsolute') |
| 66 | + ->willReturnMap([ |
| 67 | + ['core.avatar.getAvatar', ['userId' => 'alice', 'size' => 64], 'https://cloud.test/avatar/alice'], |
| 68 | + ['settings.Users.usersList', [], 'https://cloud.test/settings/users'], |
| 69 | + ]); |
| 70 | + |
| 71 | + $provider = new ProfileFieldSearchProvider($l10n, $urlGenerator, $searchService); |
| 72 | + $result = $provider->search($user, $query)->jsonSerialize(); |
| 73 | + |
| 74 | + $this->assertTrue($result['isPaginated']); |
| 75 | + $this->assertSame(1, $result['cursor']); |
| 76 | + } |
| 77 | + |
| 78 | + public function testSearchNormalizesNumericStringCursor(): void { |
| 79 | + $l10n = $this->buildL10n(); |
| 80 | + $urlGenerator = $this->createMock(IURLGenerator::class); |
| 81 | + $searchService = $this->createMock(ProfileFieldDirectorySearchService::class); |
| 82 | + $user = $this->createMock(IUser::class); |
| 83 | + $query = $this->createMock(ISearchQuery::class); |
| 84 | + $query->method('getTerm')->willReturn('latam'); |
| 85 | + $query->method('getLimit')->willReturn(1); |
| 86 | + $query->method('getCursor')->willReturn('1'); |
| 87 | + |
| 88 | + $searchService->expects($this->once()) |
| 89 | + ->method('search') |
| 90 | + ->with($user, 'latam', 1, 1) |
| 91 | + ->willReturn([ |
| 92 | + 'total' => 2, |
| 93 | + 'items' => [[ |
| 94 | + 'user_uid' => 'bruno', |
| 95 | + 'display_name' => 'Bruno Doe', |
| 96 | + 'matched_fields' => [[ |
| 97 | + 'field_key' => 'region', |
| 98 | + 'field_label' => 'Region', |
| 99 | + 'value' => 'LATAM', |
| 100 | + ]], |
| 101 | + ]], |
| 102 | + ]); |
| 103 | + |
| 104 | + $urlGenerator->expects($this->exactly(2)) |
| 105 | + ->method('linkToRouteAbsolute') |
| 106 | + ->willReturnMap([ |
| 107 | + ['core.avatar.getAvatar', ['userId' => 'bruno', 'size' => 64], 'https://cloud.test/avatar/bruno'], |
| 108 | + ['settings.Users.usersList', [], 'https://cloud.test/settings/users'], |
| 109 | + ]); |
| 110 | + |
| 111 | + $provider = new ProfileFieldSearchProvider($l10n, $urlGenerator, $searchService); |
| 112 | + $result = $provider->search($user, $query)->jsonSerialize(); |
| 113 | + |
| 114 | + $this->assertFalse($result['isPaginated']); |
| 115 | + } |
| 116 | + |
| 117 | + public function testSearchBuildsAvatarBackedEntries(): void { |
| 118 | + $l10n = $this->buildL10n(); |
| 119 | + $urlGenerator = $this->createMock(IURLGenerator::class); |
| 120 | + $searchService = $this->createMock(ProfileFieldDirectorySearchService::class); |
| 121 | + $user = $this->createMock(IUser::class); |
| 122 | + $query = $this->createMock(ISearchQuery::class); |
| 123 | + $query->method('getTerm')->willReturn('latam'); |
| 124 | + $query->method('getLimit')->willReturn(10); |
| 125 | + $query->method('getCursor')->willReturn(0); |
| 126 | + |
| 127 | + $searchService->expects($this->once()) |
| 128 | + ->method('search') |
| 129 | + ->with($user, 'latam', 10, 0) |
| 130 | + ->willReturn([ |
| 131 | + 'total' => 1, |
| 132 | + 'items' => [[ |
| 133 | + 'user_uid' => 'alice', |
| 134 | + 'display_name' => 'Alice Doe', |
| 135 | + 'matched_fields' => [[ |
| 136 | + 'field_key' => 'region', |
| 137 | + 'field_label' => 'Region', |
| 138 | + 'value' => 'LATAM', |
| 139 | + ]], |
| 140 | + ]], |
| 141 | + ]); |
| 142 | + |
| 143 | + $urlGenerator->expects($this->exactly(2)) |
| 144 | + ->method('linkToRouteAbsolute') |
| 145 | + ->willReturnMap([ |
| 146 | + ['core.avatar.getAvatar', ['userId' => 'alice', 'size' => 64], 'https://cloud.test/avatar/alice'], |
| 147 | + ['settings.Users.usersList', [], 'https://cloud.test/settings/users'], |
| 148 | + ]); |
| 149 | + |
| 150 | + $provider = new ProfileFieldSearchProvider($l10n, $urlGenerator, $searchService); |
| 151 | + $result = $provider->search($user, $query)->jsonSerialize(); |
| 152 | + $entry = $result['entries'][0]->jsonSerialize(); |
| 153 | + |
| 154 | + $this->assertSame('Profile directory', $result['name']); |
| 155 | + $this->assertCount(1, $result['entries']); |
| 156 | + $this->assertFalse($result['isPaginated']); |
| 157 | + $this->assertSame('Alice Doe', $entry['title']); |
| 158 | + $this->assertSame('Region: LATAM', $entry['subline']); |
| 159 | + $this->assertSame('https://cloud.test/settings/users?search=alice', $entry['resourceUrl']); |
| 160 | + } |
| 161 | + |
| 162 | + private function buildL10n(): IL10N&MockObject { |
| 163 | + $l10n = $this->createMock(IL10N::class); |
| 164 | + $l10n->method('t')->willReturnCallback(static fn (string $text): string => $text); |
| 165 | + |
| 166 | + return $l10n; |
| 167 | + } |
| 168 | +} |
0 commit comments