GroupLDAPPluginTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\User_LDAP\Tests;
  7. use OCA\User_LDAP\GroupPluginManager;
  8. use OCP\GroupInterface;
  9. class GroupLDAPPluginTest extends \Test\TestCase {
  10. /**
  11. * @return GroupPluginManager
  12. */
  13. private function getGroupPluginManager() {
  14. return new GroupPluginManager();
  15. }
  16. public function testImplementsActions(): void {
  17. $pluginManager = $this->getGroupPluginManager();
  18. $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPGroupPluginDummy')
  19. ->setMethods(['respondToActions'])
  20. ->getMock();
  21. $plugin->expects($this->any())
  22. ->method('respondToActions')
  23. ->willReturn(GroupInterface::CREATE_GROUP);
  24. $plugin2 = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPGroupPluginDummy')
  25. ->setMethods(['respondToActions'])
  26. ->getMock();
  27. $plugin2->expects($this->any())
  28. ->method('respondToActions')
  29. ->willReturn(GroupInterface::ADD_TO_GROUP);
  30. $pluginManager->register($plugin);
  31. $pluginManager->register($plugin2);
  32. $this->assertEquals($pluginManager->getImplementedActions(), GroupInterface::CREATE_GROUP | GroupInterface::ADD_TO_GROUP);
  33. $this->assertTrue($pluginManager->implementsActions(GroupInterface::CREATE_GROUP));
  34. $this->assertTrue($pluginManager->implementsActions(GroupInterface::ADD_TO_GROUP));
  35. }
  36. public function testCreateGroup(): void {
  37. $pluginManager = $this->getGroupPluginManager();
  38. $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPGroupPluginDummy')
  39. ->setMethods(['respondToActions', 'createGroup'])
  40. ->getMock();
  41. $plugin->expects($this->any())
  42. ->method('respondToActions')
  43. ->willReturn(GroupInterface::CREATE_GROUP);
  44. $plugin->expects($this->once())
  45. ->method('createGroup')
  46. ->with(
  47. $this->equalTo('group')
  48. );
  49. $pluginManager->register($plugin);
  50. $pluginManager->createGroup('group');
  51. }
  52. public function testCreateGroupNotRegistered(): void {
  53. $this->expectException(\Exception::class);
  54. $this->expectExceptionMessage('No plugin implements createGroup in this LDAP Backend.');
  55. $pluginManager = $this->getGroupPluginManager();
  56. $pluginManager->createGroup('foo');
  57. }
  58. public function testDeleteGroup(): void {
  59. $pluginManager = $this->getGroupPluginManager();
  60. $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPGroupPluginDummy')
  61. ->setMethods(['respondToActions', 'deleteGroup'])
  62. ->getMock();
  63. $plugin->expects($this->any())
  64. ->method('respondToActions')
  65. ->willReturn(GroupInterface::DELETE_GROUP);
  66. $plugin->expects($this->once())
  67. ->method('deleteGroup')
  68. ->with(
  69. $this->equalTo('group')
  70. )->willReturn(true);
  71. $pluginManager->register($plugin);
  72. $this->assertTrue($pluginManager->deleteGroup('group'));
  73. }
  74. public function testDeleteGroupNotRegistered(): void {
  75. $this->expectException(\Exception::class);
  76. $this->expectExceptionMessage('No plugin implements deleteGroup in this LDAP Backend.');
  77. $pluginManager = $this->getGroupPluginManager();
  78. $pluginManager->deleteGroup('foo');
  79. }
  80. public function testAddToGroup(): void {
  81. $pluginManager = $this->getGroupPluginManager();
  82. $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPGroupPluginDummy')
  83. ->setMethods(['respondToActions', 'addToGroup'])
  84. ->getMock();
  85. $plugin->expects($this->any())
  86. ->method('respondToActions')
  87. ->willReturn(GroupInterface::ADD_TO_GROUP);
  88. $plugin->expects($this->once())
  89. ->method('addToGroup')
  90. ->with(
  91. $this->equalTo('uid'),
  92. $this->equalTo('gid')
  93. );
  94. $pluginManager->register($plugin);
  95. $pluginManager->addToGroup('uid', 'gid');
  96. }
  97. public function testAddToGroupNotRegistered(): void {
  98. $this->expectException(\Exception::class);
  99. $this->expectExceptionMessage('No plugin implements addToGroup in this LDAP Backend.');
  100. $pluginManager = $this->getGroupPluginManager();
  101. $pluginManager->addToGroup('foo', 'bar');
  102. }
  103. public function testRemoveFromGroup(): void {
  104. $pluginManager = $this->getGroupPluginManager();
  105. $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPGroupPluginDummy')
  106. ->setMethods(['respondToActions', 'removeFromGroup'])
  107. ->getMock();
  108. $plugin->expects($this->any())
  109. ->method('respondToActions')
  110. ->willReturn(GroupInterface::REMOVE_FROM_GROUP);
  111. $plugin->expects($this->once())
  112. ->method('removeFromGroup')
  113. ->with(
  114. $this->equalTo('uid'),
  115. $this->equalTo('gid')
  116. );
  117. $pluginManager->register($plugin);
  118. $pluginManager->removeFromGroup('uid', 'gid');
  119. }
  120. public function testRemoveFromGroupNotRegistered(): void {
  121. $this->expectException(\Exception::class);
  122. $this->expectExceptionMessage('No plugin implements removeFromGroup in this LDAP Backend.');
  123. $pluginManager = $this->getGroupPluginManager();
  124. $pluginManager->removeFromGroup('foo', 'bar');
  125. }
  126. public function testCountUsersInGroup(): void {
  127. $pluginManager = $this->getGroupPluginManager();
  128. $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPGroupPluginDummy')
  129. ->setMethods(['respondToActions', 'countUsersInGroup'])
  130. ->getMock();
  131. $plugin->expects($this->any())
  132. ->method('respondToActions')
  133. ->willReturn(GroupInterface::COUNT_USERS);
  134. $plugin->expects($this->once())
  135. ->method('countUsersInGroup')
  136. ->with(
  137. $this->equalTo('gid'),
  138. $this->equalTo('search')
  139. );
  140. $pluginManager->register($plugin);
  141. $pluginManager->countUsersInGroup('gid', 'search');
  142. }
  143. public function testCountUsersInGroupNotRegistered(): void {
  144. $this->expectException(\Exception::class);
  145. $this->expectExceptionMessage('No plugin implements countUsersInGroup in this LDAP Backend.');
  146. $pluginManager = $this->getGroupPluginManager();
  147. $pluginManager->countUsersInGroup('foo', 'bar');
  148. }
  149. public function testgetGroupDetails(): void {
  150. $pluginManager = $this->getGroupPluginManager();
  151. $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPGroupPluginDummy')
  152. ->setMethods(['respondToActions', 'getGroupDetails'])
  153. ->getMock();
  154. $plugin->expects($this->any())
  155. ->method('respondToActions')
  156. ->willReturn(GroupInterface::GROUP_DETAILS);
  157. $plugin->expects($this->once())
  158. ->method('getGroupDetails')
  159. ->with(
  160. $this->equalTo('gid')
  161. );
  162. $pluginManager->register($plugin);
  163. $pluginManager->getGroupDetails('gid');
  164. }
  165. public function testgetGroupDetailsNotRegistered(): void {
  166. $this->expectException(\Exception::class);
  167. $this->expectExceptionMessage('No plugin implements getGroupDetails in this LDAP Backend.');
  168. $pluginManager = $this->getGroupPluginManager();
  169. $pluginManager->getGroupDetails('foo');
  170. }
  171. }