OldGroupMembershipSharesTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Repair;
  8. use OC\Repair\OldGroupMembershipShares;
  9. use OCP\IDBConnection;
  10. use OCP\IGroupManager;
  11. use OCP\Migration\IOutput;
  12. use OCP\Share\IShare;
  13. use PHPUnit\Framework\MockObject\MockObject;
  14. /**
  15. * Class OldGroupMembershipSharesTest
  16. *
  17. * @group DB
  18. *
  19. * @package Test\Repair
  20. */
  21. class OldGroupMembershipSharesTest extends \Test\TestCase {
  22. private IDBConnection $connection;
  23. private IGroupManager&MockObject $groupManager;
  24. protected function setUp(): void {
  25. parent::setUp();
  26. $this->groupManager = $this->getMockBuilder(IGroupManager::class)
  27. ->disableOriginalConstructor()
  28. ->getMock();
  29. $this->connection = \OCP\Server::get(IDBConnection::class);
  30. $this->deleteAllShares();
  31. }
  32. protected function tearDown(): void {
  33. $this->deleteAllShares();
  34. parent::tearDown();
  35. }
  36. protected function deleteAllShares() {
  37. $qb = $this->connection->getQueryBuilder();
  38. $qb->delete('share')->executeStatement();
  39. }
  40. public function testRun(): void {
  41. $repair = new OldGroupMembershipShares(
  42. $this->connection,
  43. $this->groupManager
  44. );
  45. $this->groupManager->expects($this->exactly(2))
  46. ->method('isInGroup')
  47. ->willReturnMap([
  48. ['member', 'group', true],
  49. ['not-a-member', 'group', false],
  50. ]);
  51. $parent = $this->createShare(IShare::TYPE_GROUP, 'group', null);
  52. $group2 = $this->createShare(IShare::TYPE_GROUP, 'group2', $parent);
  53. $user1 = $this->createShare(IShare::TYPE_USER, 'user1', $parent);
  54. // \OC\Share\Constant::$shareTypeGroupUserUnique === 2
  55. $member = $this->createShare(2, 'member', $parent);
  56. $notAMember = $this->createShare(2, 'not-a-member', $parent);
  57. $query = $this->connection->getQueryBuilder();
  58. $result = $query->select('id')
  59. ->from('share')
  60. ->orderBy('id', 'ASC')
  61. ->executeQuery();
  62. $rows = $result->fetchAll();
  63. $this->assertEquals([['id' => $parent], ['id' => $group2], ['id' => $user1], ['id' => $member], ['id' => $notAMember]], $rows);
  64. $result->closeCursor();
  65. /** @var IOutput | \PHPUnit\Framework\MockObject\MockObject $outputMock */
  66. $outputMock = $this->getMockBuilder('\OCP\Migration\IOutput')
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $repair->run($outputMock);
  70. $query = $this->connection->getQueryBuilder();
  71. $result = $query->select('id')
  72. ->from('share')
  73. ->orderBy('id', 'ASC')
  74. ->executeQuery();
  75. $rows = $result->fetchAll();
  76. $this->assertEquals([['id' => $parent], ['id' => $group2], ['id' => $user1], ['id' => $member]], $rows);
  77. $result->closeCursor();
  78. }
  79. /**
  80. * @param string $shareType
  81. * @param string $shareWith
  82. * @param null|int $parent
  83. * @return int
  84. */
  85. protected function createShare($shareType, $shareWith, $parent) {
  86. $qb = $this->connection->getQueryBuilder();
  87. $shareValues = [
  88. 'share_type' => $qb->expr()->literal($shareType),
  89. 'share_with' => $qb->expr()->literal($shareWith),
  90. 'uid_owner' => $qb->expr()->literal('user1'),
  91. 'item_type' => $qb->expr()->literal('folder'),
  92. 'item_source' => $qb->expr()->literal(123),
  93. 'item_target' => $qb->expr()->literal('/123'),
  94. 'file_source' => $qb->expr()->literal(123),
  95. 'file_target' => $qb->expr()->literal('/test'),
  96. 'permissions' => $qb->expr()->literal(1),
  97. 'stime' => $qb->expr()->literal(time()),
  98. 'expiration' => $qb->expr()->literal('2015-09-25 00:00:00'),
  99. ];
  100. if ($parent) {
  101. $shareValues['parent'] = $qb->expr()->literal($parent);
  102. }
  103. $qb = $this->connection->getQueryBuilder();
  104. $qb->insert('share')
  105. ->values($shareValues)
  106. ->executeStatement();
  107. return $qb->getLastInsertId();
  108. }
  109. }