1
0

OldGroupMembershipSharesTest.php 3.7 KB

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