DeleteOrphanedItemsJobTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Vincent Petry <pvince81@owncloud.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\Files\Tests\BackgroundJob;
  24. use OCA\Files\BackgroundJob\DeleteOrphanedItems;
  25. use OCP\DB\QueryBuilder\IQueryBuilder;
  26. /**
  27. * Class DeleteOrphanedItemsJobTest
  28. *
  29. * @group DB
  30. *
  31. * @package Test\BackgroundJob
  32. */
  33. class DeleteOrphanedItemsJobTest extends \Test\TestCase {
  34. /** @var \OCP\IDBConnection */
  35. protected $connection;
  36. protected function setup() {
  37. parent::setUp();
  38. $this->connection = \OC::$server->getDatabaseConnection();
  39. }
  40. protected function cleanMapping($table) {
  41. $query = $this->connection->getQueryBuilder();
  42. $query->delete($table)->execute();
  43. }
  44. protected function getMappings($table) {
  45. $query = $this->connection->getQueryBuilder();
  46. $query->select('*')
  47. ->from($table);
  48. $result = $query->execute();
  49. $mapping = $result->fetchAll();
  50. $result->closeCursor();
  51. return $mapping;
  52. }
  53. /**
  54. * Test clearing orphaned system tag mappings
  55. */
  56. public function testClearSystemTagMappings() {
  57. $this->cleanMapping('systemtag_object_mapping');
  58. $query = $this->connection->getQueryBuilder();
  59. $query->insert('filecache')
  60. ->values([
  61. 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT),
  62. 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'),
  63. 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')),
  64. ])->execute();
  65. $fileId = $query->getLastInsertId();
  66. // Existing file
  67. $query = $this->connection->getQueryBuilder();
  68. $query->insert('systemtag_object_mapping')
  69. ->values([
  70. 'objectid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
  71. 'objecttype' => $query->createNamedParameter('files'),
  72. 'systemtagid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT),
  73. ])->execute();
  74. // Non-existing file
  75. $query = $this->connection->getQueryBuilder();
  76. $query->insert('systemtag_object_mapping')
  77. ->values([
  78. 'objectid' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT),
  79. 'objecttype' => $query->createNamedParameter('files'),
  80. 'systemtagid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT),
  81. ])->execute();
  82. $mapping = $this->getMappings('systemtag_object_mapping');
  83. $this->assertCount(2, $mapping);
  84. $job = new DeleteOrphanedItems();
  85. $this->invokePrivate($job, 'cleanSystemTags');
  86. $mapping = $this->getMappings('systemtag_object_mapping');
  87. $this->assertCount(1, $mapping);
  88. $query = $this->connection->getQueryBuilder();
  89. $query->delete('filecache')
  90. ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
  91. ->execute();
  92. $this->cleanMapping('systemtag_object_mapping');
  93. }
  94. /**
  95. * Test clearing orphaned system tag mappings
  96. */
  97. public function testClearUserTagMappings() {
  98. $this->cleanMapping('vcategory_to_object');
  99. $query = $this->connection->getQueryBuilder();
  100. $query->insert('filecache')
  101. ->values([
  102. 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT),
  103. 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'),
  104. 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')),
  105. ])->execute();
  106. $fileId = $query->getLastInsertId();
  107. // Existing file
  108. $query = $this->connection->getQueryBuilder();
  109. $query->insert('vcategory_to_object')
  110. ->values([
  111. 'objid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
  112. 'type' => $query->createNamedParameter('files'),
  113. 'categoryid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT),
  114. ])->execute();
  115. // Non-existing file
  116. $query = $this->connection->getQueryBuilder();
  117. $query->insert('vcategory_to_object')
  118. ->values([
  119. 'objid' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT),
  120. 'type' => $query->createNamedParameter('files'),
  121. 'categoryid' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT),
  122. ])->execute();
  123. $mapping = $this->getMappings('vcategory_to_object');
  124. $this->assertCount(2, $mapping);
  125. $job = new DeleteOrphanedItems();
  126. $this->invokePrivate($job, 'cleanUserTags');
  127. $mapping = $this->getMappings('vcategory_to_object');
  128. $this->assertCount(1, $mapping);
  129. $query = $this->connection->getQueryBuilder();
  130. $query->delete('filecache')
  131. ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
  132. ->execute();
  133. $this->cleanMapping('vcategory_to_object');
  134. }
  135. /**
  136. * Test clearing orphaned system tag mappings
  137. */
  138. public function testClearComments() {
  139. $this->cleanMapping('comments');
  140. $query = $this->connection->getQueryBuilder();
  141. $query->insert('filecache')
  142. ->values([
  143. 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT),
  144. 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'),
  145. 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')),
  146. ])->execute();
  147. $fileId = $query->getLastInsertId();
  148. // Existing file
  149. $query = $this->connection->getQueryBuilder();
  150. $query->insert('comments')
  151. ->values([
  152. 'object_id' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
  153. 'object_type' => $query->createNamedParameter('files'),
  154. 'actor_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT),
  155. 'actor_type' => $query->createNamedParameter('users'),
  156. ])->execute();
  157. // Non-existing file
  158. $query = $this->connection->getQueryBuilder();
  159. $query->insert('comments')
  160. ->values([
  161. 'object_id' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT),
  162. 'object_type' => $query->createNamedParameter('files'),
  163. 'actor_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT),
  164. 'actor_type' => $query->createNamedParameter('users'),
  165. ])->execute();
  166. $mapping = $this->getMappings('comments');
  167. $this->assertCount(2, $mapping);
  168. $job = new DeleteOrphanedItems();
  169. $this->invokePrivate($job, 'cleanComments');
  170. $mapping = $this->getMappings('comments');
  171. $this->assertCount(1, $mapping);
  172. $query = $this->connection->getQueryBuilder();
  173. $query->delete('filecache')
  174. ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
  175. ->execute();
  176. $this->cleanMapping('comments');
  177. }
  178. /**
  179. * Test clearing orphaned system tag mappings
  180. */
  181. public function testClearCommentReadMarks() {
  182. $this->cleanMapping('comments_read_markers');
  183. $query = $this->connection->getQueryBuilder();
  184. $query->insert('filecache')
  185. ->values([
  186. 'storage' => $query->createNamedParameter(1337, IQueryBuilder::PARAM_INT),
  187. 'path' => $query->createNamedParameter('apps/files/tests/deleteorphanedtagsjobtest.php'),
  188. 'path_hash' => $query->createNamedParameter(md5('apps/files/tests/deleteorphanedtagsjobtest.php')),
  189. ])->execute();
  190. $fileId = $query->getLastInsertId();
  191. // Existing file
  192. $query = $this->connection->getQueryBuilder();
  193. $query->insert('comments_read_markers')
  194. ->values([
  195. 'object_id' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
  196. 'object_type' => $query->createNamedParameter('files'),
  197. 'user_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT),
  198. ])->execute();
  199. // Non-existing file
  200. $query = $this->connection->getQueryBuilder();
  201. $query->insert('comments_read_markers')
  202. ->values([
  203. 'object_id' => $query->createNamedParameter($fileId + 1, IQueryBuilder::PARAM_INT),
  204. 'object_type' => $query->createNamedParameter('files'),
  205. 'user_id' => $query->createNamedParameter('Alice', IQueryBuilder::PARAM_INT),
  206. ])->execute();
  207. $mapping = $this->getMappings('comments_read_markers');
  208. $this->assertCount(2, $mapping);
  209. $job = new DeleteOrphanedItems();
  210. $this->invokePrivate($job, 'cleanCommentMarkers');
  211. $mapping = $this->getMappings('comments_read_markers');
  212. $this->assertCount(1, $mapping);
  213. $query = $this->connection->getQueryBuilder();
  214. $query->delete('filecache')
  215. ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
  216. ->execute();
  217. $this->cleanMapping('comments_read_markers');
  218. }
  219. }