DeleteOrphanedItemsJobTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files\Tests\BackgroundJob;
  8. use OCA\Files\BackgroundJob\DeleteOrphanedItems;
  9. use OCP\AppFramework\Utility\ITimeFactory;
  10. use OCP\DB\QueryBuilder\IQueryBuilder;
  11. use OCP\IDBConnection;
  12. use Psr\Log\LoggerInterface;
  13. /**
  14. * Class DeleteOrphanedItemsJobTest
  15. *
  16. * @group DB
  17. *
  18. * @package Test\BackgroundJob
  19. */
  20. class DeleteOrphanedItemsJobTest extends \Test\TestCase {
  21. protected IDBConnection $connection;
  22. protected LoggerInterface $logger;
  23. protected ITimeFactory $timeFactory;
  24. protected function setUp(): void {
  25. parent::setUp();
  26. $this->connection = \OC::$server->get(IDBConnection::class);
  27. $this->timeFactory = $this->createMock(ITimeFactory::class);
  28. $this->logger = \OC::$server->get(LoggerInterface::class);
  29. }
  30. protected function cleanMapping($table) {
  31. $query = $this->connection->getQueryBuilder();
  32. $query->delete($table)->execute();
  33. }
  34. protected function getMappings($table) {
  35. $query = $this->connection->getQueryBuilder();
  36. $query->select('*')
  37. ->from($table);
  38. $result = $query->execute();
  39. $mapping = $result->fetchAll();
  40. $result->closeCursor();
  41. return $mapping;
  42. }
  43. /**
  44. * Test clearing orphaned system tag mappings
  45. */
  46. public function testClearSystemTagMappings(): void {
  47. $this->cleanMapping('systemtag_object_mapping');
  48. $query = $this->connection->getQueryBuilder();
  49. $query->insert('filecache')
  50. ->values([
  51. 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT),
  52. 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'),
  53. 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')),
  54. ])->execute();
  55. $fileId = $query->getLastInsertId();
  56. // Existing file
  57. $query = $this->connection->getQueryBuilder();
  58. $query->insert('systemtag_object_mapping')
  59. ->values([
  60. 'objectid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
  61. 'objecttype' => $query->createNamedParameter('files'),
  62. 'systemtagid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT),
  63. ])->execute();
  64. // Non-existing file
  65. $query = $this->connection->getQueryBuilder();
  66. $query->insert('systemtag_object_mapping')
  67. ->values([
  68. 'objectid' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT),
  69. 'objecttype' => $query->createNamedParameter('files'),
  70. 'systemtagid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT),
  71. ])->execute();
  72. $mapping = $this->getMappings('systemtag_object_mapping');
  73. $this->assertCount(2, $mapping);
  74. $job = new DeleteOrphanedItems($this->timeFactory, $this->connection, $this->logger);
  75. $this->invokePrivate($job, 'cleanSystemTags');
  76. $mapping = $this->getMappings('systemtag_object_mapping');
  77. $this->assertCount(1, $mapping);
  78. $query = $this->connection->getQueryBuilder();
  79. $query->delete('filecache')
  80. ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
  81. ->execute();
  82. $this->cleanMapping('systemtag_object_mapping');
  83. }
  84. /**
  85. * Test clearing orphaned system tag mappings
  86. */
  87. public function testClearUserTagMappings(): void {
  88. $this->cleanMapping('vcategory_to_object');
  89. $query = $this->connection->getQueryBuilder();
  90. $query->insert('filecache')
  91. ->values([
  92. 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT),
  93. 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'),
  94. 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')),
  95. ])->execute();
  96. $fileId = $query->getLastInsertId();
  97. // Existing file
  98. $query = $this->connection->getQueryBuilder();
  99. $query->insert('vcategory_to_object')
  100. ->values([
  101. 'objid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
  102. 'type' => $query->createNamedParameter('files'),
  103. 'categoryid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT),
  104. ])->execute();
  105. // Non-existing file
  106. $query = $this->connection->getQueryBuilder();
  107. $query->insert('vcategory_to_object')
  108. ->values([
  109. 'objid' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT),
  110. 'type' => $query->createNamedParameter('files'),
  111. 'categoryid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT),
  112. ])->execute();
  113. $mapping = $this->getMappings('vcategory_to_object');
  114. $this->assertCount(2, $mapping);
  115. $job = new DeleteOrphanedItems($this->timeFactory, $this->connection, $this->logger);
  116. $this->invokePrivate($job, 'cleanUserTags');
  117. $mapping = $this->getMappings('vcategory_to_object');
  118. $this->assertCount(1, $mapping);
  119. $query = $this->connection->getQueryBuilder();
  120. $query->delete('filecache')
  121. ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
  122. ->execute();
  123. $this->cleanMapping('vcategory_to_object');
  124. }
  125. /**
  126. * Test clearing orphaned system tag mappings
  127. */
  128. public function testClearComments(): void {
  129. $this->cleanMapping('comments');
  130. $query = $this->connection->getQueryBuilder();
  131. $query->insert('filecache')
  132. ->values([
  133. 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT),
  134. 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'),
  135. 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')),
  136. ])->execute();
  137. $fileId = $query->getLastInsertId();
  138. // Existing file
  139. $query = $this->connection->getQueryBuilder();
  140. $query->insert('comments')
  141. ->values([
  142. 'object_id' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
  143. 'object_type' => $query->createNamedParameter('files'),
  144. 'actor_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT),
  145. 'actor_type' => $query->createNamedParameter('users'),
  146. ])->execute();
  147. // Non-existing file
  148. $query = $this->connection->getQueryBuilder();
  149. $query->insert('comments')
  150. ->values([
  151. 'object_id' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT),
  152. 'object_type' => $query->createNamedParameter('files'),
  153. 'actor_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT),
  154. 'actor_type' => $query->createNamedParameter('users'),
  155. ])->execute();
  156. $mapping = $this->getMappings('comments');
  157. $this->assertCount(2, $mapping);
  158. $job = new DeleteOrphanedItems($this->timeFactory, $this->connection, $this->logger);
  159. $this->invokePrivate($job, 'cleanComments');
  160. $mapping = $this->getMappings('comments');
  161. $this->assertCount(1, $mapping);
  162. $query = $this->connection->getQueryBuilder();
  163. $query->delete('filecache')
  164. ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
  165. ->execute();
  166. $this->cleanMapping('comments');
  167. }
  168. /**
  169. * Test clearing orphaned system tag mappings
  170. */
  171. public function testClearCommentReadMarks(): void {
  172. $this->cleanMapping('comments_read_markers');
  173. $query = $this->connection->getQueryBuilder();
  174. $query->insert('filecache')
  175. ->values([
  176. 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT),
  177. 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'),
  178. 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')),
  179. ])->execute();
  180. $fileId = $query->getLastInsertId();
  181. // Existing file
  182. $query = $this->connection->getQueryBuilder();
  183. $query->insert('comments_read_markers')
  184. ->values([
  185. 'object_id' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
  186. 'object_type' => $query->createNamedParameter('files'),
  187. 'user_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT),
  188. ])->execute();
  189. // Non-existing file
  190. $query = $this->connection->getQueryBuilder();
  191. $query->insert('comments_read_markers')
  192. ->values([
  193. 'object_id' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT),
  194. 'object_type' => $query->createNamedParameter('files'),
  195. 'user_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT),
  196. ])->execute();
  197. $mapping = $this->getMappings('comments_read_markers');
  198. $this->assertCount(2, $mapping);
  199. $job = new DeleteOrphanedItems($this->timeFactory, $this->connection, $this->logger);
  200. $this->invokePrivate($job, 'cleanCommentMarkers');
  201. $mapping = $this->getMappings('comments_read_markers');
  202. $this->assertCount(1, $mapping);
  203. $query = $this->connection->getQueryBuilder();
  204. $query->delete('filecache')
  205. ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
  206. ->execute();
  207. $this->cleanMapping('comments_read_markers');
  208. }
  209. }