TestCase.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing\Tests;
  8. use OC\Files\Filesystem;
  9. use OC\User\DisplayNameCache;
  10. use OCA\Files_Sharing\AppInfo\Application;
  11. use OCA\Files_Sharing\External\MountProvider as ExternalMountProvider;
  12. use OCA\Files_Sharing\MountProvider;
  13. use OCP\Files\Config\IMountProviderCollection;
  14. use OCP\Share\IShare;
  15. use Test\Traits\MountProviderTrait;
  16. /**
  17. * Class TestCase
  18. *
  19. * @group DB
  20. *
  21. * Base class for sharing tests.
  22. */
  23. abstract class TestCase extends \Test\TestCase {
  24. use MountProviderTrait;
  25. public const TEST_FILES_SHARING_API_USER1 = 'test-share-user1';
  26. public const TEST_FILES_SHARING_API_USER2 = 'test-share-user2';
  27. public const TEST_FILES_SHARING_API_USER3 = 'test-share-user3';
  28. public const TEST_FILES_SHARING_API_USER4 = 'test-share-user4';
  29. public const TEST_FILES_SHARING_API_GROUP1 = 'test-share-group1';
  30. public $filename;
  31. public $data;
  32. /**
  33. * @var \OC\Files\View
  34. */
  35. public $view;
  36. /**
  37. * @var \OC\Files\View
  38. */
  39. public $view2;
  40. public $folder;
  41. public $subfolder;
  42. /** @var \OCP\Share\IManager */
  43. protected $shareManager;
  44. /** @var \OCP\Files\IRootFolder */
  45. protected $rootFolder;
  46. public static function setUpBeforeClass(): void {
  47. parent::setUpBeforeClass();
  48. $app = new Application();
  49. $app->registerMountProviders(
  50. \OC::$server->get(IMountProviderCollection::class),
  51. \OC::$server->get(MountProvider::class),
  52. \OC::$server->get(ExternalMountProvider::class),
  53. );
  54. // reset backend
  55. \OC_User::clearBackends();
  56. \OC::$server->getGroupManager()->clearBackends();
  57. // clear share hooks
  58. \OC_Hook::clear('OCP\\Share');
  59. \OC::registerShareHooks(\OC::$server->getSystemConfig());
  60. // create users
  61. $backend = new \Test\Util\User\Dummy();
  62. \OC_User::useBackend($backend);
  63. $backend->createUser(self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER1);
  64. $backend->createUser(self::TEST_FILES_SHARING_API_USER2, self::TEST_FILES_SHARING_API_USER2);
  65. $backend->createUser(self::TEST_FILES_SHARING_API_USER3, self::TEST_FILES_SHARING_API_USER3);
  66. $backend->createUser(self::TEST_FILES_SHARING_API_USER4, self::TEST_FILES_SHARING_API_USER4);
  67. // create group
  68. $groupBackend = new \Test\Util\Group\Dummy();
  69. $groupBackend->createGroup(self::TEST_FILES_SHARING_API_GROUP1);
  70. $groupBackend->createGroup('group');
  71. $groupBackend->createGroup('group1');
  72. $groupBackend->createGroup('group2');
  73. $groupBackend->createGroup('group3');
  74. $groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER1, 'group');
  75. $groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER2, 'group');
  76. $groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER3, 'group');
  77. $groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER2, 'group1');
  78. $groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER3, 'group2');
  79. $groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER4, 'group3');
  80. $groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER2, self::TEST_FILES_SHARING_API_GROUP1);
  81. \OC::$server->getGroupManager()->addBackend($groupBackend);
  82. }
  83. protected function setUp(): void {
  84. parent::setUp();
  85. \OC::$server->get(DisplayNameCache::class)->clear();
  86. //login as user1
  87. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  88. $this->data = 'foobar';
  89. $this->view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER1 . '/files');
  90. $this->view2 = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  91. $this->shareManager = \OC::$server->getShareManager();
  92. $this->rootFolder = \OC::$server->getRootFolder();
  93. }
  94. protected function tearDown(): void {
  95. $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  96. $qb->delete('share');
  97. $qb->execute();
  98. $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  99. $qb->delete('mounts');
  100. $qb->execute();
  101. $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  102. $qb->delete('filecache')->runAcrossAllShards();
  103. $qb->execute();
  104. parent::tearDown();
  105. }
  106. public static function tearDownAfterClass(): void {
  107. // cleanup users
  108. $user = \OC::$server->getUserManager()->get(self::TEST_FILES_SHARING_API_USER1);
  109. if ($user !== null) {
  110. $user->delete();
  111. }
  112. $user = \OC::$server->getUserManager()->get(self::TEST_FILES_SHARING_API_USER2);
  113. if ($user !== null) {
  114. $user->delete();
  115. }
  116. $user = \OC::$server->getUserManager()->get(self::TEST_FILES_SHARING_API_USER3);
  117. if ($user !== null) {
  118. $user->delete();
  119. }
  120. // delete group
  121. $group = \OC::$server->getGroupManager()->get(self::TEST_FILES_SHARING_API_GROUP1);
  122. if ($group) {
  123. $group->delete();
  124. }
  125. \OC_Util::tearDownFS();
  126. \OC_User::setUserId('');
  127. Filesystem::tearDown();
  128. // reset backend
  129. \OC_User::clearBackends();
  130. \OC_User::useBackend('database');
  131. \OC::$server->getGroupManager()->clearBackends();
  132. \OC::$server->getGroupManager()->addBackend(new \OC\Group\Database());
  133. parent::tearDownAfterClass();
  134. }
  135. /**
  136. * @param string $user
  137. * @param bool $create
  138. * @param bool $password
  139. */
  140. protected static function loginHelper($user, $create = false, $password = false) {
  141. if ($password === false) {
  142. $password = $user;
  143. }
  144. if ($create) {
  145. $userManager = \OC::$server->getUserManager();
  146. $groupManager = \OC::$server->getGroupManager();
  147. $userObject = $userManager->createUser($user, $password);
  148. $group = $groupManager->createGroup('group');
  149. if ($group && $userObject) {
  150. $group->addUser($userObject);
  151. }
  152. }
  153. \OC_Util::tearDownFS();
  154. \OC\Files\Cache\Storage::getGlobalCache()->clearCache();
  155. \OC::$server->getUserSession()->setUser(null);
  156. \OC\Files\Filesystem::tearDown();
  157. \OC::$server->getUserSession()->login($user, $password);
  158. \OC::$server->getUserFolder($user);
  159. \OC_Util::setupFS($user);
  160. }
  161. /**
  162. * get some information from a given share
  163. * @param int $shareID
  164. * @return array with: item_source, share_type, share_with, item_type, permissions
  165. */
  166. protected function getShareFromId($shareID) {
  167. $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  168. $qb->select('item_source', '`share_type', 'share_with', 'item_type', 'permissions')
  169. ->from('share')
  170. ->where(
  171. $qb->expr()->eq('id', $qb->createNamedParameter($shareID))
  172. );
  173. $result = $qb->execute();
  174. $share = $result->fetch();
  175. $result->closeCursor();
  176. return $share;
  177. }
  178. /**
  179. * @param int $type The share type
  180. * @param string $path The path to share relative to $initiators root
  181. * @param string $initiator
  182. * @param string $recipient
  183. * @param int $permissions
  184. * @return \OCP\Share\IShare
  185. */
  186. protected function share($type, $path, $initiator, $recipient, $permissions) {
  187. $userFolder = $this->rootFolder->getUserFolder($initiator);
  188. $node = $userFolder->get($path);
  189. $share = $this->shareManager->newShare();
  190. $share->setShareType($type)
  191. ->setSharedWith($recipient)
  192. ->setSharedBy($initiator)
  193. ->setNode($node)
  194. ->setPermissions($permissions);
  195. $share = $this->shareManager->createShare($share);
  196. $share->setStatus(IShare::STATUS_ACCEPTED);
  197. $share = $this->shareManager->updateShare($share);
  198. return $share;
  199. }
  200. }