SystemTagObjectMapperTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <?php
  2. /**
  3. * Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. *
  8. */
  9. namespace Test\SystemTag;
  10. use OC\SystemTag\SystemTag;
  11. use OC\SystemTag\SystemTagManager;
  12. use OC\SystemTag\SystemTagObjectMapper;
  13. use OCP\IDBConnection;
  14. use OCP\SystemTag\ISystemTag;
  15. use OCP\SystemTag\ISystemTagManager;
  16. use OCP\SystemTag\ISystemTagObjectMapper;
  17. use OCP\SystemTag\TagNotFoundException;
  18. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  19. use Test\TestCase;
  20. /**
  21. * Class TestSystemTagObjectMapper
  22. *
  23. * @group DB
  24. * @package Test\SystemTag
  25. */
  26. class SystemTagObjectMapperTest extends TestCase {
  27. /**
  28. * @var ISystemTagManager
  29. **/
  30. private $tagManager;
  31. /**
  32. * @var ISystemTagObjectMapper
  33. **/
  34. private $tagMapper;
  35. /**
  36. * @var IDBConnection
  37. */
  38. private $connection;
  39. /**
  40. * @var EventDispatcherInterface
  41. */
  42. private $dispatcher;
  43. /**
  44. * @var ISystemTag
  45. */
  46. private $tag1;
  47. /**
  48. * @var ISystemTag
  49. */
  50. private $tag2;
  51. /**
  52. * @var ISystemTag
  53. */
  54. private $tag3;
  55. public function setUp() {
  56. parent::setUp();
  57. $this->connection = \OC::$server->getDatabaseConnection();
  58. $this->pruneTagsTables();
  59. $this->tagManager = $this->getMockBuilder('OCP\SystemTag\ISystemTagManager')
  60. ->getMock();
  61. $this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')
  62. ->getMock();
  63. $this->tagMapper = new SystemTagObjectMapper(
  64. $this->connection,
  65. $this->tagManager,
  66. $this->dispatcher
  67. );
  68. $this->tag1 = new SystemTag(1, 'testtag1', false, false);
  69. $this->tag2 = new SystemTag(2, 'testtag2', true, false);
  70. $this->tag3 = new SystemTag(3, 'testtag3', false, false);
  71. $this->tagManager->expects($this->any())
  72. ->method('getTagsByIds')
  73. ->will($this->returnCallback(function($tagIds) {
  74. $result = [];
  75. if (in_array(1, $tagIds)) {
  76. $result[1] = $this->tag1;
  77. }
  78. if (in_array(2, $tagIds)) {
  79. $result[2] = $this->tag2;
  80. }
  81. if (in_array(3, $tagIds)) {
  82. $result[3] = $this->tag3;
  83. }
  84. return $result;
  85. }));
  86. $this->tagMapper->assignTags('1', 'testtype', $this->tag1->getId());
  87. $this->tagMapper->assignTags('1', 'testtype', $this->tag2->getId());
  88. $this->tagMapper->assignTags('2', 'testtype', $this->tag1->getId());
  89. $this->tagMapper->assignTags('3', 'anothertype', $this->tag1->getId());
  90. }
  91. public function tearDown() {
  92. $this->pruneTagsTables();
  93. parent::tearDown();
  94. }
  95. protected function pruneTagsTables() {
  96. $query = $this->connection->getQueryBuilder();
  97. $query->delete(SystemTagObjectMapper::RELATION_TABLE)->execute();
  98. $query->delete(SystemTagManager::TAG_TABLE)->execute();
  99. }
  100. public function testGetTagIdsForObjects() {
  101. $tagIdMapping = $this->tagMapper->getTagIdsForObjects(
  102. ['1', '2', '3', '4'],
  103. 'testtype'
  104. );
  105. $this->assertEquals([
  106. '1' => [$this->tag1->getId(), $this->tag2->getId()],
  107. '2' => [$this->tag1->getId()],
  108. '3' => [],
  109. '4' => [],
  110. ], $tagIdMapping);
  111. }
  112. public function testGetTagIdsForNoObjects() {
  113. $tagIdMapping = $this->tagMapper->getTagIdsForObjects(
  114. [],
  115. 'testtype'
  116. );
  117. $this->assertEquals([], $tagIdMapping);
  118. }
  119. public function testGetObjectsForTags() {
  120. $objectIds = $this->tagMapper->getObjectIdsForTags(
  121. [$this->tag1->getId(), $this->tag2->getId(), $this->tag3->getId()],
  122. 'testtype'
  123. );
  124. $this->assertEquals([
  125. '1',
  126. '2',
  127. ], $objectIds);
  128. }
  129. public function testGetObjectsForTagsLimit() {
  130. $objectIds = $this->tagMapper->getObjectIdsForTags(
  131. [$this->tag1->getId()],
  132. 'testtype',
  133. 1
  134. );
  135. $this->assertEquals([
  136. 1,
  137. ], $objectIds);
  138. }
  139. /**
  140. * @expectedException \InvalidArgumentException
  141. */
  142. public function testGetObjectsForTagsLimitWithMultipleTags() {
  143. $this->tagMapper->getObjectIdsForTags(
  144. [$this->tag1->getId(), $this->tag2->getId(), $this->tag3->getId()],
  145. 'testtype',
  146. 1
  147. );
  148. }
  149. public function testGetObjectsForTagsLimitOffset() {
  150. $objectIds = $this->tagMapper->getObjectIdsForTags(
  151. [$this->tag1->getId()],
  152. 'testtype',
  153. 1,
  154. '1'
  155. );
  156. $this->assertEquals([
  157. 2,
  158. ], $objectIds);
  159. }
  160. /**
  161. * @expectedException \OCP\SystemTag\TagNotFoundException
  162. */
  163. public function testGetObjectsForNonExistingTag() {
  164. $this->tagMapper->getObjectIdsForTags(
  165. [100],
  166. 'testtype'
  167. );
  168. }
  169. public function testAssignUnassignTags() {
  170. $this->tagMapper->unassignTags('1', 'testtype', [$this->tag1->getId()]);
  171. $tagIdMapping = $this->tagMapper->getTagIdsForObjects('1', 'testtype');
  172. $this->assertEquals([
  173. 1 => [$this->tag2->getId()],
  174. ], $tagIdMapping);
  175. $this->tagMapper->assignTags('1', 'testtype', [$this->tag1->getId()]);
  176. $this->tagMapper->assignTags('1', 'testtype', $this->tag3->getId());
  177. $tagIdMapping = $this->tagMapper->getTagIdsForObjects('1', 'testtype');
  178. $this->assertEquals([
  179. '1' => [$this->tag1->getId(), $this->tag2->getId(), $this->tag3->getId()],
  180. ], $tagIdMapping);
  181. }
  182. public function testReAssignUnassignTags() {
  183. // reassign tag1
  184. $this->tagMapper->assignTags('1', 'testtype', [$this->tag1->getId()]);
  185. // tag 3 was never assigned
  186. $this->tagMapper->unassignTags('1', 'testtype', [$this->tag3->getId()]);
  187. $this->assertTrue(true, 'No error when reassigning/unassigning');
  188. }
  189. /**
  190. * @expectedException \OCP\SystemTag\TagNotFoundException
  191. */
  192. public function testAssignNonExistingTags() {
  193. $this->tagMapper->assignTags('1', 'testtype', [100]);
  194. }
  195. public function testAssignNonExistingTagInArray() {
  196. $caught = false;
  197. try {
  198. $this->tagMapper->assignTags('1', 'testtype', [100, $this->tag3->getId()]);
  199. } catch (TagNotFoundException $e) {
  200. $caught = true;
  201. }
  202. $this->assertTrue($caught, 'Exception thrown');
  203. $tagIdMapping = $this->tagMapper->getTagIdsForObjects(
  204. ['1'],
  205. 'testtype'
  206. );
  207. $this->assertEquals([
  208. '1' => [$this->tag1->getId(), $this->tag2->getId()],
  209. ], $tagIdMapping, 'None of the tags got assigned');
  210. }
  211. /**
  212. * @expectedException \OCP\SystemTag\TagNotFoundException
  213. */
  214. public function testUnassignNonExistingTags() {
  215. $this->tagMapper->unassignTags('1', 'testtype', [100]);
  216. }
  217. public function testUnassignNonExistingTagsInArray() {
  218. $caught = false;
  219. try {
  220. $this->tagMapper->unassignTags('1', 'testtype', [100, $this->tag1->getId()]);
  221. } catch (TagNotFoundException $e) {
  222. $caught = true;
  223. }
  224. $this->assertTrue($caught, 'Exception thrown');
  225. $tagIdMapping = $this->tagMapper->getTagIdsForObjects(
  226. [1],
  227. 'testtype'
  228. );
  229. $this->assertEquals([
  230. '1' => [$this->tag1->getId(), $this->tag2->getId()],
  231. ], $tagIdMapping, 'None of the tags got unassigned');
  232. }
  233. public function testHaveTagAllMatches() {
  234. $this->assertTrue(
  235. $this->tagMapper->haveTag(
  236. ['1'],
  237. 'testtype',
  238. $this->tag1->getId(),
  239. true
  240. ),
  241. 'object 1 has the tag tag1'
  242. );
  243. $this->assertTrue(
  244. $this->tagMapper->haveTag(
  245. ['1', '2'],
  246. 'testtype',
  247. $this->tag1->getId(),
  248. true
  249. ),
  250. 'object 1 and object 2 ALL have the tag tag1'
  251. );
  252. $this->assertFalse(
  253. $this->tagMapper->haveTag(
  254. ['1', '2'],
  255. 'testtype',
  256. $this->tag2->getId(),
  257. true
  258. ),
  259. 'object 1 has tag2 but not object 2, so not ALL of them'
  260. );
  261. $this->assertFalse(
  262. $this->tagMapper->haveTag(
  263. ['2'],
  264. 'testtype',
  265. $this->tag2->getId(),
  266. true
  267. ),
  268. 'object 2 does not have tag2'
  269. );
  270. $this->assertFalse(
  271. $this->tagMapper->haveTag(
  272. ['3'],
  273. 'testtype',
  274. $this->tag2->getId(),
  275. true
  276. ),
  277. 'object 3 does not have tag1 due to different type'
  278. );
  279. }
  280. public function testHaveTagAtLeastOneMatch() {
  281. $this->assertTrue(
  282. $this->tagMapper->haveTag(
  283. ['1'],
  284. 'testtype',
  285. $this->tag1->getId(),
  286. false
  287. ),
  288. 'object1 has the tag tag1'
  289. );
  290. $this->assertTrue(
  291. $this->tagMapper->haveTag(
  292. ['1', '2'],
  293. 'testtype',
  294. $this->tag1->getId(),
  295. false
  296. ),
  297. 'object 1 and object 2 both the tag tag1'
  298. );
  299. $this->assertTrue(
  300. $this->tagMapper->haveTag(
  301. ['1', '2'],
  302. 'testtype',
  303. $this->tag2->getId(),
  304. false
  305. ),
  306. 'at least object 1 has the tag tag2'
  307. );
  308. $this->assertFalse(
  309. $this->tagMapper->haveTag(
  310. ['2'],
  311. 'testtype',
  312. $this->tag2->getId(),
  313. false
  314. ),
  315. 'object 2 does not have tag2'
  316. );
  317. $this->assertFalse(
  318. $this->tagMapper->haveTag(
  319. ['3'],
  320. 'testtype',
  321. $this->tag2->getId(),
  322. false
  323. ),
  324. 'object 3 does not have tag1 due to different type'
  325. );
  326. }
  327. /**
  328. * @expectedException \OCP\SystemTag\TagNotFoundException
  329. */
  330. public function testHaveTagNonExisting() {
  331. $this->tagMapper->haveTag(
  332. ['1'],
  333. 'testtype',
  334. 100
  335. );
  336. }
  337. }