|
16 | 16 | use OCA\GroupFolders\Mount\FolderStorageManager; |
17 | 17 | use OCA\GroupFolders\ResponseDefinitions; |
18 | 18 | use OCP\Constants; |
| 19 | +use OCP\DB\QueryBuilder\IQueryBuilder; |
19 | 20 | use OCP\EventDispatcher\IEventDispatcher; |
20 | 21 | use OCP\Files\FileInfo; |
21 | 22 | use OCP\Files\IMimeTypeLoader; |
@@ -517,4 +518,147 @@ public function testQuotaDefaultValue(): void { |
517 | 518 | } |
518 | 519 | $this->assertEquals(1024 ** 4, $folder->quota); |
519 | 520 | } |
| 521 | + |
| 522 | + /** |
| 523 | + * Regression test for the PROPFIND/ACL per-path query. |
| 524 | + * |
| 525 | + * `hasFolderACLDefaultNoPermission()` is called once per path during ACL |
| 526 | + * permission calculation (`ACLManager::getBasePermission`). It must be served |
| 527 | + * from a request-scoped cache instead of querying `group_folders` every time. |
| 528 | + */ |
| 529 | + public function testHasFolderACLDefaultNoPermissionIsMemoizedUntilInvalidated(): void { |
| 530 | + $folderId = $this->manager->createFolder('acl-default-test', [], true); |
| 531 | + |
| 532 | + // First read populates the cache. |
| 533 | + $this->assertTrue($this->manager->hasFolderACLDefaultNoPermission($folderId)); |
| 534 | + |
| 535 | + // Flip the stored value behind the manager's back. |
| 536 | + $db = Server::get(IDBConnection::class); |
| 537 | + $update = $db->getQueryBuilder(); |
| 538 | + $update->update('group_folders') |
| 539 | + ->set('acl_default_no_permission', $update->createNamedParameter(0, IQueryBuilder::PARAM_INT)) |
| 540 | + ->where($update->expr()->eq('folder_id', $update->createNamedParameter($folderId, IQueryBuilder::PARAM_INT))); |
| 541 | + $update->executeStatement(); |
| 542 | + |
| 543 | + // Still served from cache: proves the per-call query was eliminated. |
| 544 | + $this->assertTrue($this->manager->hasFolderACLDefaultNoPermission($folderId)); |
| 545 | + |
| 546 | + // A mutation through the manager invalidates the cache and forces a fresh read. |
| 547 | + $this->manager->setManageACL($folderId, 'group', 'somegroup', true); |
| 548 | + $this->assertFalse($this->manager->hasFolderACLDefaultNoPermission($folderId)); |
| 549 | + } |
| 550 | + |
| 551 | + /** |
| 552 | + * Regression test for the repeated manager-mapping lookups. |
| 553 | + * |
| 554 | + * `canManageACL()` is consulted once per path for folders that grant no |
| 555 | + * permission by default. The result is stable within a request, so the |
| 556 | + * underlying lookups must only run once. |
| 557 | + */ |
| 558 | + public function testCanManageACLIsMemoized(): void { |
| 559 | + $folderId = $this->manager->createFolder('manage-cache-test'); |
| 560 | + |
| 561 | + $user = $this->createMock(IUser::class); |
| 562 | + $user->method('getUID')->willReturn('alice'); |
| 563 | + |
| 564 | + $this->groupManager->method('isAdmin')->willReturn(false); |
| 565 | + // The DB-backed mapping check must run exactly once across both calls. |
| 566 | + $this->userMappingManager->expects($this->once()) |
| 567 | + ->method('userInMappings') |
| 568 | + ->willReturn(false); |
| 569 | + |
| 570 | + $this->assertFalse($this->manager->canManageACL($folderId, $user)); |
| 571 | + $this->assertFalse($this->manager->canManageACL($folderId, $user)); |
| 572 | + } |
| 573 | + |
| 574 | + /** |
| 575 | + * The `canManageACL` cache must be dropped when the manager mappings change, |
| 576 | + * so a later check within the same request observes the new state. |
| 577 | + */ |
| 578 | + public function testCanManageACLCacheInvalidatedOnManageChange(): void { |
| 579 | + $folderId = $this->manager->createFolder('manage-invalidate-test'); |
| 580 | + |
| 581 | + $user = $this->createMock(IUser::class); |
| 582 | + $user->method('getUID')->willReturn('alice'); |
| 583 | + |
| 584 | + $this->groupManager->method('isAdmin')->willReturn(false); |
| 585 | + // Recomputed once before and once after the invalidating mutation. |
| 586 | + $this->userMappingManager->expects($this->exactly(2)) |
| 587 | + ->method('userInMappings') |
| 588 | + ->willReturn(false); |
| 589 | + |
| 590 | + $this->assertFalse($this->manager->canManageACL($folderId, $user)); |
| 591 | + $this->manager->setManageACL($folderId, 'group', 'somegroup', true); |
| 592 | + $this->assertFalse($this->manager->canManageACL($folderId, $user)); |
| 593 | + } |
| 594 | + |
| 595 | + /** |
| 596 | + * The ACL storage wrapper uses the folder's stored `storage_id` instead of |
| 597 | + * resolving the numeric id per folder; this guards that the stored value |
| 598 | + * stays equal to the live numeric storage id of the folder's files storage. |
| 599 | + */ |
| 600 | + public function testStoredStorageIdMatchesLiveNumericStorageId(): void { |
| 601 | + $this->config->method('getSystemValueInt') |
| 602 | + ->with('groupfolders.quota.default', FileInfo::SPACE_UNLIMITED) |
| 603 | + ->willReturn(FileInfo::SPACE_UNLIMITED); |
| 604 | + |
| 605 | + $folderId = $this->manager->createFolder('storage-id-test'); |
| 606 | + $folder = $this->manager->getFolder($folderId); |
| 607 | + if (!$folder) { |
| 608 | + throw new \Exception('Folder not found'); |
| 609 | + } |
| 610 | + |
| 611 | + $storage = $this->folderStorageManager->getBaseStorageForFolder( |
| 612 | + $folderId, |
| 613 | + $folder->useSeparateStorage(), |
| 614 | + $folder, |
| 615 | + null, |
| 616 | + false, |
| 617 | + 'files', |
| 618 | + ); |
| 619 | + |
| 620 | + $this->assertSame($folder->storageId, $storage->getCache()->getNumericStorageId()); |
| 621 | + } |
| 622 | + |
| 623 | + /** |
| 624 | + * Deleting a group removes its manager mappings, so the `canManageACL` cache |
| 625 | + * must be dropped for a later check in the same request to stay correct. |
| 626 | + */ |
| 627 | + public function testCanManageACLCacheInvalidatedOnGroupDeletion(): void { |
| 628 | + $folderId = $this->manager->createFolder('group-delete-test'); |
| 629 | + |
| 630 | + $user = $this->createMock(IUser::class); |
| 631 | + $user->method('getUID')->willReturn('alice'); |
| 632 | + |
| 633 | + $this->groupManager->method('isAdmin')->willReturn(false); |
| 634 | + // Recomputed once before and once after the group deletion. |
| 635 | + $this->userMappingManager->expects($this->exactly(2)) |
| 636 | + ->method('userInMappings') |
| 637 | + ->willReturn(false); |
| 638 | + |
| 639 | + $this->assertFalse($this->manager->canManageACL($folderId, $user)); |
| 640 | + $this->manager->deleteGroup('somegroup'); |
| 641 | + $this->assertFalse($this->manager->canManageACL($folderId, $user)); |
| 642 | + } |
| 643 | + |
| 644 | + /** |
| 645 | + * Deleting a user removes its manager mappings, so the `canManageACL` cache |
| 646 | + * must be dropped as well. |
| 647 | + */ |
| 648 | + public function testCanManageACLCacheInvalidatedOnUserDeletion(): void { |
| 649 | + $folderId = $this->manager->createFolder('user-delete-test'); |
| 650 | + |
| 651 | + $user = $this->createMock(IUser::class); |
| 652 | + $user->method('getUID')->willReturn('alice'); |
| 653 | + |
| 654 | + $this->groupManager->method('isAdmin')->willReturn(false); |
| 655 | + // Recomputed once before and once after the user deletion. |
| 656 | + $this->userMappingManager->expects($this->exactly(2)) |
| 657 | + ->method('userInMappings') |
| 658 | + ->willReturn(false); |
| 659 | + |
| 660 | + $this->assertFalse($this->manager->canManageACL($folderId, $user)); |
| 661 | + $this->manager->deleteUser('bob'); |
| 662 | + $this->assertFalse($this->manager->canManageACL($folderId, $user)); |
| 663 | + } |
520 | 664 | } |
0 commit comments