|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (c) 2026. The Nextcloud Bookmarks contributors. |
| 5 | + * |
| 6 | + * This file is licensed under the Affero General Public License version 3 or later. See the COPYING file. |
| 7 | + */ |
| 8 | + |
| 9 | +declare(strict_types=1); |
| 10 | + |
| 11 | +namespace OCA\Bookmarks\Tests; |
| 12 | + |
| 13 | +use OCA\Bookmarks\AppInfo\Application; |
| 14 | +use OCA\Bookmarks\BackgroundJobs\ContextChatIndexJob; |
| 15 | +use OCA\Bookmarks\ContextChat\ContextChatProvider; |
| 16 | +use OCA\Bookmarks\Db\Bookmark; |
| 17 | +use OCA\Bookmarks\Service\BookmarkService; |
| 18 | +use OCA\Bookmarks\Service\UserSettingsService; |
| 19 | +use OCP\AppFramework\Utility\ITimeFactory; |
| 20 | +use OCP\BackgroundJob\IJobList; |
| 21 | +use OCP\ContextChat\ContentItem; |
| 22 | +use OCP\ContextChat\IContentManager; |
| 23 | +use OCP\IUser; |
| 24 | +use OCP\IUserManager; |
| 25 | +use PHPUnit\Framework\MockObject\MockObject; |
| 26 | + |
| 27 | +class ContextChatIndexJobTest extends TestCase { |
| 28 | + private ITimeFactory&MockObject $timeFactory; |
| 29 | + private BookmarkService&MockObject $bookmarkService; |
| 30 | + private IContentManager&MockObject $contentManager; |
| 31 | + private IUserManager&MockObject $userManager; |
| 32 | + private ContextChatProvider&MockObject $provider; |
| 33 | + private UserSettingsService&MockObject $userSettings; |
| 34 | + private IJobList&MockObject $jobList; |
| 35 | + private ContextChatIndexJob $job; |
| 36 | + |
| 37 | + protected function setUp(): void { |
| 38 | + parent::setUp(); |
| 39 | + |
| 40 | + $this->timeFactory = $this->createMock(ITimeFactory::class); |
| 41 | + $this->timeFactory->method('getTime')->willReturn(1710000000); |
| 42 | + $this->bookmarkService = $this->createMock(BookmarkService::class); |
| 43 | + $this->contentManager = $this->createMock(IContentManager::class); |
| 44 | + $this->userManager = $this->createMock(IUserManager::class); |
| 45 | + $this->provider = $this->createMock(ContextChatProvider::class); |
| 46 | + $this->userSettings = $this->createMock(UserSettingsService::class); |
| 47 | + $this->jobList = $this->createMock(IJobList::class); |
| 48 | + |
| 49 | + $this->job = new ContextChatIndexJob( |
| 50 | + $this->timeFactory, |
| 51 | + $this->bookmarkService, |
| 52 | + $this->contentManager, |
| 53 | + $this->userManager, |
| 54 | + $this->provider, |
| 55 | + $this->userSettings, |
| 56 | + ); |
| 57 | + $this->job->setArgument(['user' => 'alice']); |
| 58 | + } |
| 59 | + |
| 60 | + public function testDoesNothingWhenContextChatUnavailable(): void { |
| 61 | + $this->contentManager->expects(self::once()) |
| 62 | + ->method('isContextChatAvailable') |
| 63 | + ->willReturn(false); |
| 64 | + $this->userManager->expects(self::never())->method('get'); |
| 65 | + $this->contentManager->expects(self::never())->method('submitContent'); |
| 66 | + $this->jobList->expects(self::once())->method('remove'); |
| 67 | + $this->jobList->expects(self::once())->method('setLastRun'); |
| 68 | + $this->jobList->expects(self::once())->method('setExecutionTime'); |
| 69 | + |
| 70 | + $this->job->start($this->jobList); |
| 71 | + } |
| 72 | + |
| 73 | + public function testIndexesBookmarksForEnabledUser(): void { |
| 74 | + $user = $this->createConfiguredMock(IUser::class, ['getUID' => 'alice']); |
| 75 | + $bookmarks = [ |
| 76 | + $this->createBookmark(1, 'alice', 'One', 'First content'), |
| 77 | + $this->createBookmark(2, 'alice', 'Two', 'Second content'), |
| 78 | + ]; |
| 79 | + |
| 80 | + $this->contentManager->expects(self::once()) |
| 81 | + ->method('isContextChatAvailable') |
| 82 | + ->willReturn(true); |
| 83 | + $this->userManager->expects(self::once()) |
| 84 | + ->method('get') |
| 85 | + ->with('alice') |
| 86 | + ->willReturn($user); |
| 87 | + $this->userSettings->expects(self::once()) |
| 88 | + ->method('setUserId') |
| 89 | + ->with('alice'); |
| 90 | + $this->userSettings->expects(self::once()) |
| 91 | + ->method('get') |
| 92 | + ->with('contextchat.enabled') |
| 93 | + ->willReturn('true'); |
| 94 | + $this->bookmarkService->expects(self::once()) |
| 95 | + ->method('getIterator') |
| 96 | + ->with('alice') |
| 97 | + ->willReturn((function () use ($bookmarks) { |
| 98 | + foreach ($bookmarks as $bookmark) { |
| 99 | + yield $bookmark; |
| 100 | + } |
| 101 | + })()); |
| 102 | + $this->provider->expects(self::exactly(2)) |
| 103 | + ->method('getId') |
| 104 | + ->willReturn('bookmarks'); |
| 105 | + $this->contentManager->expects(self::once()) |
| 106 | + ->method('submitContent') |
| 107 | + ->with( |
| 108 | + Application::APP_ID, |
| 109 | + self::callback(function (array $items): bool { |
| 110 | + self::assertCount(2, $items); |
| 111 | + self::assertContainsOnlyInstancesOf(ContentItem::class, $items); |
| 112 | + self::assertSame('1', $items[0]->itemId); |
| 113 | + self::assertSame('2', $items[1]->itemId); |
| 114 | + self::assertSame(['alice'], $items[0]->users); |
| 115 | + return true; |
| 116 | + }) |
| 117 | + ); |
| 118 | + $this->jobList->expects(self::once())->method('remove'); |
| 119 | + $this->jobList->expects(self::once())->method('setLastRun'); |
| 120 | + $this->jobList->expects(self::once())->method('setExecutionTime'); |
| 121 | + |
| 122 | + $this->job->start($this->jobList); |
| 123 | + } |
| 124 | + |
| 125 | + public function testDoesNotIndexWhenUserDisabledContextChat(): void { |
| 126 | + $user = $this->createConfiguredMock(IUser::class, ['getUID' => 'alice']); |
| 127 | + |
| 128 | + $this->contentManager->expects(self::once()) |
| 129 | + ->method('isContextChatAvailable') |
| 130 | + ->willReturn(true); |
| 131 | + $this->userManager->expects(self::once()) |
| 132 | + ->method('get') |
| 133 | + ->with('alice') |
| 134 | + ->willReturn($user); |
| 135 | + $this->userSettings->expects(self::once()) |
| 136 | + ->method('setUserId') |
| 137 | + ->with('alice'); |
| 138 | + $this->userSettings->expects(self::once()) |
| 139 | + ->method('get') |
| 140 | + ->with('contextchat.enabled') |
| 141 | + ->willReturn('false'); |
| 142 | + $this->bookmarkService->expects(self::never())->method('getIterator'); |
| 143 | + $this->contentManager->expects(self::never())->method('submitContent'); |
| 144 | + $this->jobList->expects(self::once())->method('remove'); |
| 145 | + $this->jobList->expects(self::once())->method('setLastRun'); |
| 146 | + $this->jobList->expects(self::once())->method('setExecutionTime'); |
| 147 | + |
| 148 | + $this->job->start($this->jobList); |
| 149 | + } |
| 150 | + |
| 151 | + private function createBookmark(int $id, string $userId, string $title, string $textContent): Bookmark { |
| 152 | + return Bookmark::fromArray([ |
| 153 | + 'id' => $id, |
| 154 | + 'userId' => $userId, |
| 155 | + 'url' => 'https://example.com/' . $id, |
| 156 | + 'title' => $title, |
| 157 | + 'description' => '', |
| 158 | + 'lastmodified' => 1710000000 + $id, |
| 159 | + 'added' => 1710000000, |
| 160 | + 'clickcount' => 0, |
| 161 | + 'lastPreview' => 0, |
| 162 | + 'available' => true, |
| 163 | + 'archivedFile' => 0, |
| 164 | + 'textContent' => $textContent, |
| 165 | + 'htmlContent' => '', |
| 166 | + 'urlHash' => 'hash-' . $id, |
| 167 | + ]); |
| 168 | + } |
| 169 | +} |
0 commit comments