ManagerTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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 OC\User\Manager;
  11. use OCP\IConfig;
  12. use OCP\IUser;
  13. use Test\TestCase;
  14. /**
  15. * Class ManagerTest
  16. *
  17. * @group DB
  18. *
  19. * @package Test\User
  20. */
  21. class ManagerTest extends TestCase {
  22. /** @var IConfig */
  23. private $config;
  24. public function setUp() {
  25. parent::setUp();
  26. $this->config = $this->createMock(IConfig::class);
  27. }
  28. public function testGetBackends() {
  29. $userDummyBackend = $this->createMock(\Test\Util\User\Dummy::class);
  30. $manager = new \OC\User\Manager($this->config);
  31. $manager->registerBackend($userDummyBackend);
  32. $this->assertEquals([$userDummyBackend], $manager->getBackends());
  33. $dummyDatabaseBackend = $this->createMock(Database::class);
  34. $manager->registerBackend($dummyDatabaseBackend);
  35. $this->assertEquals([$userDummyBackend, $dummyDatabaseBackend], $manager->getBackends());
  36. }
  37. public function testUserExistsSingleBackendExists() {
  38. /**
  39. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  40. */
  41. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  42. $backend->expects($this->once())
  43. ->method('userExists')
  44. ->with($this->equalTo('foo'))
  45. ->will($this->returnValue(true));
  46. $manager = new \OC\User\Manager($this->config);
  47. $manager->registerBackend($backend);
  48. $this->assertTrue($manager->userExists('foo'));
  49. }
  50. public function testUserExistsSingleBackendNotExists() {
  51. /**
  52. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  53. */
  54. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  55. $backend->expects($this->once())
  56. ->method('userExists')
  57. ->with($this->equalTo('foo'))
  58. ->will($this->returnValue(false));
  59. $manager = new \OC\User\Manager($this->config);
  60. $manager->registerBackend($backend);
  61. $this->assertFalse($manager->userExists('foo'));
  62. }
  63. public function testUserExistsNoBackends() {
  64. $manager = new \OC\User\Manager($this->config);
  65. $this->assertFalse($manager->userExists('foo'));
  66. }
  67. public function testUserExistsTwoBackendsSecondExists() {
  68. /**
  69. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1
  70. */
  71. $backend1 = $this->createMock(\Test\Util\User\Dummy::class);
  72. $backend1->expects($this->once())
  73. ->method('userExists')
  74. ->with($this->equalTo('foo'))
  75. ->will($this->returnValue(false));
  76. /**
  77. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2
  78. */
  79. $backend2 = $this->createMock(\Test\Util\User\Dummy::class);
  80. $backend2->expects($this->once())
  81. ->method('userExists')
  82. ->with($this->equalTo('foo'))
  83. ->will($this->returnValue(true));
  84. $manager = new \OC\User\Manager($this->config);
  85. $manager->registerBackend($backend1);
  86. $manager->registerBackend($backend2);
  87. $this->assertTrue($manager->userExists('foo'));
  88. }
  89. public function testUserExistsTwoBackendsFirstExists() {
  90. /**
  91. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1
  92. */
  93. $backend1 = $this->createMock(\Test\Util\User\Dummy::class);
  94. $backend1->expects($this->once())
  95. ->method('userExists')
  96. ->with($this->equalTo('foo'))
  97. ->will($this->returnValue(true));
  98. /**
  99. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2
  100. */
  101. $backend2 = $this->createMock(\Test\Util\User\Dummy::class);
  102. $backend2->expects($this->never())
  103. ->method('userExists');
  104. $manager = new \OC\User\Manager($this->config);
  105. $manager->registerBackend($backend1);
  106. $manager->registerBackend($backend2);
  107. $this->assertTrue($manager->userExists('foo'));
  108. }
  109. public function testCheckPassword() {
  110. /**
  111. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  112. */
  113. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  114. $backend->expects($this->once())
  115. ->method('checkPassword')
  116. ->with($this->equalTo('foo'), $this->equalTo('bar'))
  117. ->will($this->returnValue(true));
  118. $backend->expects($this->any())
  119. ->method('implementsActions')
  120. ->will($this->returnCallback(function ($actions) {
  121. if ($actions === \OC\USER\BACKEND::CHECK_PASSWORD) {
  122. return true;
  123. } else {
  124. return false;
  125. }
  126. }));
  127. $manager = new \OC\User\Manager($this->config);
  128. $manager->registerBackend($backend);
  129. $user = $manager->checkPassword('foo', 'bar');
  130. $this->assertTrue($user instanceof \OC\User\User);
  131. }
  132. public function testCheckPasswordNotSupported() {
  133. /**
  134. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  135. */
  136. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  137. $backend->expects($this->never())
  138. ->method('checkPassword');
  139. $backend->expects($this->any())
  140. ->method('implementsActions')
  141. ->will($this->returnValue(false));
  142. $manager = new \OC\User\Manager($this->config);
  143. $manager->registerBackend($backend);
  144. $this->assertFalse($manager->checkPassword('foo', 'bar'));
  145. }
  146. public function testGetOneBackendExists() {
  147. /**
  148. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  149. */
  150. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  151. $backend->expects($this->once())
  152. ->method('userExists')
  153. ->with($this->equalTo('foo'))
  154. ->will($this->returnValue(true));
  155. $backend->expects($this->never())
  156. ->method('loginName2UserName');
  157. $manager = new \OC\User\Manager($this->config);
  158. $manager->registerBackend($backend);
  159. $this->assertEquals('foo', $manager->get('foo')->getUID());
  160. }
  161. public function testGetOneBackendNotExists() {
  162. /**
  163. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  164. */
  165. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  166. $backend->expects($this->once())
  167. ->method('userExists')
  168. ->with($this->equalTo('foo'))
  169. ->will($this->returnValue(false));
  170. $manager = new \OC\User\Manager($this->config);
  171. $manager->registerBackend($backend);
  172. $this->assertEquals(null, $manager->get('foo'));
  173. }
  174. public function testGetOneBackendDoNotTranslateLoginNames() {
  175. /**
  176. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  177. */
  178. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  179. $backend->expects($this->once())
  180. ->method('userExists')
  181. ->with($this->equalTo('bLeNdEr'))
  182. ->will($this->returnValue(true));
  183. $backend->expects($this->never())
  184. ->method('loginName2UserName');
  185. $manager = new \OC\User\Manager($this->config);
  186. $manager->registerBackend($backend);
  187. $this->assertEquals('bLeNdEr', $manager->get('bLeNdEr')->getUID());
  188. }
  189. public function testSearchOneBackend() {
  190. /**
  191. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  192. */
  193. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  194. $backend->expects($this->once())
  195. ->method('getUsers')
  196. ->with($this->equalTo('fo'))
  197. ->will($this->returnValue(array('foo', 'afoo')));
  198. $backend->expects($this->never())
  199. ->method('loginName2UserName');
  200. $manager = new \OC\User\Manager($this->config);
  201. $manager->registerBackend($backend);
  202. $result = $manager->search('fo');
  203. $this->assertEquals(2, count($result));
  204. $this->assertEquals('afoo', array_shift($result)->getUID());
  205. $this->assertEquals('foo', array_shift($result)->getUID());
  206. }
  207. public function testSearchTwoBackendLimitOffset() {
  208. /**
  209. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1
  210. */
  211. $backend1 = $this->createMock(\Test\Util\User\Dummy::class);
  212. $backend1->expects($this->once())
  213. ->method('getUsers')
  214. ->with($this->equalTo('fo'), $this->equalTo(3), $this->equalTo(1))
  215. ->will($this->returnValue(array('foo1', 'foo2')));
  216. $backend1->expects($this->never())
  217. ->method('loginName2UserName');
  218. /**
  219. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2
  220. */
  221. $backend2 = $this->createMock(\Test\Util\User\Dummy::class);
  222. $backend2->expects($this->once())
  223. ->method('getUsers')
  224. ->with($this->equalTo('fo'), $this->equalTo(3), $this->equalTo(1))
  225. ->will($this->returnValue(array('foo3')));
  226. $backend2->expects($this->never())
  227. ->method('loginName2UserName');
  228. $manager = new \OC\User\Manager($this->config);
  229. $manager->registerBackend($backend1);
  230. $manager->registerBackend($backend2);
  231. $result = $manager->search('fo', 3, 1);
  232. $this->assertEquals(3, count($result));
  233. $this->assertEquals('foo1', array_shift($result)->getUID());
  234. $this->assertEquals('foo2', array_shift($result)->getUID());
  235. $this->assertEquals('foo3', array_shift($result)->getUID());
  236. }
  237. public function dataCreateUserInvalid() {
  238. return [
  239. ['te?st', 'foo', 'Only the following characters are allowed in a username:'
  240. . ' "a-z", "A-Z", "0-9", and "_.@-\'"'],
  241. ["te\tst", '', 'Only the following characters are allowed in a username:'
  242. . ' "a-z", "A-Z", "0-9", and "_.@-\'"'],
  243. ["te\nst", '', 'Only the following characters are allowed in a username:'
  244. . ' "a-z", "A-Z", "0-9", and "_.@-\'"'],
  245. ["te\rst", '', 'Only the following characters are allowed in a username:'
  246. . ' "a-z", "A-Z", "0-9", and "_.@-\'"'],
  247. ["te\0st", '', 'Only the following characters are allowed in a username:'
  248. . ' "a-z", "A-Z", "0-9", and "_.@-\'"'],
  249. ["te\x0Bst", '', 'Only the following characters are allowed in a username:'
  250. . ' "a-z", "A-Z", "0-9", and "_.@-\'"'],
  251. ["te\xe2st", '', 'Only the following characters are allowed in a username:'
  252. . ' "a-z", "A-Z", "0-9", and "_.@-\'"'],
  253. ["te\x80st", '', 'Only the following characters are allowed in a username:'
  254. . ' "a-z", "A-Z", "0-9", and "_.@-\'"'],
  255. ["te\x8bst", '', 'Only the following characters are allowed in a username:'
  256. . ' "a-z", "A-Z", "0-9", and "_.@-\'"'],
  257. ['', 'foo', 'A valid username must be provided'],
  258. [' ', 'foo', 'A valid username must be provided'],
  259. [' test', 'foo', 'Username contains whitespace at the beginning or at the end'],
  260. ['test ', 'foo', 'Username contains whitespace at the beginning or at the end'],
  261. ['.', 'foo', 'Username must not consist of dots only'],
  262. ['..', 'foo', 'Username must not consist of dots only'],
  263. ['.test', '', 'A valid password must be provided'],
  264. ['test', '', 'A valid password must be provided'],
  265. ];
  266. }
  267. /**
  268. * @dataProvider dataCreateUserInvalid
  269. */
  270. public function testCreateUserInvalid($uid, $password, $exception) {
  271. /** @var \Test\Util\User\Dummy|\PHPUnit_Framework_MockObject_MockObject $backend */
  272. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  273. $backend->expects($this->once())
  274. ->method('implementsActions')
  275. ->with(\OC\User\Backend::CREATE_USER)
  276. ->willReturn(true);
  277. $manager = new \OC\User\Manager($this->config);
  278. $manager->registerBackend($backend);
  279. $this->expectException(\InvalidArgumentException::class, $exception);
  280. $manager->createUser($uid, $password);
  281. }
  282. public function testCreateUserSingleBackendNotExists() {
  283. /**
  284. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  285. */
  286. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  287. $backend->expects($this->any())
  288. ->method('implementsActions')
  289. ->will($this->returnValue(true));
  290. $backend->expects($this->once())
  291. ->method('createUser')
  292. ->with($this->equalTo('foo'), $this->equalTo('bar'));
  293. $backend->expects($this->once())
  294. ->method('userExists')
  295. ->with($this->equalTo('foo'))
  296. ->will($this->returnValue(false));
  297. $backend->expects($this->never())
  298. ->method('loginName2UserName');
  299. $manager = new \OC\User\Manager($this->config);
  300. $manager->registerBackend($backend);
  301. $user = $manager->createUser('foo', 'bar');
  302. $this->assertEquals('foo', $user->getUID());
  303. }
  304. /**
  305. * @expectedException \Exception
  306. */
  307. public function testCreateUserSingleBackendExists() {
  308. /**
  309. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  310. */
  311. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  312. $backend->expects($this->any())
  313. ->method('implementsActions')
  314. ->will($this->returnValue(true));
  315. $backend->expects($this->never())
  316. ->method('createUser');
  317. $backend->expects($this->once())
  318. ->method('userExists')
  319. ->with($this->equalTo('foo'))
  320. ->will($this->returnValue(true));
  321. $manager = new \OC\User\Manager($this->config);
  322. $manager->registerBackend($backend);
  323. $manager->createUser('foo', 'bar');
  324. }
  325. public function testCreateUserSingleBackendNotSupported() {
  326. /**
  327. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  328. */
  329. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  330. $backend->expects($this->any())
  331. ->method('implementsActions')
  332. ->will($this->returnValue(false));
  333. $backend->expects($this->never())
  334. ->method('createUser');
  335. $backend->expects($this->never())
  336. ->method('userExists');
  337. $manager = new \OC\User\Manager($this->config);
  338. $manager->registerBackend($backend);
  339. $this->assertFalse($manager->createUser('foo', 'bar'));
  340. }
  341. public function testCreateUserNoBackends() {
  342. $manager = new \OC\User\Manager($this->config);
  343. $this->assertFalse($manager->createUser('foo', 'bar'));
  344. }
  345. /**
  346. * @expectedException \InvalidArgumentException
  347. * @expectedExceptionMessage Could not create user
  348. */
  349. public function testCreateUserFromBackendWithBackendError() {
  350. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject $config */
  351. $config = $this->createMock(IConfig::class);
  352. /** @var \Test\Util\User\Dummy|\PHPUnit_Framework_MockObject_MockObject $backend */
  353. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  354. $backend
  355. ->expects($this->once())
  356. ->method('createUser')
  357. ->with('MyUid', 'MyPassword')
  358. ->willReturn(false);
  359. $manager = new Manager($config);
  360. $manager->createUserFromBackend('MyUid', 'MyPassword', $backend);
  361. }
  362. /**
  363. * @expectedException \Exception
  364. */
  365. public function testCreateUserTwoBackendExists() {
  366. /**
  367. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1
  368. */
  369. $backend1 = $this->createMock(\Test\Util\User\Dummy::class);
  370. $backend1->expects($this->any())
  371. ->method('implementsActions')
  372. ->will($this->returnValue(true));
  373. $backend1->expects($this->never())
  374. ->method('createUser');
  375. $backend1->expects($this->once())
  376. ->method('userExists')
  377. ->with($this->equalTo('foo'))
  378. ->will($this->returnValue(false));
  379. /**
  380. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2
  381. */
  382. $backend2 = $this->createMock(\Test\Util\User\Dummy::class);
  383. $backend2->expects($this->any())
  384. ->method('implementsActions')
  385. ->will($this->returnValue(true));
  386. $backend2->expects($this->never())
  387. ->method('createUser');
  388. $backend2->expects($this->once())
  389. ->method('userExists')
  390. ->with($this->equalTo('foo'))
  391. ->will($this->returnValue(true));
  392. $manager = new \OC\User\Manager($this->config);
  393. $manager->registerBackend($backend1);
  394. $manager->registerBackend($backend2);
  395. $manager->createUser('foo', 'bar');
  396. }
  397. public function testCountUsersNoBackend() {
  398. $manager = new \OC\User\Manager($this->config);
  399. $result = $manager->countUsers();
  400. $this->assertTrue(is_array($result));
  401. $this->assertTrue(empty($result));
  402. }
  403. public function testCountUsersOneBackend() {
  404. /**
  405. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  406. */
  407. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  408. $backend->expects($this->once())
  409. ->method('countUsers')
  410. ->will($this->returnValue(7));
  411. $backend->expects($this->once())
  412. ->method('implementsActions')
  413. ->with(\OC\USER\BACKEND::COUNT_USERS)
  414. ->will($this->returnValue(true));
  415. $backend->expects($this->once())
  416. ->method('getBackendName')
  417. ->will($this->returnValue('Mock_Test_Util_User_Dummy'));
  418. $manager = new \OC\User\Manager($this->config);
  419. $manager->registerBackend($backend);
  420. $result = $manager->countUsers();
  421. $keys = array_keys($result);
  422. $this->assertTrue(strpos($keys[0], 'Mock_Test_Util_User_Dummy') !== false);
  423. $users = array_shift($result);
  424. $this->assertEquals(7, $users);
  425. }
  426. public function testCountUsersTwoBackends() {
  427. /**
  428. * @var \Test\Util\User\Dummy | \PHPUnit_Framework_MockObject_MockObject $backend
  429. */
  430. $backend1 = $this->createMock(\Test\Util\User\Dummy::class);
  431. $backend1->expects($this->once())
  432. ->method('countUsers')
  433. ->will($this->returnValue(7));
  434. $backend1->expects($this->once())
  435. ->method('implementsActions')
  436. ->with(\OC\USER\BACKEND::COUNT_USERS)
  437. ->will($this->returnValue(true));
  438. $backend1->expects($this->once())
  439. ->method('getBackendName')
  440. ->will($this->returnValue('Mock_Test_Util_User_Dummy'));
  441. $backend2 = $this->createMock(\Test\Util\User\Dummy::class);
  442. $backend2->expects($this->once())
  443. ->method('countUsers')
  444. ->will($this->returnValue(16));
  445. $backend2->expects($this->once())
  446. ->method('implementsActions')
  447. ->with(\OC\USER\BACKEND::COUNT_USERS)
  448. ->will($this->returnValue(true));
  449. $backend2->expects($this->once())
  450. ->method('getBackendName')
  451. ->will($this->returnValue('Mock_Test_Util_User_Dummy'));
  452. $manager = new \OC\User\Manager($this->config);
  453. $manager->registerBackend($backend1);
  454. $manager->registerBackend($backend2);
  455. $result = $manager->countUsers();
  456. //because the backends have the same class name, only one value expected
  457. $this->assertEquals(1, count($result));
  458. $keys = array_keys($result);
  459. $this->assertTrue(strpos($keys[0], 'Mock_Test_Util_User_Dummy') !== false);
  460. $users = array_shift($result);
  461. //users from backends shall be summed up
  462. $this->assertEquals(7 + 16, $users);
  463. }
  464. public function testCountUsersOnlyDisabled() {
  465. $manager = \OC::$server->getUserManager();
  466. // count other users in the db before adding our own
  467. $countBefore = $manager->countDisabledUsers();
  468. //Add test users
  469. $user1 = $manager->createUser('testdisabledcount1', 'testdisabledcount1');
  470. $user2 = $manager->createUser('testdisabledcount2', 'testdisabledcount2');
  471. $user2->setEnabled(false);
  472. $user3 = $manager->createUser('testdisabledcount3', 'testdisabledcount3');
  473. $user4 = $manager->createUser('testdisabledcount4', 'testdisabledcount4');
  474. $user4->setEnabled(false);
  475. $this->assertEquals($countBefore + 2, $manager->countDisabledUsers());
  476. //cleanup
  477. $user1->delete();
  478. $user2->delete();
  479. $user3->delete();
  480. $user4->delete();
  481. }
  482. public function testCountUsersOnlySeen() {
  483. $manager = \OC::$server->getUserManager();
  484. // count other users in the db before adding our own
  485. $countBefore = $manager->countUsers(true);
  486. //Add test users
  487. $user1 = $manager->createUser('testseencount1', 'testseencount1');
  488. $user1->updateLastLoginTimestamp();
  489. $user2 = $manager->createUser('testseencount2', 'testseencount2');
  490. $user2->updateLastLoginTimestamp();
  491. $user3 = $manager->createUser('testseencount3', 'testseencount3');
  492. $user4 = $manager->createUser('testseencount4', 'testseencount4');
  493. $user4->updateLastLoginTimestamp();
  494. $this->assertEquals($countBefore + 3, $manager->countUsers(true));
  495. //cleanup
  496. $user1->delete();
  497. $user2->delete();
  498. $user3->delete();
  499. $user4->delete();
  500. }
  501. public function testCallForSeenUsers() {
  502. $manager = \OC::$server->getUserManager();
  503. // count other users in the db before adding our own
  504. $count = 0;
  505. $function = function (IUser $user) use (&$count) {
  506. $count++;
  507. };
  508. $manager->callForAllUsers($function, '', true);
  509. $countBefore = $count;
  510. //Add test users
  511. $user1 = $manager->createUser('testseen1', 'testseen1');
  512. $user1->updateLastLoginTimestamp();
  513. $user2 = $manager->createUser('testseen2', 'testseen2');
  514. $user2->updateLastLoginTimestamp();
  515. $user3 = $manager->createUser('testseen3', 'testseen3');
  516. $user4 = $manager->createUser('testseen4', 'testseen4');
  517. $user4->updateLastLoginTimestamp();
  518. $count = 0;
  519. $manager->callForAllUsers($function, '', true);
  520. $this->assertEquals($countBefore + 3, $count);
  521. //cleanup
  522. $user1->delete();
  523. $user2->delete();
  524. $user3->delete();
  525. $user4->delete();
  526. }
  527. public function testDeleteUser() {
  528. $config = $this->getMockBuilder(IConfig::class)
  529. ->disableOriginalConstructor()
  530. ->getMock();
  531. $config
  532. ->expects($this->at(0))
  533. ->method('getUserValue')
  534. ->with('foo', 'core', 'enabled')
  535. ->will($this->returnValue(true));
  536. $config
  537. ->expects($this->at(1))
  538. ->method('getUserValue')
  539. ->with('foo', 'login', 'lastLogin')
  540. ->will($this->returnValue(0));
  541. $manager = new \OC\User\Manager($config);
  542. $backend = new \Test\Util\User\Dummy();
  543. $manager->registerBackend($backend);
  544. $backend->createUser('foo', 'bar');
  545. $this->assertTrue($manager->userExists('foo'));
  546. $manager->get('foo')->delete();
  547. $this->assertFalse($manager->userExists('foo'));
  548. }
  549. }