DeleteOrphanedItemsJobTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. * @author Vincent Petry <vincent@nextcloud.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\Tests\BackgroundJob;
  25. use OCA\Files\BackgroundJob\DeleteOrphanedItems;
  26. use OCP\DB\QueryBuilder\IQueryBuilder;
  27. /**
  28. * Class DeleteOrphanedItemsJobTest
  29. *
  30. * @group DB
  31. *
  32. * @package Test\BackgroundJob
  33. */
  34. class DeleteOrphanedItemsJobTest extends \Test\TestCase {
  35. /** @var \OCP\IDBConnection */
  36. protected $connection;
  37. protected function setUp(): void {
  38. parent::setUp();
  39. $this->connection = \OC::$server->getDatabaseConnection();
  40. }
  41. protected function cleanMapping($table) {
  42. $query = $this->connection->getQueryBuilder();
  43. $query->delete($table)->execute();
  44. }
  45. protected function getMappings($table) {
  46. $query = $this->connection->getQueryBuilder();
  47. $query->select('*')
  48. ->from($table);
  49. $result = $query->execute();
  50. $mapping = $result->fetchAll();
  51. $result->closeCursor();
  52. return $mapping;
  53. }
  54. /**
  55. * Test clearing orphaned system tag mappings
  56. */
  57. public function testClearSystemTagMappings() {
  58. $this->cleanMapping('systemtag_object_mapping');
  59. $query = $this->connection->getQueryBuilder();
  60. $query->insert('filecache')
  61. ->values([
  62. 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT),
  63. 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'),
  64. 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')),
  65. ])->execute();
  66. $fileId = $query->getLastInsertId();
  67. // Existing file
  68. $query = $this->connection->getQueryBuilder();
  69. $query->insert('systemtag_object_mapping')
  70. ->values([
  71. 'objectid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
  72. 'objecttype' => $query->createNamedParameter('files'),
  73. 'systemtagid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT),
  74. ])->execute();
  75. // Non-existing file
  76. $query = $this->connection->getQueryBuilder();
  77. $query->insert('systemtag_object_mapping')
  78. ->values([
  79. 'objectid' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT),
  80. 'objecttype' => $query->createNamedParameter('files'),
  81. 'systemtagid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT),
  82. ])->execute();
  83. $mapping = $this->getMappings('systemtag_object_mapping');
  84. $this->assertCount(2, $mapping);
  85. $job = new DeleteOrphanedItems();
  86. $this->invokePrivate($job, 'cleanSystemTags');
  87. $mapping = $this->getMappings('systemtag_object_mapping');
  88. $this->assertCount(1, $mapping);
  89. $query = $this->connection->getQueryBuilder();
  90. $query->delete('filecache')
  91. ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
  92. ->execute();
  93. $this->cleanMapping('systemtag_object_mapping');
  94. }
  95. /**
  96. * Test clearing orphaned system tag mappings
  97. */
  98. public function testClearUserTagMappings() {
  99. $this->cleanMapping('vcategory_to_object');
  100. $query = $this->connection->getQueryBuilder();
  101. $query->insert('filecache')
  102. ->values([
  103. 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT),
  104. 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'),
  105. 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')),
  106. ])->execute();
  107. $fileId = $query->getLastInsertId();
  108. // Existing file
  109. $query = $this->connection->getQueryBuilder();
  110. $query->insert('vcategory_to_object')
  111. ->values([
  112. 'objid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
  113. 'type' => $query->createNamedParameter('files'),
  114. 'categoryid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT),
  115. ])->execute();
  116. // Non-existing file
  117. $query = $this->connection->getQueryBuilder();
  118. $query->insert('vcategory_to_object')
  119. ->values([
  120. 'objid' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT),
  121. 'type' => $query->createNamedParameter('files'),
  122. 'categoryid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT),
  123. ])->execute();
  124. $mapping = $this->getMappings('vcategory_to_object');
  125. $this->assertCount(2, $mapping);
  126. $job = new DeleteOrphanedItems();
  127. $this->invokePrivate($job, 'cleanUserTags');
  128. $mapping = $this->getMappings('vcategory_to_object');
  129. $this->assertCount(1, $mapping);
  130. $query = $this->connection->getQueryBuilder();
  131. $query->delete('filecache')
  132. ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
  133. ->execute();
  134. $this->cleanMapping('vcategory_to_object');
  135. }
  136. /**
  137. * Test clearing orphaned system tag mappings
  138. */
  139. public function testClearComments() {
  140. $this->cleanMapping('comments');
  141. $query = $this->connection->getQueryBuilder();
  142. $query->insert('filecache')
  143. ->values([
  144. 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT),
  145. 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'),
  146. 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')),
  147. ])->execute();
  148. $fileId = $query->getLastInsertId();
  149. // Existing file
  150. $query = $this->connection->getQueryBuilder();
  151. $query->insert('comments')
  152. ->values([
  153. 'object_id' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
  154. 'object_type' => $query->createNamedParameter('files'),
  155. 'actor_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT),
  156. 'actor_type' => $query->createNamedParameter('users'),
  157. ])->execute();
  158. // Non-existing file
  159. $query = $this->connection->getQueryBuilder();
  160. $query->insert('comments')
  161. ->values([
  162. 'object_id' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT),
  163. 'object_type' => $query->createNamedParameter('files'),
  164. 'actor_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT),
  165. 'actor_type' => $query->createNamedParameter('users'),
  166. ])->execute();
  167. $mapping = $this->getMappings('comments');
  168. $this->assertCount(2, $mapping);
  169. $job = new DeleteOrphanedItems();
  170. $this->invokePrivate($job, 'cleanComments');
  171. $mapping = $this->getMappings('comments');
  172. $this->assertCount(1, $mapping);
  173. $query = $this->connection->getQueryBuilder();
  174. $query->delete('filecache')
  175. ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
  176. ->execute();
  177. $this->cleanMapping('comments');
  178. }
  179. /**
  180. * Test clearing orphaned system tag mappings
  181. */
  182. public function testClearCommentReadMarks() {
  183. $this->cleanMapping('comments_read_markers');
  184. $query = $this->connection->getQueryBuilder();
  185. $query->insert('filecache')
  186. ->values([
  187. 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT),
  188. 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'),
  189. 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')),
  190. ])->execute();
  191. $fileId = $query->getLastInsertId();
  192. // Existing file
  193. $query = $this->connection->getQueryBuilder();
  194. $query->insert('comments_read_markers')
  195. ->values([
  196. 'object_id' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
  197. 'object_type' => $query->createNamedParameter('files'),
  198. 'user_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT),
  199. ])->execute();
  200. // Non-existing file
  201. $query = $this->connection->getQueryBuilder();
  202. $query->insert('comments_read_markers')
  203. ->values([
  204. 'object_id' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT),
  205. 'object_type' => $query->createNamedParameter('files'),
  206. 'user_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT),
  207. ])->execute();
  208. $mapping = $this->getMappings('comments_read_markers');
  209. $this->assertCount(2, $mapping);
  210. $job = new DeleteOrphanedItems();
  211. $this->invokePrivate($job, 'cleanCommentMarkers');
  212. $mapping = $this->getMappings('comments_read_markers');
  213. $this->assertCount(1, $mapping);
  214. $query = $this->connection->getQueryBuilder();
  215. $query->delete('filecache')
  216. ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
  217. ->execute();
  218. $this->cleanMapping('comments_read_markers');
  219. }
  220. }