1
0

TestCase.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\FederatedFileSharing\Tests;
  8. use OC\Files\Filesystem;
  9. use OC\Group\Database;
  10. /**
  11. * Class Test_Files_Sharing_Base
  12. *
  13. * @group DB
  14. *
  15. * Base class for sharing tests.
  16. */
  17. abstract class TestCase extends \Test\TestCase {
  18. public const TEST_FILES_SHARING_API_USER1 = 'test-share-user1';
  19. public const TEST_FILES_SHARING_API_USER2 = 'test-share-user2';
  20. public static function setUpBeforeClass(): void {
  21. parent::setUpBeforeClass();
  22. // reset backend
  23. \OC_User::clearBackends();
  24. \OC::$server->getGroupManager()->clearBackends();
  25. // create users
  26. $backend = new \Test\Util\User\Dummy();
  27. \OC_User::useBackend($backend);
  28. $backend->createUser(self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER1);
  29. $backend->createUser(self::TEST_FILES_SHARING_API_USER2, self::TEST_FILES_SHARING_API_USER2);
  30. }
  31. protected function setUp(): void {
  32. parent::setUp();
  33. //login as user1
  34. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  35. }
  36. public static function tearDownAfterClass(): void {
  37. // cleanup users
  38. $user = \OC::$server->getUserManager()->get(self::TEST_FILES_SHARING_API_USER1);
  39. if ($user !== null) {
  40. $user->delete();
  41. }
  42. $user = \OC::$server->getUserManager()->get(self::TEST_FILES_SHARING_API_USER2);
  43. if ($user !== null) {
  44. $user->delete();
  45. }
  46. \OC_Util::tearDownFS();
  47. \OC_User::setUserId('');
  48. Filesystem::tearDown();
  49. // reset backend
  50. \OC_User::clearBackends();
  51. \OC_User::useBackend('database');
  52. \OC::$server->getGroupManager()->clearBackends();
  53. \OC::$server->getGroupManager()->addBackend(new Database());
  54. parent::tearDownAfterClass();
  55. }
  56. /**
  57. * @param string $user
  58. * @param bool $create
  59. * @param bool $password
  60. */
  61. protected static function loginHelper($user, $create = false, $password = false) {
  62. if ($password === false) {
  63. $password = $user;
  64. }
  65. if ($create) {
  66. $userManager = \OC::$server->getUserManager();
  67. $groupManager = \OC::$server->getGroupManager();
  68. $userObject = $userManager->createUser($user, $password);
  69. $group = $groupManager->createGroup('group');
  70. if ($group and $userObject) {
  71. $group->addUser($userObject);
  72. }
  73. }
  74. \OC_Util::tearDownFS();
  75. \OC::$server->getUserSession()->setUser(null);
  76. Filesystem::tearDown();
  77. \OC::$server->getUserSession()->login($user, $password);
  78. \OC::$server->getUserFolder($user);
  79. \OC_Util::setupFS($user);
  80. }
  81. }