TestCase.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\FederatedFileSharing\Tests;
  27. use OC\Files\Filesystem;
  28. use OC\Group\Database;
  29. /**
  30. * Class Test_Files_Sharing_Base
  31. *
  32. * @group DB
  33. *
  34. * Base class for sharing tests.
  35. */
  36. abstract class TestCase extends \Test\TestCase {
  37. public const TEST_FILES_SHARING_API_USER1 = "test-share-user1";
  38. public const TEST_FILES_SHARING_API_USER2 = "test-share-user2";
  39. public static function setUpBeforeClass(): void {
  40. parent::setUpBeforeClass();
  41. // reset backend
  42. \OC_User::clearBackends();
  43. \OC::$server->getGroupManager()->clearBackends();
  44. // create users
  45. $backend = new \Test\Util\User\Dummy();
  46. \OC_User::useBackend($backend);
  47. $backend->createUser(self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER1);
  48. $backend->createUser(self::TEST_FILES_SHARING_API_USER2, self::TEST_FILES_SHARING_API_USER2);
  49. }
  50. protected function setUp(): void {
  51. parent::setUp();
  52. //login as user1
  53. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  54. }
  55. public static function tearDownAfterClass(): void {
  56. // cleanup users
  57. $user = \OC::$server->getUserManager()->get(self::TEST_FILES_SHARING_API_USER1);
  58. if ($user !== null) {
  59. $user->delete();
  60. }
  61. $user = \OC::$server->getUserManager()->get(self::TEST_FILES_SHARING_API_USER2);
  62. if ($user !== null) {
  63. $user->delete();
  64. }
  65. \OC_Util::tearDownFS();
  66. \OC_User::setUserId('');
  67. Filesystem::tearDown();
  68. // reset backend
  69. \OC_User::clearBackends();
  70. \OC_User::useBackend('database');
  71. \OC::$server->getGroupManager()->clearBackends();
  72. \OC::$server->getGroupManager()->addBackend(new Database());
  73. parent::tearDownAfterClass();
  74. }
  75. /**
  76. * @param string $user
  77. * @param bool $create
  78. * @param bool $password
  79. */
  80. protected static function loginHelper($user, $create = false, $password = false) {
  81. if ($password === false) {
  82. $password = $user;
  83. }
  84. if ($create) {
  85. $userManager = \OC::$server->getUserManager();
  86. $groupManager = \OC::$server->getGroupManager();
  87. $userObject = $userManager->createUser($user, $password);
  88. $group = $groupManager->createGroup('group');
  89. if ($group and $userObject) {
  90. $group->addUser($userObject);
  91. }
  92. }
  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. }