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.

tags.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. class Test_Tags extends PHPUnit_Framework_TestCase {
  23. protected $objectType;
  24. protected $user;
  25. protected $backupGlobals = FALSE;
  26. public function setUp() {
  27. OC_User::clearBackends();
  28. OC_User::useBackend('dummy');
  29. $this->user = uniqid('user_');
  30. $this->objectType = uniqid('type_');
  31. OC_User::createUser($this->user, 'pass');
  32. OC_User::setUserId($this->user);
  33. $this->tagMgr = new OC\TagManager($this->user);
  34. }
  35. public function tearDown() {
  36. //$query = OC_DB::prepare('DELETE FROM `*PREFIX*vcategories` WHERE `item_type` = ?');
  37. //$query->execute(array('test'));
  38. }
  39. public function testInstantiateWithDefaults() {
  40. $defaultTags = array('Friends', 'Family', 'Work', 'Other');
  41. $tagger = $this->tagMgr->load($this->objectType, $defaultTags);
  42. $this->assertEquals(4, count($tagger->getTags()));
  43. }
  44. public function testAddTags() {
  45. $tags = array('Friends', 'Family', 'Work', 'Other');
  46. $tagger = $this->tagMgr->load($this->objectType);
  47. foreach($tags as $tag) {
  48. $result = $tagger->add($tag);
  49. $this->assertGreaterThan(0, $result, 'add() returned an ID <= 0');
  50. $this->assertTrue((bool)$result);
  51. }
  52. $this->assertFalse($tagger->add('Family'));
  53. $this->assertFalse($tagger->add('fAMILY'));
  54. $this->assertCount(4, $tagger->getTags(), 'Wrong number of added tags');
  55. }
  56. public function testAddMultiple() {
  57. $tags = array('Friends', 'Family', 'Work', 'Other');
  58. $tagger = $this->tagMgr->load($this->objectType);
  59. foreach($tags as $tag) {
  60. $this->assertFalse($tagger->hasTag($tag));
  61. }
  62. $result = $tagger->addMultiple($tags);
  63. $this->assertTrue((bool)$result);
  64. foreach($tags as $tag) {
  65. $this->assertTrue($tagger->hasTag($tag));
  66. }
  67. $this->assertCount(4, $tagger->getTags(), 'Not all tags added');
  68. }
  69. public function testIsEmpty() {
  70. $tagger = $this->tagMgr->load($this->objectType);
  71. $this->assertEquals(0, count($tagger->getTags()));
  72. $this->assertTrue($tagger->isEmpty());
  73. $result = $tagger->add('Tag');
  74. $this->assertGreaterThan(0, $result, 'add() returned an ID <= 0');
  75. $this->assertNotEquals(false, $result, 'add() returned false');
  76. $this->assertFalse($tagger->isEmpty());
  77. }
  78. public function testdeleteTags() {
  79. $defaultTags = array('Friends', 'Family', 'Work', 'Other');
  80. $tagger = $this->tagMgr->load($this->objectType, $defaultTags);
  81. $this->assertEquals(4, count($tagger->getTags()));
  82. $tagger->delete('family');
  83. $this->assertEquals(3, count($tagger->getTags()));
  84. $tagger->delete(array('Friends', 'Work', 'Other'));
  85. $this->assertEquals(0, count($tagger->getTags()));
  86. }
  87. public function testRenameTag() {
  88. $defaultTags = array('Friends', 'Family', 'Wrok', 'Other');
  89. $tagger = $this->tagMgr->load($this->objectType, $defaultTags);
  90. $this->assertTrue($tagger->rename('Wrok', 'Work'));
  91. $this->assertTrue($tagger->hasTag('Work'));
  92. $this->assertFalse($tagger->hastag('Wrok'));
  93. $this->assertFalse($tagger->rename('Wrok', 'Work'));
  94. }
  95. public function testTagAs() {
  96. $objids = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
  97. $tagger = $this->tagMgr->load($this->objectType);
  98. foreach($objids as $id) {
  99. $this->assertTrue($tagger->tagAs($id, 'Family'));
  100. }
  101. $this->assertEquals(1, count($tagger->getTags()));
  102. $this->assertEquals(9, count($tagger->getIdsForTag('Family')));
  103. }
  104. /**
  105. * @depends testTagAs
  106. */
  107. public function testUnTag() {
  108. $objIds = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
  109. // Is this "legal"?
  110. $this->testTagAs();
  111. $tagger = $this->tagMgr->load($this->objectType);
  112. foreach($objIds as $id) {
  113. $this->assertTrue(in_array($id, $tagger->getIdsForTag('Family')));
  114. $tagger->unTag($id, 'Family');
  115. $this->assertFalse(in_array($id, $tagger->getIdsForTag('Family')));
  116. }
  117. $this->assertEquals(1, count($tagger->getTags()));
  118. $this->assertEquals(0, count($tagger->getIdsForTag('Family')));
  119. }
  120. public function testFavorite() {
  121. $tagger = $this->tagMgr->load($this->objectType);
  122. $this->assertTrue($tagger->addToFavorites(1));
  123. $this->assertTrue($tagger->removeFromFavorites(1));
  124. }
  125. }