1
0

UserTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\User;
  8. use OC\AllConfig;
  9. use OC\Files\Mount\ObjectHomeMountProvider;
  10. use OC\Hooks\PublicEmitter;
  11. use OC\User\User;
  12. use OCP\Comments\ICommentsManager;
  13. use OCP\EventDispatcher\IEventDispatcher;
  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\Server;
  21. use OCP\UserInterface;
  22. use PHPUnit\Framework\MockObject\MockObject;
  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 IEventDispatcher|MockObject */
  33. protected $dispatcher;
  34. protected function setUp(): void {
  35. parent::setUp();
  36. $this->dispatcher = Server::get(IEventDispatcher::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('getSystemValueString')
  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. $config = $this->createMock(IConfig::class);
  314. $config->method('getSystemValueBool')
  315. ->with('allow_user_to_change_display_name')
  316. ->willReturn(true);
  317. $user = new User('foo', $backend, $this->dispatcher, null, $config);
  318. $this->assertTrue($user->canChangeDisplayName());
  319. }
  320. public function testCanChangeDisplayNameNotSupported() {
  321. /**
  322. * @var Backend | MockObject $backend
  323. */
  324. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  325. $backend->expects($this->any())
  326. ->method('implementsActions')
  327. ->willReturn(false);
  328. $user = new User('foo', $backend, $this->dispatcher);
  329. $this->assertFalse($user->canChangeDisplayName());
  330. }
  331. public function testSetDisplayNameSupported() {
  332. /**
  333. * @var Backend | MockObject $backend
  334. */
  335. $backend = $this->createMock(\OC\User\Database::class);
  336. $backend->expects($this->any())
  337. ->method('implementsActions')
  338. ->willReturnCallback(function ($actions) {
  339. if ($actions === \OC\User\Backend::SET_DISPLAYNAME) {
  340. return true;
  341. } else {
  342. return false;
  343. }
  344. });
  345. $backend->expects($this->once())
  346. ->method('setDisplayName')
  347. ->with('foo', 'Foo')
  348. ->willReturn(true);
  349. $user = new User('foo', $backend, $this->createMock(IEventDispatcher::class));
  350. $this->assertTrue($user->setDisplayName('Foo'));
  351. $this->assertEquals('Foo', $user->getDisplayName());
  352. }
  353. /**
  354. * don't allow display names containing whitespaces only
  355. */
  356. public function testSetDisplayNameEmpty() {
  357. /**
  358. * @var Backend | MockObject $backend
  359. */
  360. $backend = $this->createMock(\OC\User\Database::class);
  361. $backend->expects($this->any())
  362. ->method('implementsActions')
  363. ->willReturnCallback(function ($actions) {
  364. if ($actions === \OC\User\Backend::SET_DISPLAYNAME) {
  365. return true;
  366. } else {
  367. return false;
  368. }
  369. });
  370. $user = new User('foo', $backend, $this->dispatcher);
  371. $this->assertFalse($user->setDisplayName(' '));
  372. $this->assertEquals('foo', $user->getDisplayName());
  373. }
  374. public function testSetDisplayNameNotSupported() {
  375. /**
  376. * @var Backend | MockObject $backend
  377. */
  378. $backend = $this->createMock(\OC\User\Database::class);
  379. $backend->expects($this->any())
  380. ->method('implementsActions')
  381. ->willReturn(false);
  382. $backend->expects($this->never())
  383. ->method('setDisplayName');
  384. $user = new User('foo', $backend, $this->dispatcher);
  385. $this->assertFalse($user->setDisplayName('Foo'));
  386. $this->assertEquals('foo', $user->getDisplayName());
  387. }
  388. public function testSetPasswordHooks() {
  389. $hooksCalled = 0;
  390. $test = $this;
  391. /**
  392. * @var Backend | MockObject $backend
  393. */
  394. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  395. $backend->expects($this->once())
  396. ->method('setPassword');
  397. /**
  398. * @param User $user
  399. * @param string $password
  400. */
  401. $hook = function ($user, $password) use ($test, &$hooksCalled) {
  402. $hooksCalled++;
  403. $test->assertEquals('foo', $user->getUID());
  404. $test->assertEquals('bar', $password);
  405. };
  406. $emitter = new PublicEmitter();
  407. $emitter->listen('\OC\User', 'preSetPassword', $hook);
  408. $emitter->listen('\OC\User', 'postSetPassword', $hook);
  409. $backend->expects($this->any())
  410. ->method('implementsActions')
  411. ->willReturnCallback(function ($actions) {
  412. if ($actions === \OC\User\Backend::SET_PASSWORD) {
  413. return true;
  414. } else {
  415. return false;
  416. }
  417. });
  418. $user = new User('foo', $backend, $this->dispatcher, $emitter);
  419. $user->setPassword('bar', '');
  420. $this->assertEquals(2, $hooksCalled);
  421. }
  422. public function dataDeleteHooks() {
  423. return [
  424. [true, 2],
  425. [false, 1],
  426. ];
  427. }
  428. /**
  429. * @dataProvider dataDeleteHooks
  430. * @param bool $result
  431. * @param int $expectedHooks
  432. */
  433. public function testDeleteHooks($result, $expectedHooks) {
  434. $hooksCalled = 0;
  435. $test = $this;
  436. /**
  437. * @var Backend | MockObject $backend
  438. */
  439. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  440. $backend->expects($this->once())
  441. ->method('deleteUser')
  442. ->willReturn($result);
  443. $emitter = new PublicEmitter();
  444. $user = new User('foo', $backend, $this->dispatcher, $emitter);
  445. /**
  446. * @param User $user
  447. */
  448. $hook = function ($user) use ($test, &$hooksCalled) {
  449. $hooksCalled++;
  450. $test->assertEquals('foo', $user->getUID());
  451. };
  452. $emitter->listen('\OC\User', 'preDelete', $hook);
  453. $emitter->listen('\OC\User', 'postDelete', $hook);
  454. $config = $this->createMock(IConfig::class);
  455. $commentsManager = $this->createMock(ICommentsManager::class);
  456. $notificationManager = $this->createMock(INotificationManager::class);
  457. $config->method('getSystemValue')
  458. ->willReturnArgument(1);
  459. $config->method('getSystemValueString')
  460. ->willReturnArgument(1);
  461. $config->method('getSystemValueBool')
  462. ->willReturnArgument(1);
  463. $config->method('getSystemValueInt')
  464. ->willReturnArgument(1);
  465. if ($result) {
  466. $config->expects($this->once())
  467. ->method('deleteAllUserValues')
  468. ->with('foo');
  469. $commentsManager->expects($this->once())
  470. ->method('deleteReferencesOfActor')
  471. ->with('users', 'foo');
  472. $commentsManager->expects($this->once())
  473. ->method('deleteReadMarksFromUser')
  474. ->with($user);
  475. $notification = $this->createMock(INotification::class);
  476. $notification->expects($this->once())
  477. ->method('setUser')
  478. ->with('foo');
  479. $notificationManager->expects($this->once())
  480. ->method('createNotification')
  481. ->willReturn($notification);
  482. $notificationManager->expects($this->once())
  483. ->method('markProcessed')
  484. ->with($notification);
  485. } else {
  486. $config->expects($this->never())
  487. ->method('deleteAllUserValues');
  488. $commentsManager->expects($this->never())
  489. ->method('deleteReferencesOfActor');
  490. $commentsManager->expects($this->never())
  491. ->method('deleteReadMarksFromUser');
  492. $notificationManager->expects($this->never())
  493. ->method('createNotification');
  494. $notificationManager->expects($this->never())
  495. ->method('markProcessed');
  496. }
  497. $this->overwriteService(\OCP\Notification\IManager::class, $notificationManager);
  498. $this->overwriteService(\OCP\Comments\ICommentsManager::class, $commentsManager);
  499. $this->overwriteService(AllConfig::class, $config);
  500. $this->assertSame($result, $user->delete());
  501. $this->restoreService(AllConfig::class);
  502. $this->restoreService(\OCP\Comments\ICommentsManager::class);
  503. $this->restoreService(\OCP\Notification\IManager::class);
  504. $this->assertEquals($expectedHooks, $hooksCalled);
  505. }
  506. public function dataGetCloudId(): array {
  507. return [
  508. ['https://localhost:8888/nextcloud', 'foo@localhost:8888/nextcloud'],
  509. ['http://localhost:8888/nextcloud', 'foo@http://localhost:8888/nextcloud'],
  510. ];
  511. }
  512. /**
  513. * @dataProvider dataGetCloudId
  514. */
  515. public function testGetCloudId(string $absoluteUrl, string $cloudId): void {
  516. /** @var Backend|MockObject $backend */
  517. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  518. $urlGenerator = $this->createMock(IURLGenerator::class);
  519. $urlGenerator->method('getAbsoluteURL')
  520. ->withAnyParameters()
  521. ->willReturn($absoluteUrl);
  522. $user = new User('foo', $backend, $this->dispatcher, null, null, $urlGenerator);
  523. $this->assertEquals($cloudId, $user->getCloudId());
  524. }
  525. public function testSetEMailAddressEmpty() {
  526. /**
  527. * @var Backend | MockObject $backend
  528. */
  529. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  530. $test = $this;
  531. $hooksCalled = 0;
  532. /**
  533. * @param IUser $user
  534. * @param string $feature
  535. * @param string $value
  536. */
  537. $hook = function (IUser $user, $feature, $value) use ($test, &$hooksCalled) {
  538. $hooksCalled++;
  539. $test->assertEquals('eMailAddress', $feature);
  540. $test->assertEquals('', $value);
  541. };
  542. $emitter = new PublicEmitter();
  543. $emitter->listen('\OC\User', 'changeUser', $hook);
  544. $config = $this->createMock(IConfig::class);
  545. $config->expects($this->once())
  546. ->method('deleteUserValue')
  547. ->with(
  548. 'foo',
  549. 'settings',
  550. 'email'
  551. );
  552. $user = new User('foo', $backend, $this->dispatcher, $emitter, $config);
  553. $user->setEMailAddress('');
  554. }
  555. public function testSetEMailAddress() {
  556. /**
  557. * @var UserInterface | MockObject $backend
  558. */
  559. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  560. $test = $this;
  561. $hooksCalled = 0;
  562. /**
  563. * @param IUser $user
  564. * @param string $feature
  565. * @param string $value
  566. */
  567. $hook = function (IUser $user, $feature, $value) use ($test, &$hooksCalled) {
  568. $hooksCalled++;
  569. $test->assertEquals('eMailAddress', $feature);
  570. $test->assertEquals('foo@bar.com', $value);
  571. };
  572. $emitter = new PublicEmitter();
  573. $emitter->listen('\OC\User', 'changeUser', $hook);
  574. $config = $this->createMock(IConfig::class);
  575. $config->expects($this->once())
  576. ->method('setUserValue')
  577. ->with(
  578. 'foo',
  579. 'settings',
  580. 'email',
  581. 'foo@bar.com'
  582. );
  583. $user = new User('foo', $backend, $this->dispatcher, $emitter, $config);
  584. $user->setEMailAddress('foo@bar.com');
  585. }
  586. public function testSetEMailAddressNoChange() {
  587. /**
  588. * @var UserInterface | MockObject $backend
  589. */
  590. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  591. /** @var PublicEmitter|MockObject $emitter */
  592. $emitter = $this->createMock(PublicEmitter::class);
  593. $emitter->expects($this->never())
  594. ->method('emit');
  595. $dispatcher = $this->createMock(IEventDispatcher::class);
  596. $dispatcher->expects($this->never())
  597. ->method('dispatch');
  598. $config = $this->createMock(IConfig::class);
  599. $config->expects($this->any())
  600. ->method('getUserValue')
  601. ->willReturn('foo@bar.com');
  602. $config->expects($this->any())
  603. ->method('setUserValue');
  604. $user = new User('foo', $backend, $dispatcher, $emitter, $config);
  605. $user->setEMailAddress('foo@bar.com');
  606. }
  607. public function testSetQuota() {
  608. /**
  609. * @var UserInterface | MockObject $backend
  610. */
  611. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  612. $test = $this;
  613. $hooksCalled = 0;
  614. /**
  615. * @param IUser $user
  616. * @param string $feature
  617. * @param string $value
  618. */
  619. $hook = function (IUser $user, $feature, $value) use ($test, &$hooksCalled) {
  620. $hooksCalled++;
  621. $test->assertEquals('quota', $feature);
  622. $test->assertEquals('23 TB', $value);
  623. };
  624. $emitter = new PublicEmitter();
  625. $emitter->listen('\OC\User', 'changeUser', $hook);
  626. $config = $this->createMock(IConfig::class);
  627. $config->expects($this->once())
  628. ->method('setUserValue')
  629. ->with(
  630. 'foo',
  631. 'files',
  632. 'quota',
  633. '23 TB'
  634. );
  635. $user = new User('foo', $backend, $this->dispatcher, $emitter, $config);
  636. $user->setQuota('23 TB');
  637. }
  638. public function testGetDefaultUnlimitedQuota() {
  639. /**
  640. * @var UserInterface | MockObject $backend
  641. */
  642. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  643. /** @var PublicEmitter|MockObject $emitter */
  644. $emitter = $this->createMock(PublicEmitter::class);
  645. $emitter->expects($this->never())
  646. ->method('emit');
  647. $config = $this->createMock(IConfig::class);
  648. $user = new User('foo', $backend, $this->dispatcher, $emitter, $config);
  649. $userValueMap = [
  650. ['foo', 'files', 'quota', 'default', 'default'],
  651. ];
  652. $appValueMap = [
  653. ['files', 'default_quota', 'none', 'none'],
  654. // allow unlimited quota
  655. ['files', 'allow_unlimited_quota', '1', '1'],
  656. ];
  657. $config->method('getUserValue')
  658. ->will($this->returnValueMap($userValueMap));
  659. $config->method('getAppValue')
  660. ->will($this->returnValueMap($appValueMap));
  661. $quota = $user->getQuota();
  662. $this->assertEquals('none', $quota);
  663. }
  664. public function testGetDefaultUnlimitedQuotaForbidden() {
  665. /**
  666. * @var UserInterface | MockObject $backend
  667. */
  668. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  669. /** @var PublicEmitter|MockObject $emitter */
  670. $emitter = $this->createMock(PublicEmitter::class);
  671. $emitter->expects($this->never())
  672. ->method('emit');
  673. $config = $this->createMock(IConfig::class);
  674. $user = new User('foo', $backend, $this->dispatcher, $emitter, $config);
  675. $userValueMap = [
  676. ['foo', 'files', 'quota', 'default', 'default'],
  677. ];
  678. $appValueMap = [
  679. ['files', 'default_quota', 'none', 'none'],
  680. // do not allow unlimited quota
  681. ['files', 'allow_unlimited_quota', '1', '0'],
  682. ['files', 'quota_preset', '1 GB, 5 GB, 10 GB', '1 GB, 5 GB, 10 GB'],
  683. // expect seeing 1 GB used as fallback value
  684. ['files', 'default_quota', '1 GB', '1 GB'],
  685. ];
  686. $config->method('getUserValue')
  687. ->will($this->returnValueMap($userValueMap));
  688. $config->method('getAppValue')
  689. ->will($this->returnValueMap($appValueMap));
  690. $quota = $user->getQuota();
  691. $this->assertEquals('1 GB', $quota);
  692. }
  693. public function testSetQuotaAddressNoChange() {
  694. /**
  695. * @var UserInterface | MockObject $backend
  696. */
  697. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  698. /** @var PublicEmitter|MockObject $emitter */
  699. $emitter = $this->createMock(PublicEmitter::class);
  700. $emitter->expects($this->never())
  701. ->method('emit');
  702. $config = $this->createMock(IConfig::class);
  703. $config->expects($this->any())
  704. ->method('getUserValue')
  705. ->willReturn('23 TB');
  706. $config->expects($this->never())
  707. ->method('setUserValue');
  708. $user = new User('foo', $backend, $this->dispatcher, $emitter, $config);
  709. $user->setQuota('23 TB');
  710. }
  711. public function testGetLastLogin() {
  712. /**
  713. * @var Backend | MockObject $backend
  714. */
  715. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  716. $config = $this->createMock(IConfig::class);
  717. $config->method('getUserValue')
  718. ->willReturnCallback(function ($uid, $app, $key, $default) {
  719. if ($uid === 'foo' && $app === 'login' && $key === 'lastLogin') {
  720. return 42;
  721. } else {
  722. return $default;
  723. }
  724. });
  725. $user = new User('foo', $backend, $this->dispatcher, null, $config);
  726. $this->assertSame(42, $user->getLastLogin());
  727. }
  728. public function testSetEnabled() {
  729. /**
  730. * @var Backend | MockObject $backend
  731. */
  732. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  733. $config = $this->createMock(IConfig::class);
  734. $config->expects($this->once())
  735. ->method('setUserValue')
  736. ->with(
  737. $this->equalTo('foo'),
  738. $this->equalTo('core'),
  739. $this->equalTo('enabled'),
  740. 'true'
  741. );
  742. $user = new User('foo', $backend, $this->dispatcher, null, $config);
  743. $user->setEnabled(true);
  744. }
  745. public function testSetDisabled() {
  746. /**
  747. * @var Backend | MockObject $backend
  748. */
  749. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  750. $config = $this->createMock(IConfig::class);
  751. $config->expects($this->once())
  752. ->method('setUserValue')
  753. ->with(
  754. $this->equalTo('foo'),
  755. $this->equalTo('core'),
  756. $this->equalTo('enabled'),
  757. 'false'
  758. );
  759. $user = $this->getMockBuilder(User::class)
  760. ->setConstructorArgs([
  761. 'foo',
  762. $backend,
  763. $this->dispatcher,
  764. null,
  765. $config,
  766. ])
  767. ->setMethods(['isEnabled', 'triggerChange'])
  768. ->getMock();
  769. $user->expects($this->once())
  770. ->method('isEnabled')
  771. ->willReturn(true);
  772. $user->expects($this->once())
  773. ->method('triggerChange')
  774. ->with(
  775. 'enabled',
  776. false
  777. );
  778. $user->setEnabled(false);
  779. }
  780. public function testSetDisabledAlreadyDisabled() {
  781. /**
  782. * @var Backend | MockObject $backend
  783. */
  784. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  785. $config = $this->createMock(IConfig::class);
  786. $config->expects($this->never())
  787. ->method('setUserValue');
  788. $user = $this->getMockBuilder(User::class)
  789. ->setConstructorArgs([
  790. 'foo',
  791. $backend,
  792. $this->dispatcher,
  793. null,
  794. $config,
  795. ])
  796. ->setMethods(['isEnabled', 'triggerChange'])
  797. ->getMock();
  798. $user->expects($this->once())
  799. ->method('isEnabled')
  800. ->willReturn(false);
  801. $user->expects($this->never())
  802. ->method('triggerChange');
  803. $user->setEnabled(false);
  804. }
  805. public function testGetEMailAddress() {
  806. /**
  807. * @var Backend | MockObject $backend
  808. */
  809. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  810. $config = $this->createMock(IConfig::class);
  811. $config->method('getUserValue')
  812. ->willReturnCallback(function ($uid, $app, $key, $default) {
  813. if ($uid === 'foo' && $app === 'settings' && $key === 'email') {
  814. return 'foo@bar.com';
  815. } else {
  816. return $default;
  817. }
  818. });
  819. $user = new User('foo', $backend, $this->dispatcher, null, $config);
  820. $this->assertSame('foo@bar.com', $user->getEMailAddress());
  821. }
  822. }