1
0

testcase.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Joas Schilling
  6. * @copyright 2014 Joas Schilling nickvergessen@owncloud.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library 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
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test;
  23. use OC\Command\QueueBus;
  24. use OC\Files\Filesystem;
  25. use OCP\Security\ISecureRandom;
  26. abstract class TestCase extends \PHPUnit_Framework_TestCase {
  27. /**
  28. * @var \OC\Command\QueueBus
  29. */
  30. private $commandBus;
  31. protected function setUp() {
  32. // overwrite the command bus with one we can run ourselves
  33. $this->commandBus = new QueueBus();
  34. \OC::$server->registerService('AsyncCommandBus', function () {
  35. return $this->commandBus;
  36. });
  37. }
  38. protected function tearDown() {
  39. $hookExceptions = \OC_Hook::$thrownExceptions;
  40. \OC_Hook::$thrownExceptions = [];
  41. if(!empty($hookExceptions)) {
  42. throw $hookExceptions[0];
  43. }
  44. }
  45. /**
  46. * Returns a unique identifier as uniqid() is not reliable sometimes
  47. *
  48. * @param string $prefix
  49. * @param int $length
  50. * @return string
  51. */
  52. protected static function getUniqueID($prefix = '', $length = 13) {
  53. return $prefix . \OC::$server->getSecureRandom()->getLowStrengthGenerator()->generate(
  54. $length,
  55. // Do not use dots and slashes as we use the value for file names
  56. ISecureRandom::CHAR_DIGITS . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER
  57. );
  58. }
  59. public static function tearDownAfterClass() {
  60. $dataDir = \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data-autotest');
  61. self::tearDownAfterClassCleanFileMapper($dataDir);
  62. self::tearDownAfterClassCleanStorages();
  63. self::tearDownAfterClassCleanFileCache();
  64. self::tearDownAfterClassCleanStrayDataFiles($dataDir);
  65. self::tearDownAfterClassCleanStrayHooks();
  66. parent::tearDownAfterClass();
  67. }
  68. /**
  69. * Remove all entries from the files map table
  70. *
  71. * @param string $dataDir
  72. */
  73. static protected function tearDownAfterClassCleanFileMapper($dataDir) {
  74. if (\OC_Util::runningOnWindows()) {
  75. $mapper = new \OC\Files\Mapper($dataDir);
  76. $mapper->removePath($dataDir, true, true);
  77. }
  78. }
  79. /**
  80. * Remove all entries from the storages table
  81. *
  82. * @throws \OC\DatabaseException
  83. */
  84. static protected function tearDownAfterClassCleanStorages() {
  85. $sql = 'DELETE FROM `*PREFIX*storages`';
  86. $query = \OC_DB::prepare($sql);
  87. $query->execute();
  88. }
  89. /**
  90. * Remove all entries from the filecache table
  91. *
  92. * @throws \OC\DatabaseException
  93. */
  94. static protected function tearDownAfterClassCleanFileCache() {
  95. $sql = 'DELETE FROM `*PREFIX*filecache`';
  96. $query = \OC_DB::prepare($sql);
  97. $query->execute();
  98. }
  99. /**
  100. * Remove all unused files from the data dir
  101. *
  102. * @param string $dataDir
  103. */
  104. static protected function tearDownAfterClassCleanStrayDataFiles($dataDir) {
  105. $knownEntries = array(
  106. 'owncloud.log' => true,
  107. 'owncloud.db' => true,
  108. '.ocdata' => true,
  109. '..' => true,
  110. '.' => true,
  111. );
  112. if ($dh = opendir($dataDir)) {
  113. while (($file = readdir($dh)) !== false) {
  114. if (!isset($knownEntries[$file])) {
  115. self::tearDownAfterClassCleanStrayDataUnlinkDir($dataDir . '/' . $file);
  116. }
  117. }
  118. closedir($dh);
  119. }
  120. }
  121. /**
  122. * Recursive delete files and folders from a given directory
  123. *
  124. * @param string $dir
  125. */
  126. static protected function tearDownAfterClassCleanStrayDataUnlinkDir($dir) {
  127. if ($dh = @opendir($dir)) {
  128. while (($file = readdir($dh)) !== false) {
  129. if ($file === '..' || $file === '.') {
  130. continue;
  131. }
  132. $path = $dir . '/' . $file;
  133. if (is_dir($path)) {
  134. self::tearDownAfterClassCleanStrayDataUnlinkDir($path);
  135. } else {
  136. @unlink($path);
  137. }
  138. }
  139. closedir($dh);
  140. }
  141. @rmdir($dir);
  142. }
  143. /**
  144. * Clean up the list of hooks
  145. */
  146. static protected function tearDownAfterClassCleanStrayHooks() {
  147. \OC_Hook::clear();
  148. }
  149. /**
  150. * Login and setup FS as a given user,
  151. * sets the given user as the current user.
  152. *
  153. * @param string $user user id or empty for a generic FS
  154. */
  155. static protected function loginAsUser($user = '') {
  156. self::logout();
  157. \OC\Files\Filesystem::tearDown();
  158. \OC_User::setUserId($user);
  159. \OC_Util::setupFS($user);
  160. }
  161. /**
  162. * Logout the current user and tear down the filesystem.
  163. */
  164. static protected function logout() {
  165. \OC_Util::tearDownFS();
  166. \OC_User::setUserId('');
  167. }
  168. /**
  169. * Run all commands pushed to the bus
  170. */
  171. protected function runCommands() {
  172. // get the user for which the fs is setup
  173. $view = Filesystem::getView();
  174. if ($view) {
  175. list(, $user) = explode('/', $view->getRoot());
  176. } else {
  177. $user = null;
  178. }
  179. \OC_Util::tearDownFS(); // command cant reply on the fs being setup
  180. $this->commandBus->run();
  181. \OC_Util::tearDownFS();
  182. if ($user) {
  183. \OC_Util::setupFS($user);
  184. }
  185. }
  186. }