DeleteOrphanedSharesJobTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Thomas Müller <thomas.mueller@tmit.eu>
  7. * @author Vincent Petry <pvince81@owncloud.com>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Files_Sharing\Tests;
  25. use OCA\Files_Sharing\DeleteOrphanedSharesJob;
  26. /**
  27. * Class DeleteOrphanedSharesJobTest
  28. *
  29. * @group DB
  30. *
  31. * @package OCA\Files_Sharing\Tests
  32. */
  33. class DeleteOrphanedSharesJobTest extends \Test\TestCase {
  34. /**
  35. * @var bool
  36. */
  37. private static $trashBinStatus;
  38. /**
  39. * @var DeleteOrphanedSharesJob
  40. */
  41. private $job;
  42. /**
  43. * @var \OCP\IDBConnection
  44. */
  45. private $connection;
  46. /**
  47. * @var string
  48. */
  49. private $user1;
  50. /**
  51. * @var string
  52. */
  53. private $user2;
  54. public static function setUpBeforeClass() {
  55. $appManager = \OC::$server->getAppManager();
  56. self::$trashBinStatus = $appManager->isEnabledForUser('files_trashbin');
  57. $appManager->disableApp('files_trashbin');
  58. // just in case...
  59. \OC\Files\Filesystem::getLoader()->removeStorageWrapper('oc_trashbin');
  60. }
  61. public static function tearDownAfterClass() {
  62. if (self::$trashBinStatus) {
  63. \OC::$server->getAppManager()->enableApp('files_trashbin');
  64. }
  65. }
  66. protected function setup() {
  67. parent::setUp();
  68. $this->connection = \OC::$server->getDatabaseConnection();
  69. // clear occasional leftover shares from other tests
  70. $this->connection->executeUpdate('DELETE FROM `*PREFIX*share`');
  71. $this->user1 = $this->getUniqueID('user1_');
  72. $this->user2 = $this->getUniqueID('user2_');
  73. $userManager = \OC::$server->getUserManager();
  74. $userManager->createUser($this->user1, 'pass');
  75. $userManager->createUser($this->user2, 'pass');
  76. \OC::registerShareHooks();
  77. $this->job = new DeleteOrphanedSharesJob();
  78. }
  79. protected function tearDown() {
  80. $this->connection->executeUpdate('DELETE FROM `*PREFIX*share`');
  81. $userManager = \OC::$server->getUserManager();
  82. $user1 = $userManager->get($this->user1);
  83. if($user1) {
  84. $user1->delete();
  85. }
  86. $user2 = $userManager->get($this->user2);
  87. if($user2) {
  88. $user2->delete();
  89. }
  90. $this->logout();
  91. parent::tearDown();
  92. }
  93. private function getShares() {
  94. $shares = [];
  95. $result = $this->connection->executeQuery('SELECT * FROM `*PREFIX*share`');
  96. while ($row = $result->fetch()) {
  97. $shares[] = $row;
  98. }
  99. $result->closeCursor();
  100. return $shares;
  101. }
  102. /**
  103. * Test clearing orphaned shares
  104. */
  105. public function testClearShares() {
  106. $this->loginAsUser($this->user1);
  107. $view = new \OC\Files\View('/' . $this->user1 . '/');
  108. $view->mkdir('files/test');
  109. $view->mkdir('files/test/sub');
  110. $fileInfo = $view->getFileInfo('files/test/sub');
  111. $fileId = $fileInfo->getId();
  112. $this->assertTrue(
  113. \OCP\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ),
  114. 'Failed asserting that user 1 successfully shared "test/sub" with user 2.'
  115. );
  116. $this->assertCount(1, $this->getShares());
  117. $this->job->run([]);
  118. $this->assertCount(1, $this->getShares(), 'Linked shares not deleted');
  119. $view->unlink('files/test');
  120. $this->job->run([]);
  121. $this->assertCount(0, $this->getShares(), 'Orphaned shares deleted');
  122. }
  123. public function testKeepNonFileShares() {
  124. $this->loginAsUser($this->user1);
  125. \OCP\Share::registerBackend('test', 'Test\Share\Backend');
  126. $this->assertTrue(
  127. \OCP\Share::shareItem('test', 'test.txt', \OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ),
  128. 'Failed asserting that user 1 successfully shared something with user 2.'
  129. );
  130. $this->assertCount(1, $this->getShares());
  131. $this->job->run([]);
  132. $this->assertCount(1, $this->getShares(), 'Non-file shares kept');
  133. }
  134. }