UserTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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\Hooks\PublicEmitter;
  10. use OC\User\User;
  11. use OCP\Comments\ICommentsManager;
  12. use OCP\IConfig;
  13. use OCP\IUser;
  14. use OCP\Notification\IManager as INotificationManager;
  15. use OCP\Notification\INotification;
  16. use OCP\UserInterface;
  17. use Test\TestCase;
  18. /**
  19. * Class UserTest
  20. *
  21. * @group DB
  22. *
  23. * @package Test\User
  24. */
  25. class UserTest extends TestCase {
  26. public function testDisplayName() {
  27. /**
  28. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  29. */
  30. $backend = $this->createMock(\OC\User\Backend::class);
  31. $backend->expects($this->once())
  32. ->method('getDisplayName')
  33. ->with($this->equalTo('foo'))
  34. ->will($this->returnValue('Foo'));
  35. $backend->expects($this->any())
  36. ->method('implementsActions')
  37. ->with($this->equalTo(\OC\User\Backend::GET_DISPLAYNAME))
  38. ->will($this->returnValue(true));
  39. $user = new User('foo', $backend);
  40. $this->assertEquals('Foo', $user->getDisplayName());
  41. }
  42. /**
  43. * if the display name contain whitespaces only, we expect the uid as result
  44. */
  45. public function testDisplayNameEmpty() {
  46. /**
  47. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  48. */
  49. $backend = $this->createMock(\OC\User\Backend::class);
  50. $backend->expects($this->once())
  51. ->method('getDisplayName')
  52. ->with($this->equalTo('foo'))
  53. ->will($this->returnValue(' '));
  54. $backend->expects($this->any())
  55. ->method('implementsActions')
  56. ->with($this->equalTo(\OC\User\Backend::GET_DISPLAYNAME))
  57. ->will($this->returnValue(true));
  58. $user = new User('foo', $backend);
  59. $this->assertEquals('foo', $user->getDisplayName());
  60. }
  61. public function testDisplayNameNotSupported() {
  62. /**
  63. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  64. */
  65. $backend = $this->createMock(\OC\User\Backend::class);
  66. $backend->expects($this->never())
  67. ->method('getDisplayName');
  68. $backend->expects($this->any())
  69. ->method('implementsActions')
  70. ->with($this->equalTo(\OC\User\Backend::GET_DISPLAYNAME))
  71. ->will($this->returnValue(false));
  72. $user = new User('foo', $backend);
  73. $this->assertEquals('foo', $user->getDisplayName());
  74. }
  75. public function testSetPassword() {
  76. /**
  77. * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  78. */
  79. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  80. $backend->expects($this->once())
  81. ->method('setPassword')
  82. ->with($this->equalTo('foo'), $this->equalTo('bar'));
  83. $backend->expects($this->any())
  84. ->method('implementsActions')
  85. ->will($this->returnCallback(function ($actions) {
  86. if ($actions === \OC\User\Backend::SET_PASSWORD) {
  87. return true;
  88. } else {
  89. return false;
  90. }
  91. }));
  92. $user = new User('foo', $backend);
  93. $this->assertTrue($user->setPassword('bar',''));
  94. }
  95. public function testSetPasswordNotSupported() {
  96. /**
  97. * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  98. */
  99. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  100. $backend->expects($this->never())
  101. ->method('setPassword');
  102. $backend->expects($this->any())
  103. ->method('implementsActions')
  104. ->will($this->returnValue(false));
  105. $user = new User('foo', $backend);
  106. $this->assertFalse($user->setPassword('bar',''));
  107. }
  108. public function testChangeAvatarSupportedYes() {
  109. /**
  110. * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  111. */
  112. $backend = $this->createMock(AvatarUserDummy::class);
  113. $backend->expects($this->once())
  114. ->method('canChangeAvatar')
  115. ->with($this->equalTo('foo'))
  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::PROVIDE_AVATAR) {
  121. return true;
  122. } else {
  123. return false;
  124. }
  125. }));
  126. $user = new User('foo', $backend);
  127. $this->assertTrue($user->canChangeAvatar());
  128. }
  129. public function testChangeAvatarSupportedNo() {
  130. /**
  131. * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  132. */
  133. $backend = $this->createMock(AvatarUserDummy::class);
  134. $backend->expects($this->once())
  135. ->method('canChangeAvatar')
  136. ->with($this->equalTo('foo'))
  137. ->will($this->returnValue(false));
  138. $backend->expects($this->any())
  139. ->method('implementsActions')
  140. ->will($this->returnCallback(function ($actions) {
  141. if ($actions === \OC\User\Backend::PROVIDE_AVATAR) {
  142. return true;
  143. } else {
  144. return false;
  145. }
  146. }));
  147. $user = new User('foo', $backend);
  148. $this->assertFalse($user->canChangeAvatar());
  149. }
  150. public function testChangeAvatarNotSupported() {
  151. /**
  152. * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  153. */
  154. $backend = $this->createMock(AvatarUserDummy::class);
  155. $backend->expects($this->never())
  156. ->method('canChangeAvatar');
  157. $backend->expects($this->any())
  158. ->method('implementsActions')
  159. ->willReturn(false);
  160. $user = new User('foo', $backend);
  161. $this->assertTrue($user->canChangeAvatar());
  162. }
  163. public function testDelete() {
  164. /**
  165. * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  166. */
  167. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  168. $backend->expects($this->once())
  169. ->method('deleteUser')
  170. ->with($this->equalTo('foo'));
  171. $user = new User('foo', $backend);
  172. $this->assertTrue($user->delete());
  173. }
  174. public function testDeleteWithDifferentHome() {
  175. /**
  176. * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  177. */
  178. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  179. $backend->expects($this->at(0))
  180. ->method('implementsActions')
  181. ->will($this->returnCallback(function ($actions) {
  182. if ($actions === \OC\User\Backend::GET_HOME) {
  183. return true;
  184. } else {
  185. return false;
  186. }
  187. }));
  188. // important: getHome MUST be called before deleteUser because
  189. // once the user is deleted, getHome implementations might not
  190. // return anything
  191. $backend->expects($this->at(1))
  192. ->method('getHome')
  193. ->with($this->equalTo('foo'))
  194. ->will($this->returnValue('/home/foo'));
  195. $backend->expects($this->at(2))
  196. ->method('deleteUser')
  197. ->with($this->equalTo('foo'));
  198. $user = new User('foo', $backend);
  199. $this->assertTrue($user->delete());
  200. }
  201. public function testGetHome() {
  202. /**
  203. * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  204. */
  205. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  206. $backend->expects($this->once())
  207. ->method('getHome')
  208. ->with($this->equalTo('foo'))
  209. ->will($this->returnValue('/home/foo'));
  210. $backend->expects($this->any())
  211. ->method('implementsActions')
  212. ->will($this->returnCallback(function ($actions) {
  213. if ($actions === \OC\User\Backend::GET_HOME) {
  214. return true;
  215. } else {
  216. return false;
  217. }
  218. }));
  219. $user = new User('foo', $backend);
  220. $this->assertEquals('/home/foo', $user->getHome());
  221. }
  222. public function testGetBackendClassName() {
  223. $user = new User('foo', new \Test\Util\User\Dummy());
  224. $this->assertEquals('Dummy', $user->getBackendClassName());
  225. $user = new User('foo', new \OC\User\Database());
  226. $this->assertEquals('Database', $user->getBackendClassName());
  227. }
  228. public function testGetHomeNotSupported() {
  229. /**
  230. * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  231. */
  232. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  233. $backend->expects($this->never())
  234. ->method('getHome');
  235. $backend->expects($this->any())
  236. ->method('implementsActions')
  237. ->will($this->returnValue(false));
  238. $allConfig = $this->getMockBuilder('\OCP\IConfig')
  239. ->disableOriginalConstructor()
  240. ->getMock();
  241. $allConfig->expects($this->any())
  242. ->method('getUserValue')
  243. ->will($this->returnValue(true));
  244. $allConfig->expects($this->any())
  245. ->method('getSystemValue')
  246. ->with($this->equalTo('datadirectory'))
  247. ->will($this->returnValue('arbitrary/path'));
  248. $user = new User('foo', $backend, null, $allConfig);
  249. $this->assertEquals('arbitrary/path/foo', $user->getHome());
  250. }
  251. public function testCanChangePassword() {
  252. /**
  253. * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  254. */
  255. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  256. $backend->expects($this->any())
  257. ->method('implementsActions')
  258. ->will($this->returnCallback(function ($actions) {
  259. if ($actions === \OC\User\Backend::SET_PASSWORD) {
  260. return true;
  261. } else {
  262. return false;
  263. }
  264. }));
  265. $user = new User('foo', $backend);
  266. $this->assertTrue($user->canChangePassword());
  267. }
  268. public function testCanChangePasswordNotSupported() {
  269. /**
  270. * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  271. */
  272. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  273. $backend->expects($this->any())
  274. ->method('implementsActions')
  275. ->will($this->returnValue(false));
  276. $user = new User('foo', $backend);
  277. $this->assertFalse($user->canChangePassword());
  278. }
  279. public function testCanChangeDisplayName() {
  280. /**
  281. * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  282. */
  283. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  284. $backend->expects($this->any())
  285. ->method('implementsActions')
  286. ->will($this->returnCallback(function ($actions) {
  287. if ($actions === \OC\User\Backend::SET_DISPLAYNAME) {
  288. return true;
  289. } else {
  290. return false;
  291. }
  292. }));
  293. $user = new User('foo', $backend);
  294. $this->assertTrue($user->canChangeDisplayName());
  295. }
  296. public function testCanChangeDisplayNameNotSupported() {
  297. /**
  298. * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  299. */
  300. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  301. $backend->expects($this->any())
  302. ->method('implementsActions')
  303. ->will($this->returnValue(false));
  304. $user = new User('foo', $backend);
  305. $this->assertFalse($user->canChangeDisplayName());
  306. }
  307. public function testSetDisplayNameSupported() {
  308. /**
  309. * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  310. */
  311. $backend = $this->createMock(\OC\User\Database::class);
  312. $backend->expects($this->any())
  313. ->method('implementsActions')
  314. ->will($this->returnCallback(function ($actions) {
  315. if ($actions === \OC\User\Backend::SET_DISPLAYNAME) {
  316. return true;
  317. } else {
  318. return false;
  319. }
  320. }));
  321. $backend->expects($this->once())
  322. ->method('setDisplayName')
  323. ->with('foo','Foo')
  324. ->willReturn(true);
  325. $user = new User('foo', $backend);
  326. $this->assertTrue($user->setDisplayName('Foo'));
  327. $this->assertEquals('Foo',$user->getDisplayName());
  328. }
  329. /**
  330. * don't allow display names containing whitespaces only
  331. */
  332. public function testSetDisplayNameEmpty() {
  333. /**
  334. * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  335. */
  336. $backend = $this->createMock(\OC\User\Database::class);
  337. $backend->expects($this->any())
  338. ->method('implementsActions')
  339. ->will($this->returnCallback(function ($actions) {
  340. if ($actions === \OC\User\Backend::SET_DISPLAYNAME) {
  341. return true;
  342. } else {
  343. return false;
  344. }
  345. }));
  346. $user = new User('foo', $backend);
  347. $this->assertFalse($user->setDisplayName(' '));
  348. $this->assertEquals('foo',$user->getDisplayName());
  349. }
  350. public function testSetDisplayNameNotSupported() {
  351. /**
  352. * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  353. */
  354. $backend = $this->createMock(\OC\User\Database::class);
  355. $backend->expects($this->any())
  356. ->method('implementsActions')
  357. ->willReturn(false);
  358. $backend->expects($this->never())
  359. ->method('setDisplayName');
  360. $user = new User('foo', $backend);
  361. $this->assertFalse($user->setDisplayName('Foo'));
  362. $this->assertEquals('foo',$user->getDisplayName());
  363. }
  364. public function testSetPasswordHooks() {
  365. $hooksCalled = 0;
  366. $test = $this;
  367. /**
  368. * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  369. */
  370. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  371. $backend->expects($this->once())
  372. ->method('setPassword');
  373. /**
  374. * @param User $user
  375. * @param string $password
  376. */
  377. $hook = function ($user, $password) use ($test, &$hooksCalled) {
  378. $hooksCalled++;
  379. $test->assertEquals('foo', $user->getUID());
  380. $test->assertEquals('bar', $password);
  381. };
  382. $emitter = new PublicEmitter();
  383. $emitter->listen('\OC\User', 'preSetPassword', $hook);
  384. $emitter->listen('\OC\User', 'postSetPassword', $hook);
  385. $backend->expects($this->any())
  386. ->method('implementsActions')
  387. ->will($this->returnCallback(function ($actions) {
  388. if ($actions === \OC\User\Backend::SET_PASSWORD) {
  389. return true;
  390. } else {
  391. return false;
  392. }
  393. }));
  394. $user = new User('foo', $backend, $emitter);
  395. $user->setPassword('bar','');
  396. $this->assertEquals(2, $hooksCalled);
  397. }
  398. public function dataDeleteHooks() {
  399. return [
  400. [true, 2],
  401. [false, 1],
  402. ];
  403. }
  404. /**
  405. * @dataProvider dataDeleteHooks
  406. * @param bool $result
  407. * @param int $expectedHooks
  408. */
  409. public function testDeleteHooks($result, $expectedHooks) {
  410. $hooksCalled = 0;
  411. $test = $this;
  412. /**
  413. * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  414. */
  415. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  416. $backend->expects($this->once())
  417. ->method('deleteUser')
  418. ->willReturn($result);
  419. $emitter = new PublicEmitter();
  420. $user = new User('foo', $backend, $emitter);
  421. /**
  422. * @param User $user
  423. */
  424. $hook = function ($user) use ($test, &$hooksCalled) {
  425. $hooksCalled++;
  426. $test->assertEquals('foo', $user->getUID());
  427. };
  428. $emitter->listen('\OC\User', 'preDelete', $hook);
  429. $emitter->listen('\OC\User', 'postDelete', $hook);
  430. $config = $this->createMock(IConfig::class);
  431. $commentsManager = $this->createMock(ICommentsManager::class);
  432. $notificationManager = $this->createMock(INotificationManager::class);
  433. if ($result) {
  434. $config->expects($this->once())
  435. ->method('deleteAllUserValues')
  436. ->with('foo');
  437. $commentsManager->expects($this->once())
  438. ->method('deleteReferencesOfActor')
  439. ->with('users', 'foo');
  440. $commentsManager->expects($this->once())
  441. ->method('deleteReadMarksFromUser')
  442. ->with($user);
  443. $notification = $this->createMock(INotification::class);
  444. $notification->expects($this->once())
  445. ->method('setUser')
  446. ->with('foo');
  447. $notificationManager->expects($this->once())
  448. ->method('createNotification')
  449. ->willReturn($notification);
  450. $notificationManager->expects($this->once())
  451. ->method('markProcessed')
  452. ->with($notification);
  453. } else {
  454. $config->expects($this->never())
  455. ->method('deleteAllUserValues');
  456. $commentsManager->expects($this->never())
  457. ->method('deleteReferencesOfActor');
  458. $commentsManager->expects($this->never())
  459. ->method('deleteReadMarksFromUser');
  460. $notificationManager->expects($this->never())
  461. ->method('createNotification');
  462. $notificationManager->expects($this->never())
  463. ->method('markProcessed');
  464. }
  465. $this->overwriteService('NotificationManager', $notificationManager);
  466. $this->overwriteService('CommentsManager', $commentsManager);
  467. $this->overwriteService('AllConfig', $config);
  468. $this->assertSame($result, $user->delete());
  469. $this->restoreService('AllConfig');
  470. $this->restoreService('CommentsManager');
  471. $this->restoreService('NotificationManager');
  472. $this->assertEquals($expectedHooks, $hooksCalled);
  473. }
  474. public function testGetCloudId() {
  475. /**
  476. * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  477. */
  478. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  479. $urlGenerator = $this->getMockBuilder('\OC\URLGenerator')
  480. ->setMethods(['getAbsoluteURL'])
  481. ->disableOriginalConstructor()->getMock();
  482. $urlGenerator
  483. ->expects($this->any())
  484. ->method('getAbsoluteURL')
  485. ->withAnyParameters()
  486. ->willReturn('http://localhost:8888/owncloud');
  487. $user = new User('foo', $backend, null, null, $urlGenerator);
  488. $this->assertEquals('foo@localhost:8888/owncloud', $user->getCloudId());
  489. }
  490. public function testSetEMailAddressEmpty() {
  491. /**
  492. * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  493. */
  494. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  495. $test = $this;
  496. $hooksCalled = 0;
  497. /**
  498. * @param IUser $user
  499. * @param string $feature
  500. * @param string $value
  501. */
  502. $hook = function (IUser $user, $feature, $value) use ($test, &$hooksCalled) {
  503. $hooksCalled++;
  504. $test->assertEquals('eMailAddress', $feature);
  505. $test->assertEquals('', $value);
  506. };
  507. $emitter = new PublicEmitter();
  508. $emitter->listen('\OC\User', 'changeUser', $hook);
  509. $config = $this->createMock(IConfig::class);
  510. $config->expects($this->once())
  511. ->method('deleteUserValue')
  512. ->with(
  513. 'foo',
  514. 'settings',
  515. 'email'
  516. );
  517. $user = new User('foo', $backend, $emitter, $config);
  518. $user->setEMailAddress('');
  519. }
  520. public function testSetEMailAddress() {
  521. /**
  522. * @var UserInterface | \PHPUnit_Framework_MockObject_MockObject $backend
  523. */
  524. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  525. $test = $this;
  526. $hooksCalled = 0;
  527. /**
  528. * @param IUser $user
  529. * @param string $feature
  530. * @param string $value
  531. */
  532. $hook = function (IUser $user, $feature, $value) use ($test, &$hooksCalled) {
  533. $hooksCalled++;
  534. $test->assertEquals('eMailAddress', $feature);
  535. $test->assertEquals('foo@bar.com', $value);
  536. };
  537. $emitter = new PublicEmitter();
  538. $emitter->listen('\OC\User', 'changeUser', $hook);
  539. $config = $this->createMock(IConfig::class);
  540. $config->expects($this->once())
  541. ->method('setUserValue')
  542. ->with(
  543. 'foo',
  544. 'settings',
  545. 'email',
  546. 'foo@bar.com'
  547. );
  548. $user = new User('foo', $backend, $emitter, $config);
  549. $user->setEMailAddress('foo@bar.com');
  550. }
  551. public function testSetEMailAddressNoChange() {
  552. /**
  553. * @var UserInterface | \PHPUnit_Framework_MockObject_MockObject $backend
  554. */
  555. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  556. /** @var PublicEmitter|\PHPUnit_Framework_MockObject_MockObject $emitter */
  557. $emitter = $this->createMock(PublicEmitter::class);
  558. $emitter->expects($this->never())
  559. ->method('emit');
  560. $config = $this->createMock(IConfig::class);
  561. $config->expects($this->any())
  562. ->method('getUserValue')
  563. ->willReturn('foo@bar.com');
  564. $config->expects($this->once())
  565. ->method('setUserValue')
  566. ->with(
  567. 'foo',
  568. 'settings',
  569. 'email',
  570. 'foo@bar.com'
  571. );
  572. $user = new User('foo', $backend, $emitter, $config);
  573. $user->setEMailAddress('foo@bar.com');
  574. }
  575. public function testSetQuota() {
  576. /**
  577. * @var UserInterface | \PHPUnit_Framework_MockObject_MockObject $backend
  578. */
  579. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  580. $test = $this;
  581. $hooksCalled = 0;
  582. /**
  583. * @param IUser $user
  584. * @param string $feature
  585. * @param string $value
  586. */
  587. $hook = function (IUser $user, $feature, $value) use ($test, &$hooksCalled) {
  588. $hooksCalled++;
  589. $test->assertEquals('quota', $feature);
  590. $test->assertEquals('23 TB', $value);
  591. };
  592. $emitter = new PublicEmitter();
  593. $emitter->listen('\OC\User', 'changeUser', $hook);
  594. $config = $this->createMock(IConfig::class);
  595. $config->expects($this->once())
  596. ->method('setUserValue')
  597. ->with(
  598. 'foo',
  599. 'files',
  600. 'quota',
  601. '23 TB'
  602. );
  603. $user = new User('foo', $backend, $emitter, $config);
  604. $user->setQuota('23 TB');
  605. }
  606. public function testSetQuotaAddressNoChange() {
  607. /**
  608. * @var UserInterface | \PHPUnit_Framework_MockObject_MockObject $backend
  609. */
  610. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  611. /** @var PublicEmitter|\PHPUnit_Framework_MockObject_MockObject $emitter */
  612. $emitter = $this->createMock(PublicEmitter::class);
  613. $emitter->expects($this->never())
  614. ->method('emit');
  615. $config = $this->createMock(IConfig::class);
  616. $config->expects($this->any())
  617. ->method('getUserValue')
  618. ->willReturn('23 TB');
  619. $config->expects($this->once())
  620. ->method('setUserValue')
  621. ->with(
  622. 'foo',
  623. 'files',
  624. 'quota',
  625. '23 TB'
  626. );
  627. $user = new User('foo', $backend, $emitter, $config);
  628. $user->setQuota('23 TB');
  629. }
  630. public function testGetLastLogin() {
  631. /**
  632. * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  633. */
  634. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  635. $config = $this->createMock(IConfig::class);
  636. $config->method('getUserValue')
  637. ->will($this->returnCallback(function ($uid, $app, $key, $default) {
  638. if ($uid === 'foo' && $app === 'login' && $key === 'lastLogin') {
  639. return 42;
  640. } else {
  641. return $default;
  642. }
  643. }));
  644. $user = new User('foo', $backend, null, $config);
  645. $this->assertSame(42, $user->getLastLogin());
  646. }
  647. public function testSetEnabled() {
  648. /**
  649. * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  650. */
  651. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  652. $config = $this->createMock(IConfig::class);
  653. $config->expects($this->once())
  654. ->method('setUserValue')
  655. ->with(
  656. $this->equalTo('foo'),
  657. $this->equalTo('core'),
  658. $this->equalTo('enabled'),
  659. 'true'
  660. );
  661. $user = new User('foo', $backend, null, $config);
  662. $user->setEnabled(true);
  663. }
  664. public function testSetDisabled() {
  665. /**
  666. * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  667. */
  668. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  669. $config = $this->createMock(IConfig::class);
  670. $config->expects($this->once())
  671. ->method('setUserValue')
  672. ->with(
  673. $this->equalTo('foo'),
  674. $this->equalTo('core'),
  675. $this->equalTo('enabled'),
  676. 'false'
  677. );
  678. $user = $this->getMockBuilder(User::class)
  679. ->setConstructorArgs([
  680. 'foo',
  681. $backend,
  682. null,
  683. $config,
  684. ])
  685. ->setMethods(['isEnabled', 'triggerChange'])
  686. ->getMock();
  687. $user->expects($this->once())
  688. ->method('isEnabled')
  689. ->willReturn(true);
  690. $user->expects($this->once())
  691. ->method('triggerChange')
  692. ->with(
  693. 'enabled',
  694. 'false'
  695. );
  696. $user->setEnabled(false);
  697. }
  698. public function testSetDisabledAlreadyDisabled() {
  699. /**
  700. * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  701. */
  702. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  703. $config = $this->createMock(IConfig::class);
  704. $config->expects($this->never())
  705. ->method('setUserValue');
  706. $user = $this->getMockBuilder(User::class)
  707. ->setConstructorArgs([
  708. 'foo',
  709. $backend,
  710. null,
  711. $config,
  712. ])
  713. ->setMethods(['isEnabled', 'triggerChange'])
  714. ->getMock();
  715. $user->expects($this->once())
  716. ->method('isEnabled')
  717. ->willReturn(false);
  718. $user->expects($this->never())
  719. ->method('triggerChange');
  720. $user->setEnabled(false);
  721. }
  722. public function testGetEMailAddress() {
  723. /**
  724. * @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  725. */
  726. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  727. $config = $this->createMock(IConfig::class);
  728. $config->method('getUserValue')
  729. ->will($this->returnCallback(function ($uid, $app, $key, $default) {
  730. if ($uid === 'foo' && $app === 'settings' && $key === 'email') {
  731. return 'foo@bar.com';
  732. } else {
  733. return $default;
  734. }
  735. }));
  736. $user = new User('foo', $backend, null, $config);
  737. $this->assertSame('foo@bar.com', $user->getEMailAddress());
  738. }
  739. }