|
9 | 9 | namespace OCA\groupfolders\tests\ACL; |
10 | 10 |
|
11 | 11 | use OC\Files\Cache\CacheEntry; |
| 12 | +use OC\Files\Search\SearchComparison; |
| 13 | +use OC\Files\Search\SearchOrder; |
| 14 | +use OC\Files\Search\SearchQuery; |
| 15 | +use OC\Files\Storage\Temporary; |
12 | 16 | use OCA\GroupFolders\ACL\ACLCacheWrapper; |
13 | 17 | use OCA\GroupFolders\ACL\ACLManager; |
| 18 | +use OCA\GroupFolders\ACL\Rule; |
| 19 | +use OCA\GroupFolders\ACL\RuleManager; |
| 20 | +use OCA\GroupFolders\ACL\UserMapping\IUserMapping; |
| 21 | +use OCA\GroupFolders\ACL\UserMapping\IUserMappingManager; |
14 | 22 | use OCP\Constants; |
15 | 23 | use OCP\Files\Cache\ICache; |
| 24 | +use OCP\Files\Cache\ICacheEntry; |
| 25 | +use OCP\Files\Search\ISearchComparison; |
| 26 | +use OCP\Files\Search\ISearchOrder; |
| 27 | +use OCP\IUser; |
16 | 28 | use PHPUnit\Framework\MockObject\MockObject; |
17 | 29 | use Test\TestCase; |
18 | 30 |
|
19 | 31 | /** |
20 | 32 | * @group DB |
21 | 33 | */ |
22 | 34 | class ACLCacheWrapperTest extends TestCase { |
23 | | - private ACLManager&MockObject $aclManager; |
| 35 | + private ACLManager $aclManager; |
24 | 36 | private ICache&MockObject $source; |
25 | 37 | private ACLCacheWrapper $cache; |
26 | 38 | private array $aclPermissions = []; |
27 | 39 |
|
| 40 | + private function makeRule(int $permission): Rule { |
| 41 | + return new Rule($this->createMock(IUserMapping::class), 0, Constants::PERMISSION_ALL, $permission); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @param string[] $paths |
| 46 | + * @return array<string, Rule[]> |
| 47 | + */ |
| 48 | + private function getRulesForPaths(IUser $user, int $storageId, array $paths): array { |
| 49 | + return array_combine($paths, array_map( |
| 50 | + fn (string $path) => isset($this->aclPermissions[$path]) ? [$this->makeRule($this->aclPermissions[$path])] : [], |
| 51 | + $paths |
| 52 | + )); |
| 53 | + } |
| 54 | + |
28 | 55 | #[\Override] |
29 | 56 | protected function setUp(): void { |
30 | 57 | parent::setUp(); |
31 | 58 |
|
32 | | - $this->aclManager = $this->createMock(ACLManager::class); |
33 | | - $this->aclManager->method('getACLPermissionsForPath') |
34 | | - ->willReturnCallback(fn (int $folderId, int $storageId, string $path) => $this->aclPermissions[$path] ?? Constants::PERMISSION_ALL); |
| 59 | + $user = $this->createMock(IUser::class); |
| 60 | + $ruleManager = $this->createMock(RuleManager::class); |
| 61 | + $ruleManager->method('getRulesForFilesByPath') |
| 62 | + ->willReturnCallback($this->getRulesForPaths(...)); |
| 63 | + $ruleManager->method('getRulesForPrefix') |
| 64 | + ->willReturnCallback(fn (IUser $user, int $storageId, string $prefix) => array_map( |
| 65 | + fn (int $permission) => [$this->makeRule($permission)], |
| 66 | + array_filter( |
| 67 | + $this->aclPermissions, |
| 68 | + fn (string $path) => str_starts_with($path, $prefix), |
| 69 | + ARRAY_FILTER_USE_KEY, |
| 70 | + )) |
| 71 | + ); |
| 72 | + $mappingManager = $this->createMock(IUserMappingManager::class); |
| 73 | + |
| 74 | + $this->aclManager = new ACLManager($ruleManager, $mappingManager, $user); |
| 75 | + |
35 | 76 | $this->source = $this->createMock(ICache::class); |
36 | 77 | $this->source->method('getNumericStorageId') |
37 | 78 | ->willReturn(1); |
@@ -75,4 +116,34 @@ public function testHideNonRead(): void { |
75 | 116 |
|
76 | 117 | $this->assertEquals($expected, $result); |
77 | 118 | } |
| 119 | + |
| 120 | + public function testSearchNonRead(): void { |
| 121 | + $sourceStorage = new Temporary([]); |
| 122 | + $sourceStorage->mkdir('foo'); |
| 123 | + $sourceStorage->touch('foo/test1.txt', 100); |
| 124 | + $sourceStorage->touch('foo/test2.txt', 101); |
| 125 | + $sourceStorage->touch('foo/test3.txt', 102); |
| 126 | + $sourceStorage->mkdir('bar'); |
| 127 | + $sourceStorage->touch('bar/test1.txt', 200); |
| 128 | + $sourceStorage->touch('bar/test2.txt', 201); |
| 129 | + $sourceStorage->touch('bar/test3.txt', 202); |
| 130 | + $sourceStorage->getScanner()->scan(''); |
| 131 | + $this->aclPermissions['bar'] = 0; |
| 132 | + |
| 133 | + $cache = new ACLCacheWrapper($sourceStorage->getCache(), $this->aclManager, 0, false); |
| 134 | + |
| 135 | + $query = new SearchQuery( |
| 136 | + new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%test%'), |
| 137 | + 2, |
| 138 | + 0, |
| 139 | + [new SearchOrder(ISearchOrder::DIRECTION_DESCENDING, 'mtime')] |
| 140 | + ); |
| 141 | + $result = $cache->searchQuery($query); |
| 142 | + $paths = array_map(fn (ICacheEntry $entry) => $entry->getPath(), $result); |
| 143 | + |
| 144 | + $this->assertEquals([ |
| 145 | + 'foo/test3.txt', |
| 146 | + 'foo/test2.txt' |
| 147 | + ], $paths); |
| 148 | + } |
78 | 149 | } |
0 commit comments