1
0

TestCase.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author John Molakvoæ <skjnldsv@protonmail.com>
  9. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Robin McCorkell <robin@mccorkell.me.uk>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author Thomas Müller <thomas.mueller@tmit.eu>
  16. * @author Vincent Petry <vincent@nextcloud.com>
  17. *
  18. * @license AGPL-3.0
  19. *
  20. * This code is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License, version 3,
  22. * as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License, version 3,
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>
  31. *
  32. */
  33. namespace OCA\Files_Sharing\Tests;
  34. use OC\Files\Filesystem;
  35. use OC\User\DisplayNameCache;
  36. use OCA\Files_Sharing\AppInfo\Application;
  37. use OCA\Files_Sharing\External\MountProvider as ExternalMountProvider;
  38. use OCA\Files_Sharing\MountProvider;
  39. use OCP\Files\Config\IMountProviderCollection;
  40. use OCP\Share\IShare;
  41. use Test\Traits\MountProviderTrait;
  42. /**
  43. * Class TestCase
  44. *
  45. * @group DB
  46. *
  47. * Base class for sharing tests.
  48. */
  49. abstract class TestCase extends \Test\TestCase {
  50. use MountProviderTrait;
  51. public const TEST_FILES_SHARING_API_USER1 = "test-share-user1";
  52. public const TEST_FILES_SHARING_API_USER2 = "test-share-user2";
  53. public const TEST_FILES_SHARING_API_USER3 = "test-share-user3";
  54. public const TEST_FILES_SHARING_API_USER4 = "test-share-user4";
  55. public const TEST_FILES_SHARING_API_GROUP1 = "test-share-group1";
  56. public $filename;
  57. public $data;
  58. /**
  59. * @var \OC\Files\View
  60. */
  61. public $view;
  62. /**
  63. * @var \OC\Files\View
  64. */
  65. public $view2;
  66. public $folder;
  67. public $subfolder;
  68. /** @var \OCP\Share\IManager */
  69. protected $shareManager;
  70. /** @var \OCP\Files\IRootFolder */
  71. protected $rootFolder;
  72. public static function setUpBeforeClass(): void {
  73. parent::setUpBeforeClass();
  74. $app = new Application();
  75. $app->registerMountProviders(
  76. \OC::$server->get(IMountProviderCollection::class),
  77. \OC::$server->get(MountProvider::class),
  78. \OC::$server->get(ExternalMountProvider::class),
  79. );
  80. // reset backend
  81. \OC_User::clearBackends();
  82. \OC::$server->getGroupManager()->clearBackends();
  83. // clear share hooks
  84. \OC_Hook::clear('OCP\\Share');
  85. \OC::registerShareHooks(\OC::$server->getSystemConfig());
  86. // create users
  87. $backend = new \Test\Util\User\Dummy();
  88. \OC_User::useBackend($backend);
  89. $backend->createUser(self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER1);
  90. $backend->createUser(self::TEST_FILES_SHARING_API_USER2, self::TEST_FILES_SHARING_API_USER2);
  91. $backend->createUser(self::TEST_FILES_SHARING_API_USER3, self::TEST_FILES_SHARING_API_USER3);
  92. $backend->createUser(self::TEST_FILES_SHARING_API_USER4, self::TEST_FILES_SHARING_API_USER4);
  93. // create group
  94. $groupBackend = new \Test\Util\Group\Dummy();
  95. $groupBackend->createGroup(self::TEST_FILES_SHARING_API_GROUP1);
  96. $groupBackend->createGroup('group');
  97. $groupBackend->createGroup('group1');
  98. $groupBackend->createGroup('group2');
  99. $groupBackend->createGroup('group3');
  100. $groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER1, 'group');
  101. $groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER2, 'group');
  102. $groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER3, 'group');
  103. $groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER2, 'group1');
  104. $groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER3, 'group2');
  105. $groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER4, 'group3');
  106. $groupBackend->addToGroup(self::TEST_FILES_SHARING_API_USER2, self::TEST_FILES_SHARING_API_GROUP1);
  107. \OC::$server->getGroupManager()->addBackend($groupBackend);
  108. }
  109. protected function setUp(): void {
  110. parent::setUp();
  111. \OC::$server->get(DisplayNameCache::class)->clear();
  112. //login as user1
  113. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  114. $this->data = 'foobar';
  115. $this->view = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER1 . '/files');
  116. $this->view2 = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  117. $this->shareManager = \OC::$server->getShareManager();
  118. $this->rootFolder = \OC::$server->getRootFolder();
  119. }
  120. protected function tearDown(): void {
  121. $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  122. $qb->delete('share');
  123. $qb->execute();
  124. $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  125. $qb->delete('mounts');
  126. $qb->execute();
  127. $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  128. $qb->delete('filecache');
  129. $qb->execute();
  130. parent::tearDown();
  131. }
  132. public static function tearDownAfterClass(): void {
  133. // cleanup users
  134. $user = \OC::$server->getUserManager()->get(self::TEST_FILES_SHARING_API_USER1);
  135. if ($user !== null) {
  136. $user->delete();
  137. }
  138. $user = \OC::$server->getUserManager()->get(self::TEST_FILES_SHARING_API_USER2);
  139. if ($user !== null) {
  140. $user->delete();
  141. }
  142. $user = \OC::$server->getUserManager()->get(self::TEST_FILES_SHARING_API_USER3);
  143. if ($user !== null) {
  144. $user->delete();
  145. }
  146. // delete group
  147. $group = \OC::$server->getGroupManager()->get(self::TEST_FILES_SHARING_API_GROUP1);
  148. if ($group) {
  149. $group->delete();
  150. }
  151. \OC_Util::tearDownFS();
  152. \OC_User::setUserId('');
  153. Filesystem::tearDown();
  154. // reset backend
  155. \OC_User::clearBackends();
  156. \OC_User::useBackend('database');
  157. \OC::$server->getGroupManager()->clearBackends();
  158. \OC::$server->getGroupManager()->addBackend(new \OC\Group\Database());
  159. parent::tearDownAfterClass();
  160. }
  161. /**
  162. * @param string $user
  163. * @param bool $create
  164. * @param bool $password
  165. */
  166. protected static function loginHelper($user, $create = false, $password = false) {
  167. if ($password === false) {
  168. $password = $user;
  169. }
  170. if ($create) {
  171. $userManager = \OC::$server->getUserManager();
  172. $groupManager = \OC::$server->getGroupManager();
  173. $userObject = $userManager->createUser($user, $password);
  174. $group = $groupManager->createGroup('group');
  175. if ($group && $userObject) {
  176. $group->addUser($userObject);
  177. }
  178. }
  179. \OC_Util::tearDownFS();
  180. \OC\Files\Cache\Storage::getGlobalCache()->clearCache();
  181. \OC::$server->getUserSession()->setUser(null);
  182. \OC\Files\Filesystem::tearDown();
  183. \OC::$server->getUserSession()->login($user, $password);
  184. \OC::$server->getUserFolder($user);
  185. \OC_Util::setupFS($user);
  186. }
  187. /**
  188. * get some information from a given share
  189. * @param int $shareID
  190. * @return array with: item_source, share_type, share_with, item_type, permissions
  191. */
  192. protected function getShareFromId($shareID) {
  193. $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  194. $qb->select('item_source', '`share_type', 'share_with', 'item_type', 'permissions')
  195. ->from('share')
  196. ->where(
  197. $qb->expr()->eq('id', $qb->createNamedParameter($shareID))
  198. );
  199. $result = $qb->execute();
  200. $share = $result->fetch();
  201. $result->closeCursor();
  202. return $share;
  203. }
  204. /**
  205. * @param int $type The share type
  206. * @param string $path The path to share relative to $initiators root
  207. * @param string $initiator
  208. * @param string $recipient
  209. * @param int $permissions
  210. * @return \OCP\Share\IShare
  211. */
  212. protected function share($type, $path, $initiator, $recipient, $permissions) {
  213. $userFolder = $this->rootFolder->getUserFolder($initiator);
  214. $node = $userFolder->get($path);
  215. $share = $this->shareManager->newShare();
  216. $share->setShareType($type)
  217. ->setSharedWith($recipient)
  218. ->setSharedBy($initiator)
  219. ->setNode($node)
  220. ->setPermissions($permissions);
  221. $share = $this->shareManager->createShare($share);
  222. $share->setStatus(IShare::STATUS_ACCEPTED);
  223. $share = $this->shareManager->updateShare($share);
  224. return $share;
  225. }
  226. }