TestCase.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 Test\Traits\MountProviderTrait;
  36. /**
  37. * Class TestCase
  38. *
  39. * @group DB
  40. *
  41. * Base class for sharing tests.
  42. */
  43. abstract class TestCase extends \Test\TestCase {
  44. use MountProviderTrait;
  45. const TEST_FILES_SHARING_API_USER1 = "test-share-user1";
  46. const TEST_FILES_SHARING_API_USER2 = "test-share-user2";
  47. const TEST_FILES_SHARING_API_USER3 = "test-share-user3";
  48. const TEST_FILES_SHARING_API_USER4 = "test-share-user4";
  49. const TEST_FILES_SHARING_API_GROUP1 = "test-share-group1";
  50. public $filename;
  51. public $data;
  52. /**
  53. * @var \OC\Files\View
  54. */
  55. public $view;
  56. public $folder;
  57. public $subfolder;
  58. /** @var \OCP\Share\IManager */
  59. protected $shareManager;
  60. /** @var \OCP\Files\IRootFolder */
  61. protected $rootFolder;
  62. public static function setUpBeforeClass() {
  63. parent::setUpBeforeClass();
  64. $application = new Application();
  65. $application->registerMountProviders();
  66. // reset backend
  67. \OC_User::clearBackends();
  68. \OC_Group::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_Group::useBackend($groupBackend);
  94. }
  95. protected function setUp() {
  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() {
  105. $query = \OCP\DB::prepare('DELETE FROM `*PREFIX*share`');
  106. $query->execute();
  107. parent::tearDown();
  108. }
  109. public static function tearDownAfterClass() {
  110. // cleanup users
  111. $user = \OC::$server->getUserManager()->get(self::TEST_FILES_SHARING_API_USER1);
  112. if ($user !== null) { $user->delete(); }
  113. $user = \OC::$server->getUserManager()->get(self::TEST_FILES_SHARING_API_USER2);
  114. if ($user !== null) { $user->delete(); }
  115. $user = \OC::$server->getUserManager()->get(self::TEST_FILES_SHARING_API_USER3);
  116. if ($user !== null) { $user->delete(); }
  117. // delete group
  118. \OC_Group::deleteGroup(self::TEST_FILES_SHARING_API_GROUP1);
  119. \OC_Util::tearDownFS();
  120. \OC_User::setUserId('');
  121. Filesystem::tearDown();
  122. // reset backend
  123. \OC_User::clearBackends();
  124. \OC_User::useBackend('database');
  125. \OC_Group::clearBackends();
  126. \OC_Group::useBackend(new \OC\Group\Database());
  127. parent::tearDownAfterClass();
  128. }
  129. /**
  130. * @param string $user
  131. * @param bool $create
  132. * @param bool $password
  133. */
  134. protected static function loginHelper($user, $create = false, $password = false) {
  135. if ($password === false) {
  136. $password = $user;
  137. }
  138. if ($create) {
  139. \OC::$server->getUserManager()->createUser($user, $password);
  140. \OC_Group::createGroup('group');
  141. \OC_Group::addToGroup($user, 'group');
  142. }
  143. self::resetStorage();
  144. \OC_Util::tearDownFS();
  145. \OC\Files\Cache\Storage::getGlobalCache()->clearCache();
  146. \OC::$server->getUserSession()->setUser(null);
  147. \OC\Files\Filesystem::tearDown();
  148. \OC::$server->getUserSession()->login($user, $password);
  149. \OC::$server->getUserFolder($user);
  150. \OC_Util::setupFS($user);
  151. }
  152. /**
  153. * reset init status for the share storage
  154. */
  155. protected static function resetStorage() {
  156. $storage = new \ReflectionClass('\OCA\Files_Sharing\SharedStorage');
  157. $isInitialized = $storage->getProperty('initialized');
  158. $isInitialized->setAccessible(true);
  159. $isInitialized->setValue($storage, false);
  160. $isInitialized->setAccessible(false);
  161. }
  162. /**
  163. * get some information from a given share
  164. * @param int $shareID
  165. * @return array with: item_source, share_type, share_with, item_type, permissions
  166. */
  167. protected function getShareFromId($shareID) {
  168. $sql = 'SELECT `item_source`, `share_type`, `share_with`, `item_type`, `permissions` FROM `*PREFIX*share` WHERE `id` = ?';
  169. $args = array($shareID);
  170. $query = \OCP\DB::prepare($sql);
  171. $result = $query->execute($args);
  172. $share = Null;
  173. if ($result) {
  174. $share = $result->fetchRow();
  175. }
  176. return $share;
  177. }
  178. /**
  179. * @param int $type The share type
  180. * @param string $path The path to share relative to $initiators root
  181. * @param string $initiator
  182. * @param string $recipient
  183. * @param int $permissions
  184. * @return \OCP\Share\IShare
  185. */
  186. protected function share($type, $path, $initiator, $recipient, $permissions) {
  187. $userFolder = $this->rootFolder->getUserFolder($initiator);
  188. $node = $userFolder->get($path);
  189. $share = $this->shareManager->newShare();
  190. $share->setShareType($type)
  191. ->setSharedWith($recipient)
  192. ->setSharedBy($initiator)
  193. ->setNode($node)
  194. ->setPermissions($permissions);
  195. $share = $this->shareManager->createShare($share);
  196. return $share;
  197. }
  198. }