backend.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Felix Moeller <mail@felixmoeller.de>
  5. * @author Joas Schilling <nickvergessen@owncloud.com>
  6. * @author Robin Appelman <icewind@owncloud.com>
  7. * @author Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @copyright Copyright (c) 2015, ownCloud, Inc.
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. /**
  27. * Class Test_Group_Backend
  28. *
  29. * @group DB
  30. */
  31. abstract class Test_Group_Backend extends \Test\TestCase {
  32. /**
  33. * @var OC_Group_Backend $backend
  34. */
  35. protected $backend;
  36. /**
  37. * get a new unique group name
  38. * test cases can override this in order to clean up created groups
  39. *
  40. * @return string
  41. */
  42. public function getGroupName($name = null) {
  43. if (is_null($name)) {
  44. return $this->getUniqueID('test_');
  45. } else {
  46. return $name;
  47. }
  48. }
  49. /**
  50. * get a new unique user name
  51. * test cases can override this in order to clean up created user
  52. *
  53. * @return string
  54. */
  55. public function getUserName() {
  56. return $this->getUniqueID('test_');
  57. }
  58. public function testAddRemove() {
  59. //get the number of groups we start with, in case there are exising groups
  60. $startCount = count($this->backend->getGroups());
  61. $name1 = $this->getGroupName();
  62. $name2 = $this->getGroupName();
  63. $this->backend->createGroup($name1);
  64. $count = count($this->backend->getGroups()) - $startCount;
  65. $this->assertEquals(1, $count);
  66. $this->assertTrue((array_search($name1, $this->backend->getGroups()) !== false));
  67. $this->assertFalse((array_search($name2, $this->backend->getGroups()) !== false));
  68. $this->backend->createGroup($name2);
  69. $count = count($this->backend->getGroups()) - $startCount;
  70. $this->assertEquals(2, $count);
  71. $this->assertTrue((array_search($name1, $this->backend->getGroups()) !== false));
  72. $this->assertTrue((array_search($name2, $this->backend->getGroups()) !== false));
  73. $this->backend->deleteGroup($name2);
  74. $count = count($this->backend->getGroups()) - $startCount;
  75. $this->assertEquals(1, $count);
  76. $this->assertTrue((array_search($name1, $this->backend->getGroups()) !== false));
  77. $this->assertFalse((array_search($name2, $this->backend->getGroups()) !== false));
  78. }
  79. public function testUser() {
  80. $group1 = $this->getGroupName();
  81. $group2 = $this->getGroupName();
  82. $this->backend->createGroup($group1);
  83. $this->backend->createGroup($group2);
  84. $user1 = $this->getUserName();
  85. $user2 = $this->getUserName();
  86. $this->assertFalse($this->backend->inGroup($user1, $group1));
  87. $this->assertFalse($this->backend->inGroup($user2, $group1));
  88. $this->assertFalse($this->backend->inGroup($user1, $group2));
  89. $this->assertFalse($this->backend->inGroup($user2, $group2));
  90. $this->assertTrue($this->backend->addToGroup($user1, $group1));
  91. $this->assertTrue($this->backend->inGroup($user1, $group1));
  92. $this->assertFalse($this->backend->inGroup($user2, $group1));
  93. $this->assertFalse($this->backend->inGroup($user1, $group2));
  94. $this->assertFalse($this->backend->inGroup($user2, $group2));
  95. $this->assertFalse($this->backend->addToGroup($user1, $group1));
  96. $this->assertEquals(array($user1), $this->backend->usersInGroup($group1));
  97. $this->assertEquals(array(), $this->backend->usersInGroup($group2));
  98. $this->assertEquals(array($group1), $this->backend->getUserGroups($user1));
  99. $this->assertEquals(array(), $this->backend->getUserGroups($user2));
  100. $this->backend->deleteGroup($group1);
  101. $this->assertEquals(array(), $this->backend->getUserGroups($user1));
  102. $this->assertEquals(array(), $this->backend->usersInGroup($group1));
  103. $this->assertFalse($this->backend->inGroup($user1, $group1));
  104. }
  105. public function testSearchGroups() {
  106. $name1 = $this->getGroupName('foobarbaz');
  107. $name2 = $this->getGroupName('bazbarfoo');
  108. $name3 = $this->getGroupName('notme');
  109. $this->backend->createGroup($name1);
  110. $this->backend->createGroup($name2);
  111. $this->backend->createGroup($name3);
  112. $result = $this->backend->getGroups('bar');
  113. $this->assertSame(2, count($result));
  114. }
  115. public function testSearchUsers() {
  116. $group = $this->getGroupName();
  117. $this->backend->createGroup($group);
  118. $name1 = 'foobarbaz';
  119. $name2 = 'bazbarfoo';
  120. $name3 = 'notme';
  121. $this->backend->addToGroup($name1, $group);
  122. $this->backend->addToGroup($name2, $group);
  123. $this->backend->addToGroup($name3, $group);
  124. $result = $this->backend->usersInGroup($group, 'bar');
  125. $this->assertSame(2, count($result));
  126. $result = $this->backend->countUsersInGroup($group, 'bar');
  127. $this->assertSame(2, $result);
  128. }
  129. public function testAddDouble() {
  130. $group = $this->getGroupName();
  131. $this->backend->createGroup($group);
  132. $this->backend->createGroup($group);
  133. }
  134. }