|
10 | 10 |
|
11 | 11 | use OC\Files\FileInfo; |
12 | 12 | use OC\Files\Filesystem; |
| 13 | +use OC\Files\Node\Folder; |
13 | 14 | use OC\Files\Node\Node; |
14 | 15 | use OC\Files\Storage\Wrapper\Quota; |
15 | 16 | use OC\Files\View; |
|
24 | 25 | use OCP\Files\Storage\IStorage; |
25 | 26 | use OCP\Files\StorageNotAvailableException; |
26 | 27 | use PHPUnit\Framework\MockObject\MockObject; |
| 28 | +use Sabre\DAV\Exception\NotFound; |
27 | 29 | use Test\Traits\UserTrait; |
28 | 30 |
|
29 | 31 | class TestViewDirectory extends View { |
@@ -271,6 +273,146 @@ public function testGetChildThrowInvalidPath(): void { |
271 | 273 | $dir->getChild('.'); |
272 | 274 | } |
273 | 275 |
|
| 276 | + public function testGetNodeForPath(): void { |
| 277 | + $directoryNode = $this->createMock(Folder::class); |
| 278 | + $pathNode = $this->createMock(Folder::class); |
| 279 | + $pathParentNode = $this->createMock(Folder::class); |
| 280 | + $storage = $this->createMock(IStorage::class); |
| 281 | + |
| 282 | + $directoryNode->expects($this->once()) |
| 283 | + ->method('getStorage') |
| 284 | + ->willReturn($storage); |
| 285 | + $storage->expects($this->once()) |
| 286 | + ->method('instanceOfStorage') |
| 287 | + ->willReturn(false); |
| 288 | + |
| 289 | + $directoryNode->expects($this->once()) |
| 290 | + ->method('isReadable') |
| 291 | + ->willReturn(true); |
| 292 | + $directoryNode->expects($this->once()) |
| 293 | + ->method('getPath') |
| 294 | + ->willReturn('/admin/files/'); |
| 295 | + $directoryNode->expects($this->once()) |
| 296 | + ->method('get') |
| 297 | + ->willReturn($pathNode); |
| 298 | + |
| 299 | + $pathNode->expects($this->once()) |
| 300 | + ->method('getPath') |
| 301 | + ->willReturn('/admin/files/my/deep/folder/'); |
| 302 | + $pathNode->expects($this->once()) |
| 303 | + ->method('isReadable') |
| 304 | + ->willReturn(true); |
| 305 | + $pathNode->expects($this->once()) |
| 306 | + ->method('getMimetype') |
| 307 | + ->willReturn(FileInfo::MIMETYPE_FOLDER); |
| 308 | + |
| 309 | + $this->view->method('getRelativePath') |
| 310 | + ->willReturnCallback(function ($path) { |
| 311 | + return str_replace('/admin/files/', '', $path); |
| 312 | + }); |
| 313 | + |
| 314 | + $this->view->expects($this->exactly(2)) |
| 315 | + ->method('getFileInfo') |
| 316 | + ->willReturn($pathParentNode); |
| 317 | + |
| 318 | + $pathParentNode->expects($this->exactly(2)) |
| 319 | + ->method('getPath') |
| 320 | + ->willReturnOnConsecutiveCalls('/my/deep', '/my'); |
| 321 | + $pathParentNode->expects($this->exactly(2)) |
| 322 | + ->method('isReadable') |
| 323 | + ->willReturn(true); |
| 324 | + |
| 325 | + $dir = new Directory($this->view, $directoryNode); |
| 326 | + $dir->getNodeForPath('/my/deep/folder/'); |
| 327 | + } |
| 328 | + |
| 329 | + public function testGetNodeForPathFailsWithNoReadPermissionsForParent(): void { |
| 330 | + $directoryNode = $this->createMock(Folder::class); |
| 331 | + $pathNode = $this->createMock(Folder::class); |
| 332 | + $pathParentNode = $this->createMock(Folder::class); |
| 333 | + $storage = $this->createMock(IStorage::class); |
| 334 | + |
| 335 | + $directoryNode->expects($this->once()) |
| 336 | + ->method('getStorage') |
| 337 | + ->willReturn($storage); |
| 338 | + $storage->expects($this->once()) |
| 339 | + ->method('instanceOfStorage') |
| 340 | + ->willReturn(false); |
| 341 | + |
| 342 | + $directoryNode->expects($this->once()) |
| 343 | + ->method('isReadable') |
| 344 | + ->willReturn(true); |
| 345 | + $directoryNode->expects($this->once()) |
| 346 | + ->method('getPath') |
| 347 | + ->willReturn('/admin/files/'); |
| 348 | + $directoryNode->expects($this->once()) |
| 349 | + ->method('get') |
| 350 | + ->willReturn($pathNode); |
| 351 | + |
| 352 | + $pathNode->expects($this->once()) |
| 353 | + ->method('getPath') |
| 354 | + ->willReturn('/admin/files/my/deep/folder/'); |
| 355 | + $pathNode->expects($this->once()) |
| 356 | + ->method('isReadable') |
| 357 | + ->willReturn(true); |
| 358 | + $pathNode->expects($this->once()) |
| 359 | + ->method('getMimetype') |
| 360 | + ->willReturn(FileInfo::MIMETYPE_FOLDER); |
| 361 | + |
| 362 | + $this->view->method('getRelativePath') |
| 363 | + ->willReturnCallback(function ($path) { |
| 364 | + return str_replace('/admin/files/', '', $path); |
| 365 | + }); |
| 366 | + |
| 367 | + $this->view->expects($this->exactly(2)) |
| 368 | + ->method('getFileInfo') |
| 369 | + ->willReturn($pathParentNode); |
| 370 | + |
| 371 | + $pathParentNode->expects($this->exactly(2)) |
| 372 | + ->method('getPath') |
| 373 | + ->willReturnOnConsecutiveCalls('/my/deep', '/my'); |
| 374 | + $pathParentNode->expects($this->exactly(2)) |
| 375 | + ->method('isReadable') |
| 376 | + ->willReturnOnConsecutiveCalls(true, false); |
| 377 | + |
| 378 | + $this->expectException(NotFound::class); |
| 379 | + |
| 380 | + $dir = new Directory($this->view, $directoryNode); |
| 381 | + $dir->getNodeForPath('/my/deep/folder/'); |
| 382 | + } |
| 383 | + |
| 384 | + public function testGetNodeForPathFailsWithNoReadPermissionsForPath(): void { |
| 385 | + $directoryNode = $this->createMock(Folder::class); |
| 386 | + $pathNode = $this->createMock(Folder::class); |
| 387 | + $storage = $this->createMock(IStorage::class); |
| 388 | + |
| 389 | + $directoryNode->expects($this->once()) |
| 390 | + ->method('getStorage') |
| 391 | + ->willReturn($storage); |
| 392 | + $storage->expects($this->once()) |
| 393 | + ->method('instanceOfStorage') |
| 394 | + ->willReturn(false); |
| 395 | + |
| 396 | + $directoryNode->expects($this->once()) |
| 397 | + ->method('isReadable') |
| 398 | + ->willReturn(true); |
| 399 | + $directoryNode->expects($this->once()) |
| 400 | + ->method('getPath') |
| 401 | + ->willReturn('/admin/files/'); |
| 402 | + $directoryNode->expects($this->once()) |
| 403 | + ->method('get') |
| 404 | + ->willReturn($pathNode); |
| 405 | + |
| 406 | + $pathNode->expects($this->once()) |
| 407 | + ->method('isReadable') |
| 408 | + ->willReturn(false); |
| 409 | + |
| 410 | + $this->expectException(\Sabre\DAV\Exception\Forbidden::class); |
| 411 | + |
| 412 | + $dir = new Directory($this->view, $directoryNode); |
| 413 | + $dir->getNodeForPath('/my/deep/folder/'); |
| 414 | + } |
| 415 | + |
274 | 416 | public function testGetQuotaInfoUnlimited(): void { |
275 | 417 | $this->createUser('user', 'password'); |
276 | 418 | self::loginAsUser('user'); |
|
0 commit comments