UserLDAPPluginTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 OC\User\Backend;
  26. use OCA\User_LDAP\UserPluginManager;
  27. class UserLDAPPluginTest extends \Test\TestCase {
  28. /**
  29. * @return UserPluginManager
  30. */
  31. private function getUserPluginManager() {
  32. return new UserPluginManager();
  33. }
  34. public function testImplementsActions() {
  35. $pluginManager = $this->getUserPluginManager();
  36. $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPUserPluginDummy')
  37. ->setMethods(['respondToActions'])
  38. ->getMock();
  39. $plugin->expects($this->any())
  40. ->method('respondToActions')
  41. ->willReturn(Backend::CREATE_USER);
  42. $plugin2 = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPUserPluginDummy')
  43. ->setMethods(['respondToActions'])
  44. ->getMock();
  45. $plugin2->expects($this->any())
  46. ->method('respondToActions')
  47. ->willReturn(Backend::PROVIDE_AVATAR);
  48. $pluginManager->register($plugin);
  49. $pluginManager->register($plugin2);
  50. $this->assertEquals($pluginManager->getImplementedActions(), Backend::CREATE_USER | Backend::PROVIDE_AVATAR);
  51. $this->assertTrue($pluginManager->implementsActions(Backend::CREATE_USER));
  52. $this->assertTrue($pluginManager->implementsActions(Backend::PROVIDE_AVATAR));
  53. }
  54. public function testCreateUser() {
  55. $pluginManager = $this->getUserPluginManager();
  56. $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPUserPluginDummy')
  57. ->setMethods(['respondToActions', 'createUser'])
  58. ->getMock();
  59. $plugin->expects($this->any())
  60. ->method('respondToActions')
  61. ->willReturn(Backend::CREATE_USER);
  62. $plugin->expects($this->once())
  63. ->method('createUser')
  64. ->with(
  65. $this->equalTo('user'),
  66. $this->equalTo('password')
  67. );
  68. $pluginManager->register($plugin);
  69. $pluginManager->createUser('user', 'password');
  70. }
  71. public function testCreateUserNotRegistered() {
  72. $this->expectException(\Exception::class);
  73. $this->expectExceptionMessage('No plugin implements createUser in this LDAP Backend.');
  74. $pluginManager = $this->getUserPluginManager();
  75. $pluginManager->createUser('foo','bar');
  76. }
  77. public function testSetPassword() {
  78. $pluginManager = $this->getUserPluginManager();
  79. $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPUserPluginDummy')
  80. ->setMethods(['respondToActions', 'setPassword'])
  81. ->getMock();
  82. $plugin->expects($this->any())
  83. ->method('respondToActions')
  84. ->willReturn(Backend::SET_PASSWORD);
  85. $plugin->expects($this->once())
  86. ->method('setPassword')
  87. ->with(
  88. $this->equalTo('user'),
  89. $this->equalTo('password')
  90. );
  91. $pluginManager->register($plugin);
  92. $pluginManager->setPassword('user', 'password');
  93. }
  94. public function testSetPasswordNotRegistered() {
  95. $this->expectException(\Exception::class);
  96. $this->expectExceptionMessage('No plugin implements setPassword in this LDAP Backend.');
  97. $pluginManager = $this->getUserPluginManager();
  98. $pluginManager->setPassword('foo','bar');
  99. }
  100. public function testGetHome() {
  101. $pluginManager = $this->getUserPluginManager();
  102. $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPUserPluginDummy')
  103. ->setMethods(['respondToActions', 'getHome'])
  104. ->getMock();
  105. $plugin->expects($this->any())
  106. ->method('respondToActions')
  107. ->willReturn(Backend::GET_HOME);
  108. $plugin->expects($this->once())
  109. ->method('getHome')
  110. ->with(
  111. $this->equalTo('uid')
  112. );
  113. $pluginManager->register($plugin);
  114. $pluginManager->getHome('uid');
  115. }
  116. public function testGetHomeNotRegistered() {
  117. $this->expectException(\Exception::class);
  118. $this->expectExceptionMessage('No plugin implements getHome in this LDAP Backend.');
  119. $pluginManager = $this->getUserPluginManager();
  120. $pluginManager->getHome('foo');
  121. }
  122. public function testGetDisplayName() {
  123. $pluginManager = $this->getUserPluginManager();
  124. $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPUserPluginDummy')
  125. ->setMethods(['respondToActions', 'getDisplayName'])
  126. ->getMock();
  127. $plugin->expects($this->any())
  128. ->method('respondToActions')
  129. ->willReturn(Backend::GET_DISPLAYNAME);
  130. $plugin->expects($this->once())
  131. ->method('getDisplayName')
  132. ->with(
  133. $this->equalTo('uid')
  134. );
  135. $pluginManager->register($plugin);
  136. $pluginManager->getDisplayName('uid');
  137. }
  138. public function testGetDisplayNameNotRegistered() {
  139. $this->expectException(\Exception::class);
  140. $this->expectExceptionMessage('No plugin implements getDisplayName in this LDAP Backend.');
  141. $pluginManager = $this->getUserPluginManager();
  142. $pluginManager->getDisplayName('foo');
  143. }
  144. public function testSetDisplayName() {
  145. $pluginManager = $this->getUserPluginManager();
  146. $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPUserPluginDummy')
  147. ->setMethods(['respondToActions', 'setDisplayName'])
  148. ->getMock();
  149. $plugin->expects($this->any())
  150. ->method('respondToActions')
  151. ->willReturn(Backend::SET_DISPLAYNAME);
  152. $plugin->expects($this->once())
  153. ->method('setDisplayName')
  154. ->with(
  155. $this->equalTo('user'),
  156. $this->equalTo('password')
  157. );
  158. $pluginManager->register($plugin);
  159. $pluginManager->setDisplayName('user', 'password');
  160. }
  161. public function testSetDisplayNameNotRegistered() {
  162. $this->expectException(\Exception::class);
  163. $this->expectExceptionMessage('No plugin implements setDisplayName in this LDAP Backend.');
  164. $pluginManager = $this->getUserPluginManager();
  165. $pluginManager->setDisplayName('foo', 'bar');
  166. }
  167. public function testCanChangeAvatar() {
  168. $pluginManager = $this->getUserPluginManager();
  169. $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPUserPluginDummy')
  170. ->setMethods(['respondToActions', 'canChangeAvatar'])
  171. ->getMock();
  172. $plugin->expects($this->any())
  173. ->method('respondToActions')
  174. ->willReturn(Backend::PROVIDE_AVATAR);
  175. $plugin->expects($this->once())
  176. ->method('canChangeAvatar')
  177. ->with(
  178. $this->equalTo('uid')
  179. );
  180. $pluginManager->register($plugin);
  181. $pluginManager->canChangeAvatar('uid');
  182. }
  183. public function testCanChangeAvatarNotRegistered() {
  184. $this->expectException(\Exception::class);
  185. $this->expectExceptionMessage('No plugin implements canChangeAvatar in this LDAP Backend.');
  186. $pluginManager = $this->getUserPluginManager();
  187. $pluginManager->canChangeAvatar('foo');
  188. }
  189. public function testCountUsers() {
  190. $pluginManager = $this->getUserPluginManager();
  191. $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPUserPluginDummy')
  192. ->setMethods(['respondToActions', 'countUsers'])
  193. ->getMock();
  194. $plugin->expects($this->any())
  195. ->method('respondToActions')
  196. ->willReturn(Backend::COUNT_USERS);
  197. $plugin->expects($this->once())
  198. ->method('countUsers');
  199. $pluginManager->register($plugin);
  200. $pluginManager->countUsers();
  201. }
  202. public function testCountUsersNotRegistered() {
  203. $this->expectException(\Exception::class);
  204. $this->expectExceptionMessage('No plugin implements countUsers in this LDAP Backend.');
  205. $pluginManager = $this->getUserPluginManager();
  206. $pluginManager->countUsers();
  207. }
  208. public function testDeleteUser() {
  209. $pluginManager = $this->getUserPluginManager();
  210. $plugin = $this->getMockBuilder('OCA\User_LDAP\Tests\LDAPUserPluginDummy')
  211. ->setMethods(['respondToActions', 'canDeleteUser','deleteUser'])
  212. ->getMock();
  213. $plugin->expects($this->any())
  214. ->method('respondToActions')
  215. ->willReturn(0);
  216. $plugin->expects($this->any())
  217. ->method('canDeleteUser')
  218. ->willReturn(true);
  219. $plugin->expects($this->once())
  220. ->method('deleteUser')
  221. ->with(
  222. $this->equalTo('uid')
  223. );
  224. $this->assertFalse($pluginManager->canDeleteUser());
  225. $pluginManager->register($plugin);
  226. $this->assertTrue($pluginManager->canDeleteUser());
  227. $pluginManager->deleteUser('uid');
  228. }
  229. public function testDeleteUserNotRegistered() {
  230. $this->expectException(\Exception::class);
  231. $this->expectExceptionMessage('No plugin implements deleteUser in this LDAP Backend.');
  232. $pluginManager = $this->getUserPluginManager();
  233. $pluginManager->deleteUser('foo');
  234. }
  235. }