RemoveRootSharesTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * @author Roeland Jago Douma <rullzer@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\Repair;
  22. use OC\Repair\RemoveRootShares;
  23. use OCP\Files\IRootFolder;
  24. use OCP\IDBConnection;
  25. use OCP\IUserManager;
  26. use OCP\Migration\IOutput;
  27. use Test\Traits\UserTrait;
  28. /**
  29. * Class RemoveOldSharesTest
  30. *
  31. * @package Test\Repair
  32. * @group DB
  33. */
  34. class RemoveRootSharesTest extends \Test\TestCase {
  35. use UserTrait;
  36. /** @var RemoveRootShares */
  37. protected $repair;
  38. /** @var IDBConnection */
  39. protected $connection;
  40. /** @var IOutput */
  41. private $outputMock;
  42. /** @var IUserManager */
  43. private $userManager;
  44. /** @var IRootFolder */
  45. private $rootFolder;
  46. protected function setUp() {
  47. parent::setUp();
  48. $this->outputMock = $this->getMockBuilder('\OCP\Migration\IOutput')
  49. ->disableOriginalConstructor()
  50. ->getMock();
  51. $this->userManager = \OC::$server->getUserManager();
  52. $this->rootFolder = \OC::$server->getRootFolder();
  53. $this->connection = \OC::$server->getDatabaseConnection();
  54. $this->repair = new RemoveRootShares($this->connection, $this->userManager, $this->rootFolder);
  55. }
  56. protected function tearDown() {
  57. $qb = $this->connection->getQueryBuilder();
  58. $qb->delete('share');
  59. $qb->execute();
  60. return parent::tearDown();
  61. }
  62. public function testRootSharesExist() {
  63. //Add test user
  64. $user = $this->userManager->createUser('test', 'test');
  65. $userFolder = $this->rootFolder->getUserFolder('test');
  66. $fileId = $userFolder->getId();
  67. //Now insert cyclic share
  68. $qb = $this->connection->getQueryBuilder();
  69. $qb->insert('share')
  70. ->values([
  71. 'share_type' => $qb->createNamedParameter(0),
  72. 'share_with' => $qb->createNamedParameter('foo'),
  73. 'uid_owner' => $qb->createNamedParameter('owner'),
  74. 'item_type' => $qb->createNamedParameter('file'),
  75. 'item_source' => $qb->createNamedParameter($fileId),
  76. 'item_target' => $qb->createNamedParameter('/target'),
  77. 'file_source' => $qb->createNamedParameter($fileId),
  78. 'file_target' => $qb->createNamedParameter('/target'),
  79. 'permissions' => $qb->createNamedParameter(1),
  80. ]);
  81. $qb->execute();
  82. $res = $this->invokePrivate($this->repair, 'rootSharesExist', []);
  83. $this->assertTrue($res);
  84. $user->delete();
  85. }
  86. public function testRootSharesDontExist() {
  87. //Add test user
  88. $user = $this->userManager->createUser('test', 'test');
  89. $userFolder = $this->rootFolder->getUserFolder('test');
  90. $fileId = $userFolder->getId();
  91. $user->updateLastLoginTimestamp();
  92. //Now insert cyclic share
  93. $qb = $this->connection->getQueryBuilder();
  94. $qb->insert('share')
  95. ->values([
  96. 'share_type' => $qb->createNamedParameter(0),
  97. 'share_with' => $qb->createNamedParameter('foo'),
  98. 'uid_owner' => $qb->createNamedParameter('owner'),
  99. 'item_type' => $qb->createNamedParameter('file'),
  100. 'item_source' => $qb->createNamedParameter($fileId+1),
  101. 'item_target' => $qb->createNamedParameter('/target'),
  102. 'file_source' => $qb->createNamedParameter($fileId+1),
  103. 'file_target' => $qb->createNamedParameter('/target'),
  104. 'permissions' => $qb->createNamedParameter(1),
  105. ]);
  106. $qb->execute();
  107. $res = $this->invokePrivate($this->repair, 'rootSharesExist', []);
  108. $this->assertFalse($res);
  109. $user->delete();
  110. }
  111. public function testRun() {
  112. //Add test user
  113. $user1 = $this->userManager->createUser('test1', 'test1');
  114. $userFolder = $this->rootFolder->getUserFolder('test1');
  115. $fileId = $userFolder->getId();
  116. $user1->updateLastLoginTimestamp();
  117. //Now insert cyclic share
  118. $qb = $this->connection->getQueryBuilder();
  119. $qb->insert('share')
  120. ->values([
  121. 'share_type' => $qb->createNamedParameter(0),
  122. 'share_with' => $qb->createNamedParameter('foo'),
  123. 'uid_owner' => $qb->createNamedParameter('owner'),
  124. 'item_type' => $qb->createNamedParameter('file'),
  125. 'item_source' => $qb->createNamedParameter($fileId),
  126. 'item_target' => $qb->createNamedParameter('/target'),
  127. 'file_source' => $qb->createNamedParameter($fileId),
  128. 'file_target' => $qb->createNamedParameter('/target'),
  129. 'permissions' => $qb->createNamedParameter(1),
  130. ]);
  131. $qb->execute();
  132. //Add test user
  133. $user2 = $this->userManager->createUser('test2', 'test2');
  134. $userFolder = $this->rootFolder->getUserFolder('test2');
  135. $folder = $userFolder->newFolder('foo');
  136. $fileId = $folder->getId();
  137. $user2->updateLastLoginTimestamp();
  138. //Now insert cyclic share
  139. $qb = $this->connection->getQueryBuilder();
  140. $qb->insert('share')
  141. ->values([
  142. 'share_type' => $qb->createNamedParameter(0),
  143. 'share_with' => $qb->createNamedParameter('foo'),
  144. 'uid_owner' => $qb->createNamedParameter('owner'),
  145. 'item_type' => $qb->createNamedParameter('file'),
  146. 'item_source' => $qb->createNamedParameter($fileId),
  147. 'item_target' => $qb->createNamedParameter('/target'),
  148. 'file_source' => $qb->createNamedParameter($fileId),
  149. 'file_target' => $qb->createNamedParameter('/target'),
  150. 'permissions' => $qb->createNamedParameter(1),
  151. ]);
  152. $qb->execute();
  153. $this->repair->run($this->outputMock);
  154. //Verify
  155. $qb = $this->connection->getQueryBuilder();
  156. $qb->selectAlias($qb->createFunction('COUNT(*)'), 'count')
  157. ->from('share');
  158. $cursor = $qb->execute();
  159. $data = $cursor->fetch();
  160. $cursor->closeCursor();
  161. $count = (int)$data['count'];
  162. $this->assertEquals(1, $count);
  163. $user1->delete();
  164. $user2->delete();
  165. }
  166. }