TagsTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test;
  8. use OCP\IDBConnection;
  9. use OCP\IUser;
  10. use OCP\IUserSession;
  11. use Psr\Log\LoggerInterface;
  12. /**
  13. * Class TagsTest
  14. *
  15. * @group DB
  16. */
  17. class TagsTest extends \Test\TestCase {
  18. protected $objectType;
  19. /** @var \OCP\IUser */
  20. protected $user;
  21. /** @var \OCP\IUserSession */
  22. protected $userSession;
  23. protected $backupGlobals = false;
  24. /** @var \OC\Tagging\TagMapper */
  25. protected $tagMapper;
  26. /** @var \OCP\ITagManager */
  27. protected $tagMgr;
  28. protected function setUp(): void {
  29. parent::setUp();
  30. \OC_User::clearBackends();
  31. \OC_User::useBackend('dummy');
  32. $userId = $this->getUniqueID('user_');
  33. \OC::$server->getUserManager()->createUser($userId, 'pass');
  34. \OC_User::setUserId($userId);
  35. $this->user = $this->createMock(IUser::class);
  36. $this->user->method('getUID')
  37. ->willReturn($userId);
  38. $this->userSession = $this->createMock(IUserSession::class);
  39. $this->userSession
  40. ->expects($this->any())
  41. ->method('getUser')
  42. ->willReturn($this->user);
  43. $this->objectType = $this->getUniqueID('type_');
  44. $this->tagMapper = new \OC\Tagging\TagMapper(\OC::$server->get(IDBConnection::class));
  45. $this->tagMgr = new \OC\TagManager($this->tagMapper, $this->userSession, \OC::$server->get(IDBConnection::class), \OC::$server->get(LoggerInterface::class));
  46. }
  47. protected function tearDown(): void {
  48. $conn = \OC::$server->getDatabaseConnection();
  49. $conn->executeQuery('DELETE FROM `*PREFIX*vcategory_to_object`');
  50. $conn->executeQuery('DELETE FROM `*PREFIX*vcategory`');
  51. parent::tearDown();
  52. }
  53. public function testTagManagerWithoutUserReturnsNull() {
  54. $this->userSession = $this->createMock(IUserSession::class);
  55. $this->userSession
  56. ->expects($this->any())
  57. ->method('getUser')
  58. ->willReturn(null);
  59. $this->tagMgr = new \OC\TagManager($this->tagMapper, $this->userSession, \OC::$server->getDatabaseConnection(), \OC::$server->get(LoggerInterface::class));
  60. $this->assertNull($this->tagMgr->load($this->objectType));
  61. }
  62. public function testInstantiateWithDefaults() {
  63. $defaultTags = ['Friends', 'Family', 'Work', 'Other'];
  64. $tagger = $this->tagMgr->load($this->objectType, $defaultTags);
  65. $this->assertEquals(4, count($tagger->getTags()));
  66. }
  67. public function testAddTags() {
  68. $tags = ['Friends', 'Family', 'Work', 'Other'];
  69. $tagger = $this->tagMgr->load($this->objectType);
  70. foreach ($tags as $tag) {
  71. $result = $tagger->add($tag);
  72. $this->assertGreaterThan(0, $result, 'add() returned an ID <= 0');
  73. $this->assertTrue((bool)$result);
  74. }
  75. $this->assertFalse($tagger->add('Family'));
  76. $this->assertFalse($tagger->add('fAMILY'));
  77. $this->assertCount(4, $tagger->getTags(), 'Wrong number of added tags');
  78. }
  79. public function testAddMultiple() {
  80. $tags = ['Friends', 'Family', 'Work', 'Other'];
  81. $tagger = $this->tagMgr->load($this->objectType);
  82. foreach ($tags as $tag) {
  83. $this->assertFalse($tagger->hasTag($tag));
  84. }
  85. $result = $tagger->addMultiple($tags);
  86. $this->assertTrue((bool)$result);
  87. foreach ($tags as $tag) {
  88. $this->assertTrue($tagger->hasTag($tag));
  89. }
  90. $tagMaps = $tagger->getTags();
  91. $this->assertCount(4, $tagMaps, 'Not all tags added');
  92. foreach ($tagMaps as $tagMap) {
  93. $this->assertEquals(null, $tagMap['id']);
  94. }
  95. // As addMultiple has been called without $sync=true, the tags aren't
  96. // saved to the database, so they're gone when we reload $tagger:
  97. $tagger = $this->tagMgr->load($this->objectType);
  98. $this->assertEquals(0, count($tagger->getTags()));
  99. // Now, we call addMultiple() with $sync=true so the tags will be
  100. // be saved to the database.
  101. $result = $tagger->addMultiple($tags, true);
  102. $this->assertTrue((bool)$result);
  103. $tagMaps = $tagger->getTags();
  104. foreach ($tagMaps as $tagMap) {
  105. $this->assertNotEquals(null, $tagMap['id']);
  106. }
  107. // Reload the tagger.
  108. $tagger = $this->tagMgr->load($this->objectType);
  109. foreach ($tags as $tag) {
  110. $this->assertTrue($tagger->hasTag($tag));
  111. }
  112. $this->assertCount(4, $tagger->getTags(), 'Not all previously saved tags found');
  113. }
  114. public function testIsEmpty() {
  115. $tagger = $this->tagMgr->load($this->objectType);
  116. $this->assertEquals(0, count($tagger->getTags()));
  117. $this->assertTrue($tagger->isEmpty());
  118. $result = $tagger->add('Tag');
  119. $this->assertGreaterThan(0, $result, 'add() returned an ID <= 0');
  120. $this->assertNotEquals(false, $result, 'add() returned false');
  121. $this->assertFalse($tagger->isEmpty());
  122. }
  123. public function testGetTagsForObjects() {
  124. $defaultTags = ['Friends', 'Family', 'Work', 'Other'];
  125. $tagger = $this->tagMgr->load($this->objectType, $defaultTags);
  126. $tagger->tagAs(1, 'Friends');
  127. $tagger->tagAs(1, 'Other');
  128. $tagger->tagAs(2, 'Family');
  129. $tags = $tagger->getTagsForObjects([1]);
  130. $this->assertEquals(1, count($tags));
  131. $tags = current($tags);
  132. sort($tags);
  133. $this->assertSame(['Friends', 'Other'], $tags);
  134. $tags = $tagger->getTagsForObjects([1, 2]);
  135. $this->assertEquals(2, count($tags));
  136. $tags1 = $tags[1];
  137. sort($tags1);
  138. $this->assertSame(['Friends', 'Other'], $tags1);
  139. $this->assertSame(['Family'], $tags[2]);
  140. $this->assertEquals(
  141. [],
  142. $tagger->getTagsForObjects([4])
  143. );
  144. $this->assertEquals(
  145. [],
  146. $tagger->getTagsForObjects([4, 5])
  147. );
  148. }
  149. public function testGetTagsForObjectsMassiveResults() {
  150. $defaultTags = ['tag1'];
  151. $tagger = $this->tagMgr->load($this->objectType, $defaultTags);
  152. $tagData = $tagger->getTags();
  153. $tagId = $tagData[0]['id'];
  154. $tagType = $tagData[0]['type'];
  155. $conn = \OC::$server->getDatabaseConnection();
  156. $statement = $conn->prepare(
  157. 'INSERT INTO `*PREFIX*vcategory_to_object` ' .
  158. '(`objid`, `categoryid`, `type`) VALUES ' .
  159. '(?, ?, ?)'
  160. );
  161. // insert lots of entries
  162. $idsArray = [];
  163. for ($i = 1; $i <= 1500; $i++) {
  164. $statement->execute([$i, $tagId, $tagType]);
  165. $idsArray[] = $i;
  166. }
  167. $tags = $tagger->getTagsForObjects($idsArray);
  168. $this->assertEquals(1500, count($tags));
  169. }
  170. public function testDeleteTags() {
  171. $defaultTags = ['Friends', 'Family', 'Work', 'Other'];
  172. $tagger = $this->tagMgr->load($this->objectType, $defaultTags);
  173. $this->assertEquals(4, count($tagger->getTags()));
  174. $tagger->delete('family');
  175. $this->assertEquals(3, count($tagger->getTags()));
  176. $tagger->delete(['Friends', 'Work', 'Other']);
  177. $this->assertEquals(0, count($tagger->getTags()));
  178. }
  179. public function testRenameTag() {
  180. $defaultTags = ['Friends', 'Family', 'Wrok', 'Other'];
  181. $tagger = $this->tagMgr->load($this->objectType, $defaultTags);
  182. $this->assertTrue($tagger->rename('Wrok', 'Work'));
  183. $this->assertTrue($tagger->hasTag('Work'));
  184. $this->assertFalse($tagger->hasTag('Wrok'));
  185. $this->assertFalse($tagger->rename('Wrok', 'Work')); // Rename non-existant tag.
  186. $this->assertFalse($tagger->rename('Work', 'Family')); // Collide with existing tag.
  187. }
  188. public function testTagAs() {
  189. $objids = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  190. $tagger = $this->tagMgr->load($this->objectType);
  191. foreach ($objids as $id) {
  192. $this->assertTrue($tagger->tagAs($id, 'Family'));
  193. }
  194. $this->assertEquals(1, count($tagger->getTags()));
  195. $this->assertEquals(9, count($tagger->getIdsForTag('Family')));
  196. }
  197. /**
  198. * @depends testTagAs
  199. */
  200. public function testUnTag() {
  201. $objIds = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  202. // Is this "legal"?
  203. $this->testTagAs();
  204. $tagger = $this->tagMgr->load($this->objectType);
  205. foreach ($objIds as $id) {
  206. $this->assertTrue(in_array($id, $tagger->getIdsForTag('Family')));
  207. $tagger->unTag($id, 'Family');
  208. $this->assertFalse(in_array($id, $tagger->getIdsForTag('Family')));
  209. }
  210. $this->assertEquals(1, count($tagger->getTags()));
  211. $this->assertEquals(0, count($tagger->getIdsForTag('Family')));
  212. }
  213. public function testFavorite() {
  214. $tagger = $this->tagMgr->load($this->objectType);
  215. $this->assertTrue($tagger->addToFavorites(1));
  216. $this->assertEquals([1], $tagger->getFavorites());
  217. $this->assertTrue($tagger->removeFromFavorites(1));
  218. $this->assertEquals([], $tagger->getFavorites());
  219. }
  220. }