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.

TagsTest.php 8.5KB

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