manager.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\User;
  9. class Manager extends \PHPUnit_Framework_TestCase {
  10. public function testUserExistsSingleBackendExists() {
  11. /**
  12. * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  13. */
  14. $backend = $this->getMock('\OC_User_Dummy');
  15. $backend->expects($this->once())
  16. ->method('userExists')
  17. ->with($this->equalTo('foo'))
  18. ->will($this->returnValue(true));
  19. $manager = new \OC\User\Manager();
  20. $manager->registerBackend($backend);
  21. $this->assertTrue($manager->userExists('foo'));
  22. }
  23. public function testUserExistsSingleBackendNotExists() {
  24. /**
  25. * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  26. */
  27. $backend = $this->getMock('\OC_User_Dummy');
  28. $backend->expects($this->once())
  29. ->method('userExists')
  30. ->with($this->equalTo('foo'))
  31. ->will($this->returnValue(false));
  32. $manager = new \OC\User\Manager();
  33. $manager->registerBackend($backend);
  34. $this->assertFalse($manager->userExists('foo'));
  35. }
  36. public function testUserExistsNoBackends() {
  37. $manager = new \OC\User\Manager();
  38. $this->assertFalse($manager->userExists('foo'));
  39. }
  40. public function testUserExistsTwoBackendsSecondExists() {
  41. /**
  42. * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1
  43. */
  44. $backend1 = $this->getMock('\OC_User_Dummy');
  45. $backend1->expects($this->once())
  46. ->method('userExists')
  47. ->with($this->equalTo('foo'))
  48. ->will($this->returnValue(false));
  49. /**
  50. * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2
  51. */
  52. $backend2 = $this->getMock('\OC_User_Dummy');
  53. $backend2->expects($this->once())
  54. ->method('userExists')
  55. ->with($this->equalTo('foo'))
  56. ->will($this->returnValue(true));
  57. $manager = new \OC\User\Manager();
  58. $manager->registerBackend($backend1);
  59. $manager->registerBackend($backend2);
  60. $this->assertTrue($manager->userExists('foo'));
  61. }
  62. public function testUserExistsTwoBackendsFirstExists() {
  63. /**
  64. * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1
  65. */
  66. $backend1 = $this->getMock('\OC_User_Dummy');
  67. $backend1->expects($this->once())
  68. ->method('userExists')
  69. ->with($this->equalTo('foo'))
  70. ->will($this->returnValue(true));
  71. /**
  72. * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2
  73. */
  74. $backend2 = $this->getMock('\OC_User_Dummy');
  75. $backend2->expects($this->never())
  76. ->method('userExists');
  77. $manager = new \OC\User\Manager();
  78. $manager->registerBackend($backend1);
  79. $manager->registerBackend($backend2);
  80. $this->assertTrue($manager->userExists('foo'));
  81. }
  82. public function testCheckPassword() {
  83. /**
  84. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  85. */
  86. $backend = $this->getMock('\OC_User_Dummy');
  87. $backend->expects($this->once())
  88. ->method('checkPassword')
  89. ->with($this->equalTo('foo'), $this->equalTo('bar'))
  90. ->will($this->returnValue(true));
  91. $backend->expects($this->any())
  92. ->method('implementsActions')
  93. ->will($this->returnCallback(function ($actions) {
  94. if ($actions === \OC_USER_BACKEND_CHECK_PASSWORD) {
  95. return true;
  96. } else {
  97. return false;
  98. }
  99. }));
  100. $manager = new \OC\User\Manager();
  101. $manager->registerBackend($backend);
  102. $user = $manager->checkPassword('foo', 'bar');
  103. $this->assertTrue($user instanceof \OC\User\User);
  104. }
  105. public function testCheckPasswordNotSupported() {
  106. /**
  107. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  108. */
  109. $backend = $this->getMock('\OC_User_Dummy');
  110. $backend->expects($this->never())
  111. ->method('checkPassword');
  112. $backend->expects($this->any())
  113. ->method('implementsActions')
  114. ->will($this->returnValue(false));
  115. $manager = new \OC\User\Manager();
  116. $manager->registerBackend($backend);
  117. $this->assertFalse($manager->checkPassword('foo', 'bar'));
  118. }
  119. public function testGetOneBackendExists() {
  120. /**
  121. * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  122. */
  123. $backend = $this->getMock('\OC_User_Dummy');
  124. $backend->expects($this->once())
  125. ->method('userExists')
  126. ->with($this->equalTo('foo'))
  127. ->will($this->returnValue(true));
  128. $manager = new \OC\User\Manager();
  129. $manager->registerBackend($backend);
  130. $this->assertEquals('foo', $manager->get('foo')->getUID());
  131. }
  132. public function testGetOneBackendNotExists() {
  133. /**
  134. * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  135. */
  136. $backend = $this->getMock('\OC_User_Dummy');
  137. $backend->expects($this->once())
  138. ->method('userExists')
  139. ->with($this->equalTo('foo'))
  140. ->will($this->returnValue(false));
  141. $manager = new \OC\User\Manager();
  142. $manager->registerBackend($backend);
  143. $this->assertEquals(null, $manager->get('foo'));
  144. }
  145. public function testSearchOneBackend() {
  146. /**
  147. * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  148. */
  149. $backend = $this->getMock('\OC_User_Dummy');
  150. $backend->expects($this->once())
  151. ->method('getUsers')
  152. ->with($this->equalTo('fo'))
  153. ->will($this->returnValue(array('foo', 'afoo')));
  154. $manager = new \OC\User\Manager();
  155. $manager->registerBackend($backend);
  156. $result = $manager->search('fo');
  157. $this->assertEquals(2, count($result));
  158. $this->assertEquals('afoo', array_shift($result)->getUID());
  159. $this->assertEquals('foo', array_shift($result)->getUID());
  160. }
  161. public function testSearchTwoBackendLimitOffset() {
  162. /**
  163. * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1
  164. */
  165. $backend1 = $this->getMock('\OC_User_Dummy');
  166. $backend1->expects($this->once())
  167. ->method('getUsers')
  168. ->with($this->equalTo('fo'), $this->equalTo(3), $this->equalTo(1))
  169. ->will($this->returnValue(array('foo1', 'foo2')));
  170. /**
  171. * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2
  172. */
  173. $backend2 = $this->getMock('\OC_User_Dummy');
  174. $backend2->expects($this->once())
  175. ->method('getUsers')
  176. ->with($this->equalTo('fo'), $this->equalTo(3), $this->equalTo(1))
  177. ->will($this->returnValue(array('foo3')));
  178. $manager = new \OC\User\Manager();
  179. $manager->registerBackend($backend1);
  180. $manager->registerBackend($backend2);
  181. $result = $manager->search('fo', 3, 1);
  182. $this->assertEquals(3, count($result));
  183. $this->assertEquals('foo1', array_shift($result)->getUID());
  184. $this->assertEquals('foo2', array_shift($result)->getUID());
  185. $this->assertEquals('foo3', array_shift($result)->getUID());
  186. }
  187. public function testCreateUserSingleBackendNotExists() {
  188. /**
  189. * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  190. */
  191. $backend = $this->getMock('\OC_User_Dummy');
  192. $backend->expects($this->any())
  193. ->method('implementsActions')
  194. ->will($this->returnValue(true));
  195. $backend->expects($this->once())
  196. ->method('createUser')
  197. ->with($this->equalTo('foo'), $this->equalTo('bar'));
  198. $backend->expects($this->once())
  199. ->method('userExists')
  200. ->with($this->equalTo('foo'))
  201. ->will($this->returnValue(false));
  202. $manager = new \OC\User\Manager();
  203. $manager->registerBackend($backend);
  204. $user = $manager->createUser('foo', 'bar');
  205. $this->assertEquals('foo', $user->getUID());
  206. }
  207. /**
  208. * @expectedException \Exception
  209. */
  210. public function testCreateUserSingleBackendExists() {
  211. /**
  212. * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  213. */
  214. $backend = $this->getMock('\OC_User_Dummy');
  215. $backend->expects($this->any())
  216. ->method('implementsActions')
  217. ->will($this->returnValue(true));
  218. $backend->expects($this->never())
  219. ->method('createUser');
  220. $backend->expects($this->once())
  221. ->method('userExists')
  222. ->with($this->equalTo('foo'))
  223. ->will($this->returnValue(true));
  224. $manager = new \OC\User\Manager();
  225. $manager->registerBackend($backend);
  226. $manager->createUser('foo', 'bar');
  227. }
  228. public function testCreateUserSingleBackendNotSupported() {
  229. /**
  230. * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  231. */
  232. $backend = $this->getMock('\OC_User_Dummy');
  233. $backend->expects($this->any())
  234. ->method('implementsActions')
  235. ->will($this->returnValue(false));
  236. $backend->expects($this->never())
  237. ->method('createUser');
  238. $backend->expects($this->once())
  239. ->method('userExists')
  240. ->with($this->equalTo('foo'))
  241. ->will($this->returnValue(false));
  242. $manager = new \OC\User\Manager();
  243. $manager->registerBackend($backend);
  244. $this->assertFalse($manager->createUser('foo', 'bar'));
  245. }
  246. public function testCreateUserNoBackends() {
  247. $manager = new \OC\User\Manager();
  248. $this->assertFalse($manager->createUser('foo', 'bar'));
  249. }
  250. /**
  251. * @expectedException \Exception
  252. */
  253. public function testCreateUserTwoBackendExists() {
  254. /**
  255. * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1
  256. */
  257. $backend1 = $this->getMock('\OC_User_Dummy');
  258. $backend1->expects($this->any())
  259. ->method('implementsActions')
  260. ->will($this->returnValue(true));
  261. $backend1->expects($this->never())
  262. ->method('createUser');
  263. $backend1->expects($this->once())
  264. ->method('userExists')
  265. ->with($this->equalTo('foo'))
  266. ->will($this->returnValue(false));
  267. /**
  268. * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2
  269. */
  270. $backend2 = $this->getMock('\OC_User_Dummy');
  271. $backend2->expects($this->any())
  272. ->method('implementsActions')
  273. ->will($this->returnValue(true));
  274. $backend2->expects($this->never())
  275. ->method('createUser');
  276. $backend2->expects($this->once())
  277. ->method('userExists')
  278. ->with($this->equalTo('foo'))
  279. ->will($this->returnValue(true));
  280. $manager = new \OC\User\Manager();
  281. $manager->registerBackend($backend1);
  282. $manager->registerBackend($backend2);
  283. $manager->createUser('foo', 'bar');
  284. }
  285. public function testCountUsersNoBackend() {
  286. $manager = new \OC\User\Manager();
  287. $result = $manager->countUsers();
  288. $this->assertTrue(is_array($result));
  289. $this->assertTrue(empty($result));
  290. }
  291. public function testCountUsersOneBackend() {
  292. /**
  293. * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  294. */
  295. $backend = $this->getMock('\OC_User_Dummy');
  296. $backend->expects($this->once())
  297. ->method('countUsers')
  298. ->will($this->returnValue(7));
  299. $backend->expects($this->once())
  300. ->method('implementsActions')
  301. ->with(\OC_USER_BACKEND_COUNT_USERS)
  302. ->will($this->returnValue(true));
  303. $manager = new \OC\User\Manager();
  304. $manager->registerBackend($backend);
  305. $result = $manager->countUsers();
  306. $keys = array_keys($result);
  307. $this->assertTrue(strpos($keys[0], 'Mock_OC_User_Dummy') !== false);
  308. $users = array_shift($result);
  309. $this->assertEquals(7, $users);
  310. }
  311. public function testCountUsersTwoBackends() {
  312. /**
  313. * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  314. */
  315. $backend1 = $this->getMock('\OC_User_Dummy');
  316. $backend1->expects($this->once())
  317. ->method('countUsers')
  318. ->will($this->returnValue(7));
  319. $backend1->expects($this->once())
  320. ->method('implementsActions')
  321. ->with(\OC_USER_BACKEND_COUNT_USERS)
  322. ->will($this->returnValue(true));
  323. $backend2 = $this->getMock('\OC_User_Dummy');
  324. $backend2->expects($this->once())
  325. ->method('countUsers')
  326. ->will($this->returnValue(16));
  327. $backend2->expects($this->once())
  328. ->method('implementsActions')
  329. ->with(\OC_USER_BACKEND_COUNT_USERS)
  330. ->will($this->returnValue(true));
  331. $manager = new \OC\User\Manager();
  332. $manager->registerBackend($backend1);
  333. $manager->registerBackend($backend2);
  334. $result = $manager->countUsers();
  335. //because the backends have the same class name, only one value expected
  336. $this->assertEquals(1, count($result));
  337. $keys = array_keys($result);
  338. $this->assertTrue(strpos($keys[0], 'Mock_OC_User_Dummy') !== false);
  339. $users = array_shift($result);
  340. //users from backends shall be summed up
  341. $this->assertEquals(7+16, $users);
  342. }
  343. }