testcleanuplistener.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Vincent Petry <pvince81@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. /**
  9. * Detects tests that didn't clean up properly, show a warning, then clean up after them.
  10. */
  11. class TestCleanupListener implements PHPUnit_Framework_TestListener {
  12. private $verbosity;
  13. public function __construct($verbosity = 'verbose') {
  14. $this->verbosity = $verbosity;
  15. }
  16. public function addError(PHPUnit_Framework_Test $test, Exception $e, $time) {
  17. }
  18. public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) {
  19. }
  20. public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time) {
  21. }
  22. public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time) {
  23. }
  24. public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time) {
  25. }
  26. public function startTest(PHPUnit_Framework_Test $test) {
  27. }
  28. public function endTest(PHPUnit_Framework_Test $test, $time) {
  29. }
  30. public function startTestSuite(PHPUnit_Framework_TestSuite $suite) {
  31. }
  32. public function endTestSuite(PHPUnit_Framework_TestSuite $suite) {
  33. if ($this->cleanStorages() && $this->isShowSuiteWarning()) {
  34. printf("TestSuite '%s': Did not clean up storages\n", $suite->getName());
  35. }
  36. if ($this->cleanFileCache() && $this->isShowSuiteWarning()) {
  37. printf("TestSuite '%s': Did not clean up file cache\n", $suite->getName());
  38. }
  39. if ($this->cleanStrayDataFiles() && $this->isShowSuiteWarning()) {
  40. printf("TestSuite '%s': Did not clean up data dir\n", $suite->getName());
  41. }
  42. if ($this->cleanStrayHooks() && $this->isShowSuiteWarning()) {
  43. printf("TestSuite '%s': Did not clean up hooks\n", $suite->getName());
  44. }
  45. if ($this->cleanProxies() && $this->isShowSuiteWarning()) {
  46. printf("TestSuite '%s': Did not clean up proxies\n", $suite->getName());
  47. }
  48. }
  49. private function isShowSuiteWarning() {
  50. return $this->verbosity === 'suite' || $this->verbosity === 'detail';
  51. }
  52. private function isShowDetail() {
  53. return $this->verbosity === 'detail';
  54. }
  55. /**
  56. * @param string $dir
  57. */
  58. private function unlinkDir($dir) {
  59. if ($dh = @opendir($dir)) {
  60. while (($file = readdir($dh)) !== false) {
  61. if ($file === '..' || $file === '.') {
  62. continue;
  63. }
  64. $path = $dir . '/' . $file;
  65. if (is_dir($path)) {
  66. $this->unlinkDir($path);
  67. }
  68. else {
  69. @unlink($path);
  70. }
  71. }
  72. closedir($dh);
  73. }
  74. @rmdir($dir);
  75. }
  76. private function cleanStrayDataFiles() {
  77. $knownEntries = array(
  78. 'owncloud.log' => true,
  79. 'owncloud.db' => true,
  80. '.ocdata' => true,
  81. '..' => true,
  82. '.' => true
  83. );
  84. $datadir = \OC_Config::getValue('datadirectory', \OC::$SERVERROOT . '/data');
  85. $entries = array();
  86. if ($dh = opendir($datadir)) {
  87. while (($file = readdir($dh)) !== false) {
  88. if (!isset($knownEntries[$file])) {
  89. $entries[] = $file;
  90. }
  91. }
  92. closedir($dh);
  93. }
  94. if (count($entries) > 0) {
  95. foreach ($entries as $entry) {
  96. $this->unlinkDir($datadir . '/' . $entry);
  97. if ($this->isShowDetail()) {
  98. printf("Stray datadir entry: %s\n", $entry);
  99. }
  100. }
  101. return true;
  102. }
  103. return false;
  104. }
  105. private function cleanStorages() {
  106. $sql = 'DELETE FROM `*PREFIX*storages`';
  107. $query = \OC_DB::prepare( $sql );
  108. $result = $query->execute();
  109. if ($result > 0) {
  110. return true;
  111. }
  112. return false;
  113. }
  114. private function cleanFileCache() {
  115. $sql = 'DELETE FROM `*PREFIX*filecache`';
  116. $query = \OC_DB::prepare( $sql );
  117. $result = $query->execute();
  118. if ($result > 0) {
  119. return true;
  120. }
  121. return false;
  122. }
  123. private function cleanStrayHooks() {
  124. $hasHooks = false;
  125. $hooks = OC_Hook::getHooks();
  126. if (!$hooks || sizeof($hooks) === 0) {
  127. return false;
  128. }
  129. foreach ($hooks as $signalClass => $signals) {
  130. if (sizeof($signals)) {
  131. foreach ($signals as $signalName => $handlers ) {
  132. if (sizeof($handlers) > 0) {
  133. $hasHooks = true;
  134. OC_Hook::clear($signalClass, $signalName);
  135. if ($this->isShowDetail()) {
  136. printf("Stray hook: \"%s\" \"%s\"\n", $signalClass, $signalName);
  137. }
  138. }
  139. }
  140. }
  141. }
  142. return $hasHooks;
  143. }
  144. private function cleanProxies() {
  145. $proxies = OC_FileProxy::getProxies();
  146. OC_FileProxy::clearProxies();
  147. return count($proxies) > 0;
  148. }
  149. }