Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TestCase.php 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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::$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() {
  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. $group = \OC::$server->getGroupManager()->get(self::TEST_FILES_SHARING_API_GROUP1);
  119. if ($group) {
  120. $group->delete();
  121. }
  122. \OC_Util::tearDownFS();
  123. \OC_User::setUserId('');
  124. Filesystem::tearDown();
  125. // reset backend
  126. \OC_User::clearBackends();
  127. \OC_User::useBackend('database');
  128. \OC::$server->getGroupManager()->clearBackends();
  129. \OC::$server->getGroupManager()->addBackend(new \OC\Group\Database());
  130. parent::tearDownAfterClass();
  131. }
  132. /**
  133. * @param string $user
  134. * @param bool $create
  135. * @param bool $password
  136. */
  137. protected static function loginHelper($user, $create = false, $password = false) {
  138. if ($password === false) {
  139. $password = $user;
  140. }
  141. if ($create) {
  142. $userManager = \OC::$server->getUserManager();
  143. $groupManager = \OC::$server->getGroupManager();
  144. $userObject = $userManager->createUser($user, $password);
  145. $group = $groupManager->createGroup('group');
  146. if ($group and $userObject) {
  147. $group->addUser($userObject);
  148. }
  149. }
  150. self::resetStorage();
  151. \OC_Util::tearDownFS();
  152. \OC\Files\Cache\Storage::getGlobalCache()->clearCache();
  153. \OC::$server->getUserSession()->setUser(null);
  154. \OC\Files\Filesystem::tearDown();
  155. \OC::$server->getUserSession()->login($user, $password);
  156. \OC::$server->getUserFolder($user);
  157. \OC_Util::setupFS($user);
  158. }
  159. /**
  160. * reset init status for the share storage
  161. */
  162. protected static function resetStorage() {
  163. $storage = new \ReflectionClass('\OCA\Files_Sharing\SharedStorage');
  164. $isInitialized = $storage->getProperty('initialized');
  165. $isInitialized->setAccessible(true);
  166. $isInitialized->setValue($storage, false);
  167. $isInitialized->setAccessible(false);
  168. }
  169. /**
  170. * get some information from a given share
  171. * @param int $shareID
  172. * @return array with: item_source, share_type, share_with, item_type, permissions
  173. */
  174. protected function getShareFromId($shareID) {
  175. $sql = 'SELECT `item_source`, `share_type`, `share_with`, `item_type`, `permissions` FROM `*PREFIX*share` WHERE `id` = ?';
  176. $args = array($shareID);
  177. $query = \OCP\DB::prepare($sql);
  178. $result = $query->execute($args);
  179. $share = Null;
  180. if ($result) {
  181. $share = $result->fetchRow();
  182. }
  183. return $share;
  184. }
  185. /**
  186. * @param int $type The share type
  187. * @param string $path The path to share relative to $initiators root
  188. * @param string $initiator
  189. * @param string $recipient
  190. * @param int $permissions
  191. * @return \OCP\Share\IShare
  192. */
  193. protected function share($type, $path, $initiator, $recipient, $permissions) {
  194. $userFolder = $this->rootFolder->getUserFolder($initiator);
  195. $node = $userFolder->get($path);
  196. $share = $this->shareManager->newShare();
  197. $share->setShareType($type)
  198. ->setSharedWith($recipient)
  199. ->setSharedBy($initiator)
  200. ->setNode($node)
  201. ->setPermissions($permissions);
  202. $share = $this->shareManager->createShare($share);
  203. return $share;
  204. }
  205. }