1
0

UserLDAPPluginTest.php 8.7 KB

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