TestCase.php 3.2 KB

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