TestCase.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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 DOMDocument;
  24. use DOMNode;
  25. use OC\Command\QueueBus;
  26. use OC\Files\Config\MountProviderCollection;
  27. use OC\Files\Filesystem;
  28. use OC\Files\Mount\CacheMountProvider;
  29. use OC\Files\Mount\LocalHomeMountProvider;
  30. use OC\Files\Mount\RootMountProvider;
  31. use OC\Files\SetupManager;
  32. use OC\Template\Base;
  33. use OCP\Command\IBus;
  34. use OCP\DB\QueryBuilder\IQueryBuilder;
  35. use OCP\Defaults;
  36. use OCP\IConfig;
  37. use OCP\IDBConnection;
  38. use OCP\IL10N;
  39. use OCP\Security\ISecureRandom;
  40. use Psr\Log\LoggerInterface;
  41. if (version_compare(\PHPUnit\Runner\Version::id(), 10, '>=')) {
  42. trait OnNotSuccessfulTestTrait {
  43. protected function onNotSuccessfulTest(\Throwable $t): never {
  44. $this->restoreAllServices();
  45. // restore database connection
  46. if (!$this->IsDatabaseAccessAllowed()) {
  47. \OC::$server->registerService(IDBConnection::class, function () {
  48. return self::$realDatabase;
  49. });
  50. }
  51. parent::onNotSuccessfulTest($t);
  52. }
  53. }
  54. } else {
  55. trait OnNotSuccessfulTestTrait {
  56. protected function onNotSuccessfulTest(\Throwable $t): void {
  57. $this->restoreAllServices();
  58. // restore database connection
  59. if (!$this->IsDatabaseAccessAllowed()) {
  60. \OC::$server->registerService(IDBConnection::class, function () {
  61. return self::$realDatabase;
  62. });
  63. }
  64. parent::onNotSuccessfulTest($t);
  65. }
  66. }
  67. }
  68. abstract class TestCase extends \PHPUnit\Framework\TestCase {
  69. /** @var \OC\Command\QueueBus */
  70. private $commandBus;
  71. /** @var IDBConnection */
  72. protected static $realDatabase = null;
  73. /** @var bool */
  74. private static $wasDatabaseAllowed = false;
  75. /** @var array */
  76. protected $services = [];
  77. use OnNotSuccessfulTestTrait;
  78. /**
  79. * @param string $name
  80. * @param mixed $newService
  81. * @return bool
  82. */
  83. public function overwriteService(string $name, $newService): bool {
  84. if (isset($this->services[$name])) {
  85. return false;
  86. }
  87. $this->services[$name] = \OC::$server->query($name);
  88. $container = \OC::$server->getAppContainerForService($name);
  89. $container = $container ?? \OC::$server;
  90. $container->registerService($name, function () use ($newService) {
  91. return $newService;
  92. });
  93. return true;
  94. }
  95. /**
  96. * @param string $name
  97. * @return bool
  98. */
  99. public function restoreService(string $name): bool {
  100. if (isset($this->services[$name])) {
  101. $oldService = $this->services[$name];
  102. $container = \OC::$server->getAppContainerForService($name);
  103. $container = $container ?? \OC::$server;
  104. $container->registerService($name, function () use ($oldService) {
  105. return $oldService;
  106. });
  107. unset($this->services[$name]);
  108. return true;
  109. }
  110. return false;
  111. }
  112. public function restoreAllServices() {
  113. if (!empty($this->services)) {
  114. if (!empty($this->services)) {
  115. foreach ($this->services as $name => $service) {
  116. $this->restoreService($name);
  117. }
  118. }
  119. }
  120. }
  121. protected function getTestTraits() {
  122. $traits = [];
  123. $class = $this;
  124. do {
  125. $traits = array_merge(class_uses($class), $traits);
  126. } while ($class = get_parent_class($class));
  127. foreach ($traits as $trait => $same) {
  128. $traits = array_merge(class_uses($trait), $traits);
  129. }
  130. $traits = array_unique($traits);
  131. return array_filter($traits, function ($trait) {
  132. return substr($trait, 0, 5) === 'Test\\';
  133. });
  134. }
  135. protected function setUp(): void {
  136. // overwrite the command bus with one we can run ourselves
  137. $this->commandBus = new QueueBus();
  138. $this->overwriteService('AsyncCommandBus', $this->commandBus);
  139. $this->overwriteService(IBus::class, $this->commandBus);
  140. // detect database access
  141. self::$wasDatabaseAllowed = true;
  142. if (!$this->IsDatabaseAccessAllowed()) {
  143. self::$wasDatabaseAllowed = false;
  144. if (is_null(self::$realDatabase)) {
  145. self::$realDatabase = \OC::$server->getDatabaseConnection();
  146. }
  147. \OC::$server->registerService(IDBConnection::class, function () {
  148. $this->fail('Your test case is not allowed to access the database.');
  149. });
  150. }
  151. $traits = $this->getTestTraits();
  152. foreach ($traits as $trait) {
  153. $methodName = 'setUp' . basename(str_replace('\\', '/', $trait));
  154. if (method_exists($this, $methodName)) {
  155. call_user_func([$this, $methodName]);
  156. }
  157. }
  158. }
  159. protected function tearDown(): void {
  160. $this->restoreAllServices();
  161. // restore database connection
  162. if (!$this->IsDatabaseAccessAllowed()) {
  163. \OC::$server->registerService(IDBConnection::class, function () {
  164. return self::$realDatabase;
  165. });
  166. }
  167. // further cleanup
  168. $hookExceptions = \OC_Hook::$thrownExceptions;
  169. \OC_Hook::$thrownExceptions = [];
  170. \OC::$server->getLockingProvider()->releaseAll();
  171. if (!empty($hookExceptions)) {
  172. throw $hookExceptions[0];
  173. }
  174. // fail hard if xml errors have not been cleaned up
  175. $errors = libxml_get_errors();
  176. libxml_clear_errors();
  177. if (!empty($errors)) {
  178. self::assertEquals([], $errors, "There have been xml parsing errors");
  179. }
  180. if ($this->IsDatabaseAccessAllowed()) {
  181. \OC\Files\Cache\Storage::getGlobalCache()->clearCache();
  182. }
  183. // tearDown the traits
  184. $traits = $this->getTestTraits();
  185. foreach ($traits as $trait) {
  186. $methodName = 'tearDown' . basename(str_replace('\\', '/', $trait));
  187. if (method_exists($this, $methodName)) {
  188. call_user_func([$this, $methodName]);
  189. }
  190. }
  191. }
  192. /**
  193. * Allows us to test private methods/properties
  194. *
  195. * @param $object
  196. * @param $methodName
  197. * @param array $parameters
  198. * @return mixed
  199. */
  200. protected static function invokePrivate($object, $methodName, array $parameters = []) {
  201. if (is_string($object)) {
  202. $className = $object;
  203. } else {
  204. $className = get_class($object);
  205. }
  206. $reflection = new \ReflectionClass($className);
  207. if ($reflection->hasMethod($methodName)) {
  208. $method = $reflection->getMethod($methodName);
  209. $method->setAccessible(true);
  210. return $method->invokeArgs($object, $parameters);
  211. } elseif ($reflection->hasProperty($methodName)) {
  212. $property = $reflection->getProperty($methodName);
  213. $property->setAccessible(true);
  214. if (!empty($parameters)) {
  215. if ($property->isStatic()) {
  216. $property->setValue(null, array_pop($parameters));
  217. } else {
  218. $property->setValue($object, array_pop($parameters));
  219. }
  220. }
  221. if (is_object($object)) {
  222. return $property->getValue($object);
  223. }
  224. return $property->getValue();
  225. }
  226. return false;
  227. }
  228. /**
  229. * Returns a unique identifier as uniqid() is not reliable sometimes
  230. *
  231. * @param string $prefix
  232. * @param int $length
  233. * @return string
  234. */
  235. protected static function getUniqueID($prefix = '', $length = 13) {
  236. return $prefix . \OC::$server->getSecureRandom()->generate(
  237. $length,
  238. // Do not use dots and slashes as we use the value for file names
  239. ISecureRandom::CHAR_DIGITS . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER
  240. );
  241. }
  242. public static function tearDownAfterClass(): void {
  243. if (!self::$wasDatabaseAllowed && self::$realDatabase !== null) {
  244. // in case an error is thrown in a test, PHPUnit jumps straight to tearDownAfterClass,
  245. // so we need the database again
  246. \OC::$server->registerService(IDBConnection::class, function () {
  247. return self::$realDatabase;
  248. });
  249. }
  250. $dataDir = \OC::$server->getConfig()->getSystemValueString('datadirectory', \OC::$SERVERROOT . '/data-autotest');
  251. if (self::$wasDatabaseAllowed && \OC::$server->getDatabaseConnection()) {
  252. $db = \OC::$server->getDatabaseConnection();
  253. if ($db->inTransaction()) {
  254. $db->rollBack();
  255. throw new \Exception('There was a transaction still in progress and needed to be rolled back. Please fix this in your test.');
  256. }
  257. $queryBuilder = $db->getQueryBuilder();
  258. self::tearDownAfterClassCleanShares($queryBuilder);
  259. self::tearDownAfterClassCleanStorages($queryBuilder);
  260. self::tearDownAfterClassCleanFileCache($queryBuilder);
  261. }
  262. self::tearDownAfterClassCleanStrayDataFiles($dataDir);
  263. self::tearDownAfterClassCleanStrayHooks();
  264. self::tearDownAfterClassCleanStrayLocks();
  265. /** @var SetupManager $setupManager */
  266. $setupManager = \OC::$server->get(SetupManager::class);
  267. $setupManager->tearDown();
  268. /** @var MountProviderCollection $mountProviderCollection */
  269. $mountProviderCollection = \OC::$server->get(MountProviderCollection::class);
  270. $mountProviderCollection->clearProviders();
  271. /** @var IConfig $config */
  272. $config = \OC::$server->get(IConfig::class);
  273. $mountProviderCollection->registerProvider(new CacheMountProvider($config));
  274. $mountProviderCollection->registerHomeProvider(new LocalHomeMountProvider());
  275. $mountProviderCollection->registerRootProvider(new RootMountProvider($config, \OC::$server->get(LoggerInterface::class)));
  276. $setupManager->setupRoot();
  277. parent::tearDownAfterClass();
  278. }
  279. /**
  280. * Remove all entries from the share table
  281. *
  282. * @param IQueryBuilder $queryBuilder
  283. */
  284. protected static function tearDownAfterClassCleanShares(IQueryBuilder $queryBuilder) {
  285. $queryBuilder->delete('share')
  286. ->execute();
  287. }
  288. /**
  289. * Remove all entries from the storages table
  290. *
  291. * @param IQueryBuilder $queryBuilder
  292. */
  293. protected static function tearDownAfterClassCleanStorages(IQueryBuilder $queryBuilder) {
  294. $queryBuilder->delete('storages')
  295. ->execute();
  296. }
  297. /**
  298. * Remove all entries from the filecache table
  299. *
  300. * @param IQueryBuilder $queryBuilder
  301. */
  302. protected static function tearDownAfterClassCleanFileCache(IQueryBuilder $queryBuilder) {
  303. $queryBuilder->delete('filecache')
  304. ->execute();
  305. }
  306. /**
  307. * Remove all unused files from the data dir
  308. *
  309. * @param string $dataDir
  310. */
  311. protected static function tearDownAfterClassCleanStrayDataFiles($dataDir) {
  312. $knownEntries = [
  313. 'nextcloud.log' => true,
  314. 'audit.log' => true,
  315. 'owncloud.db' => true,
  316. '.ocdata' => true,
  317. '..' => true,
  318. '.' => true,
  319. ];
  320. if ($dh = opendir($dataDir)) {
  321. while (($file = readdir($dh)) !== false) {
  322. if (!isset($knownEntries[$file])) {
  323. self::tearDownAfterClassCleanStrayDataUnlinkDir($dataDir . '/' . $file);
  324. }
  325. }
  326. closedir($dh);
  327. }
  328. }
  329. /**
  330. * Recursive delete files and folders from a given directory
  331. *
  332. * @param string $dir
  333. */
  334. protected static function tearDownAfterClassCleanStrayDataUnlinkDir($dir) {
  335. if ($dh = @opendir($dir)) {
  336. while (($file = readdir($dh)) !== false) {
  337. if (\OC\Files\Filesystem::isIgnoredDir($file)) {
  338. continue;
  339. }
  340. $path = $dir . '/' . $file;
  341. if (is_dir($path)) {
  342. self::tearDownAfterClassCleanStrayDataUnlinkDir($path);
  343. } else {
  344. @unlink($path);
  345. }
  346. }
  347. closedir($dh);
  348. }
  349. @rmdir($dir);
  350. }
  351. /**
  352. * Clean up the list of hooks
  353. */
  354. protected static function tearDownAfterClassCleanStrayHooks() {
  355. \OC_Hook::clear();
  356. }
  357. /**
  358. * Clean up the list of locks
  359. */
  360. protected static function tearDownAfterClassCleanStrayLocks() {
  361. \OC::$server->getLockingProvider()->releaseAll();
  362. }
  363. /**
  364. * Login and setup FS as a given user,
  365. * sets the given user as the current user.
  366. *
  367. * @param string $user user id or empty for a generic FS
  368. */
  369. protected static function loginAsUser($user = '') {
  370. self::logout();
  371. \OC\Files\Filesystem::tearDown();
  372. \OC_User::setUserId($user);
  373. $userObject = \OC::$server->getUserManager()->get($user);
  374. if (!is_null($userObject)) {
  375. $userObject->updateLastLoginTimestamp();
  376. }
  377. \OC_Util::setupFS($user);
  378. if (\OC::$server->getUserManager()->userExists($user)) {
  379. \OC::$server->getUserFolder($user);
  380. }
  381. }
  382. /**
  383. * Logout the current user and tear down the filesystem.
  384. */
  385. protected static function logout() {
  386. \OC_Util::tearDownFS();
  387. \OC_User::setUserId('');
  388. // needed for fully logout
  389. \OC::$server->getUserSession()->setUser(null);
  390. }
  391. /**
  392. * Run all commands pushed to the bus
  393. */
  394. protected function runCommands() {
  395. // get the user for which the fs is setup
  396. $view = Filesystem::getView();
  397. if ($view) {
  398. [, $user] = explode('/', $view->getRoot());
  399. } else {
  400. $user = null;
  401. }
  402. \OC_Util::tearDownFS(); // command can't reply on the fs being setup
  403. $this->commandBus->run();
  404. \OC_Util::tearDownFS();
  405. if ($user) {
  406. \OC_Util::setupFS($user);
  407. }
  408. }
  409. /**
  410. * Check if the given path is locked with a given type
  411. *
  412. * @param \OC\Files\View $view view
  413. * @param string $path path to check
  414. * @param int $type lock type
  415. * @param bool $onMountPoint true to check the mount point instead of the
  416. * mounted storage
  417. *
  418. * @return boolean true if the file is locked with the
  419. * given type, false otherwise
  420. */
  421. protected function isFileLocked($view, $path, $type, $onMountPoint = false) {
  422. // Note: this seems convoluted but is necessary because
  423. // the format of the lock key depends on the storage implementation
  424. // (in our case mostly md5)
  425. if ($type === \OCP\Lock\ILockingProvider::LOCK_SHARED) {
  426. // to check if the file has a shared lock, try acquiring an exclusive lock
  427. $checkType = \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE;
  428. } else {
  429. // a shared lock cannot be set if exclusive lock is in place
  430. $checkType = \OCP\Lock\ILockingProvider::LOCK_SHARED;
  431. }
  432. try {
  433. $view->lockFile($path, $checkType, $onMountPoint);
  434. // no exception, which means the lock of $type is not set
  435. // clean up
  436. $view->unlockFile($path, $checkType, $onMountPoint);
  437. return false;
  438. } catch (\OCP\Lock\LockedException $e) {
  439. // we could not acquire the counter-lock, which means
  440. // the lock of $type was in place
  441. return true;
  442. }
  443. }
  444. protected function getGroupAnnotations(): array {
  445. if (method_exists($this, 'getAnnotations')) {
  446. $annotations = $this->getAnnotations();
  447. return $annotations['class']['group'] ?? [];
  448. }
  449. $r = new \ReflectionClass($this);
  450. $doc = $r->getDocComment();
  451. preg_match_all('#@group\s+(.*?)\n#s', $doc, $annotations);
  452. return $annotations[1] ?? [];
  453. }
  454. protected function IsDatabaseAccessAllowed() {
  455. // on travis-ci.org we allow database access in any case - otherwise
  456. // this will break all apps right away
  457. if (true == getenv('TRAVIS')) {
  458. return true;
  459. }
  460. $annotations = $this->getGroupAnnotations();
  461. if (isset($annotations)) {
  462. if (in_array('DB', $annotations) || in_array('SLOWDB', $annotations)) {
  463. return true;
  464. }
  465. }
  466. return false;
  467. }
  468. /**
  469. * @param string $expectedHtml
  470. * @param string $template
  471. * @param array $vars
  472. */
  473. protected function assertTemplate($expectedHtml, $template, $vars = []) {
  474. require_once __DIR__.'/../../lib/private/legacy/template/functions.php';
  475. $requestToken = 12345;
  476. /** @var Defaults|\PHPUnit\Framework\MockObject\MockObject $l10n */
  477. $theme = $this->getMockBuilder('\OCP\Defaults')
  478. ->disableOriginalConstructor()->getMock();
  479. $theme->expects($this->any())
  480. ->method('getName')
  481. ->willReturn('Nextcloud');
  482. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject $l10n */
  483. $l10n = $this->getMockBuilder(IL10N::class)
  484. ->disableOriginalConstructor()->getMock();
  485. $l10n
  486. ->expects($this->any())
  487. ->method('t')
  488. ->willReturnCallback(function ($text, $parameters = []) {
  489. return vsprintf($text, $parameters);
  490. });
  491. $t = new Base($template, $requestToken, $l10n, $theme);
  492. $buf = $t->fetchPage($vars);
  493. $this->assertHtmlStringEqualsHtmlString($expectedHtml, $buf);
  494. }
  495. /**
  496. * @param string $expectedHtml
  497. * @param string $actualHtml
  498. * @param string $message
  499. */
  500. protected function assertHtmlStringEqualsHtmlString($expectedHtml, $actualHtml, $message = '') {
  501. $expected = new DOMDocument();
  502. $expected->preserveWhiteSpace = false;
  503. $expected->formatOutput = true;
  504. $expected->loadHTML($expectedHtml);
  505. $actual = new DOMDocument();
  506. $actual->preserveWhiteSpace = false;
  507. $actual->formatOutput = true;
  508. $actual->loadHTML($actualHtml);
  509. $this->removeWhitespaces($actual);
  510. $expectedHtml1 = $expected->saveHTML();
  511. $actualHtml1 = $actual->saveHTML();
  512. self::assertEquals($expectedHtml1, $actualHtml1, $message);
  513. }
  514. private function removeWhitespaces(DOMNode $domNode) {
  515. foreach ($domNode->childNodes as $node) {
  516. if ($node->hasChildNodes()) {
  517. $this->removeWhitespaces($node);
  518. } else {
  519. if ($node instanceof \DOMText && $node->isWhitespaceInElementContent()) {
  520. $domNode->removeChild($node);
  521. }
  522. }
  523. }
  524. }
  525. }