TestCase.php 8.0 KB

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