1
0

UserLDAPPluginTest.php 7.9 KB

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