UserLDAPPluginTest.php 8.5 KB

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