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.

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