TestCase.php 8.0 KB

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