repairinvalidsharestest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Copyright (c) 2015 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. namespace Test\Repair;
  9. use OC\Repair\RepairInvalidShares;
  10. use OC\Share\Constants;
  11. use Test\TestCase;
  12. /**
  13. * Tests for repairing invalid shares
  14. *
  15. * @group DB
  16. *
  17. * @see \OC\Repair\RepairInvalidShares
  18. */
  19. class RepairInvalidSharesTest extends TestCase {
  20. /** @var \OC\RepairStep */
  21. private $repair;
  22. /** @var \OCP\IDBConnection */
  23. private $connection;
  24. protected function setUp() {
  25. parent::setUp();
  26. $config = $this->getMockBuilder('OCP\IConfig')
  27. ->disableOriginalConstructor()
  28. ->getMock();
  29. $config->expects($this->any())
  30. ->method('getSystemValue')
  31. ->with('version')
  32. ->will($this->returnValue('8.0.0.0'));
  33. $this->connection = \OC::$server->getDatabaseConnection();
  34. $this->deleteAllShares();
  35. /** @var \OCP\IConfig $config */
  36. $this->repair = new RepairInvalidShares($config, $this->connection);
  37. }
  38. protected function tearDown() {
  39. $this->deleteAllShares();
  40. parent::tearDown();
  41. }
  42. protected function deleteAllShares() {
  43. $qb = $this->connection->getQueryBuilder();
  44. $qb->delete('share')->execute();
  45. }
  46. /**
  47. * Test remove expiration date for non-link shares
  48. */
  49. public function testRemoveExpirationDateForNonLinkShares() {
  50. // user share with bogus expiration date
  51. $qb = $this->connection->getQueryBuilder();
  52. $qb->insert('share')
  53. ->values([
  54. 'share_type' => $qb->expr()->literal(Constants::SHARE_TYPE_USER),
  55. 'share_with' => $qb->expr()->literal('recipientuser1'),
  56. 'uid_owner' => $qb->expr()->literal('user1'),
  57. 'item_type' => $qb->expr()->literal('folder'),
  58. 'item_source' => $qb->expr()->literal(123),
  59. 'item_target' => $qb->expr()->literal('/123'),
  60. 'file_source' => $qb->expr()->literal(123),
  61. 'file_target' => $qb->expr()->literal('/test'),
  62. 'permissions' => $qb->expr()->literal(1),
  63. 'stime' => $qb->expr()->literal(time()),
  64. 'expiration' => $qb->expr()->literal('2015-09-25 00:00:00')
  65. ])
  66. ->execute();
  67. $bogusShareId = $this->getLastShareId();
  68. // link share with expiration date
  69. $qb = $this->connection->getQueryBuilder();
  70. $qb->insert('share')
  71. ->values([
  72. 'share_type' => $qb->expr()->literal(Constants::SHARE_TYPE_LINK),
  73. 'uid_owner' => $qb->expr()->literal('user1'),
  74. 'item_type' => $qb->expr()->literal('folder'),
  75. 'item_source' => $qb->expr()->literal(123),
  76. 'item_target' => $qb->expr()->literal('/123'),
  77. 'file_source' => $qb->expr()->literal(123),
  78. 'file_target' => $qb->expr()->literal('/test'),
  79. 'permissions' => $qb->expr()->literal(1),
  80. 'stime' => $qb->expr()->literal(time()),
  81. 'expiration' => $qb->expr()->literal('2015-09-25 00:00:00'),
  82. 'token' => $qb->expr()->literal('abcdefg')
  83. ])->execute();
  84. $this->repair->run();
  85. $results = $this->connection->getQueryBuilder()
  86. ->select('*')
  87. ->from('share')
  88. ->orderBy('share_type', 'ASC')
  89. ->execute()
  90. ->fetchAll();
  91. $this->assertCount(2, $results);
  92. $userShare = $results[0];
  93. $linkShare = $results[1];
  94. $this->assertEquals($bogusShareId, $userShare['id'], 'sanity check');
  95. $this->assertNull($userShare['expiration'], 'bogus expiration date was removed');
  96. $this->assertNotNull($linkShare['expiration'], 'valid link share expiration date still there');
  97. }
  98. /**
  99. * Test remove shares where the parent share does not exist anymore
  100. */
  101. public function testSharesNonExistingParent() {
  102. $qb = $this->connection->getQueryBuilder();
  103. $shareValues = [
  104. 'share_type' => $qb->expr()->literal(Constants::SHARE_TYPE_USER),
  105. 'share_with' => $qb->expr()->literal('recipientuser1'),
  106. 'uid_owner' => $qb->expr()->literal('user1'),
  107. 'item_type' => $qb->expr()->literal('folder'),
  108. 'item_source' => $qb->expr()->literal(123),
  109. 'item_target' => $qb->expr()->literal('/123'),
  110. 'file_source' => $qb->expr()->literal(123),
  111. 'file_target' => $qb->expr()->literal('/test'),
  112. 'permissions' => $qb->expr()->literal(1),
  113. 'stime' => $qb->expr()->literal(time()),
  114. 'expiration' => $qb->expr()->literal('2015-09-25 00:00:00')
  115. ];
  116. // valid share
  117. $qb = $this->connection->getQueryBuilder();
  118. $qb->insert('share')
  119. ->values($shareValues)
  120. ->execute();
  121. $parent = $this->getLastShareId();
  122. // share with existing parent
  123. $qb = $this->connection->getQueryBuilder();
  124. $qb->insert('share')
  125. ->values(array_merge($shareValues, [
  126. 'parent' => $qb->expr()->literal($parent),
  127. ]))->execute();
  128. $validChild = $this->getLastShareId();
  129. // share with non-existing parent
  130. $qb = $this->connection->getQueryBuilder();
  131. $qb->insert('share')
  132. ->values(array_merge($shareValues, [
  133. 'parent' => $qb->expr()->literal($parent + 100),
  134. ]))->execute();
  135. $invalidChild = $this->getLastShareId();
  136. $query = $this->connection->getQueryBuilder();
  137. $result = $query->select('id')
  138. ->from('share')
  139. ->orderBy('id', 'ASC')
  140. ->execute();
  141. $rows = $result->fetchAll();
  142. $this->assertEquals([['id' => $parent], ['id' => $validChild], ['id' => $invalidChild]], $rows);
  143. $result->closeCursor();
  144. $this->repair->run();
  145. $query = $this->connection->getQueryBuilder();
  146. $result = $query->select('id')
  147. ->from('share')
  148. ->orderBy('id', 'ASC')
  149. ->execute();
  150. $rows = $result->fetchAll();
  151. $this->assertEquals([['id' => $parent], ['id' => $validChild]], $rows);
  152. $result->closeCursor();
  153. }
  154. /**
  155. * @return int
  156. */
  157. protected function getLastShareId() {
  158. return $this->connection->lastInsertId('*PREFIX*share');
  159. }
  160. }