GroupLDAPPluginTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 EITA Cooperative (eita.org.br)
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. * @author Vinicius Cubas Brand <vinicius@eita.org.br>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\User_LDAP\Tests;
  25. use OCA\User_LDAP\GroupPluginManager;
  26. use OCP\GroupInterface;
  27. class GroupLDAPPluginTest extends \Test\TestCase {
  28. /**
  29. * @return GroupPluginManager
  30. */
  31. private function getGroupPluginManager() {
  32. return new GroupPluginManager();
  33. }
  34. public function testImplementsActions() {
  35. $pluginManager = $this->getGroupPluginManager();
  36. $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPGroupPluginDummy')
  37. ->setMethods(['respondToActions'])
  38. ->getMock();
  39. $plugin->expects($this->any())
  40. ->method('respondToActions')
  41. ->willReturn(GroupInterface::CREATE_GROUP);
  42. $plugin2 = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPGroupPluginDummy')
  43. ->setMethods(['respondToActions'])
  44. ->getMock();
  45. $plugin2->expects($this->any())
  46. ->method('respondToActions')
  47. ->willReturn(GroupInterface::ADD_TO_GROUP);
  48. $pluginManager->register($plugin);
  49. $pluginManager->register($plugin2);
  50. $this->assertEquals($pluginManager->getImplementedActions(), GroupInterface::CREATE_GROUP | GroupInterface::ADD_TO_GROUP);
  51. $this->assertTrue($pluginManager->implementsActions(GroupInterface::CREATE_GROUP));
  52. $this->assertTrue($pluginManager->implementsActions(GroupInterface::ADD_TO_GROUP));
  53. }
  54. public function testCreateGroup() {
  55. $pluginManager = $this->getGroupPluginManager();
  56. $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPGroupPluginDummy')
  57. ->setMethods(['respondToActions', 'createGroup'])
  58. ->getMock();
  59. $plugin->expects($this->any())
  60. ->method('respondToActions')
  61. ->willReturn(GroupInterface::CREATE_GROUP);
  62. $plugin->expects($this->once())
  63. ->method('createGroup')
  64. ->with(
  65. $this->equalTo('group')
  66. );
  67. $pluginManager->register($plugin);
  68. $pluginManager->createGroup('group');
  69. }
  70. public function testCreateGroupNotRegistered() {
  71. $this->expectException(\Exception::class);
  72. $this->expectExceptionMessage('No plugin implements createGroup in this LDAP Backend.');
  73. $pluginManager = $this->getGroupPluginManager();
  74. $pluginManager->createGroup('foo');
  75. }
  76. public function testDeleteGroup() {
  77. $pluginManager = $this->getGroupPluginManager();
  78. $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPGroupPluginDummy')
  79. ->setMethods(['respondToActions', 'deleteGroup'])
  80. ->getMock();
  81. $plugin->expects($this->any())
  82. ->method('respondToActions')
  83. ->willReturn(GroupInterface::DELETE_GROUP);
  84. $plugin->expects($this->once())
  85. ->method('deleteGroup')
  86. ->with(
  87. $this->equalTo('group')
  88. );
  89. $pluginManager->register($plugin);
  90. $pluginManager->deleteGroup('group');
  91. }
  92. public function testDeleteGroupNotRegistered() {
  93. $this->expectException(\Exception::class);
  94. $this->expectExceptionMessage('No plugin implements deleteGroup in this LDAP Backend.');
  95. $pluginManager = $this->getGroupPluginManager();
  96. $pluginManager->deleteGroup('foo');
  97. }
  98. public function testAddToGroup() {
  99. $pluginManager = $this->getGroupPluginManager();
  100. $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPGroupPluginDummy')
  101. ->setMethods(['respondToActions', 'addToGroup'])
  102. ->getMock();
  103. $plugin->expects($this->any())
  104. ->method('respondToActions')
  105. ->willReturn(GroupInterface::ADD_TO_GROUP);
  106. $plugin->expects($this->once())
  107. ->method('addToGroup')
  108. ->with(
  109. $this->equalTo('uid'),
  110. $this->equalTo('gid')
  111. );
  112. $pluginManager->register($plugin);
  113. $pluginManager->addToGroup('uid', 'gid');
  114. }
  115. public function testAddToGroupNotRegistered() {
  116. $this->expectException(\Exception::class);
  117. $this->expectExceptionMessage('No plugin implements addToGroup in this LDAP Backend.');
  118. $pluginManager = $this->getGroupPluginManager();
  119. $pluginManager->addToGroup('foo', 'bar');
  120. }
  121. public function testRemoveFromGroup() {
  122. $pluginManager = $this->getGroupPluginManager();
  123. $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPGroupPluginDummy')
  124. ->setMethods(['respondToActions', 'removeFromGroup'])
  125. ->getMock();
  126. $plugin->expects($this->any())
  127. ->method('respondToActions')
  128. ->willReturn(GroupInterface::REMOVE_FROM_GROUP);
  129. $plugin->expects($this->once())
  130. ->method('removeFromGroup')
  131. ->with(
  132. $this->equalTo('uid'),
  133. $this->equalTo('gid')
  134. );
  135. $pluginManager->register($plugin);
  136. $pluginManager->removeFromGroup('uid', 'gid');
  137. }
  138. public function testRemoveFromGroupNotRegistered() {
  139. $this->expectException(\Exception::class);
  140. $this->expectExceptionMessage('No plugin implements removeFromGroup in this LDAP Backend.');
  141. $pluginManager = $this->getGroupPluginManager();
  142. $pluginManager->removeFromGroup('foo', 'bar');
  143. }
  144. public function testCountUsersInGroup() {
  145. $pluginManager = $this->getGroupPluginManager();
  146. $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPGroupPluginDummy')
  147. ->setMethods(['respondToActions', 'countUsersInGroup'])
  148. ->getMock();
  149. $plugin->expects($this->any())
  150. ->method('respondToActions')
  151. ->willReturn(GroupInterface::COUNT_USERS);
  152. $plugin->expects($this->once())
  153. ->method('countUsersInGroup')
  154. ->with(
  155. $this->equalTo('gid'),
  156. $this->equalTo('search')
  157. );
  158. $pluginManager->register($plugin);
  159. $pluginManager->countUsersInGroup('gid', 'search');
  160. }
  161. public function testCountUsersInGroupNotRegistered() {
  162. $this->expectException(\Exception::class);
  163. $this->expectExceptionMessage('No plugin implements countUsersInGroup in this LDAP Backend.');
  164. $pluginManager = $this->getGroupPluginManager();
  165. $pluginManager->countUsersInGroup('foo', 'bar');
  166. }
  167. public function testgetGroupDetails() {
  168. $pluginManager = $this->getGroupPluginManager();
  169. $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPGroupPluginDummy')
  170. ->setMethods(['respondToActions', 'getGroupDetails'])
  171. ->getMock();
  172. $plugin->expects($this->any())
  173. ->method('respondToActions')
  174. ->willReturn(GroupInterface::GROUP_DETAILS);
  175. $plugin->expects($this->once())
  176. ->method('getGroupDetails')
  177. ->with(
  178. $this->equalTo('gid')
  179. );
  180. $pluginManager->register($plugin);
  181. $pluginManager->getGroupDetails('gid');
  182. }
  183. public function testgetGroupDetailsNotRegistered() {
  184. $this->expectException(\Exception::class);
  185. $this->expectExceptionMessage('No plugin implements getGroupDetails in this LDAP Backend.');
  186. $pluginManager = $this->getGroupPluginManager();
  187. $pluginManager->getGroupDetails('foo');
  188. }
  189. }