UserTest.php 23 KB

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