vcategories.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Thomas Tanghus
  6. * @copyright 2012 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. //require_once("../lib/template.php");
  23. class Test_VCategories extends PHPUnit_Framework_TestCase {
  24. protected $objectType;
  25. protected $user;
  26. protected $backupGlobals = FALSE;
  27. public function setUp() {
  28. OC_User::clearBackends();
  29. OC_User::useBackend('dummy');
  30. $this->user = uniqid('user_');
  31. $this->objectType = uniqid('type_');
  32. OC_User::createUser($this->user, 'pass');
  33. OC_User::setUserId($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. $defcategories = array('Friends', 'Family', 'Work', 'Other');
  41. $catmgr = new OC_VCategories($this->objectType, $this->user, $defcategories);
  42. $this->assertEquals(4, count($catmgr->categories()));
  43. }
  44. public function testAddCategories() {
  45. $categories = array('Friends', 'Family', 'Work', 'Other');
  46. $catmgr = new OC_VCategories($this->objectType, $this->user);
  47. foreach($categories as $category) {
  48. $result = $catmgr->add($category);
  49. $this->assertTrue((bool)$result);
  50. }
  51. $this->assertFalse($catmgr->add('Family'));
  52. $this->assertFalse($catmgr->add('fAMILY'));
  53. $this->assertEquals(4, count($catmgr->categories()));
  54. }
  55. public function testdeleteCategories() {
  56. $defcategories = array('Friends', 'Family', 'Work', 'Other');
  57. $catmgr = new OC_VCategories($this->objectType, $this->user, $defcategories);
  58. $this->assertEquals(4, count($catmgr->categories()));
  59. $catmgr->delete('family');
  60. $this->assertEquals(3, count($catmgr->categories()));
  61. $catmgr->delete(array('Friends', 'Work', 'Other'));
  62. $this->assertEquals(0, count($catmgr->categories()));
  63. }
  64. public function testrenameCategory() {
  65. $defcategories = array('Friends', 'Family', 'Wrok', 'Other');
  66. $catmgr = new OC_VCategories($this->objectType, $this->user, $defcategories);
  67. $this->assertTrue($catmgr->rename('Wrok', 'Work'));
  68. $this->assertTrue($catmgr->hasCategory('Work'));
  69. $this->assertFalse($catmgr->hasCategory('Wrok'));
  70. $this->assertFalse($catmgr->rename('Wrok', 'Work'));
  71. }
  72. public function testAddToCategory() {
  73. $objids = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
  74. $catmgr = new OC_VCategories($this->objectType, $this->user);
  75. foreach($objids as $id) {
  76. $catmgr->addToCategory($id, 'Family');
  77. }
  78. $this->assertEquals(1, count($catmgr->categories()));
  79. $this->assertEquals(9, count($catmgr->idsForCategory('Family')));
  80. }
  81. /**
  82. * @depends testAddToCategory
  83. */
  84. public function testRemoveFromCategory() {
  85. $objids = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
  86. // Is this "legal"?
  87. $this->testAddToCategory();
  88. $catmgr = new OC_VCategories($this->objectType, $this->user);
  89. foreach($objids as $id) {
  90. $this->assertTrue(in_array($id, $catmgr->idsForCategory('Family')));
  91. $catmgr->removeFromCategory($id, 'Family');
  92. $this->assertFalse(in_array($id, $catmgr->idsForCategory('Family')));
  93. }
  94. $this->assertEquals(1, count($catmgr->categories()));
  95. $this->assertEquals(0, count($catmgr->idsForCategory('Family')));
  96. }
  97. }