GroupLDAPPluginTest.php 7.0 KB

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