OldGroupMembershipSharesTest.php 3.7 KB

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