Backend.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test\Group;
  8. /**
  9. * Class Backend
  10. *
  11. * @group DB
  12. */
  13. abstract class Backend extends \Test\TestCase {
  14. /**
  15. * @var \OC\Group\Backend $backend
  16. */
  17. protected $backend;
  18. /**
  19. * get a new unique group name
  20. * test cases can override this in order to clean up created groups
  21. *
  22. * @return string
  23. */
  24. public function getGroupName($name = null) {
  25. if (is_null($name)) {
  26. return $this->getUniqueID('test_');
  27. } else {
  28. return $name;
  29. }
  30. }
  31. /**
  32. * get a new unique user name
  33. * test cases can override this in order to clean up created user
  34. *
  35. * @return string
  36. */
  37. public function getUserName() {
  38. return $this->getUniqueID('test_');
  39. }
  40. public function testAddRemove(): void {
  41. //get the number of groups we start with, in case there are exising groups
  42. $startCount = count($this->backend->getGroups());
  43. $name1 = $this->getGroupName();
  44. $name2 = $this->getGroupName();
  45. $this->backend->createGroup($name1);
  46. $count = count($this->backend->getGroups()) - $startCount;
  47. $this->assertEquals(1, $count);
  48. $this->assertTrue((array_search($name1, $this->backend->getGroups()) !== false));
  49. $this->assertFalse((array_search($name2, $this->backend->getGroups()) !== false));
  50. $this->backend->createGroup($name2);
  51. $count = count($this->backend->getGroups()) - $startCount;
  52. $this->assertEquals(2, $count);
  53. $this->assertTrue((array_search($name1, $this->backend->getGroups()) !== false));
  54. $this->assertTrue((array_search($name2, $this->backend->getGroups()) !== false));
  55. $this->backend->deleteGroup($name2);
  56. $count = count($this->backend->getGroups()) - $startCount;
  57. $this->assertEquals(1, $count);
  58. $this->assertTrue((array_search($name1, $this->backend->getGroups()) !== false));
  59. $this->assertFalse((array_search($name2, $this->backend->getGroups()) !== false));
  60. }
  61. public function testUser(): void {
  62. $group1 = $this->getGroupName();
  63. $group2 = $this->getGroupName();
  64. $this->backend->createGroup($group1);
  65. $this->backend->createGroup($group2);
  66. $user1 = $this->getUserName();
  67. $user2 = $this->getUserName();
  68. $this->assertFalse($this->backend->inGroup($user1, $group1));
  69. $this->assertFalse($this->backend->inGroup($user2, $group1));
  70. $this->assertFalse($this->backend->inGroup($user1, $group2));
  71. $this->assertFalse($this->backend->inGroup($user2, $group2));
  72. $this->assertTrue($this->backend->addToGroup($user1, $group1));
  73. $this->assertTrue($this->backend->inGroup($user1, $group1));
  74. $this->assertFalse($this->backend->inGroup($user2, $group1));
  75. $this->assertFalse($this->backend->inGroup($user1, $group2));
  76. $this->assertFalse($this->backend->inGroup($user2, $group2));
  77. $this->assertFalse($this->backend->addToGroup($user1, $group1));
  78. $this->assertEquals([$user1], $this->backend->usersInGroup($group1));
  79. $this->assertEquals([], $this->backend->usersInGroup($group2));
  80. $this->assertEquals([$group1], $this->backend->getUserGroups($user1));
  81. $this->assertEquals([], $this->backend->getUserGroups($user2));
  82. $this->backend->deleteGroup($group1);
  83. $this->assertEquals([], $this->backend->getUserGroups($user1));
  84. $this->assertEquals([], $this->backend->usersInGroup($group1));
  85. $this->assertFalse($this->backend->inGroup($user1, $group1));
  86. }
  87. public function testSearchGroups(): void {
  88. $name1 = $this->getGroupName('foobarbaz');
  89. $name2 = $this->getGroupName('bazbarfoo');
  90. $name3 = $this->getGroupName('notme');
  91. $this->backend->createGroup($name1);
  92. $this->backend->createGroup($name2);
  93. $this->backend->createGroup($name3);
  94. $result = $this->backend->getGroups('bar');
  95. $this->assertSame(2, count($result));
  96. }
  97. public function testSearchUsers(): void {
  98. $group = $this->getGroupName();
  99. $this->backend->createGroup($group);
  100. $name1 = 'foobarbaz';
  101. $name2 = 'bazbarfoo';
  102. $name3 = 'notme';
  103. $this->backend->addToGroup($name1, $group);
  104. $this->backend->addToGroup($name2, $group);
  105. $this->backend->addToGroup($name3, $group);
  106. $result = $this->backend->usersInGroup($group, 'bar');
  107. $this->assertSame(2, count($result));
  108. $result = $this->backend->countUsersInGroup($group, 'bar');
  109. $this->assertSame(2, $result);
  110. }
  111. public function testAddDouble(): void {
  112. $group = $this->getGroupName();
  113. $this->backend->createGroup($group);
  114. $this->backend->createGroup($group);
  115. $this->addToAssertionCount(1);
  116. }
  117. }