DeleteOrphanedItemsJobTest.php 9.1 KB

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