You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestCase.php 7.6KB

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