ManagerTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. use OC\User\Database;
  10. /**
  11. * Class ManagerTest
  12. *
  13. * @group DB
  14. *
  15. * @package Test\User
  16. */
  17. class ManagerTest extends \Test\TestCase {
  18. public function testGetBackends() {
  19. $userDummyBackend = $this->createMock(\Test\Util\User\Dummy::class);
  20. $manager = new \OC\User\Manager();
  21. $manager->registerBackend($userDummyBackend);
  22. $this->assertEquals([$userDummyBackend], $manager->getBackends());
  23. $dummyDatabaseBackend = $this->createMock(Database::class);
  24. $manager->registerBackend($dummyDatabaseBackend);
  25. $this->assertEquals([$userDummyBackend, $dummyDatabaseBackend], $manager->getBackends());
  26. }
  27. public function testUserExistsSingleBackendExists() {
  28. /**
  29. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  30. */
  31. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  32. $backend->expects($this->once())
  33. ->method('userExists')
  34. ->with($this->equalTo('foo'))
  35. ->will($this->returnValue(true));
  36. $manager = new \OC\User\Manager();
  37. $manager->registerBackend($backend);
  38. $this->assertTrue($manager->userExists('foo'));
  39. }
  40. public function testUserExistsSingleBackendNotExists() {
  41. /**
  42. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  43. */
  44. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  45. $backend->expects($this->once())
  46. ->method('userExists')
  47. ->with($this->equalTo('foo'))
  48. ->will($this->returnValue(false));
  49. $manager = new \OC\User\Manager();
  50. $manager->registerBackend($backend);
  51. $this->assertFalse($manager->userExists('foo'));
  52. }
  53. public function testUserExistsNoBackends() {
  54. $manager = new \OC\User\Manager();
  55. $this->assertFalse($manager->userExists('foo'));
  56. }
  57. public function testUserExistsTwoBackendsSecondExists() {
  58. /**
  59. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1
  60. */
  61. $backend1 = $this->createMock(\Test\Util\User\Dummy::class);
  62. $backend1->expects($this->once())
  63. ->method('userExists')
  64. ->with($this->equalTo('foo'))
  65. ->will($this->returnValue(false));
  66. /**
  67. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2
  68. */
  69. $backend2 = $this->createMock(\Test\Util\User\Dummy::class);
  70. $backend2->expects($this->once())
  71. ->method('userExists')
  72. ->with($this->equalTo('foo'))
  73. ->will($this->returnValue(true));
  74. $manager = new \OC\User\Manager();
  75. $manager->registerBackend($backend1);
  76. $manager->registerBackend($backend2);
  77. $this->assertTrue($manager->userExists('foo'));
  78. }
  79. public function testUserExistsTwoBackendsFirstExists() {
  80. /**
  81. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1
  82. */
  83. $backend1 = $this->createMock(\Test\Util\User\Dummy::class);
  84. $backend1->expects($this->once())
  85. ->method('userExists')
  86. ->with($this->equalTo('foo'))
  87. ->will($this->returnValue(true));
  88. /**
  89. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2
  90. */
  91. $backend2 = $this->createMock(\Test\Util\User\Dummy::class);
  92. $backend2->expects($this->never())
  93. ->method('userExists');
  94. $manager = new \OC\User\Manager();
  95. $manager->registerBackend($backend1);
  96. $manager->registerBackend($backend2);
  97. $this->assertTrue($manager->userExists('foo'));
  98. }
  99. public function testCheckPassword() {
  100. /**
  101. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  102. */
  103. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  104. $backend->expects($this->once())
  105. ->method('checkPassword')
  106. ->with($this->equalTo('foo'), $this->equalTo('bar'))
  107. ->will($this->returnValue(true));
  108. $backend->expects($this->any())
  109. ->method('implementsActions')
  110. ->will($this->returnCallback(function ($actions) {
  111. if ($actions === \OC\USER\BACKEND::CHECK_PASSWORD) {
  112. return true;
  113. } else {
  114. return false;
  115. }
  116. }));
  117. $manager = new \OC\User\Manager();
  118. $manager->registerBackend($backend);
  119. $user = $manager->checkPassword('foo', 'bar');
  120. $this->assertTrue($user instanceof \OC\User\User);
  121. }
  122. public function testCheckPasswordNotSupported() {
  123. /**
  124. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  125. */
  126. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  127. $backend->expects($this->never())
  128. ->method('checkPassword');
  129. $backend->expects($this->any())
  130. ->method('implementsActions')
  131. ->will($this->returnValue(false));
  132. $manager = new \OC\User\Manager();
  133. $manager->registerBackend($backend);
  134. $this->assertFalse($manager->checkPassword('foo', 'bar'));
  135. }
  136. public function testGetOneBackendExists() {
  137. /**
  138. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  139. */
  140. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  141. $backend->expects($this->once())
  142. ->method('userExists')
  143. ->with($this->equalTo('foo'))
  144. ->will($this->returnValue(true));
  145. $manager = new \OC\User\Manager();
  146. $manager->registerBackend($backend);
  147. $this->assertEquals('foo', $manager->get('foo')->getUID());
  148. }
  149. public function testGetOneBackendNotExists() {
  150. /**
  151. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  152. */
  153. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  154. $backend->expects($this->once())
  155. ->method('userExists')
  156. ->with($this->equalTo('foo'))
  157. ->will($this->returnValue(false));
  158. $manager = new \OC\User\Manager();
  159. $manager->registerBackend($backend);
  160. $this->assertEquals(null, $manager->get('foo'));
  161. }
  162. public function testSearchOneBackend() {
  163. /**
  164. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  165. */
  166. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  167. $backend->expects($this->once())
  168. ->method('getUsers')
  169. ->with($this->equalTo('fo'))
  170. ->will($this->returnValue(array('foo', 'afoo')));
  171. $manager = new \OC\User\Manager();
  172. $manager->registerBackend($backend);
  173. $result = $manager->search('fo');
  174. $this->assertEquals(2, count($result));
  175. $this->assertEquals('afoo', array_shift($result)->getUID());
  176. $this->assertEquals('foo', array_shift($result)->getUID());
  177. }
  178. public function testSearchTwoBackendLimitOffset() {
  179. /**
  180. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1
  181. */
  182. $backend1 = $this->createMock(\Test\Util\User\Dummy::class);
  183. $backend1->expects($this->once())
  184. ->method('getUsers')
  185. ->with($this->equalTo('fo'), $this->equalTo(3), $this->equalTo(1))
  186. ->will($this->returnValue(array('foo1', 'foo2')));
  187. /**
  188. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2
  189. */
  190. $backend2 = $this->createMock(\Test\Util\User\Dummy::class);
  191. $backend2->expects($this->once())
  192. ->method('getUsers')
  193. ->with($this->equalTo('fo'), $this->equalTo(3), $this->equalTo(1))
  194. ->will($this->returnValue(array('foo3')));
  195. $manager = new \OC\User\Manager();
  196. $manager->registerBackend($backend1);
  197. $manager->registerBackend($backend2);
  198. $result = $manager->search('fo', 3, 1);
  199. $this->assertEquals(3, count($result));
  200. $this->assertEquals('foo1', array_shift($result)->getUID());
  201. $this->assertEquals('foo2', array_shift($result)->getUID());
  202. $this->assertEquals('foo3', array_shift($result)->getUID());
  203. }
  204. public function testCreateUserSingleBackendNotExists() {
  205. /**
  206. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  207. */
  208. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  209. $backend->expects($this->any())
  210. ->method('implementsActions')
  211. ->will($this->returnValue(true));
  212. $backend->expects($this->once())
  213. ->method('createUser')
  214. ->with($this->equalTo('foo'), $this->equalTo('bar'));
  215. $backend->expects($this->once())
  216. ->method('userExists')
  217. ->with($this->equalTo('foo'))
  218. ->will($this->returnValue(false));
  219. $manager = new \OC\User\Manager();
  220. $manager->registerBackend($backend);
  221. $user = $manager->createUser('foo', 'bar');
  222. $this->assertEquals('foo', $user->getUID());
  223. }
  224. /**
  225. * @expectedException \Exception
  226. */
  227. public function testCreateUserSingleBackendExists() {
  228. /**
  229. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  230. */
  231. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  232. $backend->expects($this->any())
  233. ->method('implementsActions')
  234. ->will($this->returnValue(true));
  235. $backend->expects($this->never())
  236. ->method('createUser');
  237. $backend->expects($this->once())
  238. ->method('userExists')
  239. ->with($this->equalTo('foo'))
  240. ->will($this->returnValue(true));
  241. $manager = new \OC\User\Manager();
  242. $manager->registerBackend($backend);
  243. $manager->createUser('foo', 'bar');
  244. }
  245. public function testCreateUserSingleBackendNotSupported() {
  246. /**
  247. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  248. */
  249. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  250. $backend->expects($this->any())
  251. ->method('implementsActions')
  252. ->will($this->returnValue(false));
  253. $backend->expects($this->never())
  254. ->method('createUser');
  255. $backend->expects($this->once())
  256. ->method('userExists')
  257. ->with($this->equalTo('foo'))
  258. ->will($this->returnValue(false));
  259. $manager = new \OC\User\Manager();
  260. $manager->registerBackend($backend);
  261. $this->assertFalse($manager->createUser('foo', 'bar'));
  262. }
  263. public function testCreateUserNoBackends() {
  264. $manager = new \OC\User\Manager();
  265. $this->assertFalse($manager->createUser('foo', 'bar'));
  266. }
  267. /**
  268. * @expectedException \Exception
  269. */
  270. public function testCreateUserTwoBackendExists() {
  271. /**
  272. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1
  273. */
  274. $backend1 = $this->createMock(\Test\Util\User\Dummy::class);
  275. $backend1->expects($this->any())
  276. ->method('implementsActions')
  277. ->will($this->returnValue(true));
  278. $backend1->expects($this->never())
  279. ->method('createUser');
  280. $backend1->expects($this->once())
  281. ->method('userExists')
  282. ->with($this->equalTo('foo'))
  283. ->will($this->returnValue(false));
  284. /**
  285. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2
  286. */
  287. $backend2 = $this->createMock(\Test\Util\User\Dummy::class);
  288. $backend2->expects($this->any())
  289. ->method('implementsActions')
  290. ->will($this->returnValue(true));
  291. $backend2->expects($this->never())
  292. ->method('createUser');
  293. $backend2->expects($this->once())
  294. ->method('userExists')
  295. ->with($this->equalTo('foo'))
  296. ->will($this->returnValue(true));
  297. $manager = new \OC\User\Manager();
  298. $manager->registerBackend($backend1);
  299. $manager->registerBackend($backend2);
  300. $manager->createUser('foo', 'bar');
  301. }
  302. public function testCountUsersNoBackend() {
  303. $manager = new \OC\User\Manager();
  304. $result = $manager->countUsers();
  305. $this->assertTrue(is_array($result));
  306. $this->assertTrue(empty($result));
  307. }
  308. public function testCountUsersOneBackend() {
  309. /**
  310. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  311. */
  312. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  313. $backend->expects($this->once())
  314. ->method('countUsers')
  315. ->will($this->returnValue(7));
  316. $backend->expects($this->once())
  317. ->method('implementsActions')
  318. ->with(\OC\USER\BACKEND::COUNT_USERS)
  319. ->will($this->returnValue(true));
  320. $backend->expects($this->once())
  321. ->method('getBackendName')
  322. ->will($this->returnValue('Mock_Test_Util_User_Dummy'));
  323. $manager = new \OC\User\Manager();
  324. $manager->registerBackend($backend);
  325. $result = $manager->countUsers();
  326. $keys = array_keys($result);
  327. $this->assertTrue(strpos($keys[0], 'Mock_Test_Util_User_Dummy') !== false);
  328. $users = array_shift($result);
  329. $this->assertEquals(7, $users);
  330. }
  331. public function testCountUsersTwoBackends() {
  332. /**
  333. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  334. */
  335. $backend1 = $this->createMock(\Test\Util\User\Dummy::class);
  336. $backend1->expects($this->once())
  337. ->method('countUsers')
  338. ->will($this->returnValue(7));
  339. $backend1->expects($this->once())
  340. ->method('implementsActions')
  341. ->with(\OC\USER\BACKEND::COUNT_USERS)
  342. ->will($this->returnValue(true));
  343. $backend1->expects($this->once())
  344. ->method('getBackendName')
  345. ->will($this->returnValue('Mock_Test_Util_User_Dummy'));
  346. $backend2 = $this->createMock(\Test\Util\User\Dummy::class);
  347. $backend2->expects($this->once())
  348. ->method('countUsers')
  349. ->will($this->returnValue(16));
  350. $backend2->expects($this->once())
  351. ->method('implementsActions')
  352. ->with(\OC\USER\BACKEND::COUNT_USERS)
  353. ->will($this->returnValue(true));
  354. $backend2->expects($this->once())
  355. ->method('getBackendName')
  356. ->will($this->returnValue('Mock_Test_Util_User_Dummy'));
  357. $manager = new \OC\User\Manager();
  358. $manager->registerBackend($backend1);
  359. $manager->registerBackend($backend2);
  360. $result = $manager->countUsers();
  361. //because the backends have the same class name, only one value expected
  362. $this->assertEquals(1, count($result));
  363. $keys = array_keys($result);
  364. $this->assertTrue(strpos($keys[0], 'Mock_Test_Util_User_Dummy') !== false);
  365. $users = array_shift($result);
  366. //users from backends shall be summed up
  367. $this->assertEquals(7 + 16, $users);
  368. }
  369. public function testDeleteUser() {
  370. $config = $this->getMockBuilder('OCP\IConfig')
  371. ->disableOriginalConstructor()
  372. ->getMock();
  373. $config
  374. ->expects($this->at(0))
  375. ->method('getUserValue')
  376. ->with('foo', 'core', 'enabled')
  377. ->will($this->returnValue(true));
  378. $config
  379. ->expects($this->at(1))
  380. ->method('getUserValue')
  381. ->with('foo', 'login', 'lastLogin')
  382. ->will($this->returnValue(0));
  383. $manager = new \OC\User\Manager($config);
  384. $backend = new \Test\Util\User\Dummy();
  385. $manager->registerBackend($backend);
  386. $backend->createUser('foo', 'bar');
  387. $this->assertTrue($manager->userExists('foo'));
  388. $manager->get('foo')->delete();
  389. $this->assertFalse($manager->userExists('foo'));
  390. }
  391. }