1
0

UserTest.php 25 KB

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