1
0

Backend.php 5.2 KB

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