diff options
Diffstat (limited to 'tests/lib/Share/ShareTest.php')
-rw-r--r-- | tests/lib/Share/ShareTest.php | 166 |
1 files changed, 93 insertions, 73 deletions
diff --git a/tests/lib/Share/ShareTest.php b/tests/lib/Share/ShareTest.php index c690ada99ca..35dc00739f6 100644 --- a/tests/lib/Share/ShareTest.php +++ b/tests/lib/Share/ShareTest.php @@ -21,6 +21,9 @@ namespace Test\Share; +use OC\Share\Share; +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IDBConnection; use OCP\IGroup; use OCP\IGroupManager; use OCP\IUser; @@ -35,36 +38,25 @@ use OCP\Share\IShare; class ShareTest extends \Test\TestCase { protected $itemType; - /** @var IUser */ - protected $user1; - /** @var IUser */ - protected $user2; - /** @var IUser */ - protected $user3; - /** @var IUser */ - protected $user4; - /** @var IUser */ - protected $user5; - /** @var IUser */ - protected $user6; - /** @var IUser */ - protected $groupAndUser_user; - - /** @var IGroup */ - protected $group1; - /** @var IGroup */ - protected $group2; - /** @var IGroup */ - protected $groupAndUser_group; - - protected $resharing; - protected $dateInFuture; - protected $dateInPast; - - /** @var IGroupManager */ - protected $groupManager; - /** @var IUserManager */ - protected $userManager; + protected IUser $user1; + protected IUser $user2; + protected IUser $user3; + protected IUser $user4; + protected IUser $user5; + protected IUser $user6; + protected IUser $groupAndUser_user; + + protected IGroup $group1; + protected IGroup $group2; + protected IGroup $groupAndUser_group; + + protected string $resharing; + protected string $dateInFuture; + protected string $dateInPast; + + protected IGroupManager $groupManager; + protected IUserManager $userManager; + private IDBConnection $connection; protected function setUp(): void { parent::setUp(); @@ -90,6 +82,7 @@ class ShareTest extends \Test\TestCase { $this->group1 = $this->groupManager->createGroup($this->getUniqueID('group1_')); $this->group2 = $this->groupManager->createGroup($this->getUniqueID('group2_')); $this->groupAndUser_group = $this->groupManager->createGroup($groupAndUserId); + $this->connection = \OC::$server->get(IDBConnection::class); $this->group1->addUser($this->user1); $this->group1->addUser($this->user2); @@ -99,7 +92,7 @@ class ShareTest extends \Test\TestCase { $this->groupAndUser_group->addUser($this->user2); $this->groupAndUser_group->addUser($this->user3); - \OC\Share\Share::registerBackend('test', 'Test\Share\Backend'); + Share::registerBackend('test', 'Test\Share\Backend'); \OC_Hook::clear('OCP\\Share'); \OC::registerShareHooks(\OC::$server->getSystemConfig()); $this->resharing = \OC::$server->getConfig()->getAppValue('core', 'shareapi_allow_resharing', 'yes'); @@ -113,8 +106,9 @@ class ShareTest extends \Test\TestCase { } protected function tearDown(): void { - $query = \OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `item_type` = ?'); - $query->execute(['test']); + $query = $this->connection->getQueryBuilder(); + $query->delete('share')->andWhere($query->expr()->eq('item_type', $query->createNamedParameter('test'))); + $query->executeStatement(); \OC::$server->getConfig()->setAppValue('core', 'shareapi_allow_resharing', $this->resharing); $this->user1->delete(); @@ -136,39 +130,51 @@ class ShareTest extends \Test\TestCase { public function testGetItemSharedWithUser() { \OC_User::setUserId($this->user1->getUID()); - //add dummy values to the share table - $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (' - .' `item_type`, `item_source`, `item_target`, `share_type`,' - .' `share_with`, `uid_owner`) VALUES (?,?,?,?,?,?)'); - $args = ['test', 99, 'target1', IShare::TYPE_USER, $this->user2->getUID(), $this->user1->getUID()]; - $query->execute($args); - $args = ['test', 99, 'target2', IShare::TYPE_USER, $this->user4->getUID(), $this->user1->getUID()]; - $query->execute($args); - $args = ['test', 99, 'target3', IShare::TYPE_USER, $this->user3->getUID(), $this->user2->getUID()]; - $query->execute($args); - $args = ['test', 99, 'target4', IShare::TYPE_USER, $this->user3->getUID(), $this->user4->getUID()]; - $query->execute($args); - $args = ['test', 99, 'target4', IShare::TYPE_USER, $this->user6->getUID(), $this->user4->getUID()]; - $query->execute($args); - - - $result1 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user2->getUID(), $this->user1->getUID()); + // add dummy values to the share table + $query = $this->connection->getQueryBuilder(); + $query->insert('share') + ->values([ + 'item_type' => $query->createParameter('itemType'), + 'item_source' => $query->createParameter('itemSource'), + 'item_target' => $query->createParameter('itemTarget'), + 'share_type' => $query->createParameter('shareType'), + 'share_with' => $query->createParameter('shareWith'), + 'uid_owner' => $query->createParameter('uidOwner') + ]); + $args = [ + ['test', 99, 'target1', IShare::TYPE_USER, $this->user2->getUID(), $this->user1->getUID()], + ['test', 99, 'target2', IShare::TYPE_USER, $this->user4->getUID(), $this->user1->getUID()], + ['test', 99, 'target3', IShare::TYPE_USER, $this->user3->getUID(), $this->user2->getUID()], + ['test', 99, 'target4', IShare::TYPE_USER, $this->user3->getUID(), $this->user4->getUID()], + ['test', 99, 'target4', IShare::TYPE_USER, $this->user6->getUID(), $this->user4->getUID()], + ]; + foreach ($args as $row) { + $query->setParameter('itemType', $row[0]); + $query->setParameter('itemSource', $row[1], IQueryBuilder::PARAM_INT); + $query->setParameter('itemTarget', $row[2]); + $query->setParameter('shareType', $row[3], IQueryBuilder::PARAM_INT); + $query->setParameter('shareWith', $row[4]); + $query->setParameter('uidOwner', $row[5]); + $query->executeStatement(); + } + + $result1 = Share::getItemSharedWithUser('test', 99, $this->user2->getUID(), $this->user1->getUID()); $this->assertSame(1, count($result1)); $this->verifyResult($result1, ['target1']); - $result2 = \OCP\Share::getItemSharedWithUser('test', 99, null, $this->user1->getUID()); + $result2 = Share::getItemSharedWithUser('test', 99, null, $this->user1->getUID()); $this->assertSame(2, count($result2)); $this->verifyResult($result2, ['target1', 'target2']); - $result3 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user3->getUID()); + $result3 = Share::getItemSharedWithUser('test', 99, $this->user3->getUID()); $this->assertSame(2, count($result3)); $this->verifyResult($result3, ['target3', 'target4']); - $result4 = \OCP\Share::getItemSharedWithUser('test', 99, null, null); + $result4 = Share::getItemSharedWithUser('test', 99, null, null); $this->assertSame(5, count($result4)); // 5 because target4 appears twice $this->verifyResult($result4, ['target1', 'target2', 'target3', 'target4']); - $result6 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user6->getUID(), null); + $result6 = Share::getItemSharedWithUser('test', 99, $this->user6->getUID(), null); $this->assertSame(1, count($result6)); $this->verifyResult($result6, ['target4']); } @@ -176,38 +182,52 @@ class ShareTest extends \Test\TestCase { public function testGetItemSharedWithUserFromGroupShare() { \OC_User::setUserId($this->user1->getUID()); - //add dummy values to the share table - $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (' - .' `item_type`, `item_source`, `item_target`, `share_type`,' - .' `share_with`, `uid_owner`) VALUES (?,?,?,?,?,?)'); - $args = ['test', 99, 'target1', IShare::TYPE_GROUP, $this->group1->getGID(), $this->user1->getUID()]; - $query->execute($args); - $args = ['test', 99, 'target2', IShare::TYPE_GROUP, $this->group2->getGID(), $this->user1->getUID()]; - $query->execute($args); - $args = ['test', 99, 'target3', IShare::TYPE_GROUP, $this->group1->getGID(), $this->user2->getUID()]; - $query->execute($args); - $args = ['test', 99, 'target4', IShare::TYPE_GROUP, $this->group1->getGID(), $this->user4->getUID()]; - $query->execute($args); + // add dummy values to the share table + $query = $this->connection->getQueryBuilder(); + $query->insert('share') + ->values([ + 'item_type' => $query->createParameter('itemType'), + 'item_source' => $query->createParameter('itemSource'), + 'item_target' => $query->createParameter('itemTarget'), + 'share_type' => $query->createParameter('shareType'), + 'share_with' => $query->createParameter('shareWith'), + 'uid_owner' => $query->createParameter('uidOwner') + ]); + $args = [ + ['test', 99, 'target1', IShare::TYPE_GROUP, $this->group1->getGID(), $this->user1->getUID()], + ['test', 99, 'target2', IShare::TYPE_GROUP, $this->group2->getGID(), $this->user1->getUID()], + ['test', 99, 'target3', IShare::TYPE_GROUP, $this->group1->getGID(), $this->user2->getUID()], + ['test', 99, 'target4', IShare::TYPE_GROUP, $this->group1->getGID(), $this->user4->getUID()], + ]; + foreach ($args as $row) { + $query->setParameter('itemType', $row[0]); + $query->setParameter('itemSource', $row[1], IQueryBuilder::PARAM_INT); + $query->setParameter('itemTarget', $row[2]); + $query->setParameter('shareType', $row[3], IQueryBuilder::PARAM_INT); + $query->setParameter('shareWith', $row[4]); + $query->setParameter('uidOwner', $row[5]); + $query->executeStatement(); + } // user2 is in group1 and group2 - $result1 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user2->getUID(), $this->user1->getUID()); + $result1 = Share::getItemSharedWithUser('test', 99, $this->user2->getUID(), $this->user1->getUID()); $this->assertSame(2, count($result1)); $this->verifyResult($result1, ['target1', 'target2']); - $result2 = \OCP\Share::getItemSharedWithUser('test', 99, null, $this->user1->getUID()); + $result2 = Share::getItemSharedWithUser('test', 99, null, $this->user1->getUID()); $this->assertSame(2, count($result2)); $this->verifyResult($result2, ['target1', 'target2']); // user3 is in group1 and group2 - $result3 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user3->getUID()); + $result3 = Share::getItemSharedWithUser('test', 99, $this->user3->getUID()); $this->assertSame(3, count($result3)); $this->verifyResult($result3, ['target1', 'target3', 'target4']); - $result4 = \OCP\Share::getItemSharedWithUser('test', 99, null, null); + $result4 = Share::getItemSharedWithUser('test', 99, null, null); $this->assertSame(4, count($result4)); $this->verifyResult($result4, ['target1', 'target2', 'target3', 'target4']); - $result6 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user6->getUID(), null); + $result6 = Share::getItemSharedWithUser('test', 99, $this->user6->getUID(), null); $this->assertSame(0, count($result6)); } @@ -227,7 +247,7 @@ class ShareTest extends \Test\TestCase { * @param string $expectedResult */ public function testRemoveProtocolFromUrl($url, $expectedResult) { - $share = new \OC\Share\Share(); + $share = new Share(); $result = self::invokePrivate($share, 'removeProtocolFromUrl', [$url]); $this->assertSame($expectedResult, $result); } @@ -316,7 +336,7 @@ class ShareTest extends \Test\TestCase { } } -class DummyShareClass extends \OC\Share\Share { +class DummyShareClass extends Share { public static function groupItemsTest($items) { return parent::groupItems($items, 'test'); } |