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 9.4KB

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