backend.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Robin Appelman
  6. * @copyright 2012 Robin Appelman icewind@owncloud.com
  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. abstract class Test_Group_Backend extends \Test\TestCase {
  23. /**
  24. * @var OC_Group_Backend $backend
  25. */
  26. protected $backend;
  27. /**
  28. * get a new unique group name
  29. * test cases can override this in order to clean up created groups
  30. * @return string
  31. */
  32. public function getGroupName($name = null) {
  33. if(is_null($name)) {
  34. return $this->getUniqueID('test_');
  35. } else {
  36. return $name;
  37. }
  38. }
  39. /**
  40. * get a new unique user name
  41. * test cases can override this in order to clean up created user
  42. * @return string
  43. */
  44. public function getUserName() {
  45. return $this->getUniqueID('test_');
  46. }
  47. public function testAddRemove() {
  48. //get the number of groups we start with, in case there are exising groups
  49. $startCount=count($this->backend->getGroups());
  50. $name1=$this->getGroupName();
  51. $name2=$this->getGroupName();
  52. $this->backend->createGroup($name1);
  53. $count=count($this->backend->getGroups())-$startCount;
  54. $this->assertEquals(1, $count);
  55. $this->assertTrue((array_search($name1, $this->backend->getGroups())!==false));
  56. $this->assertFalse((array_search($name2, $this->backend->getGroups())!==false));
  57. $this->backend->createGroup($name2);
  58. $count=count($this->backend->getGroups())-$startCount;
  59. $this->assertEquals(2, $count);
  60. $this->assertTrue((array_search($name1, $this->backend->getGroups())!==false));
  61. $this->assertTrue((array_search($name2, $this->backend->getGroups())!==false));
  62. $this->backend->deleteGroup($name2);
  63. $count=count($this->backend->getGroups())-$startCount;
  64. $this->assertEquals(1, $count);
  65. $this->assertTrue((array_search($name1, $this->backend->getGroups())!==false));
  66. $this->assertFalse((array_search($name2, $this->backend->getGroups())!==false));
  67. }
  68. public function testUser() {
  69. $group1=$this->getGroupName();
  70. $group2=$this->getGroupName();
  71. $this->backend->createGroup($group1);
  72. $this->backend->createGroup($group2);
  73. $user1=$this->getUserName();
  74. $user2=$this->getUserName();
  75. $this->assertFalse($this->backend->inGroup($user1, $group1));
  76. $this->assertFalse($this->backend->inGroup($user2, $group1));
  77. $this->assertFalse($this->backend->inGroup($user1, $group2));
  78. $this->assertFalse($this->backend->inGroup($user2, $group2));
  79. $this->assertTrue($this->backend->addToGroup($user1, $group1));
  80. $this->assertTrue($this->backend->inGroup($user1, $group1));
  81. $this->assertFalse($this->backend->inGroup($user2, $group1));
  82. $this->assertFalse($this->backend->inGroup($user1, $group2));
  83. $this->assertFalse($this->backend->inGroup($user2, $group2));
  84. $this->assertFalse($this->backend->addToGroup($user1, $group1));
  85. $this->assertEquals(array($user1), $this->backend->usersInGroup($group1));
  86. $this->assertEquals(array(), $this->backend->usersInGroup($group2));
  87. $this->assertEquals(array($group1), $this->backend->getUserGroups($user1));
  88. $this->assertEquals(array(), $this->backend->getUserGroups($user2));
  89. $this->backend->deleteGroup($group1);
  90. $this->assertEquals(array(), $this->backend->getUserGroups($user1));
  91. $this->assertEquals(array(), $this->backend->usersInGroup($group1));
  92. $this->assertFalse($this->backend->inGroup($user1, $group1));
  93. }
  94. public function testSearchGroups() {
  95. $name1 = $this->getGroupName('foobarbaz');
  96. $name2 = $this->getGroupName('bazbarfoo');
  97. $name3 = $this->getGroupName('notme');
  98. $this->backend->createGroup($name1);
  99. $this->backend->createGroup($name2);
  100. $this->backend->createGroup($name3);
  101. $result = $this->backend->getGroups('bar');
  102. $this->assertSame(2, count($result));
  103. }
  104. public function testSearchUsers() {
  105. $group = $this->getGroupName();
  106. $this->backend->createGroup($group);
  107. $name1 = 'foobarbaz';
  108. $name2 = 'bazbarfoo';
  109. $name3 = 'notme';
  110. $this->backend->addToGroup($name1, $group);
  111. $this->backend->addToGroup($name2, $group);
  112. $this->backend->addToGroup($name3, $group);
  113. $result = $this->backend->usersInGroup($group, 'bar');
  114. $this->assertSame(2, count($result));
  115. $result = $this->backend->countUsersInGroup($group, 'bar');
  116. $this->assertSame(2, $result);
  117. }
  118. }