TestCase.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 Morris Jobke <hey@morrisjobke.de>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\FederatedFileSharing\Tests;
  25. use OC\Files\Filesystem;
  26. use OC\Group\Database;
  27. /**
  28. * Class Test_Files_Sharing_Base
  29. *
  30. * @group DB
  31. *
  32. * Base class for sharing tests.
  33. */
  34. abstract class TestCase extends \Test\TestCase {
  35. const TEST_FILES_SHARING_API_USER1 = "test-share-user1";
  36. const TEST_FILES_SHARING_API_USER2 = "test-share-user2";
  37. public static function setUpBeforeClass() {
  38. parent::setUpBeforeClass();
  39. // reset backend
  40. \OC_User::clearBackends();
  41. \OC::$server->getGroupManager()->clearBackends();
  42. // create users
  43. $backend = new \Test\Util\User\Dummy();
  44. \OC_User::useBackend($backend);
  45. $backend->createUser(self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER1);
  46. $backend->createUser(self::TEST_FILES_SHARING_API_USER2, self::TEST_FILES_SHARING_API_USER2);
  47. }
  48. protected function setUp() {
  49. parent::setUp();
  50. //login as user1
  51. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  52. }
  53. public static function tearDownAfterClass() {
  54. // cleanup users
  55. $user = \OC::$server->getUserManager()->get(self::TEST_FILES_SHARING_API_USER1);
  56. if ($user !== null) {
  57. $user->delete();
  58. }
  59. $user = \OC::$server->getUserManager()->get(self::TEST_FILES_SHARING_API_USER2);
  60. if ($user !== null) {
  61. $user->delete();
  62. }
  63. \OC_Util::tearDownFS();
  64. \OC_User::setUserId('');
  65. Filesystem::tearDown();
  66. // reset backend
  67. \OC_User::clearBackends();
  68. \OC_User::useBackend('database');
  69. \OC::$server->getGroupManager()->clearBackends();
  70. \OC::$server->getGroupManager()->addBackend(new Database());
  71. parent::tearDownAfterClass();
  72. }
  73. /**
  74. * @param string $user
  75. * @param bool $create
  76. * @param bool $password
  77. */
  78. protected static function loginHelper($user, $create = false, $password = false) {
  79. if ($password === false) {
  80. $password = $user;
  81. }
  82. if ($create) {
  83. $userManager = \OC::$server->getUserManager();
  84. $groupManager = \OC::$server->getGroupManager();
  85. $userObject = $userManager->createUser($user, $password);
  86. $group = $groupManager->createGroup('group');
  87. if ($group and $userObject) {
  88. $group->addUser($userObject);
  89. }
  90. }
  91. self::resetStorage();
  92. \OC_Util::tearDownFS();
  93. \OC::$server->getUserSession()->setUser(null);
  94. \OC\Files\Filesystem::tearDown();
  95. \OC::$server->getUserSession()->login($user, $password);
  96. \OC::$server->getUserFolder($user);
  97. \OC_Util::setupFS($user);
  98. }
  99. /**
  100. * reset init status for the share storage
  101. */
  102. protected static function resetStorage() {
  103. $storage = new \ReflectionClass('\OCA\Files_Sharing\SharedStorage');
  104. $isInitialized = $storage->getProperty('initialized');
  105. $isInitialized->setAccessible(true);
  106. $isInitialized->setValue($storage, false);
  107. $isInitialized->setAccessible(false);
  108. }
  109. }