DeleteOrphanedItemsJobTest.php 9.4 KB

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