@@ -34,566 +34,566 @@ |
||
| 34 | 34 | |
| 35 | 35 | #[\PHPUnit\Framework\Attributes\Group('DB')] |
| 36 | 36 | class UserMountCacheTest extends TestCase { |
| 37 | - private IDBConnection $connection; |
|
| 38 | - private IUserManager $userManager; |
|
| 39 | - private IEventDispatcher $eventDispatcher; |
|
| 40 | - private UserMountCache $cache; |
|
| 41 | - private array $fileIds = []; |
|
| 42 | - |
|
| 43 | - protected function setUp(): void { |
|
| 44 | - parent::setUp(); |
|
| 45 | - |
|
| 46 | - $this->fileIds = []; |
|
| 47 | - |
|
| 48 | - $this->connection = Server::get(IDBConnection::class); |
|
| 49 | - |
|
| 50 | - $config = $this->getMockBuilder(IConfig::class) |
|
| 51 | - ->disableOriginalConstructor() |
|
| 52 | - ->getMock(); |
|
| 53 | - $config |
|
| 54 | - ->expects($this->any()) |
|
| 55 | - ->method('getUserValue') |
|
| 56 | - ->willReturnArgument(3); |
|
| 57 | - $config |
|
| 58 | - ->expects($this->any()) |
|
| 59 | - ->method('getAppValue') |
|
| 60 | - ->willReturnArgument(2); |
|
| 61 | - |
|
| 62 | - $this->userManager = new Manager($config, $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class), $this->createMock(LoggerInterface::class)); |
|
| 63 | - $userBackend = new Dummy(); |
|
| 64 | - $userBackend->createUser('u1', ''); |
|
| 65 | - $userBackend->createUser('u2', ''); |
|
| 66 | - $userBackend->createUser('u3', ''); |
|
| 67 | - $this->userManager->registerBackend($userBackend); |
|
| 68 | - |
|
| 69 | - $this->eventDispatcher = $this->createMock(IEventDispatcher::class); |
|
| 37 | + private IDBConnection $connection; |
|
| 38 | + private IUserManager $userManager; |
|
| 39 | + private IEventDispatcher $eventDispatcher; |
|
| 40 | + private UserMountCache $cache; |
|
| 41 | + private array $fileIds = []; |
|
| 42 | + |
|
| 43 | + protected function setUp(): void { |
|
| 44 | + parent::setUp(); |
|
| 45 | + |
|
| 46 | + $this->fileIds = []; |
|
| 47 | + |
|
| 48 | + $this->connection = Server::get(IDBConnection::class); |
|
| 49 | + |
|
| 50 | + $config = $this->getMockBuilder(IConfig::class) |
|
| 51 | + ->disableOriginalConstructor() |
|
| 52 | + ->getMock(); |
|
| 53 | + $config |
|
| 54 | + ->expects($this->any()) |
|
| 55 | + ->method('getUserValue') |
|
| 56 | + ->willReturnArgument(3); |
|
| 57 | + $config |
|
| 58 | + ->expects($this->any()) |
|
| 59 | + ->method('getAppValue') |
|
| 60 | + ->willReturnArgument(2); |
|
| 61 | + |
|
| 62 | + $this->userManager = new Manager($config, $this->createMock(ICacheFactory::class), $this->createMock(IEventDispatcher::class), $this->createMock(LoggerInterface::class)); |
|
| 63 | + $userBackend = new Dummy(); |
|
| 64 | + $userBackend->createUser('u1', ''); |
|
| 65 | + $userBackend->createUser('u2', ''); |
|
| 66 | + $userBackend->createUser('u3', ''); |
|
| 67 | + $this->userManager->registerBackend($userBackend); |
|
| 68 | + |
|
| 69 | + $this->eventDispatcher = $this->createMock(IEventDispatcher::class); |
|
| 70 | 70 | |
| 71 | - $this->cache = new UserMountCache($this->connection, |
|
| 72 | - $this->userManager, |
|
| 73 | - $this->createMock(LoggerInterface::class), |
|
| 74 | - $this->createMock(IEventLogger::class), |
|
| 75 | - $this->eventDispatcher, |
|
| 76 | - ); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - protected function tearDown(): void { |
|
| 80 | - $builder = $this->connection->getQueryBuilder(); |
|
| 81 | - |
|
| 82 | - $builder->delete('mounts')->executeStatement(); |
|
| 83 | - |
|
| 84 | - $builder = $this->connection->getQueryBuilder(); |
|
| 85 | - |
|
| 86 | - foreach ($this->fileIds as $fileId) { |
|
| 87 | - $builder->delete('filecache') |
|
| 88 | - ->where($builder->expr()->eq('fileid', new Literal($fileId))) |
|
| 89 | - ->executeStatement(); |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - private function getStorage($storageId, $rootInternalPath = '') { |
|
| 94 | - $rootId = $this->createCacheEntry($rootInternalPath, $storageId); |
|
| 95 | - |
|
| 96 | - $storageCache = $this->createMock(\OC\Files\Cache\Storage::class); |
|
| 97 | - $storageCache->expects($this->any()) |
|
| 98 | - ->method('getNumericId') |
|
| 99 | - ->willReturn($storageId); |
|
| 100 | - |
|
| 101 | - $cache = $this->createMock(Cache::class); |
|
| 102 | - $cache->expects($this->any()) |
|
| 103 | - ->method('getId') |
|
| 104 | - ->willReturn($rootId); |
|
| 105 | - $cache->method('getNumericStorageId') |
|
| 106 | - ->willReturn($storageId); |
|
| 71 | + $this->cache = new UserMountCache($this->connection, |
|
| 72 | + $this->userManager, |
|
| 73 | + $this->createMock(LoggerInterface::class), |
|
| 74 | + $this->createMock(IEventLogger::class), |
|
| 75 | + $this->eventDispatcher, |
|
| 76 | + ); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + protected function tearDown(): void { |
|
| 80 | + $builder = $this->connection->getQueryBuilder(); |
|
| 81 | + |
|
| 82 | + $builder->delete('mounts')->executeStatement(); |
|
| 83 | + |
|
| 84 | + $builder = $this->connection->getQueryBuilder(); |
|
| 85 | + |
|
| 86 | + foreach ($this->fileIds as $fileId) { |
|
| 87 | + $builder->delete('filecache') |
|
| 88 | + ->where($builder->expr()->eq('fileid', new Literal($fileId))) |
|
| 89 | + ->executeStatement(); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + private function getStorage($storageId, $rootInternalPath = '') { |
|
| 94 | + $rootId = $this->createCacheEntry($rootInternalPath, $storageId); |
|
| 95 | + |
|
| 96 | + $storageCache = $this->createMock(\OC\Files\Cache\Storage::class); |
|
| 97 | + $storageCache->expects($this->any()) |
|
| 98 | + ->method('getNumericId') |
|
| 99 | + ->willReturn($storageId); |
|
| 100 | + |
|
| 101 | + $cache = $this->createMock(Cache::class); |
|
| 102 | + $cache->expects($this->any()) |
|
| 103 | + ->method('getId') |
|
| 104 | + ->willReturn($rootId); |
|
| 105 | + $cache->method('getNumericStorageId') |
|
| 106 | + ->willReturn($storageId); |
|
| 107 | 107 | |
| 108 | - $storage = $this->createMock(Storage::class); |
|
| 109 | - $storage->expects($this->any()) |
|
| 110 | - ->method('getStorageCache') |
|
| 111 | - ->willReturn($storageCache); |
|
| 112 | - $storage->expects($this->any()) |
|
| 113 | - ->method('getCache') |
|
| 114 | - ->willReturn($cache); |
|
| 108 | + $storage = $this->createMock(Storage::class); |
|
| 109 | + $storage->expects($this->any()) |
|
| 110 | + ->method('getStorageCache') |
|
| 111 | + ->willReturn($storageCache); |
|
| 112 | + $storage->expects($this->any()) |
|
| 113 | + ->method('getCache') |
|
| 114 | + ->willReturn($cache); |
|
| 115 | 115 | |
| 116 | - return [$storage, $rootId]; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - private function clearCache() { |
|
| 120 | - $this->invokePrivate($this->cache, 'mountsForUsers', [new CappedMemoryCache()]); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - private function keyForMount(MountPoint $mount): string { |
|
| 124 | - return $mount->getStorageRootId() . '::' . $mount->getMountPoint(); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - public function testNewMounts(): void { |
|
| 128 | - $this->eventDispatcher |
|
| 129 | - ->expects($this->once()) |
|
| 130 | - ->method('dispatchTyped') |
|
| 131 | - ->with($this->callback(fn (UserMountAddedEvent $event) => $event->mountPoint->getMountPoint() === '/asd/')); |
|
| 132 | - |
|
| 133 | - $user = $this->userManager->get('u1'); |
|
| 116 | + return [$storage, $rootId]; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + private function clearCache() { |
|
| 120 | + $this->invokePrivate($this->cache, 'mountsForUsers', [new CappedMemoryCache()]); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + private function keyForMount(MountPoint $mount): string { |
|
| 124 | + return $mount->getStorageRootId() . '::' . $mount->getMountPoint(); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + public function testNewMounts(): void { |
|
| 128 | + $this->eventDispatcher |
|
| 129 | + ->expects($this->once()) |
|
| 130 | + ->method('dispatchTyped') |
|
| 131 | + ->with($this->callback(fn (UserMountAddedEvent $event) => $event->mountPoint->getMountPoint() === '/asd/')); |
|
| 132 | + |
|
| 133 | + $user = $this->userManager->get('u1'); |
|
| 134 | 134 | |
| 135 | - [$storage] = $this->getStorage(10); |
|
| 136 | - $mount = new MountPoint($storage, '/asd/'); |
|
| 135 | + [$storage] = $this->getStorage(10); |
|
| 136 | + $mount = new MountPoint($storage, '/asd/'); |
|
| 137 | 137 | |
| 138 | - $this->cache->registerMounts($user, [$mount]); |
|
| 138 | + $this->cache->registerMounts($user, [$mount]); |
|
| 139 | 139 | |
| 140 | - $this->clearCache(); |
|
| 140 | + $this->clearCache(); |
|
| 141 | 141 | |
| 142 | - $cachedMounts = $this->cache->getMountsForUser($user); |
|
| 142 | + $cachedMounts = $this->cache->getMountsForUser($user); |
|
| 143 | 143 | |
| 144 | - $this->assertCount(1, $cachedMounts); |
|
| 145 | - $cachedMount = $cachedMounts[$this->keyForMount($mount)]; |
|
| 146 | - $this->assertEquals('/asd/', $cachedMount->getMountPoint()); |
|
| 147 | - $this->assertEquals($user->getUID(), $cachedMount->getUser()->getUID()); |
|
| 148 | - $this->assertEquals($storage->getCache()->getId(''), $cachedMount->getRootId()); |
|
| 149 | - $this->assertEquals($storage->getStorageCache()->getNumericId(), $cachedMount->getStorageId()); |
|
| 150 | - } |
|
| 144 | + $this->assertCount(1, $cachedMounts); |
|
| 145 | + $cachedMount = $cachedMounts[$this->keyForMount($mount)]; |
|
| 146 | + $this->assertEquals('/asd/', $cachedMount->getMountPoint()); |
|
| 147 | + $this->assertEquals($user->getUID(), $cachedMount->getUser()->getUID()); |
|
| 148 | + $this->assertEquals($storage->getCache()->getId(''), $cachedMount->getRootId()); |
|
| 149 | + $this->assertEquals($storage->getStorageCache()->getNumericId(), $cachedMount->getStorageId()); |
|
| 150 | + } |
|
| 151 | 151 | |
| 152 | - public function testSameMounts(): void { |
|
| 153 | - $this->eventDispatcher |
|
| 154 | - ->expects($this->once()) |
|
| 155 | - ->method('dispatchTyped') |
|
| 156 | - ->with($this->callback(fn (UserMountAddedEvent $event) => $event->mountPoint->getMountPoint() === '/asd/')); |
|
| 152 | + public function testSameMounts(): void { |
|
| 153 | + $this->eventDispatcher |
|
| 154 | + ->expects($this->once()) |
|
| 155 | + ->method('dispatchTyped') |
|
| 156 | + ->with($this->callback(fn (UserMountAddedEvent $event) => $event->mountPoint->getMountPoint() === '/asd/')); |
|
| 157 | 157 | |
| 158 | - $user = $this->userManager->get('u1'); |
|
| 158 | + $user = $this->userManager->get('u1'); |
|
| 159 | 159 | |
| 160 | - [$storage] = $this->getStorage(10); |
|
| 161 | - $mount = new MountPoint($storage, '/asd/'); |
|
| 160 | + [$storage] = $this->getStorage(10); |
|
| 161 | + $mount = new MountPoint($storage, '/asd/'); |
|
| 162 | 162 | |
| 163 | - $this->cache->registerMounts($user, [$mount]); |
|
| 163 | + $this->cache->registerMounts($user, [$mount]); |
|
| 164 | 164 | |
| 165 | - $this->clearCache(); |
|
| 165 | + $this->clearCache(); |
|
| 166 | 166 | |
| 167 | - $this->cache->registerMounts($user, [$mount]); |
|
| 167 | + $this->cache->registerMounts($user, [$mount]); |
|
| 168 | 168 | |
| 169 | - $this->clearCache(); |
|
| 169 | + $this->clearCache(); |
|
| 170 | 170 | |
| 171 | - $cachedMounts = $this->cache->getMountsForUser($user); |
|
| 171 | + $cachedMounts = $this->cache->getMountsForUser($user); |
|
| 172 | 172 | |
| 173 | - $this->assertCount(1, $cachedMounts); |
|
| 174 | - $cachedMount = $cachedMounts[$this->keyForMount($mount)]; |
|
| 175 | - $this->assertEquals('/asd/', $cachedMount->getMountPoint()); |
|
| 176 | - $this->assertEquals($user->getUID(), $cachedMount->getUser()->getUID()); |
|
| 177 | - $this->assertEquals($storage->getCache()->getId(''), $cachedMount->getRootId()); |
|
| 178 | - $this->assertEquals($storage->getStorageCache()->getNumericId(), $cachedMount->getStorageId()); |
|
| 179 | - } |
|
| 173 | + $this->assertCount(1, $cachedMounts); |
|
| 174 | + $cachedMount = $cachedMounts[$this->keyForMount($mount)]; |
|
| 175 | + $this->assertEquals('/asd/', $cachedMount->getMountPoint()); |
|
| 176 | + $this->assertEquals($user->getUID(), $cachedMount->getUser()->getUID()); |
|
| 177 | + $this->assertEquals($storage->getCache()->getId(''), $cachedMount->getRootId()); |
|
| 178 | + $this->assertEquals($storage->getStorageCache()->getNumericId(), $cachedMount->getStorageId()); |
|
| 179 | + } |
|
| 180 | 180 | |
| 181 | - public function testRemoveMounts(): void { |
|
| 182 | - $operation = 0; |
|
| 183 | - $this->eventDispatcher |
|
| 184 | - ->expects($this->exactly(2)) |
|
| 185 | - ->method('dispatchTyped') |
|
| 186 | - ->with($this->callback(function (UserMountAddedEvent|UserMountRemovedEvent $event) use (&$operation) { |
|
| 187 | - return match(++$operation) { |
|
| 188 | - 1 => $event instanceof UserMountAddedEvent && $event->mountPoint->getMountPoint() === '/asd/', |
|
| 189 | - 2 => $event instanceof UserMountRemovedEvent && $event->mountPoint->getMountPoint() === '/asd/', |
|
| 190 | - default => false, |
|
| 191 | - }; |
|
| 192 | - })); |
|
| 181 | + public function testRemoveMounts(): void { |
|
| 182 | + $operation = 0; |
|
| 183 | + $this->eventDispatcher |
|
| 184 | + ->expects($this->exactly(2)) |
|
| 185 | + ->method('dispatchTyped') |
|
| 186 | + ->with($this->callback(function (UserMountAddedEvent|UserMountRemovedEvent $event) use (&$operation) { |
|
| 187 | + return match(++$operation) { |
|
| 188 | + 1 => $event instanceof UserMountAddedEvent && $event->mountPoint->getMountPoint() === '/asd/', |
|
| 189 | + 2 => $event instanceof UserMountRemovedEvent && $event->mountPoint->getMountPoint() === '/asd/', |
|
| 190 | + default => false, |
|
| 191 | + }; |
|
| 192 | + })); |
|
| 193 | 193 | |
| 194 | - $user = $this->userManager->get('u1'); |
|
| 194 | + $user = $this->userManager->get('u1'); |
|
| 195 | 195 | |
| 196 | - [$storage] = $this->getStorage(10); |
|
| 197 | - $mount = new MountPoint($storage, '/asd/'); |
|
| 196 | + [$storage] = $this->getStorage(10); |
|
| 197 | + $mount = new MountPoint($storage, '/asd/'); |
|
| 198 | 198 | |
| 199 | - $this->cache->registerMounts($user, [$mount]); |
|
| 199 | + $this->cache->registerMounts($user, [$mount]); |
|
| 200 | 200 | |
| 201 | - $this->clearCache(); |
|
| 201 | + $this->clearCache(); |
|
| 202 | 202 | |
| 203 | - $this->cache->registerMounts($user, []); |
|
| 203 | + $this->cache->registerMounts($user, []); |
|
| 204 | 204 | |
| 205 | - $this->clearCache(); |
|
| 205 | + $this->clearCache(); |
|
| 206 | 206 | |
| 207 | - $cachedMounts = $this->cache->getMountsForUser($user); |
|
| 207 | + $cachedMounts = $this->cache->getMountsForUser($user); |
|
| 208 | 208 | |
| 209 | - $this->assertCount(0, $cachedMounts); |
|
| 210 | - } |
|
| 209 | + $this->assertCount(0, $cachedMounts); |
|
| 210 | + } |
|
| 211 | 211 | |
| 212 | - public function testChangeMounts(): void { |
|
| 213 | - $operation = 0; |
|
| 214 | - $this->eventDispatcher |
|
| 215 | - ->expects($this->exactly(3)) |
|
| 216 | - ->method('dispatchTyped') |
|
| 217 | - ->with($this->callback(function (UserMountAddedEvent|UserMountRemovedEvent $event) use (&$operation) { |
|
| 218 | - return match(++$operation) { |
|
| 219 | - 1 => $event instanceof UserMountAddedEvent && $event->mountPoint->getMountPoint() === '/bar/', |
|
| 220 | - 2 => $event instanceof UserMountAddedEvent && $event->mountPoint->getMountPoint() === '/foo/', |
|
| 221 | - 3 => $event instanceof UserMountRemovedEvent && $event->mountPoint->getMountPoint() === '/bar/', |
|
| 222 | - default => false, |
|
| 223 | - }; |
|
| 224 | - })); |
|
| 212 | + public function testChangeMounts(): void { |
|
| 213 | + $operation = 0; |
|
| 214 | + $this->eventDispatcher |
|
| 215 | + ->expects($this->exactly(3)) |
|
| 216 | + ->method('dispatchTyped') |
|
| 217 | + ->with($this->callback(function (UserMountAddedEvent|UserMountRemovedEvent $event) use (&$operation) { |
|
| 218 | + return match(++$operation) { |
|
| 219 | + 1 => $event instanceof UserMountAddedEvent && $event->mountPoint->getMountPoint() === '/bar/', |
|
| 220 | + 2 => $event instanceof UserMountAddedEvent && $event->mountPoint->getMountPoint() === '/foo/', |
|
| 221 | + 3 => $event instanceof UserMountRemovedEvent && $event->mountPoint->getMountPoint() === '/bar/', |
|
| 222 | + default => false, |
|
| 223 | + }; |
|
| 224 | + })); |
|
| 225 | 225 | |
| 226 | - $user = $this->userManager->get('u1'); |
|
| 226 | + $user = $this->userManager->get('u1'); |
|
| 227 | 227 | |
| 228 | - [$storage] = $this->getStorage(10); |
|
| 229 | - $mount = new MountPoint($storage, '/bar/'); |
|
| 228 | + [$storage] = $this->getStorage(10); |
|
| 229 | + $mount = new MountPoint($storage, '/bar/'); |
|
| 230 | 230 | |
| 231 | - $this->cache->registerMounts($user, [$mount]); |
|
| 231 | + $this->cache->registerMounts($user, [$mount]); |
|
| 232 | 232 | |
| 233 | - $this->clearCache(); |
|
| 233 | + $this->clearCache(); |
|
| 234 | 234 | |
| 235 | - $mount = new MountPoint($storage, '/foo/'); |
|
| 235 | + $mount = new MountPoint($storage, '/foo/'); |
|
| 236 | 236 | |
| 237 | - $this->cache->registerMounts($user, [$mount]); |
|
| 237 | + $this->cache->registerMounts($user, [$mount]); |
|
| 238 | 238 | |
| 239 | - $this->clearCache(); |
|
| 239 | + $this->clearCache(); |
|
| 240 | 240 | |
| 241 | - $cachedMounts = $this->cache->getMountsForUser($user); |
|
| 241 | + $cachedMounts = $this->cache->getMountsForUser($user); |
|
| 242 | 242 | |
| 243 | - $this->assertCount(1, $cachedMounts); |
|
| 244 | - $cachedMount = $cachedMounts[$this->keyForMount($mount)]; |
|
| 245 | - $this->assertEquals('/foo/', $cachedMount->getMountPoint()); |
|
| 246 | - } |
|
| 243 | + $this->assertCount(1, $cachedMounts); |
|
| 244 | + $cachedMount = $cachedMounts[$this->keyForMount($mount)]; |
|
| 245 | + $this->assertEquals('/foo/', $cachedMount->getMountPoint()); |
|
| 246 | + } |
|
| 247 | 247 | |
| 248 | - public function testChangeMountId(): void { |
|
| 249 | - $operation = 0; |
|
| 250 | - $this->eventDispatcher |
|
| 251 | - ->expects($this->exactly(2)) |
|
| 252 | - ->method('dispatchTyped') |
|
| 253 | - ->with($this->callback(function (UserMountAddedEvent|UserMountUpdatedEvent $event) use (&$operation) { |
|
| 254 | - return match(++$operation) { |
|
| 255 | - 1 => $event instanceof UserMountAddedEvent && $event->mountPoint->getMountPoint() === '/foo/', |
|
| 256 | - 2 => $event instanceof UserMountUpdatedEvent && $event->oldMountPoint->getMountId() === null && $event->newMountPoint->getMountId() === 1, |
|
| 257 | - default => false, |
|
| 258 | - }; |
|
| 259 | - })); |
|
| 248 | + public function testChangeMountId(): void { |
|
| 249 | + $operation = 0; |
|
| 250 | + $this->eventDispatcher |
|
| 251 | + ->expects($this->exactly(2)) |
|
| 252 | + ->method('dispatchTyped') |
|
| 253 | + ->with($this->callback(function (UserMountAddedEvent|UserMountUpdatedEvent $event) use (&$operation) { |
|
| 254 | + return match(++$operation) { |
|
| 255 | + 1 => $event instanceof UserMountAddedEvent && $event->mountPoint->getMountPoint() === '/foo/', |
|
| 256 | + 2 => $event instanceof UserMountUpdatedEvent && $event->oldMountPoint->getMountId() === null && $event->newMountPoint->getMountId() === 1, |
|
| 257 | + default => false, |
|
| 258 | + }; |
|
| 259 | + })); |
|
| 260 | 260 | |
| 261 | - $user = $this->userManager->get('u1'); |
|
| 261 | + $user = $this->userManager->get('u1'); |
|
| 262 | 262 | |
| 263 | - [$storage] = $this->getStorage(10); |
|
| 264 | - $mount = new MountPoint($storage, '/foo/', null, null, null, null); |
|
| 263 | + [$storage] = $this->getStorage(10); |
|
| 264 | + $mount = new MountPoint($storage, '/foo/', null, null, null, null); |
|
| 265 | 265 | |
| 266 | - $this->cache->registerMounts($user, [$mount]); |
|
| 266 | + $this->cache->registerMounts($user, [$mount]); |
|
| 267 | 267 | |
| 268 | - $this->clearCache(); |
|
| 268 | + $this->clearCache(); |
|
| 269 | 269 | |
| 270 | - $mount = new MountPoint($storage, '/foo/', null, null, null, 1); |
|
| 270 | + $mount = new MountPoint($storage, '/foo/', null, null, null, 1); |
|
| 271 | 271 | |
| 272 | - $this->cache->registerMounts($user, [$mount]); |
|
| 272 | + $this->cache->registerMounts($user, [$mount]); |
|
| 273 | 273 | |
| 274 | - $this->clearCache(); |
|
| 274 | + $this->clearCache(); |
|
| 275 | 275 | |
| 276 | - $cachedMounts = $this->cache->getMountsForUser($user); |
|
| 276 | + $cachedMounts = $this->cache->getMountsForUser($user); |
|
| 277 | 277 | |
| 278 | - $this->assertCount(1, $cachedMounts); |
|
| 279 | - $cachedMount = $cachedMounts[$this->keyForMount($mount)]; |
|
| 280 | - $this->assertEquals(1, $cachedMount->getMountId()); |
|
| 281 | - } |
|
| 278 | + $this->assertCount(1, $cachedMounts); |
|
| 279 | + $cachedMount = $cachedMounts[$this->keyForMount($mount)]; |
|
| 280 | + $this->assertEquals(1, $cachedMount->getMountId()); |
|
| 281 | + } |
|
| 282 | 282 | |
| 283 | - public function testGetMountsForUser(): void { |
|
| 284 | - $user1 = $this->userManager->get('u1'); |
|
| 285 | - $user2 = $this->userManager->get('u2'); |
|
| 286 | - $user3 = $this->userManager->get('u3'); |
|
| 283 | + public function testGetMountsForUser(): void { |
|
| 284 | + $user1 = $this->userManager->get('u1'); |
|
| 285 | + $user2 = $this->userManager->get('u2'); |
|
| 286 | + $user3 = $this->userManager->get('u3'); |
|
| 287 | 287 | |
| 288 | - [$storage1, $id1] = $this->getStorage(1); |
|
| 289 | - [$storage2, $id2] = $this->getStorage(2, 'foo/bar'); |
|
| 290 | - $mount1 = new MountPoint($storage1, '/foo/'); |
|
| 291 | - $mount2 = new MountPoint($storage2, '/bar/'); |
|
| 288 | + [$storage1, $id1] = $this->getStorage(1); |
|
| 289 | + [$storage2, $id2] = $this->getStorage(2, 'foo/bar'); |
|
| 290 | + $mount1 = new MountPoint($storage1, '/foo/'); |
|
| 291 | + $mount2 = new MountPoint($storage2, '/bar/'); |
|
| 292 | 292 | |
| 293 | - $this->cache->registerMounts($user1, [$mount1, $mount2]); |
|
| 294 | - $this->cache->registerMounts($user2, [$mount2]); |
|
| 295 | - $this->cache->registerMounts($user3, [$mount2]); |
|
| 293 | + $this->cache->registerMounts($user1, [$mount1, $mount2]); |
|
| 294 | + $this->cache->registerMounts($user2, [$mount2]); |
|
| 295 | + $this->cache->registerMounts($user3, [$mount2]); |
|
| 296 | 296 | |
| 297 | - $this->clearCache(); |
|
| 297 | + $this->clearCache(); |
|
| 298 | 298 | |
| 299 | - $user3->delete(); |
|
| 299 | + $user3->delete(); |
|
| 300 | 300 | |
| 301 | - $cachedMounts = $this->cache->getMountsForUser($user1); |
|
| 301 | + $cachedMounts = $this->cache->getMountsForUser($user1); |
|
| 302 | 302 | |
| 303 | - $this->assertCount(2, $cachedMounts); |
|
| 304 | - $this->assertEquals('/foo/', $cachedMounts[$this->keyForMount($mount1)]->getMountPoint()); |
|
| 305 | - $this->assertEquals($user1->getUID(), $cachedMounts[$this->keyForMount($mount1)]->getUser()->getUID()); |
|
| 306 | - $this->assertEquals($id1, $cachedMounts[$this->keyForMount($mount1)]->getRootId()); |
|
| 307 | - $this->assertEquals(1, $cachedMounts[$this->keyForMount($mount1)]->getStorageId()); |
|
| 308 | - $this->assertEquals('', $cachedMounts[$this->keyForMount($mount1)]->getRootInternalPath()); |
|
| 309 | - |
|
| 310 | - $this->assertEquals('/bar/', $cachedMounts[$this->keyForMount($mount2)]->getMountPoint()); |
|
| 311 | - $this->assertEquals($user1->getUID(), $cachedMounts[$this->keyForMount($mount2)]->getUser()->getUID()); |
|
| 312 | - $this->assertEquals($id2, $cachedMounts[$this->keyForMount($mount2)]->getRootId()); |
|
| 313 | - $this->assertEquals(2, $cachedMounts[$this->keyForMount($mount2)]->getStorageId()); |
|
| 314 | - $this->assertEquals('foo/bar', $cachedMounts[$this->keyForMount($mount2)]->getRootInternalPath()); |
|
| 315 | - |
|
| 316 | - $cachedMounts = $this->cache->getMountsForUser($user3); |
|
| 317 | - $this->assertEmpty($cachedMounts); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - public function testGetMountsByStorageId(): void { |
|
| 321 | - $user1 = $this->userManager->get('u1'); |
|
| 322 | - $user2 = $this->userManager->get('u2'); |
|
| 323 | - |
|
| 324 | - [$storage1, $id1] = $this->getStorage(1); |
|
| 325 | - [$storage2, $id2] = $this->getStorage(2); |
|
| 326 | - $mount1 = new MountPoint($storage1, '/foo/'); |
|
| 327 | - $mount2 = new MountPoint($storage2, '/bar/'); |
|
| 328 | - |
|
| 329 | - $this->cache->registerMounts($user1, [$mount1, $mount2]); |
|
| 330 | - $this->cache->registerMounts($user2, [$mount2]); |
|
| 331 | - |
|
| 332 | - $this->clearCache(); |
|
| 333 | - |
|
| 334 | - $cachedMounts = $this->cache->getMountsForStorageId(2); |
|
| 335 | - $this->sortMounts($cachedMounts); |
|
| 336 | - |
|
| 337 | - $this->assertCount(2, $cachedMounts); |
|
| 338 | - |
|
| 339 | - $this->assertEquals('/bar/', $cachedMounts[0]->getMountPoint()); |
|
| 340 | - $this->assertEquals($user1->getUID(), $cachedMounts[0]->getUser()->getUID()); |
|
| 341 | - $this->assertEquals($id2, $cachedMounts[0]->getRootId()); |
|
| 342 | - $this->assertEquals(2, $cachedMounts[0]->getStorageId()); |
|
| 343 | - |
|
| 344 | - $this->assertEquals('/bar/', $cachedMounts[1]->getMountPoint()); |
|
| 345 | - $this->assertEquals($user2->getUID(), $cachedMounts[1]->getUser()->getUID()); |
|
| 346 | - $this->assertEquals($id2, $cachedMounts[1]->getRootId()); |
|
| 347 | - $this->assertEquals(2, $cachedMounts[1]->getStorageId()); |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - public function testGetMountsByRootId(): void { |
|
| 351 | - $user1 = $this->userManager->get('u1'); |
|
| 352 | - $user2 = $this->userManager->get('u2'); |
|
| 353 | - |
|
| 354 | - [$storage1, $id1] = $this->getStorage(1); |
|
| 355 | - [$storage2, $id2] = $this->getStorage(2); |
|
| 356 | - $mount1 = new MountPoint($storage1, '/foo/'); |
|
| 357 | - $mount2 = new MountPoint($storage2, '/bar/'); |
|
| 303 | + $this->assertCount(2, $cachedMounts); |
|
| 304 | + $this->assertEquals('/foo/', $cachedMounts[$this->keyForMount($mount1)]->getMountPoint()); |
|
| 305 | + $this->assertEquals($user1->getUID(), $cachedMounts[$this->keyForMount($mount1)]->getUser()->getUID()); |
|
| 306 | + $this->assertEquals($id1, $cachedMounts[$this->keyForMount($mount1)]->getRootId()); |
|
| 307 | + $this->assertEquals(1, $cachedMounts[$this->keyForMount($mount1)]->getStorageId()); |
|
| 308 | + $this->assertEquals('', $cachedMounts[$this->keyForMount($mount1)]->getRootInternalPath()); |
|
| 309 | + |
|
| 310 | + $this->assertEquals('/bar/', $cachedMounts[$this->keyForMount($mount2)]->getMountPoint()); |
|
| 311 | + $this->assertEquals($user1->getUID(), $cachedMounts[$this->keyForMount($mount2)]->getUser()->getUID()); |
|
| 312 | + $this->assertEquals($id2, $cachedMounts[$this->keyForMount($mount2)]->getRootId()); |
|
| 313 | + $this->assertEquals(2, $cachedMounts[$this->keyForMount($mount2)]->getStorageId()); |
|
| 314 | + $this->assertEquals('foo/bar', $cachedMounts[$this->keyForMount($mount2)]->getRootInternalPath()); |
|
| 315 | + |
|
| 316 | + $cachedMounts = $this->cache->getMountsForUser($user3); |
|
| 317 | + $this->assertEmpty($cachedMounts); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + public function testGetMountsByStorageId(): void { |
|
| 321 | + $user1 = $this->userManager->get('u1'); |
|
| 322 | + $user2 = $this->userManager->get('u2'); |
|
| 323 | + |
|
| 324 | + [$storage1, $id1] = $this->getStorage(1); |
|
| 325 | + [$storage2, $id2] = $this->getStorage(2); |
|
| 326 | + $mount1 = new MountPoint($storage1, '/foo/'); |
|
| 327 | + $mount2 = new MountPoint($storage2, '/bar/'); |
|
| 328 | + |
|
| 329 | + $this->cache->registerMounts($user1, [$mount1, $mount2]); |
|
| 330 | + $this->cache->registerMounts($user2, [$mount2]); |
|
| 331 | + |
|
| 332 | + $this->clearCache(); |
|
| 333 | + |
|
| 334 | + $cachedMounts = $this->cache->getMountsForStorageId(2); |
|
| 335 | + $this->sortMounts($cachedMounts); |
|
| 336 | + |
|
| 337 | + $this->assertCount(2, $cachedMounts); |
|
| 338 | + |
|
| 339 | + $this->assertEquals('/bar/', $cachedMounts[0]->getMountPoint()); |
|
| 340 | + $this->assertEquals($user1->getUID(), $cachedMounts[0]->getUser()->getUID()); |
|
| 341 | + $this->assertEquals($id2, $cachedMounts[0]->getRootId()); |
|
| 342 | + $this->assertEquals(2, $cachedMounts[0]->getStorageId()); |
|
| 343 | + |
|
| 344 | + $this->assertEquals('/bar/', $cachedMounts[1]->getMountPoint()); |
|
| 345 | + $this->assertEquals($user2->getUID(), $cachedMounts[1]->getUser()->getUID()); |
|
| 346 | + $this->assertEquals($id2, $cachedMounts[1]->getRootId()); |
|
| 347 | + $this->assertEquals(2, $cachedMounts[1]->getStorageId()); |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + public function testGetMountsByRootId(): void { |
|
| 351 | + $user1 = $this->userManager->get('u1'); |
|
| 352 | + $user2 = $this->userManager->get('u2'); |
|
| 353 | + |
|
| 354 | + [$storage1, $id1] = $this->getStorage(1); |
|
| 355 | + [$storage2, $id2] = $this->getStorage(2); |
|
| 356 | + $mount1 = new MountPoint($storage1, '/foo/'); |
|
| 357 | + $mount2 = new MountPoint($storage2, '/bar/'); |
|
| 358 | 358 | |
| 359 | - $this->cache->registerMounts($user1, [$mount1, $mount2]); |
|
| 360 | - $this->cache->registerMounts($user2, [$mount2]); |
|
| 359 | + $this->cache->registerMounts($user1, [$mount1, $mount2]); |
|
| 360 | + $this->cache->registerMounts($user2, [$mount2]); |
|
| 361 | 361 | |
| 362 | - $this->clearCache(); |
|
| 363 | - |
|
| 364 | - $cachedMounts = $this->cache->getMountsForRootId($id2); |
|
| 365 | - $this->sortMounts($cachedMounts); |
|
| 366 | - |
|
| 367 | - $this->assertCount(2, $cachedMounts); |
|
| 368 | - |
|
| 369 | - $this->assertEquals('/bar/', $cachedMounts[0]->getMountPoint()); |
|
| 370 | - $this->assertEquals($user1->getUID(), $cachedMounts[0]->getUser()->getUID()); |
|
| 371 | - $this->assertEquals($id2, $cachedMounts[0]->getRootId()); |
|
| 372 | - $this->assertEquals(2, $cachedMounts[0]->getStorageId()); |
|
| 373 | - |
|
| 374 | - $this->assertEquals('/bar/', $cachedMounts[1]->getMountPoint()); |
|
| 375 | - $this->assertEquals($user2->getUID(), $cachedMounts[1]->getUser()->getUID()); |
|
| 376 | - $this->assertEquals($id2, $cachedMounts[1]->getRootId()); |
|
| 377 | - $this->assertEquals(2, $cachedMounts[1]->getStorageId()); |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - private function sortMounts(&$mounts) { |
|
| 381 | - usort($mounts, function (ICachedMountInfo $a, ICachedMountInfo $b) { |
|
| 382 | - return strcmp($a->getUser()->getUID(), $b->getUser()->getUID()); |
|
| 383 | - }); |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - private function createCacheEntry($internalPath, $storageId, $size = 0) { |
|
| 387 | - $internalPath = trim($internalPath, '/'); |
|
| 388 | - try { |
|
| 389 | - $query = $this->connection->getQueryBuilder(); |
|
| 390 | - $query->insert('filecache') |
|
| 391 | - ->values([ |
|
| 392 | - 'storage' => $query->createNamedParameter($storageId), |
|
| 393 | - 'path' => $query->createNamedParameter($internalPath), |
|
| 394 | - 'path_hash' => $query->createNamedParameter(md5($internalPath)), |
|
| 395 | - 'parent' => $query->createNamedParameter(-1, IQueryBuilder::PARAM_INT), |
|
| 396 | - 'name' => $query->createNamedParameter(basename($internalPath)), |
|
| 397 | - 'mimetype' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT), |
|
| 398 | - 'mimepart' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT), |
|
| 399 | - 'size' => $query->createNamedParameter($size), |
|
| 400 | - 'storage_mtime' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT), |
|
| 401 | - 'encrypted' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT), |
|
| 402 | - 'unencrypted_size' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT), |
|
| 403 | - 'etag' => $query->createNamedParameter(''), |
|
| 404 | - 'permissions' => $query->createNamedParameter(31, IQueryBuilder::PARAM_INT), |
|
| 405 | - ]); |
|
| 406 | - $query->executeStatement(); |
|
| 407 | - $id = $query->getLastInsertId(); |
|
| 408 | - $this->fileIds[] = $id; |
|
| 409 | - } catch (DbalException $e) { |
|
| 410 | - if ($e->getReason() === DbalException::REASON_UNIQUE_CONSTRAINT_VIOLATION) { |
|
| 411 | - $query = $this->connection->getQueryBuilder(); |
|
| 412 | - $query->select('fileid') |
|
| 413 | - ->from('filecache') |
|
| 414 | - ->where($query->expr()->eq('storage', $query->createNamedParameter($storageId))) |
|
| 415 | - ->andWhere($query->expr()->eq('path_hash', $query->createNamedParameter(md5($internalPath)))); |
|
| 416 | - $id = (int)$query->executeQuery()->fetchOne(); |
|
| 417 | - } else { |
|
| 418 | - throw $e; |
|
| 419 | - } |
|
| 420 | - } |
|
| 421 | - return $id; |
|
| 422 | - } |
|
| 362 | + $this->clearCache(); |
|
| 363 | + |
|
| 364 | + $cachedMounts = $this->cache->getMountsForRootId($id2); |
|
| 365 | + $this->sortMounts($cachedMounts); |
|
| 366 | + |
|
| 367 | + $this->assertCount(2, $cachedMounts); |
|
| 368 | + |
|
| 369 | + $this->assertEquals('/bar/', $cachedMounts[0]->getMountPoint()); |
|
| 370 | + $this->assertEquals($user1->getUID(), $cachedMounts[0]->getUser()->getUID()); |
|
| 371 | + $this->assertEquals($id2, $cachedMounts[0]->getRootId()); |
|
| 372 | + $this->assertEquals(2, $cachedMounts[0]->getStorageId()); |
|
| 373 | + |
|
| 374 | + $this->assertEquals('/bar/', $cachedMounts[1]->getMountPoint()); |
|
| 375 | + $this->assertEquals($user2->getUID(), $cachedMounts[1]->getUser()->getUID()); |
|
| 376 | + $this->assertEquals($id2, $cachedMounts[1]->getRootId()); |
|
| 377 | + $this->assertEquals(2, $cachedMounts[1]->getStorageId()); |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + private function sortMounts(&$mounts) { |
|
| 381 | + usort($mounts, function (ICachedMountInfo $a, ICachedMountInfo $b) { |
|
| 382 | + return strcmp($a->getUser()->getUID(), $b->getUser()->getUID()); |
|
| 383 | + }); |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + private function createCacheEntry($internalPath, $storageId, $size = 0) { |
|
| 387 | + $internalPath = trim($internalPath, '/'); |
|
| 388 | + try { |
|
| 389 | + $query = $this->connection->getQueryBuilder(); |
|
| 390 | + $query->insert('filecache') |
|
| 391 | + ->values([ |
|
| 392 | + 'storage' => $query->createNamedParameter($storageId), |
|
| 393 | + 'path' => $query->createNamedParameter($internalPath), |
|
| 394 | + 'path_hash' => $query->createNamedParameter(md5($internalPath)), |
|
| 395 | + 'parent' => $query->createNamedParameter(-1, IQueryBuilder::PARAM_INT), |
|
| 396 | + 'name' => $query->createNamedParameter(basename($internalPath)), |
|
| 397 | + 'mimetype' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT), |
|
| 398 | + 'mimepart' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT), |
|
| 399 | + 'size' => $query->createNamedParameter($size), |
|
| 400 | + 'storage_mtime' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT), |
|
| 401 | + 'encrypted' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT), |
|
| 402 | + 'unencrypted_size' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT), |
|
| 403 | + 'etag' => $query->createNamedParameter(''), |
|
| 404 | + 'permissions' => $query->createNamedParameter(31, IQueryBuilder::PARAM_INT), |
|
| 405 | + ]); |
|
| 406 | + $query->executeStatement(); |
|
| 407 | + $id = $query->getLastInsertId(); |
|
| 408 | + $this->fileIds[] = $id; |
|
| 409 | + } catch (DbalException $e) { |
|
| 410 | + if ($e->getReason() === DbalException::REASON_UNIQUE_CONSTRAINT_VIOLATION) { |
|
| 411 | + $query = $this->connection->getQueryBuilder(); |
|
| 412 | + $query->select('fileid') |
|
| 413 | + ->from('filecache') |
|
| 414 | + ->where($query->expr()->eq('storage', $query->createNamedParameter($storageId))) |
|
| 415 | + ->andWhere($query->expr()->eq('path_hash', $query->createNamedParameter(md5($internalPath)))); |
|
| 416 | + $id = (int)$query->executeQuery()->fetchOne(); |
|
| 417 | + } else { |
|
| 418 | + throw $e; |
|
| 419 | + } |
|
| 420 | + } |
|
| 421 | + return $id; |
|
| 422 | + } |
|
| 423 | 423 | |
| 424 | - public function testGetMountsForFileIdRootId(): void { |
|
| 425 | - $user1 = $this->userManager->get('u1'); |
|
| 424 | + public function testGetMountsForFileIdRootId(): void { |
|
| 425 | + $user1 = $this->userManager->get('u1'); |
|
| 426 | 426 | |
| 427 | - [$storage1, $rootId] = $this->getStorage(2); |
|
| 428 | - $mount1 = new MountPoint($storage1, '/foo/'); |
|
| 427 | + [$storage1, $rootId] = $this->getStorage(2); |
|
| 428 | + $mount1 = new MountPoint($storage1, '/foo/'); |
|
| 429 | 429 | |
| 430 | - $this->cache->registerMounts($user1, [$mount1]); |
|
| 430 | + $this->cache->registerMounts($user1, [$mount1]); |
|
| 431 | 431 | |
| 432 | - $this->clearCache(); |
|
| 432 | + $this->clearCache(); |
|
| 433 | 433 | |
| 434 | - $cachedMounts = $this->cache->getMountsForFileId($rootId); |
|
| 434 | + $cachedMounts = $this->cache->getMountsForFileId($rootId); |
|
| 435 | 435 | |
| 436 | - $this->assertCount(1, $cachedMounts); |
|
| 436 | + $this->assertCount(1, $cachedMounts); |
|
| 437 | 437 | |
| 438 | - $this->assertEquals('/foo/', $cachedMounts[0]->getMountPoint()); |
|
| 439 | - $this->assertEquals($user1->getUID(), $cachedMounts[0]->getUser()->getUID()); |
|
| 440 | - $this->assertEquals($rootId, $cachedMounts[0]->getRootId()); |
|
| 441 | - $this->assertEquals(2, $cachedMounts[0]->getStorageId()); |
|
| 442 | - } |
|
| 438 | + $this->assertEquals('/foo/', $cachedMounts[0]->getMountPoint()); |
|
| 439 | + $this->assertEquals($user1->getUID(), $cachedMounts[0]->getUser()->getUID()); |
|
| 440 | + $this->assertEquals($rootId, $cachedMounts[0]->getRootId()); |
|
| 441 | + $this->assertEquals(2, $cachedMounts[0]->getStorageId()); |
|
| 442 | + } |
|
| 443 | 443 | |
| 444 | - public function testGetMountsForFileIdSubFolder(): void { |
|
| 445 | - $user1 = $this->userManager->get('u1'); |
|
| 444 | + public function testGetMountsForFileIdSubFolder(): void { |
|
| 445 | + $user1 = $this->userManager->get('u1'); |
|
| 446 | 446 | |
| 447 | - $fileId = $this->createCacheEntry('/foo/bar', 2); |
|
| 447 | + $fileId = $this->createCacheEntry('/foo/bar', 2); |
|
| 448 | 448 | |
| 449 | - [$storage1, $rootId] = $this->getStorage(2); |
|
| 450 | - $mount1 = new MountPoint($storage1, '/foo/'); |
|
| 449 | + [$storage1, $rootId] = $this->getStorage(2); |
|
| 450 | + $mount1 = new MountPoint($storage1, '/foo/'); |
|
| 451 | 451 | |
| 452 | - $this->cache->registerMounts($user1, [$mount1]); |
|
| 452 | + $this->cache->registerMounts($user1, [$mount1]); |
|
| 453 | 453 | |
| 454 | - $this->clearCache(); |
|
| 454 | + $this->clearCache(); |
|
| 455 | 455 | |
| 456 | - $cachedMounts = $this->cache->getMountsForFileId($fileId); |
|
| 457 | - |
|
| 458 | - $this->assertCount(1, $cachedMounts); |
|
| 459 | - |
|
| 460 | - $this->assertEquals('/foo/', $cachedMounts[0]->getMountPoint()); |
|
| 461 | - $this->assertEquals($user1->getUID(), $cachedMounts[0]->getUser()->getUID()); |
|
| 462 | - $this->assertEquals($rootId, $cachedMounts[0]->getRootId()); |
|
| 463 | - $this->assertEquals(2, $cachedMounts[0]->getStorageId()); |
|
| 464 | - $this->assertEquals('foo/bar', $cachedMounts[0]->getInternalPath()); |
|
| 465 | - $this->assertEquals('/foo/foo/bar', $cachedMounts[0]->getPath()); |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - public function testGetMountsForFileIdSubFolderMount(): void { |
|
| 469 | - $user1 = $this->userManager->get('u1'); |
|
| 470 | - |
|
| 471 | - [$storage1, $rootId] = $this->getStorage(2); |
|
| 472 | - $folderId = $this->createCacheEntry('/foo', 2); |
|
| 473 | - $fileId = $this->createCacheEntry('/foo/bar', 2); |
|
| 456 | + $cachedMounts = $this->cache->getMountsForFileId($fileId); |
|
| 457 | + |
|
| 458 | + $this->assertCount(1, $cachedMounts); |
|
| 459 | + |
|
| 460 | + $this->assertEquals('/foo/', $cachedMounts[0]->getMountPoint()); |
|
| 461 | + $this->assertEquals($user1->getUID(), $cachedMounts[0]->getUser()->getUID()); |
|
| 462 | + $this->assertEquals($rootId, $cachedMounts[0]->getRootId()); |
|
| 463 | + $this->assertEquals(2, $cachedMounts[0]->getStorageId()); |
|
| 464 | + $this->assertEquals('foo/bar', $cachedMounts[0]->getInternalPath()); |
|
| 465 | + $this->assertEquals('/foo/foo/bar', $cachedMounts[0]->getPath()); |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + public function testGetMountsForFileIdSubFolderMount(): void { |
|
| 469 | + $user1 = $this->userManager->get('u1'); |
|
| 470 | + |
|
| 471 | + [$storage1, $rootId] = $this->getStorage(2); |
|
| 472 | + $folderId = $this->createCacheEntry('/foo', 2); |
|
| 473 | + $fileId = $this->createCacheEntry('/foo/bar', 2); |
|
| 474 | 474 | |
| 475 | 475 | |
| 476 | - $mount1 = $this->getMockBuilder(MountPoint::class) |
|
| 477 | - ->setConstructorArgs([$storage1, '/']) |
|
| 478 | - ->onlyMethods(['getStorageRootId']) |
|
| 479 | - ->getMock(); |
|
| 476 | + $mount1 = $this->getMockBuilder(MountPoint::class) |
|
| 477 | + ->setConstructorArgs([$storage1, '/']) |
|
| 478 | + ->onlyMethods(['getStorageRootId']) |
|
| 479 | + ->getMock(); |
|
| 480 | 480 | |
| 481 | - $mount1->expects($this->any()) |
|
| 482 | - ->method('getStorageRootId') |
|
| 483 | - ->willReturn($folderId); |
|
| 481 | + $mount1->expects($this->any()) |
|
| 482 | + ->method('getStorageRootId') |
|
| 483 | + ->willReturn($folderId); |
|
| 484 | 484 | |
| 485 | - $this->cache->registerMounts($user1, [$mount1]); |
|
| 485 | + $this->cache->registerMounts($user1, [$mount1]); |
|
| 486 | 486 | |
| 487 | - $this->clearCache(); |
|
| 487 | + $this->clearCache(); |
|
| 488 | 488 | |
| 489 | - $cachedMounts = $this->cache->getMountsForFileId($fileId); |
|
| 489 | + $cachedMounts = $this->cache->getMountsForFileId($fileId); |
|
| 490 | 490 | |
| 491 | - $this->assertCount(1, $cachedMounts); |
|
| 491 | + $this->assertCount(1, $cachedMounts); |
|
| 492 | 492 | |
| 493 | - $this->assertEquals('/', $cachedMounts[0]->getMountPoint()); |
|
| 494 | - $this->assertEquals($user1->getUID(), $cachedMounts[0]->getUser()->getUID()); |
|
| 495 | - $this->assertEquals($folderId, $cachedMounts[0]->getRootId()); |
|
| 496 | - $this->assertEquals(2, $cachedMounts[0]->getStorageId()); |
|
| 497 | - $this->assertEquals('foo', $cachedMounts[0]->getRootInternalPath()); |
|
| 498 | - $this->assertEquals('bar', $cachedMounts[0]->getInternalPath()); |
|
| 499 | - $this->assertEquals('/bar', $cachedMounts[0]->getPath()); |
|
| 500 | - } |
|
| 493 | + $this->assertEquals('/', $cachedMounts[0]->getMountPoint()); |
|
| 494 | + $this->assertEquals($user1->getUID(), $cachedMounts[0]->getUser()->getUID()); |
|
| 495 | + $this->assertEquals($folderId, $cachedMounts[0]->getRootId()); |
|
| 496 | + $this->assertEquals(2, $cachedMounts[0]->getStorageId()); |
|
| 497 | + $this->assertEquals('foo', $cachedMounts[0]->getRootInternalPath()); |
|
| 498 | + $this->assertEquals('bar', $cachedMounts[0]->getInternalPath()); |
|
| 499 | + $this->assertEquals('/bar', $cachedMounts[0]->getPath()); |
|
| 500 | + } |
|
| 501 | 501 | |
| 502 | - public function testGetMountsForFileIdSubFolderMountOutside(): void { |
|
| 503 | - $user1 = $this->userManager->get('u1'); |
|
| 502 | + public function testGetMountsForFileIdSubFolderMountOutside(): void { |
|
| 503 | + $user1 = $this->userManager->get('u1'); |
|
| 504 | 504 | |
| 505 | - [$storage1, $rootId] = $this->getStorage(2); |
|
| 506 | - $folderId = $this->createCacheEntry('/foo', 2); |
|
| 507 | - $fileId = $this->createCacheEntry('/bar/asd', 2); |
|
| 505 | + [$storage1, $rootId] = $this->getStorage(2); |
|
| 506 | + $folderId = $this->createCacheEntry('/foo', 2); |
|
| 507 | + $fileId = $this->createCacheEntry('/bar/asd', 2); |
|
| 508 | 508 | |
| 509 | - $mount1 = $this->getMockBuilder(MountPoint::class) |
|
| 510 | - ->setConstructorArgs([$storage1, '/foo/']) |
|
| 511 | - ->onlyMethods(['getStorageRootId']) |
|
| 512 | - ->getMock(); |
|
| 509 | + $mount1 = $this->getMockBuilder(MountPoint::class) |
|
| 510 | + ->setConstructorArgs([$storage1, '/foo/']) |
|
| 511 | + ->onlyMethods(['getStorageRootId']) |
|
| 512 | + ->getMock(); |
|
| 513 | 513 | |
| 514 | - $mount1->expects($this->any()) |
|
| 515 | - ->method('getStorageRootId') |
|
| 516 | - ->willReturn($folderId); |
|
| 514 | + $mount1->expects($this->any()) |
|
| 515 | + ->method('getStorageRootId') |
|
| 516 | + ->willReturn($folderId); |
|
| 517 | 517 | |
| 518 | - $this->cache->registerMounts($user1, [$mount1]); |
|
| 518 | + $this->cache->registerMounts($user1, [$mount1]); |
|
| 519 | 519 | |
| 520 | - $this->cache->registerMounts($user1, [$mount1]); |
|
| 520 | + $this->cache->registerMounts($user1, [$mount1]); |
|
| 521 | 521 | |
| 522 | - $this->clearCache(); |
|
| 522 | + $this->clearCache(); |
|
| 523 | 523 | |
| 524 | - $cachedMounts = $this->cache->getMountsForFileId($fileId); |
|
| 524 | + $cachedMounts = $this->cache->getMountsForFileId($fileId); |
|
| 525 | 525 | |
| 526 | - $this->assertCount(0, $cachedMounts); |
|
| 527 | - } |
|
| 526 | + $this->assertCount(0, $cachedMounts); |
|
| 527 | + } |
|
| 528 | 528 | |
| 529 | 529 | |
| 530 | - public function testGetMountsForFileIdDeletedUser(): void { |
|
| 531 | - $user1 = $this->userManager->get('u1'); |
|
| 530 | + public function testGetMountsForFileIdDeletedUser(): void { |
|
| 531 | + $user1 = $this->userManager->get('u1'); |
|
| 532 | 532 | |
| 533 | - [$storage1, $rootId] = $this->getStorage(2); |
|
| 534 | - $rootId = $this->createCacheEntry('', 2); |
|
| 535 | - $mount1 = new MountPoint($storage1, '/foo/'); |
|
| 536 | - $this->cache->registerMounts($user1, [$mount1]); |
|
| 533 | + [$storage1, $rootId] = $this->getStorage(2); |
|
| 534 | + $rootId = $this->createCacheEntry('', 2); |
|
| 535 | + $mount1 = new MountPoint($storage1, '/foo/'); |
|
| 536 | + $this->cache->registerMounts($user1, [$mount1]); |
|
| 537 | 537 | |
| 538 | - $user1->delete(); |
|
| 539 | - $this->clearCache(); |
|
| 538 | + $user1->delete(); |
|
| 539 | + $this->clearCache(); |
|
| 540 | 540 | |
| 541 | - $cachedMounts = $this->cache->getMountsForFileId($rootId); |
|
| 542 | - $this->assertEmpty($cachedMounts); |
|
| 543 | - } |
|
| 541 | + $cachedMounts = $this->cache->getMountsForFileId($rootId); |
|
| 542 | + $this->assertEmpty($cachedMounts); |
|
| 543 | + } |
|
| 544 | 544 | |
| 545 | - public function testGetUsedSpaceForUsers(): void { |
|
| 546 | - $user1 = $this->userManager->get('u1'); |
|
| 547 | - $user2 = $this->userManager->get('u2'); |
|
| 545 | + public function testGetUsedSpaceForUsers(): void { |
|
| 546 | + $user1 = $this->userManager->get('u1'); |
|
| 547 | + $user2 = $this->userManager->get('u2'); |
|
| 548 | 548 | |
| 549 | - /** @var Storage $storage1 */ |
|
| 550 | - [$storage1, $rootId] = $this->getStorage(2); |
|
| 551 | - $folderId = $this->createCacheEntry('files', 2, 100); |
|
| 552 | - $fileId = $this->createCacheEntry('files/foo', 2, 7); |
|
| 553 | - $storage1->getCache()->put($folderId, ['size' => 100]); |
|
| 554 | - $storage1->getCache()->update($fileId, ['size' => 70]); |
|
| 549 | + /** @var Storage $storage1 */ |
|
| 550 | + [$storage1, $rootId] = $this->getStorage(2); |
|
| 551 | + $folderId = $this->createCacheEntry('files', 2, 100); |
|
| 552 | + $fileId = $this->createCacheEntry('files/foo', 2, 7); |
|
| 553 | + $storage1->getCache()->put($folderId, ['size' => 100]); |
|
| 554 | + $storage1->getCache()->update($fileId, ['size' => 70]); |
|
| 555 | 555 | |
| 556 | - $mount1 = $this->getMockBuilder(MountPoint::class) |
|
| 557 | - ->setConstructorArgs([$storage1, '/u1/']) |
|
| 558 | - ->onlyMethods(['getStorageRootId', 'getNumericStorageId']) |
|
| 559 | - ->getMock(); |
|
| 556 | + $mount1 = $this->getMockBuilder(MountPoint::class) |
|
| 557 | + ->setConstructorArgs([$storage1, '/u1/']) |
|
| 558 | + ->onlyMethods(['getStorageRootId', 'getNumericStorageId']) |
|
| 559 | + ->getMock(); |
|
| 560 | 560 | |
| 561 | - $mount1->expects($this->any()) |
|
| 562 | - ->method('getStorageRootId') |
|
| 563 | - ->willReturn($rootId); |
|
| 561 | + $mount1->expects($this->any()) |
|
| 562 | + ->method('getStorageRootId') |
|
| 563 | + ->willReturn($rootId); |
|
| 564 | 564 | |
| 565 | - $mount1->expects($this->any()) |
|
| 566 | - ->method('getNumericStorageId') |
|
| 567 | - ->willReturn(2); |
|
| 565 | + $mount1->expects($this->any()) |
|
| 566 | + ->method('getNumericStorageId') |
|
| 567 | + ->willReturn(2); |
|
| 568 | 568 | |
| 569 | - $this->cache->registerMounts($user1, [$mount1]); |
|
| 569 | + $this->cache->registerMounts($user1, [$mount1]); |
|
| 570 | 570 | |
| 571 | - $result = $this->cache->getUsedSpaceForUsers([$user1, $user2]); |
|
| 572 | - $this->assertEquals(['u1' => 100], $result); |
|
| 573 | - } |
|
| 571 | + $result = $this->cache->getUsedSpaceForUsers([$user1, $user2]); |
|
| 572 | + $this->assertEquals(['u1' => 100], $result); |
|
| 573 | + } |
|
| 574 | 574 | |
| 575 | 575 | |
| 576 | - public function testMigrateMountProvider(): void { |
|
| 577 | - $user1 = $this->userManager->get('u1'); |
|
| 576 | + public function testMigrateMountProvider(): void { |
|
| 577 | + $user1 = $this->userManager->get('u1'); |
|
| 578 | 578 | |
| 579 | - [$storage1, $rootId] = $this->getStorage(2); |
|
| 580 | - $rootId = $this->createCacheEntry('', 2); |
|
| 581 | - $mount1 = new MountPoint($storage1, '/foo/'); |
|
| 582 | - $this->cache->registerMounts($user1, [$mount1]); |
|
| 579 | + [$storage1, $rootId] = $this->getStorage(2); |
|
| 580 | + $rootId = $this->createCacheEntry('', 2); |
|
| 581 | + $mount1 = new MountPoint($storage1, '/foo/'); |
|
| 582 | + $this->cache->registerMounts($user1, [$mount1]); |
|
| 583 | 583 | |
| 584 | - $this->clearCache(); |
|
| 584 | + $this->clearCache(); |
|
| 585 | 585 | |
| 586 | - $cachedMounts = $this->cache->getMountsForUser($user1); |
|
| 587 | - $this->assertCount(1, $cachedMounts); |
|
| 588 | - $this->assertEquals('', $cachedMounts[$this->keyForMount($mount1)]->getMountProvider()); |
|
| 586 | + $cachedMounts = $this->cache->getMountsForUser($user1); |
|
| 587 | + $this->assertCount(1, $cachedMounts); |
|
| 588 | + $this->assertEquals('', $cachedMounts[$this->keyForMount($mount1)]->getMountProvider()); |
|
| 589 | 589 | |
| 590 | - $mount1 = new MountPoint($storage1, '/foo/', null, null, null, null, 'dummy'); |
|
| 591 | - $this->cache->registerMounts($user1, [$mount1], ['dummy']); |
|
| 590 | + $mount1 = new MountPoint($storage1, '/foo/', null, null, null, null, 'dummy'); |
|
| 591 | + $this->cache->registerMounts($user1, [$mount1], ['dummy']); |
|
| 592 | 592 | |
| 593 | - $this->clearCache(); |
|
| 593 | + $this->clearCache(); |
|
| 594 | 594 | |
| 595 | - $cachedMounts = $this->cache->getMountsForUser($user1); |
|
| 596 | - $this->assertCount(1, $cachedMounts); |
|
| 597 | - $this->assertEquals('dummy', $cachedMounts[$this->keyForMount($mount1)]->getMountProvider()); |
|
| 598 | - } |
|
| 595 | + $cachedMounts = $this->cache->getMountsForUser($user1); |
|
| 596 | + $this->assertCount(1, $cachedMounts); |
|
| 597 | + $this->assertEquals('dummy', $cachedMounts[$this->keyForMount($mount1)]->getMountProvider()); |
|
| 598 | + } |
|
| 599 | 599 | } |
@@ -31,78 +31,78 @@ |
||
| 31 | 31 | |
| 32 | 32 | class TemplateManagerTest extends TestCase { |
| 33 | 33 | |
| 34 | - private IRootFolder $rootFolder; |
|
| 35 | - private Coordinator $bootstrapCoordinator; |
|
| 36 | - |
|
| 37 | - private TemplateManager $templateManager; |
|
| 38 | - |
|
| 39 | - protected function setUp(): void { |
|
| 40 | - parent::setUp(); |
|
| 41 | - |
|
| 42 | - $l10n = $this->createMock(IL10N::class); |
|
| 43 | - $l10n->method('t') |
|
| 44 | - ->willReturnCallback(fn ($string, $params) => sprintf($string, ...$params)); |
|
| 45 | - $l10nFactory = $this->createMock(IFactory::class); |
|
| 46 | - $l10nFactory->method('get') |
|
| 47 | - ->willReturn($l10n); |
|
| 48 | - $database = $this->createMock(IDBConnection::class); |
|
| 49 | - $database->method('supports4ByteText')->willReturn(true); |
|
| 50 | - $config = $this->createMock(IConfig::class); |
|
| 51 | - $logger = new NullLogger(); |
|
| 52 | - |
|
| 53 | - $filenameValidator = new FilenameValidator( |
|
| 54 | - $l10nFactory, |
|
| 55 | - $database, |
|
| 56 | - $config, |
|
| 57 | - $logger, |
|
| 58 | - ); |
|
| 59 | - |
|
| 60 | - $serverContainer = $this->createMock(IServerContainer::class); |
|
| 61 | - $eventDispatcher = $this->createMock(IEventDispatcher::class); |
|
| 62 | - $this->bootstrapCoordinator = $this->createMock(Coordinator::class); |
|
| 63 | - $this->bootstrapCoordinator->method('getRegistrationContext') |
|
| 64 | - ->willReturn(new RegistrationContext($logger)); |
|
| 65 | - $this->rootFolder = $this->createMock(IRootFolder::class); |
|
| 66 | - $userSession = $this->createMock(IUserSession::class); |
|
| 67 | - $userManager = $this->createMock(IUserManager::class); |
|
| 68 | - $previewManager = $this->createMock(IPreview::class); |
|
| 69 | - |
|
| 70 | - $this->templateManager = new TemplateManager( |
|
| 71 | - $serverContainer, |
|
| 72 | - $eventDispatcher, |
|
| 73 | - $this->bootstrapCoordinator, |
|
| 74 | - $this->rootFolder, |
|
| 75 | - $userSession, |
|
| 76 | - $userManager, |
|
| 77 | - $previewManager, |
|
| 78 | - $config, |
|
| 79 | - $l10nFactory, |
|
| 80 | - $logger, |
|
| 81 | - $filenameValidator |
|
| 82 | - ); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - public function testCreateFromTemplateShoudValidateFilename(): void { |
|
| 86 | - $this->expectException(GenericFileException::class); |
|
| 87 | - |
|
| 88 | - $fileDirectory = '/'; |
|
| 89 | - $filePath = $fileDirectory . str_repeat('a', 251); |
|
| 90 | - |
|
| 91 | - $userFolder = $this->createMock(Folder::class); |
|
| 92 | - $userFolder->method('get') |
|
| 93 | - ->willReturnCallback(function ($path) use ($filePath, $fileDirectory) { |
|
| 94 | - if ($path === $filePath) { |
|
| 95 | - throw new NotFoundException(); |
|
| 96 | - } |
|
| 97 | - return $this->createMock(Folder::class); |
|
| 98 | - }); |
|
| 99 | - $userFolder->method('nodeExists') |
|
| 100 | - ->willReturnCallback(function ($path) use ($filePath, $fileDirectory) { |
|
| 101 | - return $path === $fileDirectory; |
|
| 102 | - }); |
|
| 103 | - $this->rootFolder->method('getUserFolder') |
|
| 104 | - ->willReturn($userFolder); |
|
| 105 | - |
|
| 106 | - $this->templateManager->createFromTemplate($filePath); |
|
| 107 | - } |
|
| 34 | + private IRootFolder $rootFolder; |
|
| 35 | + private Coordinator $bootstrapCoordinator; |
|
| 36 | + |
|
| 37 | + private TemplateManager $templateManager; |
|
| 38 | + |
|
| 39 | + protected function setUp(): void { |
|
| 40 | + parent::setUp(); |
|
| 41 | + |
|
| 42 | + $l10n = $this->createMock(IL10N::class); |
|
| 43 | + $l10n->method('t') |
|
| 44 | + ->willReturnCallback(fn ($string, $params) => sprintf($string, ...$params)); |
|
| 45 | + $l10nFactory = $this->createMock(IFactory::class); |
|
| 46 | + $l10nFactory->method('get') |
|
| 47 | + ->willReturn($l10n); |
|
| 48 | + $database = $this->createMock(IDBConnection::class); |
|
| 49 | + $database->method('supports4ByteText')->willReturn(true); |
|
| 50 | + $config = $this->createMock(IConfig::class); |
|
| 51 | + $logger = new NullLogger(); |
|
| 52 | + |
|
| 53 | + $filenameValidator = new FilenameValidator( |
|
| 54 | + $l10nFactory, |
|
| 55 | + $database, |
|
| 56 | + $config, |
|
| 57 | + $logger, |
|
| 58 | + ); |
|
| 59 | + |
|
| 60 | + $serverContainer = $this->createMock(IServerContainer::class); |
|
| 61 | + $eventDispatcher = $this->createMock(IEventDispatcher::class); |
|
| 62 | + $this->bootstrapCoordinator = $this->createMock(Coordinator::class); |
|
| 63 | + $this->bootstrapCoordinator->method('getRegistrationContext') |
|
| 64 | + ->willReturn(new RegistrationContext($logger)); |
|
| 65 | + $this->rootFolder = $this->createMock(IRootFolder::class); |
|
| 66 | + $userSession = $this->createMock(IUserSession::class); |
|
| 67 | + $userManager = $this->createMock(IUserManager::class); |
|
| 68 | + $previewManager = $this->createMock(IPreview::class); |
|
| 69 | + |
|
| 70 | + $this->templateManager = new TemplateManager( |
|
| 71 | + $serverContainer, |
|
| 72 | + $eventDispatcher, |
|
| 73 | + $this->bootstrapCoordinator, |
|
| 74 | + $this->rootFolder, |
|
| 75 | + $userSession, |
|
| 76 | + $userManager, |
|
| 77 | + $previewManager, |
|
| 78 | + $config, |
|
| 79 | + $l10nFactory, |
|
| 80 | + $logger, |
|
| 81 | + $filenameValidator |
|
| 82 | + ); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + public function testCreateFromTemplateShoudValidateFilename(): void { |
|
| 86 | + $this->expectException(GenericFileException::class); |
|
| 87 | + |
|
| 88 | + $fileDirectory = '/'; |
|
| 89 | + $filePath = $fileDirectory . str_repeat('a', 251); |
|
| 90 | + |
|
| 91 | + $userFolder = $this->createMock(Folder::class); |
|
| 92 | + $userFolder->method('get') |
|
| 93 | + ->willReturnCallback(function ($path) use ($filePath, $fileDirectory) { |
|
| 94 | + if ($path === $filePath) { |
|
| 95 | + throw new NotFoundException(); |
|
| 96 | + } |
|
| 97 | + return $this->createMock(Folder::class); |
|
| 98 | + }); |
|
| 99 | + $userFolder->method('nodeExists') |
|
| 100 | + ->willReturnCallback(function ($path) use ($filePath, $fileDirectory) { |
|
| 101 | + return $path === $fileDirectory; |
|
| 102 | + }); |
|
| 103 | + $this->rootFolder->method('getUserFolder') |
|
| 104 | + ->willReturn($userFolder); |
|
| 105 | + |
|
| 106 | + $this->templateManager->createFromTemplate($filePath); |
|
| 107 | + } |
|
| 108 | 108 | } |
@@ -23,189 +23,189 @@ |
||
| 23 | 23 | |
| 24 | 24 | #[\PHPUnit\Framework\Attributes\Group('DB')] |
| 25 | 25 | class SearchBuilderTest extends TestCase { |
| 26 | - /** @var IQueryBuilder */ |
|
| 27 | - private $builder; |
|
| 28 | - |
|
| 29 | - /** @var IMimeTypeLoader&\PHPUnit\Framework\MockObject\MockObject */ |
|
| 30 | - private $mimetypeLoader; |
|
| 31 | - |
|
| 32 | - /** @var IFilesMetadataManager&\PHPUnit\Framework\MockObject\MockObject */ |
|
| 33 | - private $filesMetadataManager; |
|
| 34 | - |
|
| 35 | - /** @var SearchBuilder */ |
|
| 36 | - private $searchBuilder; |
|
| 37 | - |
|
| 38 | - /** @var integer */ |
|
| 39 | - private $numericStorageId; |
|
| 40 | - |
|
| 41 | - protected function setUp(): void { |
|
| 42 | - parent::setUp(); |
|
| 43 | - $this->builder = Server::get(IDBConnection::class)->getQueryBuilder(); |
|
| 44 | - $this->mimetypeLoader = $this->createMock(IMimeTypeLoader::class); |
|
| 45 | - $this->filesMetadataManager = $this->createMock(IFilesMetadataManager::class); |
|
| 46 | - |
|
| 47 | - $this->mimetypeLoader->expects($this->any()) |
|
| 48 | - ->method('getId') |
|
| 49 | - ->willReturnMap([ |
|
| 50 | - ['text', 1], |
|
| 51 | - ['text/plain', 2], |
|
| 52 | - ['text/xml', 3], |
|
| 53 | - ['image/jpg', 4], |
|
| 54 | - ['image/png', 5], |
|
| 55 | - ['image', 6], |
|
| 56 | - ]); |
|
| 57 | - |
|
| 58 | - $this->mimetypeLoader->expects($this->any()) |
|
| 59 | - ->method('getMimetypeById') |
|
| 60 | - ->willReturnMap([ |
|
| 61 | - [1, 'text'], |
|
| 62 | - [2, 'text/plain'], |
|
| 63 | - [3, 'text/xml'], |
|
| 64 | - [4, 'image/jpg'], |
|
| 65 | - [5, 'image/png'], |
|
| 66 | - [6, 'image'] |
|
| 67 | - ]); |
|
| 68 | - |
|
| 69 | - $this->searchBuilder = new SearchBuilder($this->mimetypeLoader, $this->filesMetadataManager); |
|
| 70 | - $this->numericStorageId = 10000; |
|
| 71 | - |
|
| 72 | - $this->builder->select(['fileid']) |
|
| 73 | - ->from('filecache', 'file') // alias needed for QuerySearchHelper#getOperatorFieldAndValue |
|
| 74 | - ->where($this->builder->expr()->eq('storage', new Literal($this->numericStorageId))); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - protected function tearDown(): void { |
|
| 78 | - parent::tearDown(); |
|
| 79 | - |
|
| 80 | - $builder = Server::get(IDBConnection::class)->getQueryBuilder(); |
|
| 81 | - |
|
| 82 | - $builder->delete('filecache') |
|
| 83 | - ->where($builder->expr()->eq('storage', $builder->createNamedParameter($this->numericStorageId, IQueryBuilder::PARAM_INT))); |
|
| 84 | - |
|
| 85 | - $builder->executeStatement(); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - private function addCacheEntry(array $data) { |
|
| 89 | - $data['storage'] = $this->numericStorageId; |
|
| 90 | - $data['etag'] = 'unimportant'; |
|
| 91 | - $data['storage_mtime'] = $data['mtime']; |
|
| 92 | - if (!isset($data['path'])) { |
|
| 93 | - $data['path'] = 'random/' . $this->getUniqueID(); |
|
| 94 | - } |
|
| 95 | - $data['path_hash'] = md5($data['path']); |
|
| 96 | - if (!isset($data['mtime'])) { |
|
| 97 | - $data['mtime'] = 100; |
|
| 98 | - } |
|
| 99 | - if (!isset($data['size'])) { |
|
| 100 | - $data['size'] = 100; |
|
| 101 | - } |
|
| 102 | - $data['name'] = basename($data['path']); |
|
| 103 | - $data['parent'] = -1; |
|
| 104 | - if (isset($data['mimetype'])) { |
|
| 105 | - [$mimepart,] = explode('/', $data['mimetype']); |
|
| 106 | - $data['mimepart'] = $this->mimetypeLoader->getId($mimepart); |
|
| 107 | - $data['mimetype'] = $this->mimetypeLoader->getId($data['mimetype']); |
|
| 108 | - } else { |
|
| 109 | - $data['mimepart'] = 1; |
|
| 110 | - $data['mimetype'] = 1; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - $builder = Server::get(IDBConnection::class)->getQueryBuilder(); |
|
| 114 | - |
|
| 115 | - $values = []; |
|
| 116 | - foreach ($data as $key => $value) { |
|
| 117 | - $values[$key] = $builder->createNamedParameter($value); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - $builder->insert('filecache') |
|
| 121 | - ->values($values) |
|
| 122 | - ->executeStatement(); |
|
| 123 | - |
|
| 124 | - return $builder->getLastInsertId(); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - private function search(ISearchOperator $operator) { |
|
| 128 | - $dbOperator = $this->searchBuilder->searchOperatorToDBExpr($this->builder, $operator); |
|
| 129 | - $this->builder->andWhere($dbOperator); |
|
| 130 | - |
|
| 131 | - $result = $this->builder->executeQuery(); |
|
| 132 | - $rows = $result->fetchFirstColumn(); |
|
| 133 | - $result->closeCursor(); |
|
| 134 | - |
|
| 135 | - return $rows; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - public static function comparisonProvider(): array { |
|
| 139 | - return [ |
|
| 140 | - [new SearchComparison(ISearchComparison::COMPARE_GREATER_THAN, 'mtime', 125), [1]], |
|
| 141 | - [new SearchComparison(ISearchComparison::COMPARE_LESS_THAN, 'mtime', 125), [0]], |
|
| 142 | - [new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'size', 125), []], |
|
| 143 | - [new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'size', 50), [0, 1]], |
|
| 144 | - [new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'name', 'foobar'), [0]], |
|
| 145 | - [new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', 'foo%'), [0, 1]], |
|
| 146 | - [new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', 'image/jpg'), [0]], |
|
| 147 | - [new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', 'image/%'), [0, 1]], |
|
| 148 | - [new SearchComparison(ISearchComparison::COMPARE_IN, 'mimetype', ['image/jpg', 'image/png']), [0, 1]], |
|
| 149 | - [new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_AND, [ |
|
| 150 | - new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'size', 50), |
|
| 151 | - new SearchComparison(ISearchComparison::COMPARE_LESS_THAN, 'mtime', 125) |
|
| 152 | - ]), [0]], |
|
| 153 | - [new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_AND, [ |
|
| 154 | - new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'size', 50), |
|
| 155 | - new SearchComparison(ISearchComparison::COMPARE_LESS_THAN, 'mtime', 125), |
|
| 156 | - new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', 'text/%') |
|
| 157 | - ]), []], |
|
| 158 | - [new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR, [ |
|
| 159 | - new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mtime', 100), |
|
| 160 | - new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mtime', 150), |
|
| 161 | - ]), [0, 1]], |
|
| 162 | - [new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_NOT, [ |
|
| 163 | - new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mtime', 150), |
|
| 164 | - ]), [0]], |
|
| 165 | - [new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_NOT, [ |
|
| 166 | - new SearchComparison(ISearchComparison::COMPARE_GREATER_THAN, 'mtime', 125), |
|
| 167 | - ]), [0]], |
|
| 168 | - [new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_NOT, [ |
|
| 169 | - new SearchComparison(ISearchComparison::COMPARE_LESS_THAN, 'mtime', 125), |
|
| 170 | - ]), [1]], |
|
| 171 | - [new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_NOT, [ |
|
| 172 | - new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%bar'), |
|
| 173 | - ]), [1]], |
|
| 174 | - |
|
| 175 | - ]; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * |
|
| 180 | - * @param ISearchOperator $operator |
|
| 181 | - * @param array $fileIds |
|
| 182 | - */ |
|
| 183 | - #[\PHPUnit\Framework\Attributes\DataProvider('comparisonProvider')] |
|
| 184 | - public function testComparison(ISearchOperator $operator, array $fileIds): void { |
|
| 185 | - $fileId = []; |
|
| 186 | - $fileId[] = $this->addCacheEntry([ |
|
| 187 | - 'path' => 'foobar', |
|
| 188 | - 'mtime' => 100, |
|
| 189 | - 'size' => 50, |
|
| 190 | - 'mimetype' => 'image/jpg' |
|
| 191 | - ]); |
|
| 192 | - |
|
| 193 | - $fileId[] = $this->addCacheEntry([ |
|
| 194 | - 'path' => 'fooasd', |
|
| 195 | - 'mtime' => 150, |
|
| 196 | - 'size' => 50, |
|
| 197 | - 'mimetype' => 'image/png' |
|
| 198 | - ]); |
|
| 199 | - |
|
| 200 | - $fileIds = array_map(function ($i) use ($fileId) { |
|
| 201 | - return $fileId[$i]; |
|
| 202 | - }, $fileIds); |
|
| 203 | - |
|
| 204 | - $results = $this->search($operator); |
|
| 205 | - |
|
| 206 | - sort($fileIds); |
|
| 207 | - sort($results); |
|
| 208 | - |
|
| 209 | - $this->assertEquals($fileIds, $results); |
|
| 210 | - } |
|
| 26 | + /** @var IQueryBuilder */ |
|
| 27 | + private $builder; |
|
| 28 | + |
|
| 29 | + /** @var IMimeTypeLoader&\PHPUnit\Framework\MockObject\MockObject */ |
|
| 30 | + private $mimetypeLoader; |
|
| 31 | + |
|
| 32 | + /** @var IFilesMetadataManager&\PHPUnit\Framework\MockObject\MockObject */ |
|
| 33 | + private $filesMetadataManager; |
|
| 34 | + |
|
| 35 | + /** @var SearchBuilder */ |
|
| 36 | + private $searchBuilder; |
|
| 37 | + |
|
| 38 | + /** @var integer */ |
|
| 39 | + private $numericStorageId; |
|
| 40 | + |
|
| 41 | + protected function setUp(): void { |
|
| 42 | + parent::setUp(); |
|
| 43 | + $this->builder = Server::get(IDBConnection::class)->getQueryBuilder(); |
|
| 44 | + $this->mimetypeLoader = $this->createMock(IMimeTypeLoader::class); |
|
| 45 | + $this->filesMetadataManager = $this->createMock(IFilesMetadataManager::class); |
|
| 46 | + |
|
| 47 | + $this->mimetypeLoader->expects($this->any()) |
|
| 48 | + ->method('getId') |
|
| 49 | + ->willReturnMap([ |
|
| 50 | + ['text', 1], |
|
| 51 | + ['text/plain', 2], |
|
| 52 | + ['text/xml', 3], |
|
| 53 | + ['image/jpg', 4], |
|
| 54 | + ['image/png', 5], |
|
| 55 | + ['image', 6], |
|
| 56 | + ]); |
|
| 57 | + |
|
| 58 | + $this->mimetypeLoader->expects($this->any()) |
|
| 59 | + ->method('getMimetypeById') |
|
| 60 | + ->willReturnMap([ |
|
| 61 | + [1, 'text'], |
|
| 62 | + [2, 'text/plain'], |
|
| 63 | + [3, 'text/xml'], |
|
| 64 | + [4, 'image/jpg'], |
|
| 65 | + [5, 'image/png'], |
|
| 66 | + [6, 'image'] |
|
| 67 | + ]); |
|
| 68 | + |
|
| 69 | + $this->searchBuilder = new SearchBuilder($this->mimetypeLoader, $this->filesMetadataManager); |
|
| 70 | + $this->numericStorageId = 10000; |
|
| 71 | + |
|
| 72 | + $this->builder->select(['fileid']) |
|
| 73 | + ->from('filecache', 'file') // alias needed for QuerySearchHelper#getOperatorFieldAndValue |
|
| 74 | + ->where($this->builder->expr()->eq('storage', new Literal($this->numericStorageId))); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + protected function tearDown(): void { |
|
| 78 | + parent::tearDown(); |
|
| 79 | + |
|
| 80 | + $builder = Server::get(IDBConnection::class)->getQueryBuilder(); |
|
| 81 | + |
|
| 82 | + $builder->delete('filecache') |
|
| 83 | + ->where($builder->expr()->eq('storage', $builder->createNamedParameter($this->numericStorageId, IQueryBuilder::PARAM_INT))); |
|
| 84 | + |
|
| 85 | + $builder->executeStatement(); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + private function addCacheEntry(array $data) { |
|
| 89 | + $data['storage'] = $this->numericStorageId; |
|
| 90 | + $data['etag'] = 'unimportant'; |
|
| 91 | + $data['storage_mtime'] = $data['mtime']; |
|
| 92 | + if (!isset($data['path'])) { |
|
| 93 | + $data['path'] = 'random/' . $this->getUniqueID(); |
|
| 94 | + } |
|
| 95 | + $data['path_hash'] = md5($data['path']); |
|
| 96 | + if (!isset($data['mtime'])) { |
|
| 97 | + $data['mtime'] = 100; |
|
| 98 | + } |
|
| 99 | + if (!isset($data['size'])) { |
|
| 100 | + $data['size'] = 100; |
|
| 101 | + } |
|
| 102 | + $data['name'] = basename($data['path']); |
|
| 103 | + $data['parent'] = -1; |
|
| 104 | + if (isset($data['mimetype'])) { |
|
| 105 | + [$mimepart,] = explode('/', $data['mimetype']); |
|
| 106 | + $data['mimepart'] = $this->mimetypeLoader->getId($mimepart); |
|
| 107 | + $data['mimetype'] = $this->mimetypeLoader->getId($data['mimetype']); |
|
| 108 | + } else { |
|
| 109 | + $data['mimepart'] = 1; |
|
| 110 | + $data['mimetype'] = 1; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + $builder = Server::get(IDBConnection::class)->getQueryBuilder(); |
|
| 114 | + |
|
| 115 | + $values = []; |
|
| 116 | + foreach ($data as $key => $value) { |
|
| 117 | + $values[$key] = $builder->createNamedParameter($value); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + $builder->insert('filecache') |
|
| 121 | + ->values($values) |
|
| 122 | + ->executeStatement(); |
|
| 123 | + |
|
| 124 | + return $builder->getLastInsertId(); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + private function search(ISearchOperator $operator) { |
|
| 128 | + $dbOperator = $this->searchBuilder->searchOperatorToDBExpr($this->builder, $operator); |
|
| 129 | + $this->builder->andWhere($dbOperator); |
|
| 130 | + |
|
| 131 | + $result = $this->builder->executeQuery(); |
|
| 132 | + $rows = $result->fetchFirstColumn(); |
|
| 133 | + $result->closeCursor(); |
|
| 134 | + |
|
| 135 | + return $rows; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + public static function comparisonProvider(): array { |
|
| 139 | + return [ |
|
| 140 | + [new SearchComparison(ISearchComparison::COMPARE_GREATER_THAN, 'mtime', 125), [1]], |
|
| 141 | + [new SearchComparison(ISearchComparison::COMPARE_LESS_THAN, 'mtime', 125), [0]], |
|
| 142 | + [new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'size', 125), []], |
|
| 143 | + [new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'size', 50), [0, 1]], |
|
| 144 | + [new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'name', 'foobar'), [0]], |
|
| 145 | + [new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', 'foo%'), [0, 1]], |
|
| 146 | + [new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', 'image/jpg'), [0]], |
|
| 147 | + [new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', 'image/%'), [0, 1]], |
|
| 148 | + [new SearchComparison(ISearchComparison::COMPARE_IN, 'mimetype', ['image/jpg', 'image/png']), [0, 1]], |
|
| 149 | + [new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_AND, [ |
|
| 150 | + new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'size', 50), |
|
| 151 | + new SearchComparison(ISearchComparison::COMPARE_LESS_THAN, 'mtime', 125) |
|
| 152 | + ]), [0]], |
|
| 153 | + [new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_AND, [ |
|
| 154 | + new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'size', 50), |
|
| 155 | + new SearchComparison(ISearchComparison::COMPARE_LESS_THAN, 'mtime', 125), |
|
| 156 | + new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', 'text/%') |
|
| 157 | + ]), []], |
|
| 158 | + [new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR, [ |
|
| 159 | + new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mtime', 100), |
|
| 160 | + new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mtime', 150), |
|
| 161 | + ]), [0, 1]], |
|
| 162 | + [new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_NOT, [ |
|
| 163 | + new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mtime', 150), |
|
| 164 | + ]), [0]], |
|
| 165 | + [new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_NOT, [ |
|
| 166 | + new SearchComparison(ISearchComparison::COMPARE_GREATER_THAN, 'mtime', 125), |
|
| 167 | + ]), [0]], |
|
| 168 | + [new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_NOT, [ |
|
| 169 | + new SearchComparison(ISearchComparison::COMPARE_LESS_THAN, 'mtime', 125), |
|
| 170 | + ]), [1]], |
|
| 171 | + [new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_NOT, [ |
|
| 172 | + new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%bar'), |
|
| 173 | + ]), [1]], |
|
| 174 | + |
|
| 175 | + ]; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * |
|
| 180 | + * @param ISearchOperator $operator |
|
| 181 | + * @param array $fileIds |
|
| 182 | + */ |
|
| 183 | + #[\PHPUnit\Framework\Attributes\DataProvider('comparisonProvider')] |
|
| 184 | + public function testComparison(ISearchOperator $operator, array $fileIds): void { |
|
| 185 | + $fileId = []; |
|
| 186 | + $fileId[] = $this->addCacheEntry([ |
|
| 187 | + 'path' => 'foobar', |
|
| 188 | + 'mtime' => 100, |
|
| 189 | + 'size' => 50, |
|
| 190 | + 'mimetype' => 'image/jpg' |
|
| 191 | + ]); |
|
| 192 | + |
|
| 193 | + $fileId[] = $this->addCacheEntry([ |
|
| 194 | + 'path' => 'fooasd', |
|
| 195 | + 'mtime' => 150, |
|
| 196 | + 'size' => 50, |
|
| 197 | + 'mimetype' => 'image/png' |
|
| 198 | + ]); |
|
| 199 | + |
|
| 200 | + $fileIds = array_map(function ($i) use ($fileId) { |
|
| 201 | + return $fileId[$i]; |
|
| 202 | + }, $fileIds); |
|
| 203 | + |
|
| 204 | + $results = $this->search($operator); |
|
| 205 | + |
|
| 206 | + sort($fileIds); |
|
| 207 | + sort($results); |
|
| 208 | + |
|
| 209 | + $this->assertEquals($fileIds, $results); |
|
| 210 | + } |
|
| 211 | 211 | } |
@@ -25,109 +25,109 @@ |
||
| 25 | 25 | #[\PHPUnit\Framework\Attributes\Group('DB')] |
| 26 | 26 | class OldGroupMembershipSharesTest extends \Test\TestCase { |
| 27 | 27 | |
| 28 | - private IDBConnection $connection; |
|
| 29 | - private IGroupManager&MockObject $groupManager; |
|
| 30 | - |
|
| 31 | - protected function setUp(): void { |
|
| 32 | - parent::setUp(); |
|
| 33 | - |
|
| 34 | - $this->groupManager = $this->getMockBuilder(IGroupManager::class) |
|
| 35 | - ->disableOriginalConstructor() |
|
| 36 | - ->getMock(); |
|
| 37 | - $this->connection = Server::get(IDBConnection::class); |
|
| 38 | - |
|
| 39 | - $this->deleteAllShares(); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - protected function tearDown(): void { |
|
| 43 | - $this->deleteAllShares(); |
|
| 44 | - |
|
| 45 | - parent::tearDown(); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - protected function deleteAllShares() { |
|
| 49 | - $qb = $this->connection->getQueryBuilder(); |
|
| 50 | - $qb->delete('share')->executeStatement(); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - public function testRun(): void { |
|
| 54 | - $repair = new OldGroupMembershipShares( |
|
| 55 | - $this->connection, |
|
| 56 | - $this->groupManager |
|
| 57 | - ); |
|
| 58 | - |
|
| 59 | - $this->groupManager->expects($this->exactly(2)) |
|
| 60 | - ->method('isInGroup') |
|
| 61 | - ->willReturnMap([ |
|
| 62 | - ['member', 'group', true], |
|
| 63 | - ['not-a-member', 'group', false], |
|
| 64 | - ]); |
|
| 65 | - |
|
| 66 | - $parent = $this->createShare(IShare::TYPE_GROUP, 'group', null); |
|
| 67 | - $group2 = $this->createShare(IShare::TYPE_GROUP, 'group2', $parent); |
|
| 68 | - $user1 = $this->createShare(IShare::TYPE_USER, 'user1', $parent); |
|
| 69 | - |
|
| 70 | - // \OC\Share\Constant::$shareTypeGroupUserUnique === 2 |
|
| 71 | - $member = $this->createShare(2, 'member', $parent); |
|
| 72 | - $notAMember = $this->createShare(2, 'not-a-member', $parent); |
|
| 73 | - |
|
| 74 | - $query = $this->connection->getQueryBuilder(); |
|
| 75 | - $result = $query->select('id') |
|
| 76 | - ->from('share') |
|
| 77 | - ->orderBy('id', 'ASC') |
|
| 78 | - ->executeQuery(); |
|
| 79 | - $rows = $result->fetchAllAssociative(); |
|
| 80 | - $this->assertEquals([['id' => $parent], ['id' => $group2], ['id' => $user1], ['id' => $member], ['id' => $notAMember]], $rows); |
|
| 81 | - $result->closeCursor(); |
|
| 82 | - |
|
| 83 | - /** @var IOutput | \PHPUnit\Framework\MockObject\MockObject $outputMock */ |
|
| 84 | - $outputMock = $this->getMockBuilder('\OCP\Migration\IOutput') |
|
| 85 | - ->disableOriginalConstructor() |
|
| 86 | - ->getMock(); |
|
| 87 | - |
|
| 88 | - $repair->run($outputMock); |
|
| 89 | - |
|
| 90 | - $query = $this->connection->getQueryBuilder(); |
|
| 91 | - $result = $query->select('id') |
|
| 92 | - ->from('share') |
|
| 93 | - ->orderBy('id', 'ASC') |
|
| 94 | - ->executeQuery(); |
|
| 95 | - $rows = $result->fetchAllAssociative(); |
|
| 96 | - $this->assertEquals([['id' => $parent], ['id' => $group2], ['id' => $user1], ['id' => $member]], $rows); |
|
| 97 | - $result->closeCursor(); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * @param string $shareType |
|
| 102 | - * @param string $shareWith |
|
| 103 | - * @param null|int $parent |
|
| 104 | - * @return int |
|
| 105 | - */ |
|
| 106 | - protected function createShare($shareType, $shareWith, $parent) { |
|
| 107 | - $qb = $this->connection->getQueryBuilder(); |
|
| 108 | - $shareValues = [ |
|
| 109 | - 'share_type' => $qb->expr()->literal($shareType), |
|
| 110 | - 'share_with' => $qb->expr()->literal($shareWith), |
|
| 111 | - 'uid_owner' => $qb->expr()->literal('user1'), |
|
| 112 | - 'item_type' => $qb->expr()->literal('folder'), |
|
| 113 | - 'item_source' => $qb->expr()->literal(123), |
|
| 114 | - 'item_target' => $qb->expr()->literal('/123'), |
|
| 115 | - 'file_source' => $qb->expr()->literal(123), |
|
| 116 | - 'file_target' => $qb->expr()->literal('/test'), |
|
| 117 | - 'permissions' => $qb->expr()->literal(1), |
|
| 118 | - 'stime' => $qb->expr()->literal(time()), |
|
| 119 | - 'expiration' => $qb->expr()->literal('2015-09-25 00:00:00'), |
|
| 120 | - ]; |
|
| 121 | - |
|
| 122 | - if ($parent) { |
|
| 123 | - $shareValues['parent'] = $qb->expr()->literal($parent); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - $qb = $this->connection->getQueryBuilder(); |
|
| 127 | - $qb->insert('share') |
|
| 128 | - ->values($shareValues) |
|
| 129 | - ->executeStatement(); |
|
| 130 | - |
|
| 131 | - return $qb->getLastInsertId(); |
|
| 132 | - } |
|
| 28 | + private IDBConnection $connection; |
|
| 29 | + private IGroupManager&MockObject $groupManager; |
|
| 30 | + |
|
| 31 | + protected function setUp(): void { |
|
| 32 | + parent::setUp(); |
|
| 33 | + |
|
| 34 | + $this->groupManager = $this->getMockBuilder(IGroupManager::class) |
|
| 35 | + ->disableOriginalConstructor() |
|
| 36 | + ->getMock(); |
|
| 37 | + $this->connection = Server::get(IDBConnection::class); |
|
| 38 | + |
|
| 39 | + $this->deleteAllShares(); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + protected function tearDown(): void { |
|
| 43 | + $this->deleteAllShares(); |
|
| 44 | + |
|
| 45 | + parent::tearDown(); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + protected function deleteAllShares() { |
|
| 49 | + $qb = $this->connection->getQueryBuilder(); |
|
| 50 | + $qb->delete('share')->executeStatement(); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + public function testRun(): void { |
|
| 54 | + $repair = new OldGroupMembershipShares( |
|
| 55 | + $this->connection, |
|
| 56 | + $this->groupManager |
|
| 57 | + ); |
|
| 58 | + |
|
| 59 | + $this->groupManager->expects($this->exactly(2)) |
|
| 60 | + ->method('isInGroup') |
|
| 61 | + ->willReturnMap([ |
|
| 62 | + ['member', 'group', true], |
|
| 63 | + ['not-a-member', 'group', false], |
|
| 64 | + ]); |
|
| 65 | + |
|
| 66 | + $parent = $this->createShare(IShare::TYPE_GROUP, 'group', null); |
|
| 67 | + $group2 = $this->createShare(IShare::TYPE_GROUP, 'group2', $parent); |
|
| 68 | + $user1 = $this->createShare(IShare::TYPE_USER, 'user1', $parent); |
|
| 69 | + |
|
| 70 | + // \OC\Share\Constant::$shareTypeGroupUserUnique === 2 |
|
| 71 | + $member = $this->createShare(2, 'member', $parent); |
|
| 72 | + $notAMember = $this->createShare(2, 'not-a-member', $parent); |
|
| 73 | + |
|
| 74 | + $query = $this->connection->getQueryBuilder(); |
|
| 75 | + $result = $query->select('id') |
|
| 76 | + ->from('share') |
|
| 77 | + ->orderBy('id', 'ASC') |
|
| 78 | + ->executeQuery(); |
|
| 79 | + $rows = $result->fetchAllAssociative(); |
|
| 80 | + $this->assertEquals([['id' => $parent], ['id' => $group2], ['id' => $user1], ['id' => $member], ['id' => $notAMember]], $rows); |
|
| 81 | + $result->closeCursor(); |
|
| 82 | + |
|
| 83 | + /** @var IOutput | \PHPUnit\Framework\MockObject\MockObject $outputMock */ |
|
| 84 | + $outputMock = $this->getMockBuilder('\OCP\Migration\IOutput') |
|
| 85 | + ->disableOriginalConstructor() |
|
| 86 | + ->getMock(); |
|
| 87 | + |
|
| 88 | + $repair->run($outputMock); |
|
| 89 | + |
|
| 90 | + $query = $this->connection->getQueryBuilder(); |
|
| 91 | + $result = $query->select('id') |
|
| 92 | + ->from('share') |
|
| 93 | + ->orderBy('id', 'ASC') |
|
| 94 | + ->executeQuery(); |
|
| 95 | + $rows = $result->fetchAllAssociative(); |
|
| 96 | + $this->assertEquals([['id' => $parent], ['id' => $group2], ['id' => $user1], ['id' => $member]], $rows); |
|
| 97 | + $result->closeCursor(); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * @param string $shareType |
|
| 102 | + * @param string $shareWith |
|
| 103 | + * @param null|int $parent |
|
| 104 | + * @return int |
|
| 105 | + */ |
|
| 106 | + protected function createShare($shareType, $shareWith, $parent) { |
|
| 107 | + $qb = $this->connection->getQueryBuilder(); |
|
| 108 | + $shareValues = [ |
|
| 109 | + 'share_type' => $qb->expr()->literal($shareType), |
|
| 110 | + 'share_with' => $qb->expr()->literal($shareWith), |
|
| 111 | + 'uid_owner' => $qb->expr()->literal('user1'), |
|
| 112 | + 'item_type' => $qb->expr()->literal('folder'), |
|
| 113 | + 'item_source' => $qb->expr()->literal(123), |
|
| 114 | + 'item_target' => $qb->expr()->literal('/123'), |
|
| 115 | + 'file_source' => $qb->expr()->literal(123), |
|
| 116 | + 'file_target' => $qb->expr()->literal('/test'), |
|
| 117 | + 'permissions' => $qb->expr()->literal(1), |
|
| 118 | + 'stime' => $qb->expr()->literal(time()), |
|
| 119 | + 'expiration' => $qb->expr()->literal('2015-09-25 00:00:00'), |
|
| 120 | + ]; |
|
| 121 | + |
|
| 122 | + if ($parent) { |
|
| 123 | + $shareValues['parent'] = $qb->expr()->literal($parent); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + $qb = $this->connection->getQueryBuilder(); |
|
| 127 | + $qb->insert('share') |
|
| 128 | + ->values($shareValues) |
|
| 129 | + ->executeStatement(); |
|
| 130 | + |
|
| 131 | + return $qb->getLastInsertId(); |
|
| 132 | + } |
|
| 133 | 133 | } |
@@ -25,132 +25,132 @@ |
||
| 25 | 25 | #[\PHPUnit\Framework\Attributes\Group('DB')] |
| 26 | 26 | class UpdateLanguageCodesTest extends TestCase { |
| 27 | 27 | |
| 28 | - protected IDBConnection $connection; |
|
| 29 | - private IConfig&MockObject $config; |
|
| 30 | - |
|
| 31 | - protected function setUp(): void { |
|
| 32 | - parent::setUp(); |
|
| 33 | - |
|
| 34 | - $this->connection = Server::get(IDBConnection::class); |
|
| 35 | - $this->config = $this->createMock(IConfig::class); |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - public function testRun(): void { |
|
| 39 | - $users = [ |
|
| 40 | - ['userid' => 'user1', 'configvalue' => 'fi_FI'], |
|
| 41 | - ['userid' => 'user2', 'configvalue' => 'de'], |
|
| 42 | - ['userid' => 'user3', 'configvalue' => 'fi'], |
|
| 43 | - ['userid' => 'user4', 'configvalue' => 'ja'], |
|
| 44 | - ['userid' => 'user5', 'configvalue' => 'bg_BG'], |
|
| 45 | - ['userid' => 'user6', 'configvalue' => 'ja'], |
|
| 46 | - ['userid' => 'user7', 'configvalue' => 'th_TH'], |
|
| 47 | - ['userid' => 'user8', 'configvalue' => 'th_TH'], |
|
| 48 | - ]; |
|
| 49 | - |
|
| 50 | - // insert test data |
|
| 51 | - $qb = $this->connection->getQueryBuilder(); |
|
| 52 | - $qb->insert('preferences') |
|
| 53 | - ->values([ |
|
| 54 | - 'userid' => $qb->createParameter('userid'), |
|
| 55 | - 'appid' => $qb->createParameter('appid'), |
|
| 56 | - 'configkey' => $qb->createParameter('configkey'), |
|
| 57 | - 'configvalue' => $qb->createParameter('configvalue'), |
|
| 58 | - ]); |
|
| 59 | - foreach ($users as $user) { |
|
| 60 | - $qb->setParameters([ |
|
| 61 | - 'userid' => $user['userid'], |
|
| 62 | - 'appid' => 'core', |
|
| 63 | - 'configkey' => 'lang', |
|
| 64 | - 'configvalue' => $user['configvalue'], |
|
| 65 | - ])->executeStatement(); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - // check if test data is written to DB |
|
| 69 | - $qb = $this->connection->getQueryBuilder(); |
|
| 70 | - $result = $qb->select(['userid', 'configvalue']) |
|
| 71 | - ->from('preferences') |
|
| 72 | - ->where($qb->expr()->eq('appid', $qb->createNamedParameter('core'))) |
|
| 73 | - ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang'))) |
|
| 74 | - ->orderBy('userid') |
|
| 75 | - ->executeQuery(); |
|
| 76 | - |
|
| 77 | - $rows = $result->fetchAllAssociative(); |
|
| 78 | - $result->closeCursor(); |
|
| 79 | - |
|
| 80 | - $this->assertSame($users, $rows, 'Asserts that the entries are the ones from the test data set'); |
|
| 81 | - |
|
| 82 | - $expectedOutput = [ |
|
| 83 | - ['Changed 1 setting(s) from "bg_BG" to "bg" in preferences table.'], |
|
| 84 | - ['Changed 0 setting(s) from "cs_CZ" to "cs" in preferences table.'], |
|
| 85 | - ['Changed 1 setting(s) from "fi_FI" to "fi" in preferences table.'], |
|
| 86 | - ['Changed 0 setting(s) from "hu_HU" to "hu" in preferences table.'], |
|
| 87 | - ['Changed 0 setting(s) from "nb_NO" to "nb" in preferences table.'], |
|
| 88 | - ['Changed 0 setting(s) from "sk_SK" to "sk" in preferences table.'], |
|
| 89 | - ['Changed 2 setting(s) from "th_TH" to "th" in preferences table.'], |
|
| 90 | - ]; |
|
| 91 | - $outputMessages = []; |
|
| 92 | - /** @var IOutput&MockObject $outputMock */ |
|
| 93 | - $outputMock = $this->createMock(IOutput::class); |
|
| 94 | - $outputMock->expects($this->exactly(7)) |
|
| 95 | - ->method('info') |
|
| 96 | - ->willReturnCallback(function () use (&$outputMessages): void { |
|
| 97 | - $outputMessages[] = func_get_args(); |
|
| 98 | - }); |
|
| 99 | - |
|
| 100 | - $this->config->expects($this->once()) |
|
| 101 | - ->method('getSystemValueString') |
|
| 102 | - ->with('version', '0.0.0') |
|
| 103 | - ->willReturn('12.0.0.13'); |
|
| 104 | - |
|
| 105 | - // run repair step |
|
| 106 | - $repair = new UpdateLanguageCodes($this->connection, $this->config); |
|
| 107 | - $repair->run($outputMock); |
|
| 108 | - |
|
| 109 | - // check if test data is correctly modified in DB |
|
| 110 | - $qb = $this->connection->getQueryBuilder(); |
|
| 111 | - $result = $qb->select(['userid', 'configvalue']) |
|
| 112 | - ->from('preferences') |
|
| 113 | - ->where($qb->expr()->eq('appid', $qb->createNamedParameter('core'))) |
|
| 114 | - ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang'))) |
|
| 115 | - ->orderBy('userid') |
|
| 116 | - ->executeQuery(); |
|
| 117 | - |
|
| 118 | - $rows = $result->fetchAllAssociative(); |
|
| 119 | - $result->closeCursor(); |
|
| 120 | - |
|
| 121 | - // value has changed for one user |
|
| 122 | - $users[0]['configvalue'] = 'fi'; |
|
| 123 | - $users[4]['configvalue'] = 'bg'; |
|
| 124 | - $users[6]['configvalue'] = 'th'; |
|
| 125 | - $users[7]['configvalue'] = 'th'; |
|
| 126 | - $this->assertSame($users, $rows, 'Asserts that the entries are updated correctly.'); |
|
| 127 | - |
|
| 128 | - // remove test data |
|
| 129 | - foreach ($users as $user) { |
|
| 130 | - $qb = $this->connection->getQueryBuilder(); |
|
| 131 | - $qb->delete('preferences') |
|
| 132 | - ->where($qb->expr()->eq('userid', $qb->createNamedParameter($user['userid']))) |
|
| 133 | - ->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter('core'))) |
|
| 134 | - ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang'))) |
|
| 135 | - ->andWhere($qb->expr()->eq('configvalue', $qb->createNamedParameter($user['configvalue']), IQueryBuilder::PARAM_STR)) |
|
| 136 | - ->executeStatement(); |
|
| 137 | - } |
|
| 138 | - self::assertEquals($expectedOutput, $outputMessages); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - public function testSecondRun(): void { |
|
| 142 | - /** @var IOutput&MockObject $outputMock */ |
|
| 143 | - $outputMock = $this->createMock(IOutput::class); |
|
| 144 | - $outputMock->expects($this->never()) |
|
| 145 | - ->method('info'); |
|
| 146 | - |
|
| 147 | - $this->config->expects($this->once()) |
|
| 148 | - ->method('getSystemValueString') |
|
| 149 | - ->with('version', '0.0.0') |
|
| 150 | - ->willReturn('12.0.0.14'); |
|
| 151 | - |
|
| 152 | - // run repair step |
|
| 153 | - $repair = new UpdateLanguageCodes($this->connection, $this->config); |
|
| 154 | - $repair->run($outputMock); |
|
| 155 | - } |
|
| 28 | + protected IDBConnection $connection; |
|
| 29 | + private IConfig&MockObject $config; |
|
| 30 | + |
|
| 31 | + protected function setUp(): void { |
|
| 32 | + parent::setUp(); |
|
| 33 | + |
|
| 34 | + $this->connection = Server::get(IDBConnection::class); |
|
| 35 | + $this->config = $this->createMock(IConfig::class); |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + public function testRun(): void { |
|
| 39 | + $users = [ |
|
| 40 | + ['userid' => 'user1', 'configvalue' => 'fi_FI'], |
|
| 41 | + ['userid' => 'user2', 'configvalue' => 'de'], |
|
| 42 | + ['userid' => 'user3', 'configvalue' => 'fi'], |
|
| 43 | + ['userid' => 'user4', 'configvalue' => 'ja'], |
|
| 44 | + ['userid' => 'user5', 'configvalue' => 'bg_BG'], |
|
| 45 | + ['userid' => 'user6', 'configvalue' => 'ja'], |
|
| 46 | + ['userid' => 'user7', 'configvalue' => 'th_TH'], |
|
| 47 | + ['userid' => 'user8', 'configvalue' => 'th_TH'], |
|
| 48 | + ]; |
|
| 49 | + |
|
| 50 | + // insert test data |
|
| 51 | + $qb = $this->connection->getQueryBuilder(); |
|
| 52 | + $qb->insert('preferences') |
|
| 53 | + ->values([ |
|
| 54 | + 'userid' => $qb->createParameter('userid'), |
|
| 55 | + 'appid' => $qb->createParameter('appid'), |
|
| 56 | + 'configkey' => $qb->createParameter('configkey'), |
|
| 57 | + 'configvalue' => $qb->createParameter('configvalue'), |
|
| 58 | + ]); |
|
| 59 | + foreach ($users as $user) { |
|
| 60 | + $qb->setParameters([ |
|
| 61 | + 'userid' => $user['userid'], |
|
| 62 | + 'appid' => 'core', |
|
| 63 | + 'configkey' => 'lang', |
|
| 64 | + 'configvalue' => $user['configvalue'], |
|
| 65 | + ])->executeStatement(); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + // check if test data is written to DB |
|
| 69 | + $qb = $this->connection->getQueryBuilder(); |
|
| 70 | + $result = $qb->select(['userid', 'configvalue']) |
|
| 71 | + ->from('preferences') |
|
| 72 | + ->where($qb->expr()->eq('appid', $qb->createNamedParameter('core'))) |
|
| 73 | + ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang'))) |
|
| 74 | + ->orderBy('userid') |
|
| 75 | + ->executeQuery(); |
|
| 76 | + |
|
| 77 | + $rows = $result->fetchAllAssociative(); |
|
| 78 | + $result->closeCursor(); |
|
| 79 | + |
|
| 80 | + $this->assertSame($users, $rows, 'Asserts that the entries are the ones from the test data set'); |
|
| 81 | + |
|
| 82 | + $expectedOutput = [ |
|
| 83 | + ['Changed 1 setting(s) from "bg_BG" to "bg" in preferences table.'], |
|
| 84 | + ['Changed 0 setting(s) from "cs_CZ" to "cs" in preferences table.'], |
|
| 85 | + ['Changed 1 setting(s) from "fi_FI" to "fi" in preferences table.'], |
|
| 86 | + ['Changed 0 setting(s) from "hu_HU" to "hu" in preferences table.'], |
|
| 87 | + ['Changed 0 setting(s) from "nb_NO" to "nb" in preferences table.'], |
|
| 88 | + ['Changed 0 setting(s) from "sk_SK" to "sk" in preferences table.'], |
|
| 89 | + ['Changed 2 setting(s) from "th_TH" to "th" in preferences table.'], |
|
| 90 | + ]; |
|
| 91 | + $outputMessages = []; |
|
| 92 | + /** @var IOutput&MockObject $outputMock */ |
|
| 93 | + $outputMock = $this->createMock(IOutput::class); |
|
| 94 | + $outputMock->expects($this->exactly(7)) |
|
| 95 | + ->method('info') |
|
| 96 | + ->willReturnCallback(function () use (&$outputMessages): void { |
|
| 97 | + $outputMessages[] = func_get_args(); |
|
| 98 | + }); |
|
| 99 | + |
|
| 100 | + $this->config->expects($this->once()) |
|
| 101 | + ->method('getSystemValueString') |
|
| 102 | + ->with('version', '0.0.0') |
|
| 103 | + ->willReturn('12.0.0.13'); |
|
| 104 | + |
|
| 105 | + // run repair step |
|
| 106 | + $repair = new UpdateLanguageCodes($this->connection, $this->config); |
|
| 107 | + $repair->run($outputMock); |
|
| 108 | + |
|
| 109 | + // check if test data is correctly modified in DB |
|
| 110 | + $qb = $this->connection->getQueryBuilder(); |
|
| 111 | + $result = $qb->select(['userid', 'configvalue']) |
|
| 112 | + ->from('preferences') |
|
| 113 | + ->where($qb->expr()->eq('appid', $qb->createNamedParameter('core'))) |
|
| 114 | + ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang'))) |
|
| 115 | + ->orderBy('userid') |
|
| 116 | + ->executeQuery(); |
|
| 117 | + |
|
| 118 | + $rows = $result->fetchAllAssociative(); |
|
| 119 | + $result->closeCursor(); |
|
| 120 | + |
|
| 121 | + // value has changed for one user |
|
| 122 | + $users[0]['configvalue'] = 'fi'; |
|
| 123 | + $users[4]['configvalue'] = 'bg'; |
|
| 124 | + $users[6]['configvalue'] = 'th'; |
|
| 125 | + $users[7]['configvalue'] = 'th'; |
|
| 126 | + $this->assertSame($users, $rows, 'Asserts that the entries are updated correctly.'); |
|
| 127 | + |
|
| 128 | + // remove test data |
|
| 129 | + foreach ($users as $user) { |
|
| 130 | + $qb = $this->connection->getQueryBuilder(); |
|
| 131 | + $qb->delete('preferences') |
|
| 132 | + ->where($qb->expr()->eq('userid', $qb->createNamedParameter($user['userid']))) |
|
| 133 | + ->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter('core'))) |
|
| 134 | + ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang'))) |
|
| 135 | + ->andWhere($qb->expr()->eq('configvalue', $qb->createNamedParameter($user['configvalue']), IQueryBuilder::PARAM_STR)) |
|
| 136 | + ->executeStatement(); |
|
| 137 | + } |
|
| 138 | + self::assertEquals($expectedOutput, $outputMessages); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + public function testSecondRun(): void { |
|
| 142 | + /** @var IOutput&MockObject $outputMock */ |
|
| 143 | + $outputMock = $this->createMock(IOutput::class); |
|
| 144 | + $outputMock->expects($this->never()) |
|
| 145 | + ->method('info'); |
|
| 146 | + |
|
| 147 | + $this->config->expects($this->once()) |
|
| 148 | + ->method('getSystemValueString') |
|
| 149 | + ->with('version', '0.0.0') |
|
| 150 | + ->willReturn('12.0.0.14'); |
|
| 151 | + |
|
| 152 | + // run repair step |
|
| 153 | + $repair = new UpdateLanguageCodes($this->connection, $this->config); |
|
| 154 | + $repair->run($outputMock); |
|
| 155 | + } |
|
| 156 | 156 | } |
@@ -26,166 +26,166 @@ |
||
| 26 | 26 | #[\PHPUnit\Framework\Attributes\Group('DB')] |
| 27 | 27 | class RepairInvalidSharesTest extends TestCase { |
| 28 | 28 | |
| 29 | - private RepairInvalidShares $repair; |
|
| 30 | - private IDBConnection $connection; |
|
| 31 | - |
|
| 32 | - protected function setUp(): void { |
|
| 33 | - parent::setUp(); |
|
| 34 | - |
|
| 35 | - $config = $this->getMockBuilder(IConfig::class) |
|
| 36 | - ->disableOriginalConstructor() |
|
| 37 | - ->getMock(); |
|
| 38 | - $config->expects($this->any()) |
|
| 39 | - ->method('getSystemValueString') |
|
| 40 | - ->with('version') |
|
| 41 | - ->willReturn('12.0.0.0'); |
|
| 42 | - |
|
| 43 | - $this->connection = Server::get(IDBConnection::class); |
|
| 44 | - $this->deleteAllShares(); |
|
| 45 | - |
|
| 46 | - $this->repair = new RepairInvalidShares($config, $this->connection); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - protected function tearDown(): void { |
|
| 50 | - $this->deleteAllShares(); |
|
| 51 | - |
|
| 52 | - parent::tearDown(); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - protected function deleteAllShares() { |
|
| 56 | - $qb = $this->connection->getQueryBuilder(); |
|
| 57 | - $qb->delete('share')->executeStatement(); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Test remove shares where the parent share does not exist anymore |
|
| 62 | - */ |
|
| 63 | - public function testSharesNonExistingParent(): void { |
|
| 64 | - $qb = $this->connection->getQueryBuilder(); |
|
| 65 | - $shareValues = [ |
|
| 66 | - 'share_type' => $qb->expr()->literal(IShare::TYPE_USER), |
|
| 67 | - 'share_with' => $qb->expr()->literal('recipientuser1'), |
|
| 68 | - 'uid_owner' => $qb->expr()->literal('user1'), |
|
| 69 | - 'item_type' => $qb->expr()->literal('folder'), |
|
| 70 | - 'item_source' => $qb->expr()->literal(123), |
|
| 71 | - 'item_target' => $qb->expr()->literal('/123'), |
|
| 72 | - 'file_source' => $qb->expr()->literal(123), |
|
| 73 | - 'file_target' => $qb->expr()->literal('/test'), |
|
| 74 | - 'permissions' => $qb->expr()->literal(1), |
|
| 75 | - 'stime' => $qb->expr()->literal(time()), |
|
| 76 | - 'expiration' => $qb->expr()->literal('2015-09-25 00:00:00') |
|
| 77 | - ]; |
|
| 78 | - |
|
| 79 | - // valid share |
|
| 80 | - $qb = $this->connection->getQueryBuilder(); |
|
| 81 | - $qb->insert('share') |
|
| 82 | - ->values($shareValues) |
|
| 83 | - ->executeStatement(); |
|
| 84 | - $parent = $qb->getLastInsertId(); |
|
| 85 | - |
|
| 86 | - // share with existing parent |
|
| 87 | - $qb = $this->connection->getQueryBuilder(); |
|
| 88 | - $qb->insert('share') |
|
| 89 | - ->values(array_merge($shareValues, [ |
|
| 90 | - 'parent' => $qb->expr()->literal($parent), |
|
| 91 | - ]))->executeStatement(); |
|
| 92 | - $validChild = $qb->getLastInsertId(); |
|
| 93 | - |
|
| 94 | - // share with non-existing parent |
|
| 95 | - $qb = $this->connection->getQueryBuilder(); |
|
| 96 | - $qb->insert('share') |
|
| 97 | - ->values(array_merge($shareValues, [ |
|
| 98 | - 'parent' => $qb->expr()->literal($parent + 100), |
|
| 99 | - ]))->executeStatement(); |
|
| 100 | - $invalidChild = $qb->getLastInsertId(); |
|
| 101 | - |
|
| 102 | - $query = $this->connection->getQueryBuilder(); |
|
| 103 | - $result = $query->select('id') |
|
| 104 | - ->from('share') |
|
| 105 | - ->orderBy('id', 'ASC') |
|
| 106 | - ->executeQuery(); |
|
| 107 | - $rows = $result->fetchAllAssociative(); |
|
| 108 | - $this->assertEquals([['id' => $parent], ['id' => $validChild], ['id' => $invalidChild]], $rows); |
|
| 109 | - $result->closeCursor(); |
|
| 110 | - |
|
| 111 | - /** @var IOutput | \PHPUnit\Framework\MockObject\MockObject $outputMock */ |
|
| 112 | - $outputMock = $this->getMockBuilder('\OCP\Migration\IOutput') |
|
| 113 | - ->disableOriginalConstructor() |
|
| 114 | - ->getMock(); |
|
| 115 | - |
|
| 116 | - $this->repair->run($outputMock); |
|
| 117 | - |
|
| 118 | - $query = $this->connection->getQueryBuilder(); |
|
| 119 | - $result = $query->select('id') |
|
| 120 | - ->from('share') |
|
| 121 | - ->orderBy('id', 'ASC') |
|
| 122 | - ->executeQuery(); |
|
| 123 | - $rows = $result->fetchAllAssociative(); |
|
| 124 | - $this->assertEquals([['id' => $parent], ['id' => $validChild]], $rows); |
|
| 125 | - $result->closeCursor(); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - public static function fileSharePermissionsProvider(): array { |
|
| 129 | - return [ |
|
| 130 | - // unchanged for folder |
|
| 131 | - [ |
|
| 132 | - 'folder', |
|
| 133 | - 31, |
|
| 134 | - 31, |
|
| 135 | - ], |
|
| 136 | - // unchanged for read-write + share |
|
| 137 | - [ |
|
| 138 | - 'file', |
|
| 139 | - Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE, |
|
| 140 | - Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE, |
|
| 141 | - ], |
|
| 142 | - // fixed for all perms |
|
| 143 | - [ |
|
| 144 | - 'file', |
|
| 145 | - Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE | Constants::PERMISSION_SHARE, |
|
| 146 | - Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE, |
|
| 147 | - ], |
|
| 148 | - ]; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Test adjusting file share permissions |
|
| 153 | - */ |
|
| 154 | - #[\PHPUnit\Framework\Attributes\DataProvider('fileSharePermissionsProvider')] |
|
| 155 | - public function testFileSharePermissions($itemType, $testPerms, $expectedPerms): void { |
|
| 156 | - $qb = $this->connection->getQueryBuilder(); |
|
| 157 | - $qb->insert('share') |
|
| 158 | - ->values([ |
|
| 159 | - 'share_type' => $qb->expr()->literal(IShare::TYPE_LINK), |
|
| 160 | - 'uid_owner' => $qb->expr()->literal('user1'), |
|
| 161 | - 'item_type' => $qb->expr()->literal($itemType), |
|
| 162 | - 'item_source' => $qb->expr()->literal(123), |
|
| 163 | - 'item_target' => $qb->expr()->literal('/123'), |
|
| 164 | - 'file_source' => $qb->expr()->literal(123), |
|
| 165 | - 'file_target' => $qb->expr()->literal('/test'), |
|
| 166 | - 'permissions' => $qb->expr()->literal($testPerms), |
|
| 167 | - 'stime' => $qb->expr()->literal(time()), |
|
| 168 | - ]) |
|
| 169 | - ->executeStatement(); |
|
| 170 | - |
|
| 171 | - /** @var IOutput | \PHPUnit\Framework\MockObject\MockObject $outputMock */ |
|
| 172 | - $outputMock = $this->getMockBuilder('\OCP\Migration\IOutput') |
|
| 173 | - ->disableOriginalConstructor() |
|
| 174 | - ->getMock(); |
|
| 175 | - |
|
| 176 | - $this->repair->run($outputMock); |
|
| 177 | - |
|
| 178 | - $results = $this->connection->getQueryBuilder() |
|
| 179 | - ->select('*') |
|
| 180 | - ->from('share') |
|
| 181 | - ->orderBy('permissions', 'ASC') |
|
| 182 | - ->executeQuery() |
|
| 183 | - ->fetchAllAssociative(); |
|
| 184 | - |
|
| 185 | - $this->assertCount(1, $results); |
|
| 186 | - |
|
| 187 | - $updatedShare = $results[0]; |
|
| 188 | - |
|
| 189 | - $this->assertEquals($expectedPerms, $updatedShare['permissions']); |
|
| 190 | - } |
|
| 29 | + private RepairInvalidShares $repair; |
|
| 30 | + private IDBConnection $connection; |
|
| 31 | + |
|
| 32 | + protected function setUp(): void { |
|
| 33 | + parent::setUp(); |
|
| 34 | + |
|
| 35 | + $config = $this->getMockBuilder(IConfig::class) |
|
| 36 | + ->disableOriginalConstructor() |
|
| 37 | + ->getMock(); |
|
| 38 | + $config->expects($this->any()) |
|
| 39 | + ->method('getSystemValueString') |
|
| 40 | + ->with('version') |
|
| 41 | + ->willReturn('12.0.0.0'); |
|
| 42 | + |
|
| 43 | + $this->connection = Server::get(IDBConnection::class); |
|
| 44 | + $this->deleteAllShares(); |
|
| 45 | + |
|
| 46 | + $this->repair = new RepairInvalidShares($config, $this->connection); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + protected function tearDown(): void { |
|
| 50 | + $this->deleteAllShares(); |
|
| 51 | + |
|
| 52 | + parent::tearDown(); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + protected function deleteAllShares() { |
|
| 56 | + $qb = $this->connection->getQueryBuilder(); |
|
| 57 | + $qb->delete('share')->executeStatement(); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Test remove shares where the parent share does not exist anymore |
|
| 62 | + */ |
|
| 63 | + public function testSharesNonExistingParent(): void { |
|
| 64 | + $qb = $this->connection->getQueryBuilder(); |
|
| 65 | + $shareValues = [ |
|
| 66 | + 'share_type' => $qb->expr()->literal(IShare::TYPE_USER), |
|
| 67 | + 'share_with' => $qb->expr()->literal('recipientuser1'), |
|
| 68 | + 'uid_owner' => $qb->expr()->literal('user1'), |
|
| 69 | + 'item_type' => $qb->expr()->literal('folder'), |
|
| 70 | + 'item_source' => $qb->expr()->literal(123), |
|
| 71 | + 'item_target' => $qb->expr()->literal('/123'), |
|
| 72 | + 'file_source' => $qb->expr()->literal(123), |
|
| 73 | + 'file_target' => $qb->expr()->literal('/test'), |
|
| 74 | + 'permissions' => $qb->expr()->literal(1), |
|
| 75 | + 'stime' => $qb->expr()->literal(time()), |
|
| 76 | + 'expiration' => $qb->expr()->literal('2015-09-25 00:00:00') |
|
| 77 | + ]; |
|
| 78 | + |
|
| 79 | + // valid share |
|
| 80 | + $qb = $this->connection->getQueryBuilder(); |
|
| 81 | + $qb->insert('share') |
|
| 82 | + ->values($shareValues) |
|
| 83 | + ->executeStatement(); |
|
| 84 | + $parent = $qb->getLastInsertId(); |
|
| 85 | + |
|
| 86 | + // share with existing parent |
|
| 87 | + $qb = $this->connection->getQueryBuilder(); |
|
| 88 | + $qb->insert('share') |
|
| 89 | + ->values(array_merge($shareValues, [ |
|
| 90 | + 'parent' => $qb->expr()->literal($parent), |
|
| 91 | + ]))->executeStatement(); |
|
| 92 | + $validChild = $qb->getLastInsertId(); |
|
| 93 | + |
|
| 94 | + // share with non-existing parent |
|
| 95 | + $qb = $this->connection->getQueryBuilder(); |
|
| 96 | + $qb->insert('share') |
|
| 97 | + ->values(array_merge($shareValues, [ |
|
| 98 | + 'parent' => $qb->expr()->literal($parent + 100), |
|
| 99 | + ]))->executeStatement(); |
|
| 100 | + $invalidChild = $qb->getLastInsertId(); |
|
| 101 | + |
|
| 102 | + $query = $this->connection->getQueryBuilder(); |
|
| 103 | + $result = $query->select('id') |
|
| 104 | + ->from('share') |
|
| 105 | + ->orderBy('id', 'ASC') |
|
| 106 | + ->executeQuery(); |
|
| 107 | + $rows = $result->fetchAllAssociative(); |
|
| 108 | + $this->assertEquals([['id' => $parent], ['id' => $validChild], ['id' => $invalidChild]], $rows); |
|
| 109 | + $result->closeCursor(); |
|
| 110 | + |
|
| 111 | + /** @var IOutput | \PHPUnit\Framework\MockObject\MockObject $outputMock */ |
|
| 112 | + $outputMock = $this->getMockBuilder('\OCP\Migration\IOutput') |
|
| 113 | + ->disableOriginalConstructor() |
|
| 114 | + ->getMock(); |
|
| 115 | + |
|
| 116 | + $this->repair->run($outputMock); |
|
| 117 | + |
|
| 118 | + $query = $this->connection->getQueryBuilder(); |
|
| 119 | + $result = $query->select('id') |
|
| 120 | + ->from('share') |
|
| 121 | + ->orderBy('id', 'ASC') |
|
| 122 | + ->executeQuery(); |
|
| 123 | + $rows = $result->fetchAllAssociative(); |
|
| 124 | + $this->assertEquals([['id' => $parent], ['id' => $validChild]], $rows); |
|
| 125 | + $result->closeCursor(); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + public static function fileSharePermissionsProvider(): array { |
|
| 129 | + return [ |
|
| 130 | + // unchanged for folder |
|
| 131 | + [ |
|
| 132 | + 'folder', |
|
| 133 | + 31, |
|
| 134 | + 31, |
|
| 135 | + ], |
|
| 136 | + // unchanged for read-write + share |
|
| 137 | + [ |
|
| 138 | + 'file', |
|
| 139 | + Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE, |
|
| 140 | + Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE, |
|
| 141 | + ], |
|
| 142 | + // fixed for all perms |
|
| 143 | + [ |
|
| 144 | + 'file', |
|
| 145 | + Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE | Constants::PERMISSION_SHARE, |
|
| 146 | + Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE, |
|
| 147 | + ], |
|
| 148 | + ]; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Test adjusting file share permissions |
|
| 153 | + */ |
|
| 154 | + #[\PHPUnit\Framework\Attributes\DataProvider('fileSharePermissionsProvider')] |
|
| 155 | + public function testFileSharePermissions($itemType, $testPerms, $expectedPerms): void { |
|
| 156 | + $qb = $this->connection->getQueryBuilder(); |
|
| 157 | + $qb->insert('share') |
|
| 158 | + ->values([ |
|
| 159 | + 'share_type' => $qb->expr()->literal(IShare::TYPE_LINK), |
|
| 160 | + 'uid_owner' => $qb->expr()->literal('user1'), |
|
| 161 | + 'item_type' => $qb->expr()->literal($itemType), |
|
| 162 | + 'item_source' => $qb->expr()->literal(123), |
|
| 163 | + 'item_target' => $qb->expr()->literal('/123'), |
|
| 164 | + 'file_source' => $qb->expr()->literal(123), |
|
| 165 | + 'file_target' => $qb->expr()->literal('/test'), |
|
| 166 | + 'permissions' => $qb->expr()->literal($testPerms), |
|
| 167 | + 'stime' => $qb->expr()->literal(time()), |
|
| 168 | + ]) |
|
| 169 | + ->executeStatement(); |
|
| 170 | + |
|
| 171 | + /** @var IOutput | \PHPUnit\Framework\MockObject\MockObject $outputMock */ |
|
| 172 | + $outputMock = $this->getMockBuilder('\OCP\Migration\IOutput') |
|
| 173 | + ->disableOriginalConstructor() |
|
| 174 | + ->getMock(); |
|
| 175 | + |
|
| 176 | + $this->repair->run($outputMock); |
|
| 177 | + |
|
| 178 | + $results = $this->connection->getQueryBuilder() |
|
| 179 | + ->select('*') |
|
| 180 | + ->from('share') |
|
| 181 | + ->orderBy('permissions', 'ASC') |
|
| 182 | + ->executeQuery() |
|
| 183 | + ->fetchAllAssociative(); |
|
| 184 | + |
|
| 185 | + $this->assertCount(1, $results); |
|
| 186 | + |
|
| 187 | + $updatedShare = $results[0]; |
|
| 188 | + |
|
| 189 | + $this->assertEquals($expectedPerms, $updatedShare['permissions']); |
|
| 190 | + } |
|
| 191 | 191 | } |
@@ -25,415 +25,415 @@ |
||
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | class ResourcesRoomsUpdaterTest extends TestCase { |
| 28 | - private ResourcesRoomsUpdater $updater; |
|
| 29 | - |
|
| 30 | - /** @var IResourceManager|MockObject */ |
|
| 31 | - private $resourceManager; |
|
| 32 | - |
|
| 33 | - /** @var IRoomManager|MockObject */ |
|
| 34 | - private $roomManager; |
|
| 35 | - |
|
| 36 | - /** @var ContainerInterface|MockObject */ |
|
| 37 | - private $container; |
|
| 38 | - |
|
| 39 | - /** @var CalDavBackend|MockObject */ |
|
| 40 | - private $calDavBackend; |
|
| 41 | - |
|
| 42 | - protected function setUp(): void { |
|
| 43 | - parent::setUp(); |
|
| 44 | - |
|
| 45 | - $this->resourceManager = $this->createMock(IResourceManager::class); |
|
| 46 | - $this->roomManager = $this->createMock(IRoomManager::class); |
|
| 47 | - $this->container = $this->createMock(ContainerInterface::class); |
|
| 48 | - $this->calDavBackend = $this->createMock(CalDavBackend::class); |
|
| 49 | - |
|
| 50 | - $this->container->method('get') |
|
| 51 | - ->willReturnMap([ |
|
| 52 | - [IResourceManager::class, $this->resourceManager], |
|
| 53 | - [IRoomManager::class, $this->roomManager], |
|
| 54 | - ]); |
|
| 55 | - |
|
| 56 | - $this->updater = new ResourcesRoomsUpdater( |
|
| 57 | - $this->container, |
|
| 58 | - self::$realDatabase, |
|
| 59 | - $this->calDavBackend |
|
| 60 | - ); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - protected function tearDown(): void { |
|
| 64 | - $query = self::$realDatabase->getQueryBuilder(); |
|
| 65 | - $query->delete('calendar_resources')->executeStatement(); |
|
| 66 | - $query->delete('calendar_resources_md')->executeStatement(); |
|
| 67 | - $query->delete('calendar_rooms')->executeStatement(); |
|
| 68 | - $query->delete('calendar_rooms_md')->executeStatement(); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Data in Cache: |
|
| 73 | - * resources: |
|
| 74 | - * [backend1, res1, Beamer1, {}] - [] |
|
| 75 | - * [backend1, res2, TV1, {}] - [] |
|
| 76 | - * [backend2, res3, Beamer2, {}] - ['meta1' => 'value1', 'meta2' => 'value2'] |
|
| 77 | - * [backend2, res4, TV2, {}] - ['meta1' => 'value1', 'meta3' => 'value3-old'] |
|
| 78 | - * [backend3, res5, Beamer3, {}] - [] |
|
| 79 | - * [backend3, res6, Pointer, {foo, bar}] - ['meta99' => 'value99'] |
|
| 80 | - * |
|
| 81 | - * Data in Backend: |
|
| 82 | - * backend1 gone |
|
| 83 | - * backend2 throws BackendTemporarilyUnavailableException |
|
| 84 | - * [backend3, res6, Pointer123, {foo, biz}] - ['meta99' => 'value99-new', 'meta123' => 'meta456'] |
|
| 85 | - * [backend3, res7, Resource4, {biz}] - ['meta1' => 'value1'] |
|
| 86 | - * [backend4, res8, Beamer, {}] - ['meta2' => 'value2'] |
|
| 87 | - * [backend4, res9, Beamer2, {}] - [] |
|
| 88 | - * |
|
| 89 | - * Expected after run: |
|
| 90 | - * [backend1, res1, Beamer1, {}] - [] |
|
| 91 | - * [backend1, res2, TV1, {}] - [] |
|
| 92 | - * [backend2, res3, Beamer2, {}] - ['meta1' => 'value1', 'meta2' => 'value2'] |
|
| 93 | - * [backend2, res4, TV2, {}] - ['meta1' => 'value1', 'meta3' => 'value3-old'] |
|
| 94 | - * [backend3, res6, Pointer123, {foo, biz}] - ['meta99' => 'value99-new', 'meta123' => 'meta456'] |
|
| 95 | - * [backend3, res7, Resource4, {biz}] - ['meta1' => 'value1'] |
|
| 96 | - * [backend4, res8, Beamer, {}] - ['meta2' => 'value2'] |
|
| 97 | - * [backend4, res9, Beamer2, {}] - [] |
|
| 98 | - */ |
|
| 99 | - |
|
| 100 | - public function testUpdateBoth(): void { |
|
| 101 | - $this->createTestResourcesInCache(); |
|
| 102 | - |
|
| 103 | - $backend2 = $this->createMock(IBackend::class); |
|
| 104 | - $backend3 = $this->createMock(IBackend::class); |
|
| 105 | - $backend4 = $this->createMock(IBackend::class); |
|
| 106 | - |
|
| 107 | - $res6 = $this->createMock(tmpI::class); |
|
| 108 | - $res7 = $this->createMock(tmpI::class); |
|
| 109 | - $res8 = $this->createMock(tmpI::class); |
|
| 110 | - $res9 = $this->createMock(IResource::class); |
|
| 111 | - |
|
| 112 | - $backend2->method('getBackendIdentifier') |
|
| 113 | - ->willReturn('backend2'); |
|
| 114 | - $backend2->method('listAllResources') |
|
| 115 | - ->willThrowException(new BackendTemporarilyUnavailableException()); |
|
| 116 | - $backend2->method('getResource') |
|
| 117 | - ->willThrowException(new BackendTemporarilyUnavailableException()); |
|
| 118 | - $backend2->method('getAllResources') |
|
| 119 | - ->willThrowException(new BackendTemporarilyUnavailableException()); |
|
| 120 | - $backend3->method('getBackendIdentifier') |
|
| 121 | - ->willReturn('backend3'); |
|
| 122 | - $backend3->method('listAllResources') |
|
| 123 | - ->willReturn(['res6', 'res7']); |
|
| 124 | - $backend3->method('getResource') |
|
| 125 | - ->willReturnMap([ |
|
| 126 | - ['res6', $res6], |
|
| 127 | - ['res7', $res7], |
|
| 128 | - ]); |
|
| 129 | - $backend4->method('getBackendIdentifier') |
|
| 130 | - ->willReturn('backend4'); |
|
| 131 | - $backend4->method('listAllResources') |
|
| 132 | - ->willReturn(['res8', 'res9']); |
|
| 133 | - $backend4->method('getResource') |
|
| 134 | - ->willReturnMap([ |
|
| 135 | - ['res8', $res8], |
|
| 136 | - ['res9', $res9], |
|
| 137 | - ]); |
|
| 138 | - |
|
| 139 | - $res6->method('getId')->willReturn('res6'); |
|
| 140 | - $res6->method('getDisplayName')->willReturn('Pointer123'); |
|
| 141 | - $res6->method('getGroupRestrictions')->willReturn(['foo', 'biz']); |
|
| 142 | - $res6->method('getEMail')->willReturn('[email protected]'); |
|
| 143 | - $res6->method('getBackend')->willReturn($backend3); |
|
| 144 | - |
|
| 145 | - $res6->method('getAllAvailableMetadataKeys')->willReturn(['meta99', 'meta123']); |
|
| 146 | - $res6->method('getMetadataForKey')->willReturnCallback(function ($key) { |
|
| 147 | - switch ($key) { |
|
| 148 | - case 'meta99': |
|
| 149 | - return 'value99-new'; |
|
| 150 | - |
|
| 151 | - case 'meta123': |
|
| 152 | - return 'meta456'; |
|
| 153 | - |
|
| 154 | - default: |
|
| 155 | - return null; |
|
| 156 | - } |
|
| 157 | - }); |
|
| 158 | - |
|
| 159 | - $res7->method('getId')->willReturn('res7'); |
|
| 160 | - $res7->method('getDisplayName')->willReturn('Resource4'); |
|
| 161 | - $res7->method('getGroupRestrictions')->willReturn(['biz']); |
|
| 162 | - $res7->method('getEMail')->willReturn('[email protected]'); |
|
| 163 | - $res7->method('getBackend')->willReturn($backend3); |
|
| 164 | - $res7->method('getAllAvailableMetadataKeys')->willReturn(['meta1']); |
|
| 165 | - $res7->method('getMetadataForKey')->willReturnCallback(function ($key) { |
|
| 166 | - switch ($key) { |
|
| 167 | - case 'meta1': |
|
| 168 | - return 'value1'; |
|
| 169 | - |
|
| 170 | - default: |
|
| 171 | - return null; |
|
| 172 | - } |
|
| 173 | - }); |
|
| 174 | - |
|
| 175 | - $res8->method('getId')->willReturn('res8'); |
|
| 176 | - $res8->method('getDisplayName')->willReturn('Beamer'); |
|
| 177 | - $res8->method('getGroupRestrictions')->willReturn([]); |
|
| 178 | - $res8->method('getEMail')->willReturn('[email protected]'); |
|
| 179 | - $res8->method('getBackend')->willReturn($backend4); |
|
| 180 | - $res8->method('getAllAvailableMetadataKeys')->willReturn(['meta2']); |
|
| 181 | - $res8->method('getMetadataForKey')->willReturnCallback(function ($key) { |
|
| 182 | - switch ($key) { |
|
| 183 | - case 'meta2': |
|
| 184 | - return 'value2'; |
|
| 185 | - |
|
| 186 | - default: |
|
| 187 | - return null; |
|
| 188 | - } |
|
| 189 | - }); |
|
| 190 | - |
|
| 191 | - $res9->method('getId')->willReturn('res9'); |
|
| 192 | - $res9->method('getDisplayName')->willReturn('Beamer2'); |
|
| 193 | - $res9->method('getGroupRestrictions')->willReturn([]); |
|
| 194 | - $res9->method('getEMail')->willReturn('[email protected]'); |
|
| 195 | - $res9->method('getBackend')->willReturn($backend4); |
|
| 196 | - |
|
| 197 | - $this->resourceManager |
|
| 198 | - ->method('getBackends') |
|
| 199 | - ->willReturn([ |
|
| 200 | - $backend2, $backend3, $backend4 |
|
| 201 | - ]); |
|
| 202 | - $this->resourceManager |
|
| 203 | - ->method('getBackend') |
|
| 204 | - ->willReturnMap([ |
|
| 205 | - ['backend2', $backend2], |
|
| 206 | - ['backend3', $backend3], |
|
| 207 | - ['backend4', $backend4], |
|
| 208 | - ]); |
|
| 209 | - |
|
| 210 | - $this->updater->updateResources(); |
|
| 211 | - $this->updater->updateRooms(); |
|
| 212 | - |
|
| 213 | - $query = self::$realDatabase->getQueryBuilder(); |
|
| 214 | - $query->select('*')->from('calendar_resources'); |
|
| 215 | - |
|
| 216 | - $rows = []; |
|
| 217 | - $ids = []; |
|
| 218 | - $stmt = $query->executeQuery(); |
|
| 219 | - while ($row = $stmt->fetchAssociative()) { |
|
| 220 | - $ids[$row['backend_id'] . '::' . $row['resource_id']] = $row['id']; |
|
| 221 | - unset($row['id']); |
|
| 222 | - $rows[] = $row; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - $this->assertEquals([ |
|
| 226 | - [ |
|
| 227 | - 'backend_id' => 'backend1', |
|
| 228 | - 'resource_id' => 'res1', |
|
| 229 | - 'displayname' => 'Beamer1', |
|
| 230 | - 'email' => '[email protected]', |
|
| 231 | - 'group_restrictions' => '[]', |
|
| 232 | - ], |
|
| 233 | - [ |
|
| 234 | - 'backend_id' => 'backend1', |
|
| 235 | - 'resource_id' => 'res2', |
|
| 236 | - 'displayname' => 'TV1', |
|
| 237 | - 'email' => '[email protected]', |
|
| 238 | - 'group_restrictions' => '[]', |
|
| 239 | - ], |
|
| 240 | - [ |
|
| 241 | - 'backend_id' => 'backend2', |
|
| 242 | - 'resource_id' => 'res3', |
|
| 243 | - 'displayname' => 'Beamer2', |
|
| 244 | - 'email' => '[email protected]', |
|
| 245 | - 'group_restrictions' => '[]', |
|
| 246 | - ], |
|
| 247 | - [ |
|
| 248 | - 'backend_id' => 'backend2', |
|
| 249 | - 'resource_id' => 'res4', |
|
| 250 | - 'displayname' => 'TV2', |
|
| 251 | - 'email' => '[email protected]', |
|
| 252 | - 'group_restrictions' => '[]', |
|
| 253 | - ], |
|
| 254 | - [ |
|
| 255 | - 'backend_id' => 'backend3', |
|
| 256 | - 'resource_id' => 'res6', |
|
| 257 | - 'displayname' => 'Pointer123', |
|
| 258 | - 'email' => '[email protected]', |
|
| 259 | - 'group_restrictions' => '["foo","biz"]', |
|
| 260 | - ], |
|
| 261 | - [ |
|
| 262 | - 'backend_id' => 'backend3', |
|
| 263 | - 'resource_id' => 'res7', |
|
| 264 | - 'displayname' => 'Resource4', |
|
| 265 | - 'email' => '[email protected]', |
|
| 266 | - 'group_restrictions' => '["biz"]', |
|
| 267 | - ], |
|
| 268 | - [ |
|
| 269 | - 'backend_id' => 'backend4', |
|
| 270 | - 'resource_id' => 'res8', |
|
| 271 | - 'displayname' => 'Beamer', |
|
| 272 | - 'email' => '[email protected]', |
|
| 273 | - 'group_restrictions' => '[]', |
|
| 274 | - ], |
|
| 275 | - [ |
|
| 276 | - 'backend_id' => 'backend4', |
|
| 277 | - 'resource_id' => 'res9', |
|
| 278 | - 'displayname' => 'Beamer2', |
|
| 279 | - 'email' => '[email protected]', |
|
| 280 | - 'group_restrictions' => '[]', |
|
| 281 | - ], |
|
| 282 | - ], $rows); |
|
| 283 | - |
|
| 284 | - $query2 = self::$realDatabase->getQueryBuilder(); |
|
| 285 | - $query2->select('*')->from('calendar_resources_md'); |
|
| 286 | - |
|
| 287 | - $rows2 = []; |
|
| 288 | - $stmt = $query2->executeQuery(); |
|
| 289 | - while ($row = $stmt->fetchAssociative()) { |
|
| 290 | - unset($row['id']); |
|
| 291 | - $rows2[] = $row; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - $this->assertEquals([ |
|
| 295 | - [ |
|
| 296 | - 'resource_id' => $ids['backend2::res3'], |
|
| 297 | - 'key' => 'meta1', |
|
| 298 | - 'value' => 'value1', |
|
| 299 | - ], |
|
| 300 | - [ |
|
| 301 | - 'resource_id' => $ids['backend2::res3'], |
|
| 302 | - 'key' => 'meta2', |
|
| 303 | - 'value' => 'value2', |
|
| 304 | - ], |
|
| 305 | - [ |
|
| 306 | - 'resource_id' => $ids['backend2::res4'], |
|
| 307 | - 'key' => 'meta1', |
|
| 308 | - 'value' => 'value1', |
|
| 309 | - ], |
|
| 310 | - [ |
|
| 311 | - 'resource_id' => $ids['backend2::res4'], |
|
| 312 | - 'key' => 'meta3', |
|
| 313 | - 'value' => 'value3-old', |
|
| 314 | - ], |
|
| 315 | - [ |
|
| 316 | - 'resource_id' => $ids['backend3::res6'], |
|
| 317 | - 'key' => 'meta99', |
|
| 318 | - 'value' => 'value99-new', |
|
| 319 | - ], |
|
| 320 | - [ |
|
| 321 | - 'resource_id' => $ids['backend3::res7'], |
|
| 322 | - 'key' => 'meta1', |
|
| 323 | - 'value' => 'value1', |
|
| 324 | - ], |
|
| 325 | - [ |
|
| 326 | - 'resource_id' => $ids['backend3::res6'], |
|
| 327 | - 'key' => 'meta123', |
|
| 328 | - 'value' => 'meta456', |
|
| 329 | - ], |
|
| 330 | - [ |
|
| 331 | - 'resource_id' => $ids['backend4::res8'], |
|
| 332 | - 'key' => 'meta2', |
|
| 333 | - 'value' => 'value2', |
|
| 334 | - ] |
|
| 335 | - ], $rows2); |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - protected function createTestResourcesInCache() { |
|
| 339 | - $query = self::$realDatabase->getQueryBuilder(); |
|
| 340 | - $query->insert('calendar_resources') |
|
| 341 | - ->values([ |
|
| 342 | - 'backend_id' => $query->createNamedParameter('backend1'), |
|
| 343 | - 'resource_id' => $query->createNamedParameter('res1'), |
|
| 344 | - 'email' => $query->createNamedParameter('[email protected]'), |
|
| 345 | - 'displayname' => $query->createNamedParameter('Beamer1'), |
|
| 346 | - 'group_restrictions' => $query->createNamedParameter('[]'), |
|
| 347 | - ]) |
|
| 348 | - ->executeStatement(); |
|
| 349 | - |
|
| 350 | - $query->insert('calendar_resources') |
|
| 351 | - ->values([ |
|
| 352 | - 'backend_id' => $query->createNamedParameter('backend1'), |
|
| 353 | - 'resource_id' => $query->createNamedParameter('res2'), |
|
| 354 | - 'email' => $query->createNamedParameter('[email protected]'), |
|
| 355 | - 'displayname' => $query->createNamedParameter('TV1'), |
|
| 356 | - 'group_restrictions' => $query->createNamedParameter('[]'), |
|
| 357 | - ]) |
|
| 358 | - ->executeStatement(); |
|
| 359 | - |
|
| 360 | - $query->insert('calendar_resources') |
|
| 361 | - ->values([ |
|
| 362 | - 'backend_id' => $query->createNamedParameter('backend2'), |
|
| 363 | - 'resource_id' => $query->createNamedParameter('res3'), |
|
| 364 | - 'email' => $query->createNamedParameter('[email protected]'), |
|
| 365 | - 'displayname' => $query->createNamedParameter('Beamer2'), |
|
| 366 | - 'group_restrictions' => $query->createNamedParameter('[]'), |
|
| 367 | - ]) |
|
| 368 | - ->executeStatement(); |
|
| 369 | - $id3 = $query->getLastInsertId(); |
|
| 370 | - |
|
| 371 | - $query->insert('calendar_resources') |
|
| 372 | - ->values([ |
|
| 373 | - 'backend_id' => $query->createNamedParameter('backend2'), |
|
| 374 | - 'resource_id' => $query->createNamedParameter('res4'), |
|
| 375 | - 'email' => $query->createNamedParameter('[email protected]'), |
|
| 376 | - 'displayname' => $query->createNamedParameter('TV2'), |
|
| 377 | - 'group_restrictions' => $query->createNamedParameter('[]'), |
|
| 378 | - ]) |
|
| 379 | - ->executeStatement(); |
|
| 380 | - $id4 = $query->getLastInsertId(); |
|
| 381 | - |
|
| 382 | - $query->insert('calendar_resources') |
|
| 383 | - ->values([ |
|
| 384 | - 'backend_id' => $query->createNamedParameter('backend3'), |
|
| 385 | - 'resource_id' => $query->createNamedParameter('res5'), |
|
| 386 | - 'email' => $query->createNamedParameter('[email protected]'), |
|
| 387 | - 'displayname' => $query->createNamedParameter('Beamer3'), |
|
| 388 | - 'group_restrictions' => $query->createNamedParameter('[]'), |
|
| 389 | - ]) |
|
| 390 | - ->executeStatement(); |
|
| 391 | - |
|
| 392 | - $query->insert('calendar_resources') |
|
| 393 | - ->values([ |
|
| 394 | - 'backend_id' => $query->createNamedParameter('backend3'), |
|
| 395 | - 'resource_id' => $query->createNamedParameter('res6'), |
|
| 396 | - 'email' => $query->createNamedParameter('[email protected]'), |
|
| 397 | - 'displayname' => $query->createNamedParameter('Pointer'), |
|
| 398 | - 'group_restrictions' => $query->createNamedParameter('["foo", "bar"]'), |
|
| 399 | - ]) |
|
| 400 | - ->executeStatement(); |
|
| 401 | - $id6 = $query->getLastInsertId(); |
|
| 402 | - |
|
| 403 | - $query->insert('calendar_resources_md') |
|
| 404 | - ->values([ |
|
| 405 | - 'resource_id' => $query->createNamedParameter($id3), |
|
| 406 | - 'key' => $query->createNamedParameter('meta1'), |
|
| 407 | - 'value' => $query->createNamedParameter('value1') |
|
| 408 | - ]) |
|
| 409 | - ->executeStatement(); |
|
| 410 | - $query->insert('calendar_resources_md') |
|
| 411 | - ->values([ |
|
| 412 | - 'resource_id' => $query->createNamedParameter($id3), |
|
| 413 | - 'key' => $query->createNamedParameter('meta2'), |
|
| 414 | - 'value' => $query->createNamedParameter('value2') |
|
| 415 | - ]) |
|
| 416 | - ->executeStatement(); |
|
| 417 | - $query->insert('calendar_resources_md') |
|
| 418 | - ->values([ |
|
| 419 | - 'resource_id' => $query->createNamedParameter($id4), |
|
| 420 | - 'key' => $query->createNamedParameter('meta1'), |
|
| 421 | - 'value' => $query->createNamedParameter('value1') |
|
| 422 | - ]) |
|
| 423 | - ->executeStatement(); |
|
| 424 | - $query->insert('calendar_resources_md') |
|
| 425 | - ->values([ |
|
| 426 | - 'resource_id' => $query->createNamedParameter($id4), |
|
| 427 | - 'key' => $query->createNamedParameter('meta3'), |
|
| 428 | - 'value' => $query->createNamedParameter('value3-old') |
|
| 429 | - ]) |
|
| 430 | - ->executeStatement(); |
|
| 431 | - $query->insert('calendar_resources_md') |
|
| 432 | - ->values([ |
|
| 433 | - 'resource_id' => $query->createNamedParameter($id6), |
|
| 434 | - 'key' => $query->createNamedParameter('meta99'), |
|
| 435 | - 'value' => $query->createNamedParameter('value99') |
|
| 436 | - ]) |
|
| 437 | - ->executeStatement(); |
|
| 438 | - } |
|
| 28 | + private ResourcesRoomsUpdater $updater; |
|
| 29 | + |
|
| 30 | + /** @var IResourceManager|MockObject */ |
|
| 31 | + private $resourceManager; |
|
| 32 | + |
|
| 33 | + /** @var IRoomManager|MockObject */ |
|
| 34 | + private $roomManager; |
|
| 35 | + |
|
| 36 | + /** @var ContainerInterface|MockObject */ |
|
| 37 | + private $container; |
|
| 38 | + |
|
| 39 | + /** @var CalDavBackend|MockObject */ |
|
| 40 | + private $calDavBackend; |
|
| 41 | + |
|
| 42 | + protected function setUp(): void { |
|
| 43 | + parent::setUp(); |
|
| 44 | + |
|
| 45 | + $this->resourceManager = $this->createMock(IResourceManager::class); |
|
| 46 | + $this->roomManager = $this->createMock(IRoomManager::class); |
|
| 47 | + $this->container = $this->createMock(ContainerInterface::class); |
|
| 48 | + $this->calDavBackend = $this->createMock(CalDavBackend::class); |
|
| 49 | + |
|
| 50 | + $this->container->method('get') |
|
| 51 | + ->willReturnMap([ |
|
| 52 | + [IResourceManager::class, $this->resourceManager], |
|
| 53 | + [IRoomManager::class, $this->roomManager], |
|
| 54 | + ]); |
|
| 55 | + |
|
| 56 | + $this->updater = new ResourcesRoomsUpdater( |
|
| 57 | + $this->container, |
|
| 58 | + self::$realDatabase, |
|
| 59 | + $this->calDavBackend |
|
| 60 | + ); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + protected function tearDown(): void { |
|
| 64 | + $query = self::$realDatabase->getQueryBuilder(); |
|
| 65 | + $query->delete('calendar_resources')->executeStatement(); |
|
| 66 | + $query->delete('calendar_resources_md')->executeStatement(); |
|
| 67 | + $query->delete('calendar_rooms')->executeStatement(); |
|
| 68 | + $query->delete('calendar_rooms_md')->executeStatement(); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Data in Cache: |
|
| 73 | + * resources: |
|
| 74 | + * [backend1, res1, Beamer1, {}] - [] |
|
| 75 | + * [backend1, res2, TV1, {}] - [] |
|
| 76 | + * [backend2, res3, Beamer2, {}] - ['meta1' => 'value1', 'meta2' => 'value2'] |
|
| 77 | + * [backend2, res4, TV2, {}] - ['meta1' => 'value1', 'meta3' => 'value3-old'] |
|
| 78 | + * [backend3, res5, Beamer3, {}] - [] |
|
| 79 | + * [backend3, res6, Pointer, {foo, bar}] - ['meta99' => 'value99'] |
|
| 80 | + * |
|
| 81 | + * Data in Backend: |
|
| 82 | + * backend1 gone |
|
| 83 | + * backend2 throws BackendTemporarilyUnavailableException |
|
| 84 | + * [backend3, res6, Pointer123, {foo, biz}] - ['meta99' => 'value99-new', 'meta123' => 'meta456'] |
|
| 85 | + * [backend3, res7, Resource4, {biz}] - ['meta1' => 'value1'] |
|
| 86 | + * [backend4, res8, Beamer, {}] - ['meta2' => 'value2'] |
|
| 87 | + * [backend4, res9, Beamer2, {}] - [] |
|
| 88 | + * |
|
| 89 | + * Expected after run: |
|
| 90 | + * [backend1, res1, Beamer1, {}] - [] |
|
| 91 | + * [backend1, res2, TV1, {}] - [] |
|
| 92 | + * [backend2, res3, Beamer2, {}] - ['meta1' => 'value1', 'meta2' => 'value2'] |
|
| 93 | + * [backend2, res4, TV2, {}] - ['meta1' => 'value1', 'meta3' => 'value3-old'] |
|
| 94 | + * [backend3, res6, Pointer123, {foo, biz}] - ['meta99' => 'value99-new', 'meta123' => 'meta456'] |
|
| 95 | + * [backend3, res7, Resource4, {biz}] - ['meta1' => 'value1'] |
|
| 96 | + * [backend4, res8, Beamer, {}] - ['meta2' => 'value2'] |
|
| 97 | + * [backend4, res9, Beamer2, {}] - [] |
|
| 98 | + */ |
|
| 99 | + |
|
| 100 | + public function testUpdateBoth(): void { |
|
| 101 | + $this->createTestResourcesInCache(); |
|
| 102 | + |
|
| 103 | + $backend2 = $this->createMock(IBackend::class); |
|
| 104 | + $backend3 = $this->createMock(IBackend::class); |
|
| 105 | + $backend4 = $this->createMock(IBackend::class); |
|
| 106 | + |
|
| 107 | + $res6 = $this->createMock(tmpI::class); |
|
| 108 | + $res7 = $this->createMock(tmpI::class); |
|
| 109 | + $res8 = $this->createMock(tmpI::class); |
|
| 110 | + $res9 = $this->createMock(IResource::class); |
|
| 111 | + |
|
| 112 | + $backend2->method('getBackendIdentifier') |
|
| 113 | + ->willReturn('backend2'); |
|
| 114 | + $backend2->method('listAllResources') |
|
| 115 | + ->willThrowException(new BackendTemporarilyUnavailableException()); |
|
| 116 | + $backend2->method('getResource') |
|
| 117 | + ->willThrowException(new BackendTemporarilyUnavailableException()); |
|
| 118 | + $backend2->method('getAllResources') |
|
| 119 | + ->willThrowException(new BackendTemporarilyUnavailableException()); |
|
| 120 | + $backend3->method('getBackendIdentifier') |
|
| 121 | + ->willReturn('backend3'); |
|
| 122 | + $backend3->method('listAllResources') |
|
| 123 | + ->willReturn(['res6', 'res7']); |
|
| 124 | + $backend3->method('getResource') |
|
| 125 | + ->willReturnMap([ |
|
| 126 | + ['res6', $res6], |
|
| 127 | + ['res7', $res7], |
|
| 128 | + ]); |
|
| 129 | + $backend4->method('getBackendIdentifier') |
|
| 130 | + ->willReturn('backend4'); |
|
| 131 | + $backend4->method('listAllResources') |
|
| 132 | + ->willReturn(['res8', 'res9']); |
|
| 133 | + $backend4->method('getResource') |
|
| 134 | + ->willReturnMap([ |
|
| 135 | + ['res8', $res8], |
|
| 136 | + ['res9', $res9], |
|
| 137 | + ]); |
|
| 138 | + |
|
| 139 | + $res6->method('getId')->willReturn('res6'); |
|
| 140 | + $res6->method('getDisplayName')->willReturn('Pointer123'); |
|
| 141 | + $res6->method('getGroupRestrictions')->willReturn(['foo', 'biz']); |
|
| 142 | + $res6->method('getEMail')->willReturn('[email protected]'); |
|
| 143 | + $res6->method('getBackend')->willReturn($backend3); |
|
| 144 | + |
|
| 145 | + $res6->method('getAllAvailableMetadataKeys')->willReturn(['meta99', 'meta123']); |
|
| 146 | + $res6->method('getMetadataForKey')->willReturnCallback(function ($key) { |
|
| 147 | + switch ($key) { |
|
| 148 | + case 'meta99': |
|
| 149 | + return 'value99-new'; |
|
| 150 | + |
|
| 151 | + case 'meta123': |
|
| 152 | + return 'meta456'; |
|
| 153 | + |
|
| 154 | + default: |
|
| 155 | + return null; |
|
| 156 | + } |
|
| 157 | + }); |
|
| 158 | + |
|
| 159 | + $res7->method('getId')->willReturn('res7'); |
|
| 160 | + $res7->method('getDisplayName')->willReturn('Resource4'); |
|
| 161 | + $res7->method('getGroupRestrictions')->willReturn(['biz']); |
|
| 162 | + $res7->method('getEMail')->willReturn('[email protected]'); |
|
| 163 | + $res7->method('getBackend')->willReturn($backend3); |
|
| 164 | + $res7->method('getAllAvailableMetadataKeys')->willReturn(['meta1']); |
|
| 165 | + $res7->method('getMetadataForKey')->willReturnCallback(function ($key) { |
|
| 166 | + switch ($key) { |
|
| 167 | + case 'meta1': |
|
| 168 | + return 'value1'; |
|
| 169 | + |
|
| 170 | + default: |
|
| 171 | + return null; |
|
| 172 | + } |
|
| 173 | + }); |
|
| 174 | + |
|
| 175 | + $res8->method('getId')->willReturn('res8'); |
|
| 176 | + $res8->method('getDisplayName')->willReturn('Beamer'); |
|
| 177 | + $res8->method('getGroupRestrictions')->willReturn([]); |
|
| 178 | + $res8->method('getEMail')->willReturn('[email protected]'); |
|
| 179 | + $res8->method('getBackend')->willReturn($backend4); |
|
| 180 | + $res8->method('getAllAvailableMetadataKeys')->willReturn(['meta2']); |
|
| 181 | + $res8->method('getMetadataForKey')->willReturnCallback(function ($key) { |
|
| 182 | + switch ($key) { |
|
| 183 | + case 'meta2': |
|
| 184 | + return 'value2'; |
|
| 185 | + |
|
| 186 | + default: |
|
| 187 | + return null; |
|
| 188 | + } |
|
| 189 | + }); |
|
| 190 | + |
|
| 191 | + $res9->method('getId')->willReturn('res9'); |
|
| 192 | + $res9->method('getDisplayName')->willReturn('Beamer2'); |
|
| 193 | + $res9->method('getGroupRestrictions')->willReturn([]); |
|
| 194 | + $res9->method('getEMail')->willReturn('[email protected]'); |
|
| 195 | + $res9->method('getBackend')->willReturn($backend4); |
|
| 196 | + |
|
| 197 | + $this->resourceManager |
|
| 198 | + ->method('getBackends') |
|
| 199 | + ->willReturn([ |
|
| 200 | + $backend2, $backend3, $backend4 |
|
| 201 | + ]); |
|
| 202 | + $this->resourceManager |
|
| 203 | + ->method('getBackend') |
|
| 204 | + ->willReturnMap([ |
|
| 205 | + ['backend2', $backend2], |
|
| 206 | + ['backend3', $backend3], |
|
| 207 | + ['backend4', $backend4], |
|
| 208 | + ]); |
|
| 209 | + |
|
| 210 | + $this->updater->updateResources(); |
|
| 211 | + $this->updater->updateRooms(); |
|
| 212 | + |
|
| 213 | + $query = self::$realDatabase->getQueryBuilder(); |
|
| 214 | + $query->select('*')->from('calendar_resources'); |
|
| 215 | + |
|
| 216 | + $rows = []; |
|
| 217 | + $ids = []; |
|
| 218 | + $stmt = $query->executeQuery(); |
|
| 219 | + while ($row = $stmt->fetchAssociative()) { |
|
| 220 | + $ids[$row['backend_id'] . '::' . $row['resource_id']] = $row['id']; |
|
| 221 | + unset($row['id']); |
|
| 222 | + $rows[] = $row; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + $this->assertEquals([ |
|
| 226 | + [ |
|
| 227 | + 'backend_id' => 'backend1', |
|
| 228 | + 'resource_id' => 'res1', |
|
| 229 | + 'displayname' => 'Beamer1', |
|
| 230 | + 'email' => '[email protected]', |
|
| 231 | + 'group_restrictions' => '[]', |
|
| 232 | + ], |
|
| 233 | + [ |
|
| 234 | + 'backend_id' => 'backend1', |
|
| 235 | + 'resource_id' => 'res2', |
|
| 236 | + 'displayname' => 'TV1', |
|
| 237 | + 'email' => '[email protected]', |
|
| 238 | + 'group_restrictions' => '[]', |
|
| 239 | + ], |
|
| 240 | + [ |
|
| 241 | + 'backend_id' => 'backend2', |
|
| 242 | + 'resource_id' => 'res3', |
|
| 243 | + 'displayname' => 'Beamer2', |
|
| 244 | + 'email' => '[email protected]', |
|
| 245 | + 'group_restrictions' => '[]', |
|
| 246 | + ], |
|
| 247 | + [ |
|
| 248 | + 'backend_id' => 'backend2', |
|
| 249 | + 'resource_id' => 'res4', |
|
| 250 | + 'displayname' => 'TV2', |
|
| 251 | + 'email' => '[email protected]', |
|
| 252 | + 'group_restrictions' => '[]', |
|
| 253 | + ], |
|
| 254 | + [ |
|
| 255 | + 'backend_id' => 'backend3', |
|
| 256 | + 'resource_id' => 'res6', |
|
| 257 | + 'displayname' => 'Pointer123', |
|
| 258 | + 'email' => '[email protected]', |
|
| 259 | + 'group_restrictions' => '["foo","biz"]', |
|
| 260 | + ], |
|
| 261 | + [ |
|
| 262 | + 'backend_id' => 'backend3', |
|
| 263 | + 'resource_id' => 'res7', |
|
| 264 | + 'displayname' => 'Resource4', |
|
| 265 | + 'email' => '[email protected]', |
|
| 266 | + 'group_restrictions' => '["biz"]', |
|
| 267 | + ], |
|
| 268 | + [ |
|
| 269 | + 'backend_id' => 'backend4', |
|
| 270 | + 'resource_id' => 'res8', |
|
| 271 | + 'displayname' => 'Beamer', |
|
| 272 | + 'email' => '[email protected]', |
|
| 273 | + 'group_restrictions' => '[]', |
|
| 274 | + ], |
|
| 275 | + [ |
|
| 276 | + 'backend_id' => 'backend4', |
|
| 277 | + 'resource_id' => 'res9', |
|
| 278 | + 'displayname' => 'Beamer2', |
|
| 279 | + 'email' => '[email protected]', |
|
| 280 | + 'group_restrictions' => '[]', |
|
| 281 | + ], |
|
| 282 | + ], $rows); |
|
| 283 | + |
|
| 284 | + $query2 = self::$realDatabase->getQueryBuilder(); |
|
| 285 | + $query2->select('*')->from('calendar_resources_md'); |
|
| 286 | + |
|
| 287 | + $rows2 = []; |
|
| 288 | + $stmt = $query2->executeQuery(); |
|
| 289 | + while ($row = $stmt->fetchAssociative()) { |
|
| 290 | + unset($row['id']); |
|
| 291 | + $rows2[] = $row; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + $this->assertEquals([ |
|
| 295 | + [ |
|
| 296 | + 'resource_id' => $ids['backend2::res3'], |
|
| 297 | + 'key' => 'meta1', |
|
| 298 | + 'value' => 'value1', |
|
| 299 | + ], |
|
| 300 | + [ |
|
| 301 | + 'resource_id' => $ids['backend2::res3'], |
|
| 302 | + 'key' => 'meta2', |
|
| 303 | + 'value' => 'value2', |
|
| 304 | + ], |
|
| 305 | + [ |
|
| 306 | + 'resource_id' => $ids['backend2::res4'], |
|
| 307 | + 'key' => 'meta1', |
|
| 308 | + 'value' => 'value1', |
|
| 309 | + ], |
|
| 310 | + [ |
|
| 311 | + 'resource_id' => $ids['backend2::res4'], |
|
| 312 | + 'key' => 'meta3', |
|
| 313 | + 'value' => 'value3-old', |
|
| 314 | + ], |
|
| 315 | + [ |
|
| 316 | + 'resource_id' => $ids['backend3::res6'], |
|
| 317 | + 'key' => 'meta99', |
|
| 318 | + 'value' => 'value99-new', |
|
| 319 | + ], |
|
| 320 | + [ |
|
| 321 | + 'resource_id' => $ids['backend3::res7'], |
|
| 322 | + 'key' => 'meta1', |
|
| 323 | + 'value' => 'value1', |
|
| 324 | + ], |
|
| 325 | + [ |
|
| 326 | + 'resource_id' => $ids['backend3::res6'], |
|
| 327 | + 'key' => 'meta123', |
|
| 328 | + 'value' => 'meta456', |
|
| 329 | + ], |
|
| 330 | + [ |
|
| 331 | + 'resource_id' => $ids['backend4::res8'], |
|
| 332 | + 'key' => 'meta2', |
|
| 333 | + 'value' => 'value2', |
|
| 334 | + ] |
|
| 335 | + ], $rows2); |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + protected function createTestResourcesInCache() { |
|
| 339 | + $query = self::$realDatabase->getQueryBuilder(); |
|
| 340 | + $query->insert('calendar_resources') |
|
| 341 | + ->values([ |
|
| 342 | + 'backend_id' => $query->createNamedParameter('backend1'), |
|
| 343 | + 'resource_id' => $query->createNamedParameter('res1'), |
|
| 344 | + 'email' => $query->createNamedParameter('[email protected]'), |
|
| 345 | + 'displayname' => $query->createNamedParameter('Beamer1'), |
|
| 346 | + 'group_restrictions' => $query->createNamedParameter('[]'), |
|
| 347 | + ]) |
|
| 348 | + ->executeStatement(); |
|
| 349 | + |
|
| 350 | + $query->insert('calendar_resources') |
|
| 351 | + ->values([ |
|
| 352 | + 'backend_id' => $query->createNamedParameter('backend1'), |
|
| 353 | + 'resource_id' => $query->createNamedParameter('res2'), |
|
| 354 | + 'email' => $query->createNamedParameter('[email protected]'), |
|
| 355 | + 'displayname' => $query->createNamedParameter('TV1'), |
|
| 356 | + 'group_restrictions' => $query->createNamedParameter('[]'), |
|
| 357 | + ]) |
|
| 358 | + ->executeStatement(); |
|
| 359 | + |
|
| 360 | + $query->insert('calendar_resources') |
|
| 361 | + ->values([ |
|
| 362 | + 'backend_id' => $query->createNamedParameter('backend2'), |
|
| 363 | + 'resource_id' => $query->createNamedParameter('res3'), |
|
| 364 | + 'email' => $query->createNamedParameter('[email protected]'), |
|
| 365 | + 'displayname' => $query->createNamedParameter('Beamer2'), |
|
| 366 | + 'group_restrictions' => $query->createNamedParameter('[]'), |
|
| 367 | + ]) |
|
| 368 | + ->executeStatement(); |
|
| 369 | + $id3 = $query->getLastInsertId(); |
|
| 370 | + |
|
| 371 | + $query->insert('calendar_resources') |
|
| 372 | + ->values([ |
|
| 373 | + 'backend_id' => $query->createNamedParameter('backend2'), |
|
| 374 | + 'resource_id' => $query->createNamedParameter('res4'), |
|
| 375 | + 'email' => $query->createNamedParameter('[email protected]'), |
|
| 376 | + 'displayname' => $query->createNamedParameter('TV2'), |
|
| 377 | + 'group_restrictions' => $query->createNamedParameter('[]'), |
|
| 378 | + ]) |
|
| 379 | + ->executeStatement(); |
|
| 380 | + $id4 = $query->getLastInsertId(); |
|
| 381 | + |
|
| 382 | + $query->insert('calendar_resources') |
|
| 383 | + ->values([ |
|
| 384 | + 'backend_id' => $query->createNamedParameter('backend3'), |
|
| 385 | + 'resource_id' => $query->createNamedParameter('res5'), |
|
| 386 | + 'email' => $query->createNamedParameter('[email protected]'), |
|
| 387 | + 'displayname' => $query->createNamedParameter('Beamer3'), |
|
| 388 | + 'group_restrictions' => $query->createNamedParameter('[]'), |
|
| 389 | + ]) |
|
| 390 | + ->executeStatement(); |
|
| 391 | + |
|
| 392 | + $query->insert('calendar_resources') |
|
| 393 | + ->values([ |
|
| 394 | + 'backend_id' => $query->createNamedParameter('backend3'), |
|
| 395 | + 'resource_id' => $query->createNamedParameter('res6'), |
|
| 396 | + 'email' => $query->createNamedParameter('[email protected]'), |
|
| 397 | + 'displayname' => $query->createNamedParameter('Pointer'), |
|
| 398 | + 'group_restrictions' => $query->createNamedParameter('["foo", "bar"]'), |
|
| 399 | + ]) |
|
| 400 | + ->executeStatement(); |
|
| 401 | + $id6 = $query->getLastInsertId(); |
|
| 402 | + |
|
| 403 | + $query->insert('calendar_resources_md') |
|
| 404 | + ->values([ |
|
| 405 | + 'resource_id' => $query->createNamedParameter($id3), |
|
| 406 | + 'key' => $query->createNamedParameter('meta1'), |
|
| 407 | + 'value' => $query->createNamedParameter('value1') |
|
| 408 | + ]) |
|
| 409 | + ->executeStatement(); |
|
| 410 | + $query->insert('calendar_resources_md') |
|
| 411 | + ->values([ |
|
| 412 | + 'resource_id' => $query->createNamedParameter($id3), |
|
| 413 | + 'key' => $query->createNamedParameter('meta2'), |
|
| 414 | + 'value' => $query->createNamedParameter('value2') |
|
| 415 | + ]) |
|
| 416 | + ->executeStatement(); |
|
| 417 | + $query->insert('calendar_resources_md') |
|
| 418 | + ->values([ |
|
| 419 | + 'resource_id' => $query->createNamedParameter($id4), |
|
| 420 | + 'key' => $query->createNamedParameter('meta1'), |
|
| 421 | + 'value' => $query->createNamedParameter('value1') |
|
| 422 | + ]) |
|
| 423 | + ->executeStatement(); |
|
| 424 | + $query->insert('calendar_resources_md') |
|
| 425 | + ->values([ |
|
| 426 | + 'resource_id' => $query->createNamedParameter($id4), |
|
| 427 | + 'key' => $query->createNamedParameter('meta3'), |
|
| 428 | + 'value' => $query->createNamedParameter('value3-old') |
|
| 429 | + ]) |
|
| 430 | + ->executeStatement(); |
|
| 431 | + $query->insert('calendar_resources_md') |
|
| 432 | + ->values([ |
|
| 433 | + 'resource_id' => $query->createNamedParameter($id6), |
|
| 434 | + 'key' => $query->createNamedParameter('meta99'), |
|
| 435 | + 'value' => $query->createNamedParameter('value99') |
|
| 436 | + ]) |
|
| 437 | + ->executeStatement(); |
|
| 438 | + } |
|
| 439 | 439 | } |
@@ -21,513 +21,513 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | #[\PHPUnit\Framework\Attributes\Group('DB')] |
| 23 | 23 | class AllConfigTest extends \Test\TestCase { |
| 24 | - /** @var IDBConnection */ |
|
| 25 | - protected $connection; |
|
| 26 | - |
|
| 27 | - protected function getConfig($systemConfig = null, $connection = null) { |
|
| 28 | - if ($this->connection === null) { |
|
| 29 | - $this->connection = Server::get(IDBConnection::class); |
|
| 30 | - } |
|
| 31 | - if ($connection === null) { |
|
| 32 | - $connection = $this->connection; |
|
| 33 | - } |
|
| 34 | - if ($systemConfig === null) { |
|
| 35 | - $systemConfig = $this->getMockBuilder('\OC\SystemConfig') |
|
| 36 | - ->disableOriginalConstructor() |
|
| 37 | - ->getMock(); |
|
| 38 | - } |
|
| 39 | - return new AllConfig($systemConfig, $connection); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - public function testDeleteUserValue(): void { |
|
| 43 | - $config = $this->getConfig(); |
|
| 44 | - |
|
| 45 | - // preparation - add something to the database |
|
| 46 | - $this->connection->executeUpdate( |
|
| 47 | - 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' |
|
| 48 | - . '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', |
|
| 49 | - ['userDelete', 'appDelete', 'keyDelete', 'valueDelete'] |
|
| 50 | - ); |
|
| 51 | - |
|
| 52 | - $config->deleteUserValue('userDelete', 'appDelete', 'keyDelete'); |
|
| 53 | - |
|
| 54 | - $result = $this->connection->executeQuery( |
|
| 55 | - 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences` WHERE `userid` = ?', |
|
| 56 | - ['userDelete'] |
|
| 57 | - )->fetchAssociative(); |
|
| 58 | - $actualCount = $result['count']; |
|
| 59 | - |
|
| 60 | - $this->assertEquals(0, $actualCount, 'There was one value in the database and after the tests there should be no entry left.'); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - public function testSetUserValue(): void { |
|
| 64 | - $selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?'; |
|
| 65 | - $config = $this->getConfig(); |
|
| 66 | - |
|
| 67 | - $config->setUserValue('userSet', 'appSet', 'keySet', 'valueSet'); |
|
| 68 | - |
|
| 69 | - $result = $this->connection->executeQuery($selectAllSQL, ['userSet'])->fetchAllAssociative(); |
|
| 70 | - |
|
| 71 | - $this->assertEquals(1, count($result)); |
|
| 72 | - $this->assertEquals([ |
|
| 73 | - 'userid' => 'userSet', |
|
| 74 | - 'appid' => 'appSet', |
|
| 75 | - 'configkey' => 'keySet', |
|
| 76 | - 'configvalue' => 'valueSet' |
|
| 77 | - ], $result[0]); |
|
| 78 | - |
|
| 79 | - // test if the method overwrites existing database entries |
|
| 80 | - $config->setUserValue('userSet', 'appSet', 'keySet', 'valueSet2'); |
|
| 81 | - |
|
| 82 | - $result = $this->connection->executeQuery($selectAllSQL, ['userSet'])->fetchAllAssociative(); |
|
| 83 | - |
|
| 84 | - $this->assertEquals(1, count($result)); |
|
| 85 | - $this->assertEquals([ |
|
| 86 | - 'userid' => 'userSet', |
|
| 87 | - 'appid' => 'appSet', |
|
| 88 | - 'configkey' => 'keySet', |
|
| 89 | - 'configvalue' => 'valueSet2' |
|
| 90 | - ], $result[0]); |
|
| 91 | - |
|
| 92 | - // cleanup - it therefore relies on the successful execution of the previous test |
|
| 93 | - $config->deleteUserValue('userSet', 'appSet', 'keySet'); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * This test needs to stay! Emails are expected to be lowercase due to performance reasons. |
|
| 98 | - * This way we can skip the expensive casing change on the database. |
|
| 99 | - */ |
|
| 100 | - public function testSetUserValueSettingsEmail(): void { |
|
| 101 | - $selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?'; |
|
| 102 | - $config = $this->getConfig(); |
|
| 103 | - |
|
| 104 | - $config->setUserValue('userSet', 'settings', 'email', '[email protected]'); |
|
| 105 | - |
|
| 106 | - $result = $this->connection->executeQuery($selectAllSQL, ['userSet'])->fetchAllAssociative(); |
|
| 107 | - |
|
| 108 | - $this->assertEquals(1, count($result)); |
|
| 109 | - $this->assertEquals([ |
|
| 110 | - 'userid' => 'userSet', |
|
| 111 | - 'appid' => 'settings', |
|
| 112 | - 'configkey' => 'email', |
|
| 113 | - 'configvalue' => '[email protected]' |
|
| 114 | - ], $result[0]); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - public function testSetUserValueWithPreCondition(): void { |
|
| 118 | - $config = $this->getConfig(); |
|
| 119 | - |
|
| 120 | - $selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?'; |
|
| 121 | - |
|
| 122 | - $config->setUserValue('userPreCond', 'appPreCond', 'keyPreCond', 'valuePreCond'); |
|
| 123 | - |
|
| 124 | - $result = $this->connection->executeQuery($selectAllSQL, ['userPreCond'])->fetchAllAssociative(); |
|
| 125 | - |
|
| 126 | - $this->assertEquals(1, count($result)); |
|
| 127 | - $this->assertEquals([ |
|
| 128 | - 'userid' => 'userPreCond', |
|
| 129 | - 'appid' => 'appPreCond', |
|
| 130 | - 'configkey' => 'keyPreCond', |
|
| 131 | - 'configvalue' => 'valuePreCond' |
|
| 132 | - ], $result[0]); |
|
| 133 | - |
|
| 134 | - // test if the method overwrites existing database entries with valid precond |
|
| 135 | - $config->setUserValue('userPreCond', 'appPreCond', 'keyPreCond', 'valuePreCond2', 'valuePreCond'); |
|
| 136 | - |
|
| 137 | - $result = $this->connection->executeQuery($selectAllSQL, ['userPreCond'])->fetchAllAssociative(); |
|
| 138 | - |
|
| 139 | - $this->assertEquals(1, count($result)); |
|
| 140 | - $this->assertEquals([ |
|
| 141 | - 'userid' => 'userPreCond', |
|
| 142 | - 'appid' => 'appPreCond', |
|
| 143 | - 'configkey' => 'keyPreCond', |
|
| 144 | - 'configvalue' => 'valuePreCond2' |
|
| 145 | - ], $result[0]); |
|
| 146 | - |
|
| 147 | - // cleanup |
|
| 148 | - $config->deleteUserValue('userPreCond', 'appPreCond', 'keyPreCond'); |
|
| 149 | - } |
|
| 24 | + /** @var IDBConnection */ |
|
| 25 | + protected $connection; |
|
| 26 | + |
|
| 27 | + protected function getConfig($systemConfig = null, $connection = null) { |
|
| 28 | + if ($this->connection === null) { |
|
| 29 | + $this->connection = Server::get(IDBConnection::class); |
|
| 30 | + } |
|
| 31 | + if ($connection === null) { |
|
| 32 | + $connection = $this->connection; |
|
| 33 | + } |
|
| 34 | + if ($systemConfig === null) { |
|
| 35 | + $systemConfig = $this->getMockBuilder('\OC\SystemConfig') |
|
| 36 | + ->disableOriginalConstructor() |
|
| 37 | + ->getMock(); |
|
| 38 | + } |
|
| 39 | + return new AllConfig($systemConfig, $connection); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + public function testDeleteUserValue(): void { |
|
| 43 | + $config = $this->getConfig(); |
|
| 44 | + |
|
| 45 | + // preparation - add something to the database |
|
| 46 | + $this->connection->executeUpdate( |
|
| 47 | + 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' |
|
| 48 | + . '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', |
|
| 49 | + ['userDelete', 'appDelete', 'keyDelete', 'valueDelete'] |
|
| 50 | + ); |
|
| 51 | + |
|
| 52 | + $config->deleteUserValue('userDelete', 'appDelete', 'keyDelete'); |
|
| 53 | + |
|
| 54 | + $result = $this->connection->executeQuery( |
|
| 55 | + 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences` WHERE `userid` = ?', |
|
| 56 | + ['userDelete'] |
|
| 57 | + )->fetchAssociative(); |
|
| 58 | + $actualCount = $result['count']; |
|
| 59 | + |
|
| 60 | + $this->assertEquals(0, $actualCount, 'There was one value in the database and after the tests there should be no entry left.'); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + public function testSetUserValue(): void { |
|
| 64 | + $selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?'; |
|
| 65 | + $config = $this->getConfig(); |
|
| 66 | + |
|
| 67 | + $config->setUserValue('userSet', 'appSet', 'keySet', 'valueSet'); |
|
| 68 | + |
|
| 69 | + $result = $this->connection->executeQuery($selectAllSQL, ['userSet'])->fetchAllAssociative(); |
|
| 70 | + |
|
| 71 | + $this->assertEquals(1, count($result)); |
|
| 72 | + $this->assertEquals([ |
|
| 73 | + 'userid' => 'userSet', |
|
| 74 | + 'appid' => 'appSet', |
|
| 75 | + 'configkey' => 'keySet', |
|
| 76 | + 'configvalue' => 'valueSet' |
|
| 77 | + ], $result[0]); |
|
| 78 | + |
|
| 79 | + // test if the method overwrites existing database entries |
|
| 80 | + $config->setUserValue('userSet', 'appSet', 'keySet', 'valueSet2'); |
|
| 81 | + |
|
| 82 | + $result = $this->connection->executeQuery($selectAllSQL, ['userSet'])->fetchAllAssociative(); |
|
| 83 | + |
|
| 84 | + $this->assertEquals(1, count($result)); |
|
| 85 | + $this->assertEquals([ |
|
| 86 | + 'userid' => 'userSet', |
|
| 87 | + 'appid' => 'appSet', |
|
| 88 | + 'configkey' => 'keySet', |
|
| 89 | + 'configvalue' => 'valueSet2' |
|
| 90 | + ], $result[0]); |
|
| 91 | + |
|
| 92 | + // cleanup - it therefore relies on the successful execution of the previous test |
|
| 93 | + $config->deleteUserValue('userSet', 'appSet', 'keySet'); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * This test needs to stay! Emails are expected to be lowercase due to performance reasons. |
|
| 98 | + * This way we can skip the expensive casing change on the database. |
|
| 99 | + */ |
|
| 100 | + public function testSetUserValueSettingsEmail(): void { |
|
| 101 | + $selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?'; |
|
| 102 | + $config = $this->getConfig(); |
|
| 103 | + |
|
| 104 | + $config->setUserValue('userSet', 'settings', 'email', '[email protected]'); |
|
| 105 | + |
|
| 106 | + $result = $this->connection->executeQuery($selectAllSQL, ['userSet'])->fetchAllAssociative(); |
|
| 107 | + |
|
| 108 | + $this->assertEquals(1, count($result)); |
|
| 109 | + $this->assertEquals([ |
|
| 110 | + 'userid' => 'userSet', |
|
| 111 | + 'appid' => 'settings', |
|
| 112 | + 'configkey' => 'email', |
|
| 113 | + 'configvalue' => '[email protected]' |
|
| 114 | + ], $result[0]); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + public function testSetUserValueWithPreCondition(): void { |
|
| 118 | + $config = $this->getConfig(); |
|
| 119 | + |
|
| 120 | + $selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?'; |
|
| 121 | + |
|
| 122 | + $config->setUserValue('userPreCond', 'appPreCond', 'keyPreCond', 'valuePreCond'); |
|
| 123 | + |
|
| 124 | + $result = $this->connection->executeQuery($selectAllSQL, ['userPreCond'])->fetchAllAssociative(); |
|
| 125 | + |
|
| 126 | + $this->assertEquals(1, count($result)); |
|
| 127 | + $this->assertEquals([ |
|
| 128 | + 'userid' => 'userPreCond', |
|
| 129 | + 'appid' => 'appPreCond', |
|
| 130 | + 'configkey' => 'keyPreCond', |
|
| 131 | + 'configvalue' => 'valuePreCond' |
|
| 132 | + ], $result[0]); |
|
| 133 | + |
|
| 134 | + // test if the method overwrites existing database entries with valid precond |
|
| 135 | + $config->setUserValue('userPreCond', 'appPreCond', 'keyPreCond', 'valuePreCond2', 'valuePreCond'); |
|
| 136 | + |
|
| 137 | + $result = $this->connection->executeQuery($selectAllSQL, ['userPreCond'])->fetchAllAssociative(); |
|
| 138 | + |
|
| 139 | + $this->assertEquals(1, count($result)); |
|
| 140 | + $this->assertEquals([ |
|
| 141 | + 'userid' => 'userPreCond', |
|
| 142 | + 'appid' => 'appPreCond', |
|
| 143 | + 'configkey' => 'keyPreCond', |
|
| 144 | + 'configvalue' => 'valuePreCond2' |
|
| 145 | + ], $result[0]); |
|
| 146 | + |
|
| 147 | + // cleanup |
|
| 148 | + $config->deleteUserValue('userPreCond', 'appPreCond', 'keyPreCond'); |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - public static function dataSetUserValueUnexpectedValue(): array { |
|
| 152 | - return [ |
|
| 153 | - [true], |
|
| 154 | - [false], |
|
| 155 | - [null], |
|
| 156 | - [new \stdClass()], |
|
| 157 | - ]; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * @param mixed $value |
|
| 162 | - */ |
|
| 163 | - #[\PHPUnit\Framework\Attributes\DataProvider('dataSetUserValueUnexpectedValue')] |
|
| 164 | - public function testSetUserValueUnexpectedValue($value): void { |
|
| 165 | - $this->expectException(\UnexpectedValueException::class); |
|
| 166 | - |
|
| 167 | - $config = $this->getConfig(); |
|
| 168 | - $config->setUserValue('userSetBool', 'appSetBool', 'keySetBool', $value); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - |
|
| 172 | - public function testSetUserValueWithPreConditionFailure(): void { |
|
| 173 | - $this->expectException(PreConditionNotMetException::class); |
|
| 174 | - |
|
| 175 | - $config = $this->getConfig(); |
|
| 176 | - |
|
| 177 | - $selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?'; |
|
| 151 | + public static function dataSetUserValueUnexpectedValue(): array { |
|
| 152 | + return [ |
|
| 153 | + [true], |
|
| 154 | + [false], |
|
| 155 | + [null], |
|
| 156 | + [new \stdClass()], |
|
| 157 | + ]; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * @param mixed $value |
|
| 162 | + */ |
|
| 163 | + #[\PHPUnit\Framework\Attributes\DataProvider('dataSetUserValueUnexpectedValue')] |
|
| 164 | + public function testSetUserValueUnexpectedValue($value): void { |
|
| 165 | + $this->expectException(\UnexpectedValueException::class); |
|
| 166 | + |
|
| 167 | + $config = $this->getConfig(); |
|
| 168 | + $config->setUserValue('userSetBool', 'appSetBool', 'keySetBool', $value); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + |
|
| 172 | + public function testSetUserValueWithPreConditionFailure(): void { |
|
| 173 | + $this->expectException(PreConditionNotMetException::class); |
|
| 174 | + |
|
| 175 | + $config = $this->getConfig(); |
|
| 176 | + |
|
| 177 | + $selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?'; |
|
| 178 | 178 | |
| 179 | - $config->setUserValue('userPreCond1', 'appPreCond', 'keyPreCond', 'valuePreCond'); |
|
| 180 | - |
|
| 181 | - $result = $this->connection->executeQuery($selectAllSQL, ['userPreCond1'])->fetchAllAssociative(); |
|
| 182 | - |
|
| 183 | - $this->assertEquals(1, count($result)); |
|
| 184 | - $this->assertEquals([ |
|
| 185 | - 'userid' => 'userPreCond1', |
|
| 186 | - 'appid' => 'appPreCond', |
|
| 187 | - 'configkey' => 'keyPreCond', |
|
| 188 | - 'configvalue' => 'valuePreCond' |
|
| 189 | - ], $result[0]); |
|
| 190 | - |
|
| 191 | - // test if the method overwrites existing database entries with valid precond |
|
| 192 | - $config->setUserValue('userPreCond1', 'appPreCond', 'keyPreCond', 'valuePreCond2', 'valuePreCond3'); |
|
| 193 | - |
|
| 194 | - $result = $this->connection->executeQuery($selectAllSQL, ['userPreCond1'])->fetchAllAssociative(); |
|
| 195 | - |
|
| 196 | - $this->assertEquals(1, count($result)); |
|
| 197 | - $this->assertEquals([ |
|
| 198 | - 'userid' => 'userPreCond1', |
|
| 199 | - 'appid' => 'appPreCond', |
|
| 200 | - 'configkey' => 'keyPreCond', |
|
| 201 | - 'configvalue' => 'valuePreCond' |
|
| 202 | - ], $result[0]); |
|
| 179 | + $config->setUserValue('userPreCond1', 'appPreCond', 'keyPreCond', 'valuePreCond'); |
|
| 180 | + |
|
| 181 | + $result = $this->connection->executeQuery($selectAllSQL, ['userPreCond1'])->fetchAllAssociative(); |
|
| 182 | + |
|
| 183 | + $this->assertEquals(1, count($result)); |
|
| 184 | + $this->assertEquals([ |
|
| 185 | + 'userid' => 'userPreCond1', |
|
| 186 | + 'appid' => 'appPreCond', |
|
| 187 | + 'configkey' => 'keyPreCond', |
|
| 188 | + 'configvalue' => 'valuePreCond' |
|
| 189 | + ], $result[0]); |
|
| 190 | + |
|
| 191 | + // test if the method overwrites existing database entries with valid precond |
|
| 192 | + $config->setUserValue('userPreCond1', 'appPreCond', 'keyPreCond', 'valuePreCond2', 'valuePreCond3'); |
|
| 193 | + |
|
| 194 | + $result = $this->connection->executeQuery($selectAllSQL, ['userPreCond1'])->fetchAllAssociative(); |
|
| 195 | + |
|
| 196 | + $this->assertEquals(1, count($result)); |
|
| 197 | + $this->assertEquals([ |
|
| 198 | + 'userid' => 'userPreCond1', |
|
| 199 | + 'appid' => 'appPreCond', |
|
| 200 | + 'configkey' => 'keyPreCond', |
|
| 201 | + 'configvalue' => 'valuePreCond' |
|
| 202 | + ], $result[0]); |
|
| 203 | 203 | |
| 204 | - // cleanup |
|
| 205 | - $config->deleteUserValue('userPreCond1', 'appPreCond', 'keyPreCond'); |
|
| 206 | - } |
|
| 204 | + // cleanup |
|
| 205 | + $config->deleteUserValue('userPreCond1', 'appPreCond', 'keyPreCond'); |
|
| 206 | + } |
|
| 207 | 207 | |
| 208 | - public function testSetUserValueWithPreConditionFailureWhenResultStillMatches(): void { |
|
| 209 | - $this->expectException(PreConditionNotMetException::class); |
|
| 208 | + public function testSetUserValueWithPreConditionFailureWhenResultStillMatches(): void { |
|
| 209 | + $this->expectException(PreConditionNotMetException::class); |
|
| 210 | 210 | |
| 211 | - $config = $this->getConfig(); |
|
| 211 | + $config = $this->getConfig(); |
|
| 212 | 212 | |
| 213 | - $selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?'; |
|
| 213 | + $selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?'; |
|
| 214 | 214 | |
| 215 | - $config->setUserValue('userPreCond1', 'appPreCond', 'keyPreCond', 'valuePreCond'); |
|
| 215 | + $config->setUserValue('userPreCond1', 'appPreCond', 'keyPreCond', 'valuePreCond'); |
|
| 216 | 216 | |
| 217 | - $result = $this->connection->executeQuery($selectAllSQL, ['userPreCond1'])->fetchAllAssociative(); |
|
| 218 | - |
|
| 219 | - $this->assertCount(1, $result); |
|
| 220 | - $this->assertEquals([ |
|
| 221 | - 'userid' => 'userPreCond1', |
|
| 222 | - 'appid' => 'appPreCond', |
|
| 223 | - 'configkey' => 'keyPreCond', |
|
| 224 | - 'configvalue' => 'valuePreCond' |
|
| 225 | - ], $result[0]); |
|
| 226 | - |
|
| 227 | - // test if the method throws with invalid precondition when the value is the same |
|
| 228 | - $config->setUserValue('userPreCond1', 'appPreCond', 'keyPreCond', 'valuePreCond', 'valuePreCond3'); |
|
| 229 | - |
|
| 230 | - $result = $this->connection->executeQuery($selectAllSQL, ['userPreCond1'])->fetchAllAssociative(); |
|
| 231 | - |
|
| 232 | - $this->assertCount(1, $result); |
|
| 233 | - $this->assertEquals([ |
|
| 234 | - 'userid' => 'userPreCond1', |
|
| 235 | - 'appid' => 'appPreCond', |
|
| 236 | - 'configkey' => 'keyPreCond', |
|
| 237 | - 'configvalue' => 'valuePreCond' |
|
| 238 | - ], $result[0]); |
|
| 239 | - |
|
| 240 | - // cleanup |
|
| 241 | - $config->deleteUserValue('userPreCond1', 'appPreCond', 'keyPreCond'); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - public function testSetUserValueUnchanged(): void { |
|
| 245 | - // TODO - FIXME until the dependency injection is handled properly (in AllConfig) |
|
| 246 | - $this->markTestSkipped('Skipped because this is just testable if database connection can be injected'); |
|
| 247 | - |
|
| 248 | - $resultMock = $this->getMockBuilder('\Doctrine\DBAL\Driver\Statement') |
|
| 249 | - ->disableOriginalConstructor()->getMock(); |
|
| 250 | - $resultMock->expects($this->once()) |
|
| 251 | - ->method('fetchColumn') |
|
| 252 | - ->willReturn('valueSetUnchanged'); |
|
| 253 | - |
|
| 254 | - $connectionMock = $this->createMock(IDBConnection::class); |
|
| 255 | - $connectionMock->expects($this->once()) |
|
| 256 | - ->method('executeQuery') |
|
| 257 | - ->with($this->equalTo('SELECT `configvalue` FROM `*PREFIX*preferences` ' |
|
| 258 | - . 'WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'), |
|
| 259 | - $this->equalTo(['userSetUnchanged', 'appSetUnchanged', 'keySetUnchanged'])) |
|
| 260 | - ->willReturn($resultMock); |
|
| 261 | - $connectionMock->expects($this->never()) |
|
| 262 | - ->method('executeUpdate'); |
|
| 263 | - |
|
| 264 | - $config = $this->getConfig(null, $connectionMock); |
|
| 265 | - |
|
| 266 | - $config->setUserValue('userSetUnchanged', 'appSetUnchanged', 'keySetUnchanged', 'valueSetUnchanged'); |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - public function testGetUserValue(): void { |
|
| 270 | - $config = $this->getConfig(); |
|
| 271 | - |
|
| 272 | - // setup - it therefore relies on the successful execution of the previous test |
|
| 273 | - $config->setUserValue('userGet', 'appGet', 'keyGet', 'valueGet'); |
|
| 274 | - $value = $config->getUserValue('userGet', 'appGet', 'keyGet'); |
|
| 275 | - |
|
| 276 | - $this->assertEquals('valueGet', $value); |
|
| 277 | - |
|
| 278 | - $result = $this->connection->executeQuery( |
|
| 279 | - 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?', |
|
| 280 | - ['userGet'] |
|
| 281 | - )->fetchAllAssociative(); |
|
| 282 | - |
|
| 283 | - $this->assertEquals(1, count($result)); |
|
| 284 | - $this->assertEquals([ |
|
| 285 | - 'userid' => 'userGet', |
|
| 286 | - 'appid' => 'appGet', |
|
| 287 | - 'configkey' => 'keyGet', |
|
| 288 | - 'configvalue' => 'valueGet' |
|
| 289 | - ], $result[0]); |
|
| 290 | - |
|
| 291 | - // drop data from database - but the config option should be cached in the config object |
|
| 292 | - $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences` WHERE `userid` = ?', ['userGet']); |
|
| 293 | - |
|
| 294 | - // testing the caching mechanism |
|
| 295 | - $value = $config->getUserValue('userGet', 'appGet', 'keyGet'); |
|
| 296 | - |
|
| 297 | - $this->assertEquals('valueGet', $value); |
|
| 298 | - |
|
| 299 | - $result = $this->connection->executeQuery( |
|
| 300 | - 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?', |
|
| 301 | - ['userGet'] |
|
| 302 | - )->fetchAllAssociative(); |
|
| 303 | - |
|
| 304 | - $this->assertEquals(0, count($result)); |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - public function testGetUserKeys(): void { |
|
| 308 | - $config = $this->getConfig(); |
|
| 309 | - |
|
| 310 | - // preparation - add something to the database |
|
| 311 | - $data = [ |
|
| 312 | - ['userFetch', 'appFetch1', 'keyFetch1', 'value1'], |
|
| 313 | - ['userFetch', 'appFetch1', 'keyFetch2', 'value2'], |
|
| 314 | - ['userFetch', 'appFetch2', 'keyFetch3', 'value3'], |
|
| 315 | - ['userFetch', 'appFetch1', 'keyFetch4', 'value4'], |
|
| 316 | - ['userFetch', 'appFetch4', 'keyFetch1', 'value5'], |
|
| 317 | - ['userFetch', 'appFetch5', 'keyFetch1', 'value6'], |
|
| 318 | - ['userFetch2', 'appFetch', 'keyFetch1', 'value7'] |
|
| 319 | - ]; |
|
| 320 | - foreach ($data as $entry) { |
|
| 321 | - $this->connection->executeUpdate( |
|
| 322 | - 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' |
|
| 323 | - . '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', |
|
| 324 | - $entry |
|
| 325 | - ); |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - $value = $config->getUserKeys('userFetch', 'appFetch1'); |
|
| 329 | - $this->assertEquals(['keyFetch1', 'keyFetch2', 'keyFetch4'], $value); |
|
| 330 | - |
|
| 331 | - $value = $config->getUserKeys('userFetch2', 'appFetch'); |
|
| 332 | - $this->assertEquals(['keyFetch1'], $value); |
|
| 333 | - |
|
| 334 | - // cleanup |
|
| 335 | - $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`'); |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - public function testGetUserKeysAllInts(): void { |
|
| 339 | - $config = $this->getConfig(); |
|
| 340 | - |
|
| 341 | - // preparation - add something to the database |
|
| 342 | - $data = [ |
|
| 343 | - ['userFetch8', 'appFetch1', '123', 'value'], |
|
| 344 | - ['userFetch8', 'appFetch1', '456', 'value'], |
|
| 345 | - ]; |
|
| 346 | - foreach ($data as $entry) { |
|
| 347 | - $this->connection->executeUpdate( |
|
| 348 | - 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' |
|
| 349 | - . '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', |
|
| 350 | - $entry |
|
| 351 | - ); |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - $value = $config->getUserKeys('userFetch8', 'appFetch1'); |
|
| 355 | - $this->assertEquals(['123', '456'], $value); |
|
| 356 | - $this->assertIsString($value[0]); |
|
| 357 | - $this->assertIsString($value[1]); |
|
| 358 | - |
|
| 359 | - // cleanup |
|
| 360 | - $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`'); |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - public function testGetUserValueDefault(): void { |
|
| 364 | - $config = $this->getConfig(); |
|
| 365 | - |
|
| 366 | - $this->assertEquals('', $config->getUserValue('userGetUnset', 'appGetUnset', 'keyGetUnset')); |
|
| 367 | - $this->assertEquals(null, $config->getUserValue('userGetUnset', 'appGetUnset', 'keyGetUnset', null)); |
|
| 368 | - $this->assertEquals('foobar', $config->getUserValue('userGetUnset', 'appGetUnset', 'keyGetUnset', 'foobar')); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - public function testGetUserValueForUsers(): void { |
|
| 372 | - $config = $this->getConfig(); |
|
| 373 | - |
|
| 374 | - // preparation - add something to the database |
|
| 375 | - $data = [ |
|
| 376 | - ['userFetch1', 'appFetch2', 'keyFetch1', 'value1'], |
|
| 377 | - ['userFetch2', 'appFetch2', 'keyFetch1', 'value2'], |
|
| 378 | - ['userFetch3', 'appFetch2', 'keyFetch1', 3], |
|
| 379 | - ['userFetch4', 'appFetch2', 'keyFetch1', 'value4'], |
|
| 380 | - ['userFetch5', 'appFetch2', 'keyFetch1', 'value5'], |
|
| 381 | - ['userFetch6', 'appFetch2', 'keyFetch1', 'value6'], |
|
| 382 | - ['userFetch7', 'appFetch2', 'keyFetch1', 'value7'] |
|
| 383 | - ]; |
|
| 384 | - foreach ($data as $entry) { |
|
| 385 | - $this->connection->executeUpdate( |
|
| 386 | - 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' |
|
| 387 | - . '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', |
|
| 388 | - $entry |
|
| 389 | - ); |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - $value = $config->getUserValueForUsers('appFetch2', 'keyFetch1', |
|
| 393 | - ['userFetch1', 'userFetch2', 'userFetch3', 'userFetch5']); |
|
| 394 | - $this->assertEquals([ |
|
| 395 | - 'userFetch1' => 'value1', |
|
| 396 | - 'userFetch2' => 'value2', |
|
| 397 | - 'userFetch3' => 3, |
|
| 398 | - 'userFetch5' => 'value5' |
|
| 399 | - ], $value); |
|
| 400 | - |
|
| 401 | - $value = $config->getUserValueForUsers('appFetch2', 'keyFetch1', |
|
| 402 | - ['userFetch1', 'userFetch4', 'userFetch9']); |
|
| 403 | - $this->assertEquals([ |
|
| 404 | - 'userFetch1' => 'value1', |
|
| 405 | - 'userFetch4' => 'value4' |
|
| 406 | - ], $value, 'userFetch9 is an non-existent user and should not be shown.'); |
|
| 407 | - |
|
| 408 | - // cleanup |
|
| 409 | - $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`'); |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - public function testDeleteAllUserValues(): void { |
|
| 413 | - $config = $this->getConfig(); |
|
| 414 | - |
|
| 415 | - // preparation - add something to the database |
|
| 416 | - $data = [ |
|
| 417 | - ['userFetch3', 'appFetch1', 'keyFetch1', 'value1'], |
|
| 418 | - ['userFetch3', 'appFetch1', 'keyFetch2', 'value2'], |
|
| 419 | - ['userFetch3', 'appFetch2', 'keyFetch3', 'value3'], |
|
| 420 | - ['userFetch3', 'appFetch1', 'keyFetch4', 'value4'], |
|
| 421 | - ['userFetch3', 'appFetch4', 'keyFetch1', 'value5'], |
|
| 422 | - ['userFetch3', 'appFetch5', 'keyFetch1', 'value6'], |
|
| 423 | - ['userFetch4', 'appFetch2', 'keyFetch1', 'value7'] |
|
| 424 | - ]; |
|
| 425 | - foreach ($data as $entry) { |
|
| 426 | - $this->connection->executeUpdate( |
|
| 427 | - 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' |
|
| 428 | - . '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', |
|
| 429 | - $entry |
|
| 430 | - ); |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - $config->deleteAllUserValues('userFetch3'); |
|
| 434 | - |
|
| 435 | - $result = $this->connection->executeQuery( |
|
| 436 | - 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences`' |
|
| 437 | - )->fetchAssociative(); |
|
| 438 | - $actualCount = $result['count']; |
|
| 439 | - |
|
| 440 | - $this->assertEquals(1, $actualCount, 'After removing `userFetch3` there should be exactly 1 entry left.'); |
|
| 441 | - |
|
| 442 | - // cleanup |
|
| 443 | - $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`'); |
|
| 444 | - } |
|
| 445 | - |
|
| 446 | - public function testDeleteAppFromAllUsers(): void { |
|
| 447 | - $config = $this->getConfig(); |
|
| 448 | - |
|
| 449 | - // preparation - add something to the database |
|
| 450 | - $data = [ |
|
| 451 | - ['userFetch5', 'appFetch1', 'keyFetch1', 'value1'], |
|
| 452 | - ['userFetch5', 'appFetch1', 'keyFetch2', 'value2'], |
|
| 453 | - ['userFetch5', 'appFetch2', 'keyFetch3', 'value3'], |
|
| 454 | - ['userFetch5', 'appFetch1', 'keyFetch4', 'value4'], |
|
| 455 | - ['userFetch5', 'appFetch4', 'keyFetch1', 'value5'], |
|
| 456 | - ['userFetch5', 'appFetch5', 'keyFetch1', 'value6'], |
|
| 457 | - ['userFetch6', 'appFetch2', 'keyFetch1', 'value7'] |
|
| 458 | - ]; |
|
| 459 | - foreach ($data as $entry) { |
|
| 460 | - $this->connection->executeUpdate( |
|
| 461 | - 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' |
|
| 462 | - . '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', |
|
| 463 | - $entry |
|
| 464 | - ); |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - $config->deleteAppFromAllUsers('appFetch1'); |
|
| 468 | - |
|
| 469 | - $result = $this->connection->executeQuery( |
|
| 470 | - 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences`' |
|
| 471 | - )->fetchAssociative(); |
|
| 472 | - $actualCount = $result['count']; |
|
| 473 | - |
|
| 474 | - $this->assertEquals(4, $actualCount, 'After removing `appFetch1` there should be exactly 4 entries left.'); |
|
| 475 | - |
|
| 476 | - $config->deleteAppFromAllUsers('appFetch2'); |
|
| 477 | - |
|
| 478 | - $result = $this->connection->executeQuery( |
|
| 479 | - 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences`' |
|
| 480 | - )->fetchAssociative(); |
|
| 481 | - $actualCount = $result['count']; |
|
| 482 | - |
|
| 483 | - $this->assertEquals(2, $actualCount, 'After removing `appFetch2` there should be exactly 2 entries left.'); |
|
| 484 | - |
|
| 485 | - // cleanup |
|
| 486 | - $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`'); |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - public function testGetUsersForUserValue(): void { |
|
| 490 | - // mock the check for the database to run the correct SQL statements for each database type |
|
| 491 | - $systemConfig = $this->getMockBuilder('\OC\SystemConfig') |
|
| 492 | - ->disableOriginalConstructor() |
|
| 493 | - ->getMock(); |
|
| 494 | - $config = $this->getConfig($systemConfig); |
|
| 495 | - |
|
| 496 | - // preparation - add something to the database |
|
| 497 | - $data = [ |
|
| 498 | - ['user1', 'appFetch9', 'keyFetch9', 'value9'], |
|
| 499 | - ['user2', 'appFetch9', 'keyFetch9', 'value9'], |
|
| 500 | - ['user3', 'appFetch9', 'keyFetch9', 'value8'], |
|
| 501 | - ['user4', 'appFetch9', 'keyFetch8', 'value9'], |
|
| 502 | - ['user5', 'appFetch8', 'keyFetch9', 'value9'], |
|
| 503 | - ['user6', 'appFetch9', 'keyFetch9', 'value9'], |
|
| 504 | - ]; |
|
| 505 | - foreach ($data as $entry) { |
|
| 506 | - $this->connection->executeUpdate( |
|
| 507 | - 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' |
|
| 508 | - . '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', |
|
| 509 | - $entry |
|
| 510 | - ); |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - $value = $config->getUsersForUserValue('appFetch9', 'keyFetch9', 'value9'); |
|
| 514 | - $this->assertEquals(['user1', 'user2', 'user6'], $value); |
|
| 515 | - |
|
| 516 | - // cleanup |
|
| 517 | - $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`'); |
|
| 518 | - } |
|
| 519 | - |
|
| 520 | - public function testGetUsersForUserValueCaseInsensitive(): void { |
|
| 521 | - // mock the check for the database to run the correct SQL statements for each database type |
|
| 522 | - $systemConfig = $this->createMock(SystemConfig::class); |
|
| 523 | - $config = $this->getConfig($systemConfig); |
|
| 524 | - |
|
| 525 | - $config->setUserValue('user1', 'myApp', 'myKey', 'test123'); |
|
| 526 | - $config->setUserValue('user2', 'myApp', 'myKey', 'TEST123'); |
|
| 527 | - $config->setUserValue('user3', 'myApp', 'myKey', 'test12345'); |
|
| 528 | - |
|
| 529 | - $users = $config->getUsersForUserValueCaseInsensitive('myApp', 'myKey', 'test123'); |
|
| 530 | - $this->assertSame(2, count($users)); |
|
| 531 | - $this->assertSame(['user1', 'user2'], $users); |
|
| 532 | - } |
|
| 217 | + $result = $this->connection->executeQuery($selectAllSQL, ['userPreCond1'])->fetchAllAssociative(); |
|
| 218 | + |
|
| 219 | + $this->assertCount(1, $result); |
|
| 220 | + $this->assertEquals([ |
|
| 221 | + 'userid' => 'userPreCond1', |
|
| 222 | + 'appid' => 'appPreCond', |
|
| 223 | + 'configkey' => 'keyPreCond', |
|
| 224 | + 'configvalue' => 'valuePreCond' |
|
| 225 | + ], $result[0]); |
|
| 226 | + |
|
| 227 | + // test if the method throws with invalid precondition when the value is the same |
|
| 228 | + $config->setUserValue('userPreCond1', 'appPreCond', 'keyPreCond', 'valuePreCond', 'valuePreCond3'); |
|
| 229 | + |
|
| 230 | + $result = $this->connection->executeQuery($selectAllSQL, ['userPreCond1'])->fetchAllAssociative(); |
|
| 231 | + |
|
| 232 | + $this->assertCount(1, $result); |
|
| 233 | + $this->assertEquals([ |
|
| 234 | + 'userid' => 'userPreCond1', |
|
| 235 | + 'appid' => 'appPreCond', |
|
| 236 | + 'configkey' => 'keyPreCond', |
|
| 237 | + 'configvalue' => 'valuePreCond' |
|
| 238 | + ], $result[0]); |
|
| 239 | + |
|
| 240 | + // cleanup |
|
| 241 | + $config->deleteUserValue('userPreCond1', 'appPreCond', 'keyPreCond'); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + public function testSetUserValueUnchanged(): void { |
|
| 245 | + // TODO - FIXME until the dependency injection is handled properly (in AllConfig) |
|
| 246 | + $this->markTestSkipped('Skipped because this is just testable if database connection can be injected'); |
|
| 247 | + |
|
| 248 | + $resultMock = $this->getMockBuilder('\Doctrine\DBAL\Driver\Statement') |
|
| 249 | + ->disableOriginalConstructor()->getMock(); |
|
| 250 | + $resultMock->expects($this->once()) |
|
| 251 | + ->method('fetchColumn') |
|
| 252 | + ->willReturn('valueSetUnchanged'); |
|
| 253 | + |
|
| 254 | + $connectionMock = $this->createMock(IDBConnection::class); |
|
| 255 | + $connectionMock->expects($this->once()) |
|
| 256 | + ->method('executeQuery') |
|
| 257 | + ->with($this->equalTo('SELECT `configvalue` FROM `*PREFIX*preferences` ' |
|
| 258 | + . 'WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'), |
|
| 259 | + $this->equalTo(['userSetUnchanged', 'appSetUnchanged', 'keySetUnchanged'])) |
|
| 260 | + ->willReturn($resultMock); |
|
| 261 | + $connectionMock->expects($this->never()) |
|
| 262 | + ->method('executeUpdate'); |
|
| 263 | + |
|
| 264 | + $config = $this->getConfig(null, $connectionMock); |
|
| 265 | + |
|
| 266 | + $config->setUserValue('userSetUnchanged', 'appSetUnchanged', 'keySetUnchanged', 'valueSetUnchanged'); |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + public function testGetUserValue(): void { |
|
| 270 | + $config = $this->getConfig(); |
|
| 271 | + |
|
| 272 | + // setup - it therefore relies on the successful execution of the previous test |
|
| 273 | + $config->setUserValue('userGet', 'appGet', 'keyGet', 'valueGet'); |
|
| 274 | + $value = $config->getUserValue('userGet', 'appGet', 'keyGet'); |
|
| 275 | + |
|
| 276 | + $this->assertEquals('valueGet', $value); |
|
| 277 | + |
|
| 278 | + $result = $this->connection->executeQuery( |
|
| 279 | + 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?', |
|
| 280 | + ['userGet'] |
|
| 281 | + )->fetchAllAssociative(); |
|
| 282 | + |
|
| 283 | + $this->assertEquals(1, count($result)); |
|
| 284 | + $this->assertEquals([ |
|
| 285 | + 'userid' => 'userGet', |
|
| 286 | + 'appid' => 'appGet', |
|
| 287 | + 'configkey' => 'keyGet', |
|
| 288 | + 'configvalue' => 'valueGet' |
|
| 289 | + ], $result[0]); |
|
| 290 | + |
|
| 291 | + // drop data from database - but the config option should be cached in the config object |
|
| 292 | + $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences` WHERE `userid` = ?', ['userGet']); |
|
| 293 | + |
|
| 294 | + // testing the caching mechanism |
|
| 295 | + $value = $config->getUserValue('userGet', 'appGet', 'keyGet'); |
|
| 296 | + |
|
| 297 | + $this->assertEquals('valueGet', $value); |
|
| 298 | + |
|
| 299 | + $result = $this->connection->executeQuery( |
|
| 300 | + 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?', |
|
| 301 | + ['userGet'] |
|
| 302 | + )->fetchAllAssociative(); |
|
| 303 | + |
|
| 304 | + $this->assertEquals(0, count($result)); |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + public function testGetUserKeys(): void { |
|
| 308 | + $config = $this->getConfig(); |
|
| 309 | + |
|
| 310 | + // preparation - add something to the database |
|
| 311 | + $data = [ |
|
| 312 | + ['userFetch', 'appFetch1', 'keyFetch1', 'value1'], |
|
| 313 | + ['userFetch', 'appFetch1', 'keyFetch2', 'value2'], |
|
| 314 | + ['userFetch', 'appFetch2', 'keyFetch3', 'value3'], |
|
| 315 | + ['userFetch', 'appFetch1', 'keyFetch4', 'value4'], |
|
| 316 | + ['userFetch', 'appFetch4', 'keyFetch1', 'value5'], |
|
| 317 | + ['userFetch', 'appFetch5', 'keyFetch1', 'value6'], |
|
| 318 | + ['userFetch2', 'appFetch', 'keyFetch1', 'value7'] |
|
| 319 | + ]; |
|
| 320 | + foreach ($data as $entry) { |
|
| 321 | + $this->connection->executeUpdate( |
|
| 322 | + 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' |
|
| 323 | + . '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', |
|
| 324 | + $entry |
|
| 325 | + ); |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + $value = $config->getUserKeys('userFetch', 'appFetch1'); |
|
| 329 | + $this->assertEquals(['keyFetch1', 'keyFetch2', 'keyFetch4'], $value); |
|
| 330 | + |
|
| 331 | + $value = $config->getUserKeys('userFetch2', 'appFetch'); |
|
| 332 | + $this->assertEquals(['keyFetch1'], $value); |
|
| 333 | + |
|
| 334 | + // cleanup |
|
| 335 | + $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`'); |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + public function testGetUserKeysAllInts(): void { |
|
| 339 | + $config = $this->getConfig(); |
|
| 340 | + |
|
| 341 | + // preparation - add something to the database |
|
| 342 | + $data = [ |
|
| 343 | + ['userFetch8', 'appFetch1', '123', 'value'], |
|
| 344 | + ['userFetch8', 'appFetch1', '456', 'value'], |
|
| 345 | + ]; |
|
| 346 | + foreach ($data as $entry) { |
|
| 347 | + $this->connection->executeUpdate( |
|
| 348 | + 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' |
|
| 349 | + . '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', |
|
| 350 | + $entry |
|
| 351 | + ); |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + $value = $config->getUserKeys('userFetch8', 'appFetch1'); |
|
| 355 | + $this->assertEquals(['123', '456'], $value); |
|
| 356 | + $this->assertIsString($value[0]); |
|
| 357 | + $this->assertIsString($value[1]); |
|
| 358 | + |
|
| 359 | + // cleanup |
|
| 360 | + $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`'); |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + public function testGetUserValueDefault(): void { |
|
| 364 | + $config = $this->getConfig(); |
|
| 365 | + |
|
| 366 | + $this->assertEquals('', $config->getUserValue('userGetUnset', 'appGetUnset', 'keyGetUnset')); |
|
| 367 | + $this->assertEquals(null, $config->getUserValue('userGetUnset', 'appGetUnset', 'keyGetUnset', null)); |
|
| 368 | + $this->assertEquals('foobar', $config->getUserValue('userGetUnset', 'appGetUnset', 'keyGetUnset', 'foobar')); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + public function testGetUserValueForUsers(): void { |
|
| 372 | + $config = $this->getConfig(); |
|
| 373 | + |
|
| 374 | + // preparation - add something to the database |
|
| 375 | + $data = [ |
|
| 376 | + ['userFetch1', 'appFetch2', 'keyFetch1', 'value1'], |
|
| 377 | + ['userFetch2', 'appFetch2', 'keyFetch1', 'value2'], |
|
| 378 | + ['userFetch3', 'appFetch2', 'keyFetch1', 3], |
|
| 379 | + ['userFetch4', 'appFetch2', 'keyFetch1', 'value4'], |
|
| 380 | + ['userFetch5', 'appFetch2', 'keyFetch1', 'value5'], |
|
| 381 | + ['userFetch6', 'appFetch2', 'keyFetch1', 'value6'], |
|
| 382 | + ['userFetch7', 'appFetch2', 'keyFetch1', 'value7'] |
|
| 383 | + ]; |
|
| 384 | + foreach ($data as $entry) { |
|
| 385 | + $this->connection->executeUpdate( |
|
| 386 | + 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' |
|
| 387 | + . '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', |
|
| 388 | + $entry |
|
| 389 | + ); |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + $value = $config->getUserValueForUsers('appFetch2', 'keyFetch1', |
|
| 393 | + ['userFetch1', 'userFetch2', 'userFetch3', 'userFetch5']); |
|
| 394 | + $this->assertEquals([ |
|
| 395 | + 'userFetch1' => 'value1', |
|
| 396 | + 'userFetch2' => 'value2', |
|
| 397 | + 'userFetch3' => 3, |
|
| 398 | + 'userFetch5' => 'value5' |
|
| 399 | + ], $value); |
|
| 400 | + |
|
| 401 | + $value = $config->getUserValueForUsers('appFetch2', 'keyFetch1', |
|
| 402 | + ['userFetch1', 'userFetch4', 'userFetch9']); |
|
| 403 | + $this->assertEquals([ |
|
| 404 | + 'userFetch1' => 'value1', |
|
| 405 | + 'userFetch4' => 'value4' |
|
| 406 | + ], $value, 'userFetch9 is an non-existent user and should not be shown.'); |
|
| 407 | + |
|
| 408 | + // cleanup |
|
| 409 | + $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`'); |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + public function testDeleteAllUserValues(): void { |
|
| 413 | + $config = $this->getConfig(); |
|
| 414 | + |
|
| 415 | + // preparation - add something to the database |
|
| 416 | + $data = [ |
|
| 417 | + ['userFetch3', 'appFetch1', 'keyFetch1', 'value1'], |
|
| 418 | + ['userFetch3', 'appFetch1', 'keyFetch2', 'value2'], |
|
| 419 | + ['userFetch3', 'appFetch2', 'keyFetch3', 'value3'], |
|
| 420 | + ['userFetch3', 'appFetch1', 'keyFetch4', 'value4'], |
|
| 421 | + ['userFetch3', 'appFetch4', 'keyFetch1', 'value5'], |
|
| 422 | + ['userFetch3', 'appFetch5', 'keyFetch1', 'value6'], |
|
| 423 | + ['userFetch4', 'appFetch2', 'keyFetch1', 'value7'] |
|
| 424 | + ]; |
|
| 425 | + foreach ($data as $entry) { |
|
| 426 | + $this->connection->executeUpdate( |
|
| 427 | + 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' |
|
| 428 | + . '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', |
|
| 429 | + $entry |
|
| 430 | + ); |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + $config->deleteAllUserValues('userFetch3'); |
|
| 434 | + |
|
| 435 | + $result = $this->connection->executeQuery( |
|
| 436 | + 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences`' |
|
| 437 | + )->fetchAssociative(); |
|
| 438 | + $actualCount = $result['count']; |
|
| 439 | + |
|
| 440 | + $this->assertEquals(1, $actualCount, 'After removing `userFetch3` there should be exactly 1 entry left.'); |
|
| 441 | + |
|
| 442 | + // cleanup |
|
| 443 | + $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`'); |
|
| 444 | + } |
|
| 445 | + |
|
| 446 | + public function testDeleteAppFromAllUsers(): void { |
|
| 447 | + $config = $this->getConfig(); |
|
| 448 | + |
|
| 449 | + // preparation - add something to the database |
|
| 450 | + $data = [ |
|
| 451 | + ['userFetch5', 'appFetch1', 'keyFetch1', 'value1'], |
|
| 452 | + ['userFetch5', 'appFetch1', 'keyFetch2', 'value2'], |
|
| 453 | + ['userFetch5', 'appFetch2', 'keyFetch3', 'value3'], |
|
| 454 | + ['userFetch5', 'appFetch1', 'keyFetch4', 'value4'], |
|
| 455 | + ['userFetch5', 'appFetch4', 'keyFetch1', 'value5'], |
|
| 456 | + ['userFetch5', 'appFetch5', 'keyFetch1', 'value6'], |
|
| 457 | + ['userFetch6', 'appFetch2', 'keyFetch1', 'value7'] |
|
| 458 | + ]; |
|
| 459 | + foreach ($data as $entry) { |
|
| 460 | + $this->connection->executeUpdate( |
|
| 461 | + 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' |
|
| 462 | + . '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', |
|
| 463 | + $entry |
|
| 464 | + ); |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + $config->deleteAppFromAllUsers('appFetch1'); |
|
| 468 | + |
|
| 469 | + $result = $this->connection->executeQuery( |
|
| 470 | + 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences`' |
|
| 471 | + )->fetchAssociative(); |
|
| 472 | + $actualCount = $result['count']; |
|
| 473 | + |
|
| 474 | + $this->assertEquals(4, $actualCount, 'After removing `appFetch1` there should be exactly 4 entries left.'); |
|
| 475 | + |
|
| 476 | + $config->deleteAppFromAllUsers('appFetch2'); |
|
| 477 | + |
|
| 478 | + $result = $this->connection->executeQuery( |
|
| 479 | + 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences`' |
|
| 480 | + )->fetchAssociative(); |
|
| 481 | + $actualCount = $result['count']; |
|
| 482 | + |
|
| 483 | + $this->assertEquals(2, $actualCount, 'After removing `appFetch2` there should be exactly 2 entries left.'); |
|
| 484 | + |
|
| 485 | + // cleanup |
|
| 486 | + $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`'); |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + public function testGetUsersForUserValue(): void { |
|
| 490 | + // mock the check for the database to run the correct SQL statements for each database type |
|
| 491 | + $systemConfig = $this->getMockBuilder('\OC\SystemConfig') |
|
| 492 | + ->disableOriginalConstructor() |
|
| 493 | + ->getMock(); |
|
| 494 | + $config = $this->getConfig($systemConfig); |
|
| 495 | + |
|
| 496 | + // preparation - add something to the database |
|
| 497 | + $data = [ |
|
| 498 | + ['user1', 'appFetch9', 'keyFetch9', 'value9'], |
|
| 499 | + ['user2', 'appFetch9', 'keyFetch9', 'value9'], |
|
| 500 | + ['user3', 'appFetch9', 'keyFetch9', 'value8'], |
|
| 501 | + ['user4', 'appFetch9', 'keyFetch8', 'value9'], |
|
| 502 | + ['user5', 'appFetch8', 'keyFetch9', 'value9'], |
|
| 503 | + ['user6', 'appFetch9', 'keyFetch9', 'value9'], |
|
| 504 | + ]; |
|
| 505 | + foreach ($data as $entry) { |
|
| 506 | + $this->connection->executeUpdate( |
|
| 507 | + 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' |
|
| 508 | + . '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', |
|
| 509 | + $entry |
|
| 510 | + ); |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + $value = $config->getUsersForUserValue('appFetch9', 'keyFetch9', 'value9'); |
|
| 514 | + $this->assertEquals(['user1', 'user2', 'user6'], $value); |
|
| 515 | + |
|
| 516 | + // cleanup |
|
| 517 | + $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`'); |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + public function testGetUsersForUserValueCaseInsensitive(): void { |
|
| 521 | + // mock the check for the database to run the correct SQL statements for each database type |
|
| 522 | + $systemConfig = $this->createMock(SystemConfig::class); |
|
| 523 | + $config = $this->getConfig($systemConfig); |
|
| 524 | + |
|
| 525 | + $config->setUserValue('user1', 'myApp', 'myKey', 'test123'); |
|
| 526 | + $config->setUserValue('user2', 'myApp', 'myKey', 'TEST123'); |
|
| 527 | + $config->setUserValue('user3', 'myApp', 'myKey', 'test12345'); |
|
| 528 | + |
|
| 529 | + $users = $config->getUsersForUserValueCaseInsensitive('myApp', 'myKey', 'test123'); |
|
| 530 | + $this->assertSame(2, count($users)); |
|
| 531 | + $this->assertSame(['user1', 'user2'], $users); |
|
| 532 | + } |
|
| 533 | 533 | } |
@@ -21,278 +21,278 @@ |
||
| 21 | 21 | |
| 22 | 22 | #[\PHPUnit\Framework\Attributes\Group('DB')] |
| 23 | 23 | class SubAdminTest extends \Test\TestCase { |
| 24 | - /** @var IUserManager */ |
|
| 25 | - private $userManager; |
|
| 26 | - |
|
| 27 | - /** @var IGroupManager */ |
|
| 28 | - private $groupManager; |
|
| 29 | - |
|
| 30 | - /** @var IDBConnection */ |
|
| 31 | - private $dbConn; |
|
| 32 | - |
|
| 33 | - /** @var IEventDispatcher */ |
|
| 34 | - private $eventDispatcher; |
|
| 35 | - |
|
| 36 | - /** @var IUser[] */ |
|
| 37 | - private $users; |
|
| 38 | - |
|
| 39 | - /** @var IGroup[] */ |
|
| 40 | - private $groups; |
|
| 41 | - |
|
| 42 | - protected function setUp(): void { |
|
| 43 | - parent::setUp(); |
|
| 44 | - |
|
| 45 | - $this->users = []; |
|
| 46 | - $this->groups = []; |
|
| 47 | - |
|
| 48 | - $this->userManager = Server::get(IUserManager::class); |
|
| 49 | - $this->groupManager = Server::get(IGroupManager::class); |
|
| 50 | - $this->dbConn = Server::get(IDBConnection::class); |
|
| 51 | - $this->eventDispatcher = Server::get(IEventDispatcher::class); |
|
| 52 | - |
|
| 53 | - // Create 3 users and 3 groups |
|
| 54 | - for ($i = 0; $i < 3; $i++) { |
|
| 55 | - $this->users[] = $this->userManager->createUser('user' . $i, 'user'); |
|
| 56 | - $this->groups[] = $this->groupManager->createGroup('group' . $i); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - // Create admin group |
|
| 60 | - if (!$this->groupManager->groupExists('admin')) { |
|
| 61 | - $this->groupManager->createGroup('admin'); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - // Create "orphaned" users and groups (scenario: temporarily disabled |
|
| 65 | - // backend) |
|
| 66 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 67 | - $qb->insert('group_admin') |
|
| 68 | - ->values([ |
|
| 69 | - 'gid' => $qb->createNamedParameter($this->groups[0]->getGID()), |
|
| 70 | - 'uid' => $qb->createNamedParameter('orphanedUser') |
|
| 71 | - ]) |
|
| 72 | - ->executeStatement(); |
|
| 73 | - $qb->insert('group_admin') |
|
| 74 | - ->values([ |
|
| 75 | - 'gid' => $qb->createNamedParameter('orphanedGroup'), |
|
| 76 | - 'uid' => $qb->createNamedParameter('orphanedUser') |
|
| 77 | - ]) |
|
| 78 | - ->executeStatement(); |
|
| 79 | - $qb->insert('group_admin') |
|
| 80 | - ->values([ |
|
| 81 | - 'gid' => $qb->createNamedParameter('orphanedGroup'), |
|
| 82 | - 'uid' => $qb->createNamedParameter($this->users[0]->getUID()) |
|
| 83 | - ]) |
|
| 84 | - ->executeStatement(); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - protected function tearDown(): void { |
|
| 88 | - foreach ($this->users as $user) { |
|
| 89 | - $user->delete(); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - foreach ($this->groups as $group) { |
|
| 93 | - $group->delete(); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 97 | - $qb->delete('group_admin') |
|
| 98 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter('orphanedUser'))) |
|
| 99 | - ->orWhere($qb->expr()->eq('gid', $qb->createNamedParameter('orphanedGroup'))) |
|
| 100 | - ->executeStatement(); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - public function testCreateSubAdmin(): void { |
|
| 104 | - $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 105 | - $subAdmin->createSubAdmin($this->users[0], $this->groups[0]); |
|
| 106 | - |
|
| 107 | - // Look for subadmin in the database |
|
| 108 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 109 | - $result = $qb->select(['gid', 'uid']) |
|
| 110 | - ->from('group_admin') |
|
| 111 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($this->groups[0]->getGID()))) |
|
| 112 | - ->andWHere($qb->expr()->eq('uid', $qb->createNamedParameter($this->users[0]->getUID()))) |
|
| 113 | - ->executeQuery() |
|
| 114 | - ->fetchAssociative(); |
|
| 115 | - $this->assertEquals( |
|
| 116 | - [ |
|
| 117 | - 'gid' => $this->groups[0]->getGID(), |
|
| 118 | - 'uid' => $this->users[0]->getUID() |
|
| 119 | - ], $result); |
|
| 120 | - |
|
| 121 | - // Delete subadmin |
|
| 122 | - $qb->delete('group_admin') |
|
| 123 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($this->groups[0]->getGID()))) |
|
| 124 | - ->andWHere($qb->expr()->eq('uid', $qb->createNamedParameter($this->users[0]->getUID()))) |
|
| 125 | - ->executeStatement(); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - public function testDeleteSubAdmin(): void { |
|
| 129 | - $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 130 | - $subAdmin->createSubAdmin($this->users[0], $this->groups[0]); |
|
| 131 | - $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]); |
|
| 132 | - |
|
| 133 | - // DB query should be empty |
|
| 134 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 135 | - $result = $qb->select(['gid', 'uid']) |
|
| 136 | - ->from('group_admin') |
|
| 137 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($this->groups[0]->getGID()))) |
|
| 138 | - ->andWHere($qb->expr()->eq('uid', $qb->createNamedParameter($this->users[0]->getUID()))) |
|
| 139 | - ->executeQuery() |
|
| 140 | - ->fetchAssociative(); |
|
| 141 | - $this->assertEmpty($result); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - public function testGetSubAdminsGroups(): void { |
|
| 145 | - $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 146 | - $subAdmin->createSubAdmin($this->users[0], $this->groups[0]); |
|
| 147 | - $subAdmin->createSubAdmin($this->users[0], $this->groups[1]); |
|
| 148 | - |
|
| 149 | - $result = $subAdmin->getSubAdminsGroups($this->users[0]); |
|
| 150 | - |
|
| 151 | - $this->assertContains($this->groups[0], $result); |
|
| 152 | - $this->assertContains($this->groups[1], $result); |
|
| 153 | - $this->assertNotContains($this->groups[2], $result); |
|
| 154 | - $this->assertNotContains(null, $result); |
|
| 155 | - |
|
| 156 | - $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]); |
|
| 157 | - $subAdmin->deleteSubAdmin($this->users[0], $this->groups[1]); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - public function testGetGroupsSubAdmins(): void { |
|
| 161 | - $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 162 | - $subAdmin->createSubAdmin($this->users[0], $this->groups[0]); |
|
| 163 | - $subAdmin->createSubAdmin($this->users[1], $this->groups[0]); |
|
| 164 | - |
|
| 165 | - $result = $subAdmin->getGroupsSubAdmins($this->groups[0]); |
|
| 166 | - |
|
| 167 | - $this->assertContains($this->users[0], $result); |
|
| 168 | - $this->assertContains($this->users[1], $result); |
|
| 169 | - $this->assertNotContains($this->users[2], $result); |
|
| 170 | - $this->assertNotContains(null, $result); |
|
| 171 | - |
|
| 172 | - $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]); |
|
| 173 | - $subAdmin->deleteSubAdmin($this->users[1], $this->groups[0]); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - public function testGetAllSubAdmin(): void { |
|
| 177 | - $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 178 | - |
|
| 179 | - $subAdmin->createSubAdmin($this->users[0], $this->groups[0]); |
|
| 180 | - $subAdmin->createSubAdmin($this->users[1], $this->groups[1]); |
|
| 181 | - $subAdmin->createSubAdmin($this->users[2], $this->groups[1]); |
|
| 182 | - |
|
| 183 | - $result = $subAdmin->getAllSubAdmins(); |
|
| 184 | - |
|
| 185 | - $this->assertContains(['user' => $this->users[0], 'group' => $this->groups[0]], $result); |
|
| 186 | - $this->assertContains(['user' => $this->users[1], 'group' => $this->groups[1]], $result); |
|
| 187 | - $this->assertContains(['user' => $this->users[2], 'group' => $this->groups[1]], $result); |
|
| 188 | - $this->assertNotContains(['user' => null, 'group' => null], $result); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - public function testIsSubAdminofGroup(): void { |
|
| 192 | - $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 193 | - $subAdmin->createSubAdmin($this->users[0], $this->groups[0]); |
|
| 194 | - |
|
| 195 | - $this->assertTrue($subAdmin->isSubAdminOfGroup($this->users[0], $this->groups[0])); |
|
| 196 | - $this->assertFalse($subAdmin->isSubAdminOfGroup($this->users[0], $this->groups[1])); |
|
| 197 | - $this->assertFalse($subAdmin->isSubAdminOfGroup($this->users[1], $this->groups[0])); |
|
| 198 | - |
|
| 199 | - $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - public function testIsSubAdmin(): void { |
|
| 203 | - $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 204 | - $subAdmin->createSubAdmin($this->users[0], $this->groups[0]); |
|
| 205 | - |
|
| 206 | - $this->assertTrue($subAdmin->isSubAdmin($this->users[0])); |
|
| 207 | - $this->assertFalse($subAdmin->isSubAdmin($this->users[1])); |
|
| 208 | - |
|
| 209 | - $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - public function testIsSubAdminAsAdmin(): void { |
|
| 213 | - $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 214 | - $this->groupManager->get('admin')->addUser($this->users[0]); |
|
| 215 | - |
|
| 216 | - $this->assertTrue($subAdmin->isSubAdmin($this->users[0])); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - public function testIsUserAccessible(): void { |
|
| 220 | - $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 221 | - $this->groups[0]->addUser($this->users[1]); |
|
| 222 | - $this->groups[1]->addUser($this->users[1]); |
|
| 223 | - $this->groups[1]->addUser($this->users[2]); |
|
| 224 | - $subAdmin->createSubAdmin($this->users[0], $this->groups[0]); |
|
| 225 | - $subAdmin->createSubAdmin($this->users[2], $this->groups[2]); |
|
| 226 | - |
|
| 227 | - $this->assertTrue($subAdmin->isUserAccessible($this->users[0], $this->users[1])); |
|
| 228 | - $this->assertFalse($subAdmin->isUserAccessible($this->users[0], $this->users[2])); |
|
| 229 | - $this->assertFalse($subAdmin->isUserAccessible($this->users[2], $this->users[0])); |
|
| 230 | - |
|
| 231 | - $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]); |
|
| 232 | - $subAdmin->deleteSubAdmin($this->users[2], $this->groups[2]); |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - public function testIsUserAccessibleAsUser(): void { |
|
| 236 | - $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 237 | - $this->assertFalse($subAdmin->isUserAccessible($this->users[0], $this->users[1])); |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - public function testIsUserAccessibleAdmin(): void { |
|
| 241 | - $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 242 | - $subAdmin->createSubAdmin($this->users[0], $this->groups[0]); |
|
| 243 | - $this->groupManager->get('admin')->addUser($this->users[1]); |
|
| 244 | - |
|
| 245 | - $this->assertFalse($subAdmin->isUserAccessible($this->users[0], $this->users[1])); |
|
| 246 | - } |
|
| 24 | + /** @var IUserManager */ |
|
| 25 | + private $userManager; |
|
| 26 | + |
|
| 27 | + /** @var IGroupManager */ |
|
| 28 | + private $groupManager; |
|
| 29 | + |
|
| 30 | + /** @var IDBConnection */ |
|
| 31 | + private $dbConn; |
|
| 32 | + |
|
| 33 | + /** @var IEventDispatcher */ |
|
| 34 | + private $eventDispatcher; |
|
| 35 | + |
|
| 36 | + /** @var IUser[] */ |
|
| 37 | + private $users; |
|
| 38 | + |
|
| 39 | + /** @var IGroup[] */ |
|
| 40 | + private $groups; |
|
| 41 | + |
|
| 42 | + protected function setUp(): void { |
|
| 43 | + parent::setUp(); |
|
| 44 | + |
|
| 45 | + $this->users = []; |
|
| 46 | + $this->groups = []; |
|
| 47 | + |
|
| 48 | + $this->userManager = Server::get(IUserManager::class); |
|
| 49 | + $this->groupManager = Server::get(IGroupManager::class); |
|
| 50 | + $this->dbConn = Server::get(IDBConnection::class); |
|
| 51 | + $this->eventDispatcher = Server::get(IEventDispatcher::class); |
|
| 52 | + |
|
| 53 | + // Create 3 users and 3 groups |
|
| 54 | + for ($i = 0; $i < 3; $i++) { |
|
| 55 | + $this->users[] = $this->userManager->createUser('user' . $i, 'user'); |
|
| 56 | + $this->groups[] = $this->groupManager->createGroup('group' . $i); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + // Create admin group |
|
| 60 | + if (!$this->groupManager->groupExists('admin')) { |
|
| 61 | + $this->groupManager->createGroup('admin'); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + // Create "orphaned" users and groups (scenario: temporarily disabled |
|
| 65 | + // backend) |
|
| 66 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 67 | + $qb->insert('group_admin') |
|
| 68 | + ->values([ |
|
| 69 | + 'gid' => $qb->createNamedParameter($this->groups[0]->getGID()), |
|
| 70 | + 'uid' => $qb->createNamedParameter('orphanedUser') |
|
| 71 | + ]) |
|
| 72 | + ->executeStatement(); |
|
| 73 | + $qb->insert('group_admin') |
|
| 74 | + ->values([ |
|
| 75 | + 'gid' => $qb->createNamedParameter('orphanedGroup'), |
|
| 76 | + 'uid' => $qb->createNamedParameter('orphanedUser') |
|
| 77 | + ]) |
|
| 78 | + ->executeStatement(); |
|
| 79 | + $qb->insert('group_admin') |
|
| 80 | + ->values([ |
|
| 81 | + 'gid' => $qb->createNamedParameter('orphanedGroup'), |
|
| 82 | + 'uid' => $qb->createNamedParameter($this->users[0]->getUID()) |
|
| 83 | + ]) |
|
| 84 | + ->executeStatement(); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + protected function tearDown(): void { |
|
| 88 | + foreach ($this->users as $user) { |
|
| 89 | + $user->delete(); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + foreach ($this->groups as $group) { |
|
| 93 | + $group->delete(); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 97 | + $qb->delete('group_admin') |
|
| 98 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter('orphanedUser'))) |
|
| 99 | + ->orWhere($qb->expr()->eq('gid', $qb->createNamedParameter('orphanedGroup'))) |
|
| 100 | + ->executeStatement(); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + public function testCreateSubAdmin(): void { |
|
| 104 | + $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 105 | + $subAdmin->createSubAdmin($this->users[0], $this->groups[0]); |
|
| 106 | + |
|
| 107 | + // Look for subadmin in the database |
|
| 108 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 109 | + $result = $qb->select(['gid', 'uid']) |
|
| 110 | + ->from('group_admin') |
|
| 111 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($this->groups[0]->getGID()))) |
|
| 112 | + ->andWHere($qb->expr()->eq('uid', $qb->createNamedParameter($this->users[0]->getUID()))) |
|
| 113 | + ->executeQuery() |
|
| 114 | + ->fetchAssociative(); |
|
| 115 | + $this->assertEquals( |
|
| 116 | + [ |
|
| 117 | + 'gid' => $this->groups[0]->getGID(), |
|
| 118 | + 'uid' => $this->users[0]->getUID() |
|
| 119 | + ], $result); |
|
| 120 | + |
|
| 121 | + // Delete subadmin |
|
| 122 | + $qb->delete('group_admin') |
|
| 123 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($this->groups[0]->getGID()))) |
|
| 124 | + ->andWHere($qb->expr()->eq('uid', $qb->createNamedParameter($this->users[0]->getUID()))) |
|
| 125 | + ->executeStatement(); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + public function testDeleteSubAdmin(): void { |
|
| 129 | + $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 130 | + $subAdmin->createSubAdmin($this->users[0], $this->groups[0]); |
|
| 131 | + $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]); |
|
| 132 | + |
|
| 133 | + // DB query should be empty |
|
| 134 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 135 | + $result = $qb->select(['gid', 'uid']) |
|
| 136 | + ->from('group_admin') |
|
| 137 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($this->groups[0]->getGID()))) |
|
| 138 | + ->andWHere($qb->expr()->eq('uid', $qb->createNamedParameter($this->users[0]->getUID()))) |
|
| 139 | + ->executeQuery() |
|
| 140 | + ->fetchAssociative(); |
|
| 141 | + $this->assertEmpty($result); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + public function testGetSubAdminsGroups(): void { |
|
| 145 | + $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 146 | + $subAdmin->createSubAdmin($this->users[0], $this->groups[0]); |
|
| 147 | + $subAdmin->createSubAdmin($this->users[0], $this->groups[1]); |
|
| 148 | + |
|
| 149 | + $result = $subAdmin->getSubAdminsGroups($this->users[0]); |
|
| 150 | + |
|
| 151 | + $this->assertContains($this->groups[0], $result); |
|
| 152 | + $this->assertContains($this->groups[1], $result); |
|
| 153 | + $this->assertNotContains($this->groups[2], $result); |
|
| 154 | + $this->assertNotContains(null, $result); |
|
| 155 | + |
|
| 156 | + $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]); |
|
| 157 | + $subAdmin->deleteSubAdmin($this->users[0], $this->groups[1]); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + public function testGetGroupsSubAdmins(): void { |
|
| 161 | + $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 162 | + $subAdmin->createSubAdmin($this->users[0], $this->groups[0]); |
|
| 163 | + $subAdmin->createSubAdmin($this->users[1], $this->groups[0]); |
|
| 164 | + |
|
| 165 | + $result = $subAdmin->getGroupsSubAdmins($this->groups[0]); |
|
| 166 | + |
|
| 167 | + $this->assertContains($this->users[0], $result); |
|
| 168 | + $this->assertContains($this->users[1], $result); |
|
| 169 | + $this->assertNotContains($this->users[2], $result); |
|
| 170 | + $this->assertNotContains(null, $result); |
|
| 171 | + |
|
| 172 | + $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]); |
|
| 173 | + $subAdmin->deleteSubAdmin($this->users[1], $this->groups[0]); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + public function testGetAllSubAdmin(): void { |
|
| 177 | + $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 178 | + |
|
| 179 | + $subAdmin->createSubAdmin($this->users[0], $this->groups[0]); |
|
| 180 | + $subAdmin->createSubAdmin($this->users[1], $this->groups[1]); |
|
| 181 | + $subAdmin->createSubAdmin($this->users[2], $this->groups[1]); |
|
| 182 | + |
|
| 183 | + $result = $subAdmin->getAllSubAdmins(); |
|
| 184 | + |
|
| 185 | + $this->assertContains(['user' => $this->users[0], 'group' => $this->groups[0]], $result); |
|
| 186 | + $this->assertContains(['user' => $this->users[1], 'group' => $this->groups[1]], $result); |
|
| 187 | + $this->assertContains(['user' => $this->users[2], 'group' => $this->groups[1]], $result); |
|
| 188 | + $this->assertNotContains(['user' => null, 'group' => null], $result); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + public function testIsSubAdminofGroup(): void { |
|
| 192 | + $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 193 | + $subAdmin->createSubAdmin($this->users[0], $this->groups[0]); |
|
| 194 | + |
|
| 195 | + $this->assertTrue($subAdmin->isSubAdminOfGroup($this->users[0], $this->groups[0])); |
|
| 196 | + $this->assertFalse($subAdmin->isSubAdminOfGroup($this->users[0], $this->groups[1])); |
|
| 197 | + $this->assertFalse($subAdmin->isSubAdminOfGroup($this->users[1], $this->groups[0])); |
|
| 198 | + |
|
| 199 | + $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + public function testIsSubAdmin(): void { |
|
| 203 | + $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 204 | + $subAdmin->createSubAdmin($this->users[0], $this->groups[0]); |
|
| 205 | + |
|
| 206 | + $this->assertTrue($subAdmin->isSubAdmin($this->users[0])); |
|
| 207 | + $this->assertFalse($subAdmin->isSubAdmin($this->users[1])); |
|
| 208 | + |
|
| 209 | + $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + public function testIsSubAdminAsAdmin(): void { |
|
| 213 | + $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 214 | + $this->groupManager->get('admin')->addUser($this->users[0]); |
|
| 215 | + |
|
| 216 | + $this->assertTrue($subAdmin->isSubAdmin($this->users[0])); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + public function testIsUserAccessible(): void { |
|
| 220 | + $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 221 | + $this->groups[0]->addUser($this->users[1]); |
|
| 222 | + $this->groups[1]->addUser($this->users[1]); |
|
| 223 | + $this->groups[1]->addUser($this->users[2]); |
|
| 224 | + $subAdmin->createSubAdmin($this->users[0], $this->groups[0]); |
|
| 225 | + $subAdmin->createSubAdmin($this->users[2], $this->groups[2]); |
|
| 226 | + |
|
| 227 | + $this->assertTrue($subAdmin->isUserAccessible($this->users[0], $this->users[1])); |
|
| 228 | + $this->assertFalse($subAdmin->isUserAccessible($this->users[0], $this->users[2])); |
|
| 229 | + $this->assertFalse($subAdmin->isUserAccessible($this->users[2], $this->users[0])); |
|
| 230 | + |
|
| 231 | + $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]); |
|
| 232 | + $subAdmin->deleteSubAdmin($this->users[2], $this->groups[2]); |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + public function testIsUserAccessibleAsUser(): void { |
|
| 236 | + $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 237 | + $this->assertFalse($subAdmin->isUserAccessible($this->users[0], $this->users[1])); |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + public function testIsUserAccessibleAdmin(): void { |
|
| 241 | + $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 242 | + $subAdmin->createSubAdmin($this->users[0], $this->groups[0]); |
|
| 243 | + $this->groupManager->get('admin')->addUser($this->users[1]); |
|
| 244 | + |
|
| 245 | + $this->assertFalse($subAdmin->isUserAccessible($this->users[0], $this->users[1])); |
|
| 246 | + } |
|
| 247 | 247 | |
| 248 | - public function testPostDeleteUser(): void { |
|
| 249 | - $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 250 | - |
|
| 251 | - $user = array_shift($this->users); |
|
| 252 | - foreach ($this->groups as $group) { |
|
| 253 | - $subAdmin->createSubAdmin($user, $group); |
|
| 254 | - } |
|
| 248 | + public function testPostDeleteUser(): void { |
|
| 249 | + $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 250 | + |
|
| 251 | + $user = array_shift($this->users); |
|
| 252 | + foreach ($this->groups as $group) { |
|
| 253 | + $subAdmin->createSubAdmin($user, $group); |
|
| 254 | + } |
|
| 255 | 255 | |
| 256 | - $user->delete(); |
|
| 257 | - $this->assertEmpty($subAdmin->getAllSubAdmins()); |
|
| 258 | - } |
|
| 256 | + $user->delete(); |
|
| 257 | + $this->assertEmpty($subAdmin->getAllSubAdmins()); |
|
| 258 | + } |
|
| 259 | 259 | |
| 260 | - public function testPostDeleteGroup(): void { |
|
| 261 | - $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 262 | - |
|
| 263 | - $group = array_shift($this->groups); |
|
| 264 | - foreach ($this->users as $user) { |
|
| 265 | - $subAdmin->createSubAdmin($user, $group); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - $group->delete(); |
|
| 269 | - $this->assertEmpty($subAdmin->getAllSubAdmins()); |
|
| 270 | - } |
|
| 260 | + public function testPostDeleteGroup(): void { |
|
| 261 | + $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 262 | + |
|
| 263 | + $group = array_shift($this->groups); |
|
| 264 | + foreach ($this->users as $user) { |
|
| 265 | + $subAdmin->createSubAdmin($user, $group); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + $group->delete(); |
|
| 269 | + $this->assertEmpty($subAdmin->getAllSubAdmins()); |
|
| 270 | + } |
|
| 271 | 271 | |
| 272 | - public function testHooks(): void { |
|
| 273 | - $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 274 | - |
|
| 275 | - $test = $this; |
|
| 276 | - $u = $this->users[0]; |
|
| 277 | - $g = $this->groups[0]; |
|
| 278 | - $count = 0; |
|
| 279 | - |
|
| 280 | - $this->eventDispatcher->addListener(SubAdminAddedEvent::class, function (SubAdminAddedEvent $event) use ($test, $u, $g, &$count): void { |
|
| 281 | - $test->assertEquals($u->getUID(), $event->getUser()->getUID()); |
|
| 282 | - $test->assertEquals($g->getGID(), $event->getGroup()->getGID()); |
|
| 283 | - $count++; |
|
| 284 | - }); |
|
| 285 | - |
|
| 286 | - $this->eventDispatcher->addListener(SubAdminRemovedEvent::class, function ($event) use ($test, $u, $g, &$count): void { |
|
| 287 | - $test->assertEquals($u->getUID(), $event->getUser()->getUID()); |
|
| 288 | - $test->assertEquals($g->getGID(), $event->getGroup()->getGID()); |
|
| 289 | - $count++; |
|
| 290 | - }); |
|
| 291 | - |
|
| 292 | - $subAdmin->createSubAdmin($u, $g); |
|
| 293 | - $this->assertEquals(1, $count); |
|
| 294 | - |
|
| 295 | - $subAdmin->deleteSubAdmin($u, $g); |
|
| 296 | - $this->assertEquals(2, $count); |
|
| 297 | - } |
|
| 272 | + public function testHooks(): void { |
|
| 273 | + $subAdmin = new SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher); |
|
| 274 | + |
|
| 275 | + $test = $this; |
|
| 276 | + $u = $this->users[0]; |
|
| 277 | + $g = $this->groups[0]; |
|
| 278 | + $count = 0; |
|
| 279 | + |
|
| 280 | + $this->eventDispatcher->addListener(SubAdminAddedEvent::class, function (SubAdminAddedEvent $event) use ($test, $u, $g, &$count): void { |
|
| 281 | + $test->assertEquals($u->getUID(), $event->getUser()->getUID()); |
|
| 282 | + $test->assertEquals($g->getGID(), $event->getGroup()->getGID()); |
|
| 283 | + $count++; |
|
| 284 | + }); |
|
| 285 | + |
|
| 286 | + $this->eventDispatcher->addListener(SubAdminRemovedEvent::class, function ($event) use ($test, $u, $g, &$count): void { |
|
| 287 | + $test->assertEquals($u->getUID(), $event->getUser()->getUID()); |
|
| 288 | + $test->assertEquals($g->getGID(), $event->getGroup()->getGID()); |
|
| 289 | + $count++; |
|
| 290 | + }); |
|
| 291 | + |
|
| 292 | + $subAdmin->createSubAdmin($u, $g); |
|
| 293 | + $this->assertEquals(1, $count); |
|
| 294 | + |
|
| 295 | + $subAdmin->deleteSubAdmin($u, $g); |
|
| 296 | + $this->assertEquals(2, $count); |
|
| 297 | + } |
|
| 298 | 298 | } |