TestCase.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Robin McCorkell <robin@mccorkell.me.uk>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. * @author Thomas Müller <thomas.mueller@tmit.eu>
  15. * @author Vincent Petry <pvince81@owncloud.com>
  16. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. namespace OCA\Files_Sharing\Tests;
  33. use OC\Files\Cache\Scanner;
  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. const TEST_FILES_SHARING_API_USER1 = "test-share-user1";
  48. const TEST_FILES_SHARING_API_USER2 = "test-share-user2";
  49. const TEST_FILES_SHARING_API_USER3 = "test-share-user3";
  50. const TEST_FILES_SHARING_API_USER4 = "test-share-user4";
  51. 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();
  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) { $user->delete(); }
  121. $user = \OC::$server->getUserManager()->get(self::TEST_FILES_SHARING_API_USER2);
  122. if ($user !== null) { $user->delete(); }
  123. $user = \OC::$server->getUserManager()->get(self::TEST_FILES_SHARING_API_USER3);
  124. if ($user !== null) { $user->delete(); }
  125. // delete group
  126. $group = \OC::$server->getGroupManager()->get(self::TEST_FILES_SHARING_API_GROUP1);
  127. if ($group) {
  128. $group->delete();
  129. }
  130. \OC_Util::tearDownFS();
  131. \OC_User::setUserId('');
  132. Filesystem::tearDown();
  133. // reset backend
  134. \OC_User::clearBackends();
  135. \OC_User::useBackend('database');
  136. \OC::$server->getGroupManager()->clearBackends();
  137. \OC::$server->getGroupManager()->addBackend(new \OC\Group\Database());
  138. parent::tearDownAfterClass();
  139. }
  140. /**
  141. * @param string $user
  142. * @param bool $create
  143. * @param bool $password
  144. */
  145. protected static function loginHelper($user, $create = false, $password = false) {
  146. if ($password === false) {
  147. $password = $user;
  148. }
  149. if ($create) {
  150. $userManager = \OC::$server->getUserManager();
  151. $groupManager = \OC::$server->getGroupManager();
  152. $userObject = $userManager->createUser($user, $password);
  153. $group = $groupManager->createGroup('group');
  154. if ($group and $userObject) {
  155. $group->addUser($userObject);
  156. }
  157. }
  158. self::resetStorage();
  159. \OC_Util::tearDownFS();
  160. \OC\Files\Cache\Storage::getGlobalCache()->clearCache();
  161. \OC::$server->getUserSession()->setUser(null);
  162. \OC\Files\Filesystem::tearDown();
  163. \OC::$server->getUserSession()->login($user, $password);
  164. \OC::$server->getUserFolder($user);
  165. \OC_Util::setupFS($user);
  166. }
  167. /**
  168. * reset init status for the share storage
  169. */
  170. protected static function resetStorage() {
  171. $storage = new \ReflectionClass('\OCA\Files_Sharing\SharedStorage');
  172. $isInitialized = $storage->getProperty('initialized');
  173. $isInitialized->setAccessible(true);
  174. $isInitialized->setValue($storage, false);
  175. $isInitialized->setAccessible(false);
  176. }
  177. /**
  178. * get some information from a given share
  179. * @param int $shareID
  180. * @return array with: item_source, share_type, share_with, item_type, permissions
  181. */
  182. protected function getShareFromId($shareID) {
  183. $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  184. $qb->select('item_source', '`share_type', 'share_with', 'item_type', 'permissions')
  185. ->from('share')
  186. ->where(
  187. $qb->expr()->eq('id', $qb->createNamedParameter($shareID))
  188. );
  189. $result = $qb->execute();
  190. $share = $result->fetch();
  191. $result->closeCursor();
  192. return $share;
  193. }
  194. /**
  195. * @param int $type The share type
  196. * @param string $path The path to share relative to $initiators root
  197. * @param string $initiator
  198. * @param string $recipient
  199. * @param int $permissions
  200. * @return \OCP\Share\IShare
  201. */
  202. protected function share($type, $path, $initiator, $recipient, $permissions) {
  203. $userFolder = $this->rootFolder->getUserFolder($initiator);
  204. $node = $userFolder->get($path);
  205. $share = $this->shareManager->newShare();
  206. $share->setShareType($type)
  207. ->setSharedWith($recipient)
  208. ->setSharedBy($initiator)
  209. ->setNode($node)
  210. ->setPermissions($permissions);
  211. $share = $this->shareManager->createShare($share);
  212. $share->setStatus(IShare::STATUS_ACCEPTED);
  213. $share = $this->shareManager->updateShare($share);
  214. return $share;
  215. }
  216. }