You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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