GroupLDAPPluginTest.php 7.2 KB

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