ManagerTest.php 16 KB

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