SharingMapperTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. use OCA\DAV\DAV\Sharing\SharingMapper;
  8. use OCP\IDBConnection;
  9. use OCP\Server;
  10. use Test\TestCase;
  11. /**
  12. * @group DB
  13. */
  14. class SharingMapperTest extends TestCase {
  15. private SharingMapper $mapper;
  16. private IDBConnection $db;
  17. protected function setUp(): void {
  18. parent::setUp();
  19. $this->db = Server::get(IDBConnection::class);
  20. $this->mapper = new SharingMapper($this->db);
  21. $qb = $this->db->getQueryBuilder();
  22. $qb->delete('dav_shares')->executeStatement();
  23. }
  24. public function testShareAndGet(): void {
  25. $resourceId = 42;
  26. $resourceType = 'calendar';
  27. $access = 3;
  28. $principal = 'principals/users/bob';
  29. $this->mapper->share($resourceId, $resourceType, $access, $principal);
  30. $shares = $this->mapper->getSharesForId($resourceId, $resourceType);
  31. $this->assertCount(1, $shares);
  32. }
  33. public function testShareDelete(): void {
  34. $resourceId = 42;
  35. $resourceType = 'calendar';
  36. $access = 3;
  37. $principal = 'principals/users/bob';
  38. $this->mapper->share($resourceId, $resourceType, $access, $principal);
  39. $this->mapper->deleteShare($resourceId, $resourceType, $principal);
  40. $shares = $this->mapper->getSharesForId($resourceId, $resourceType);
  41. $this->assertEmpty($shares);
  42. }
  43. public function testShareUnshare(): void {
  44. $resourceId = 42;
  45. $resourceType = 'calendar';
  46. $access = 3;
  47. $principal = 'principals/groups/alicegroup';
  48. $userPrincipal = 'principals/users/alice';
  49. $this->mapper->share($resourceId, $resourceType, $access, $principal);
  50. $this->mapper->unshare($resourceId, $resourceType, $userPrincipal);
  51. $shares = $this->mapper->getSharesForId($resourceId, $resourceType);
  52. $this->assertCount(1, $shares);
  53. }
  54. public function testShareDeleteAll(): void {
  55. $resourceId = 42;
  56. $resourceType = 'calendar';
  57. $access = 3;
  58. $principal = 'principals/groups/alicegroup';
  59. $userPrincipal = 'principals/users/alice';
  60. $this->mapper->share($resourceId, $resourceType, $access, $principal);
  61. $this->mapper->unshare($resourceId, $resourceType, $userPrincipal);
  62. $shares = $this->mapper->getSharesForId($resourceId, $resourceType);
  63. $this->assertCount(1, $shares);
  64. $this->mapper->deleteAllShares($resourceId, $resourceType);
  65. $shares = $this->mapper->getSharesForId($resourceId, $resourceType);
  66. $this->assertEmpty($shares);
  67. }
  68. public function testShareDeleteAllForUser(): void {
  69. $resourceId = 42;
  70. $resourceType = 'calendar';
  71. $access = 3;
  72. $principal = 'principals/groups/alicegroup';
  73. $this->mapper->share($resourceId, $resourceType, $access, $principal);
  74. $shares = $this->mapper->getSharesForId($resourceId, $resourceType);
  75. $this->assertCount(1, $shares);
  76. $this->mapper->deleteAllSharesByUser($principal, $resourceType);
  77. $shares = $this->mapper->getSharesForId($resourceId, $resourceType);
  78. $this->assertEmpty($shares);
  79. }
  80. }