1
0

TestCase.php 7.1 KB

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