TestCase.php 3.6 KB

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