1
0

UserTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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(): void {
  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(): void {
  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(): void {
  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(): void {
  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(): void {
  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(): void {
  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(): void {
  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(): void {
  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(): void {
  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(): void {
  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(): void {
  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(): void {
  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(): void {
  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(): void {
  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(): void {
  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(): void {
  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(): void {
  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(): void {
  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(): void {
  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(): void {
  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(): void {
  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): void {
  434. $hooksCalled = 0;
  435. $test = $this;
  436. /**
  437. * @var UserInterface&MockObject $backend
  438. */
  439. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  440. $backend->expects($this->once())
  441. ->method('deleteUser')
  442. ->willReturn($result);
  443. $config = $this->createMock(IConfig::class);
  444. $config->method('getSystemValue')
  445. ->willReturnArgument(1);
  446. $config->method('getSystemValueString')
  447. ->willReturnArgument(1);
  448. $config->method('getSystemValueBool')
  449. ->willReturnArgument(1);
  450. $config->method('getSystemValueInt')
  451. ->willReturnArgument(1);
  452. $emitter = new PublicEmitter();
  453. $user = new User('foo', $backend, $this->dispatcher, $emitter, $config);
  454. /**
  455. * @param User $user
  456. */
  457. $hook = function ($user) use ($test, &$hooksCalled) {
  458. $hooksCalled++;
  459. $test->assertEquals('foo', $user->getUID());
  460. };
  461. $emitter->listen('\OC\User', 'preDelete', $hook);
  462. $emitter->listen('\OC\User', 'postDelete', $hook);
  463. $commentsManager = $this->createMock(ICommentsManager::class);
  464. $notificationManager = $this->createMock(INotificationManager::class);
  465. if ($result) {
  466. $config->expects($this->atLeastOnce())
  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->assertSame($result, $user->delete());
  500. $this->restoreService(AllConfig::class);
  501. $this->restoreService(\OCP\Comments\ICommentsManager::class);
  502. $this->restoreService(\OCP\Notification\IManager::class);
  503. $this->assertEquals($expectedHooks, $hooksCalled);
  504. }
  505. public function testDeleteRecoverState() {
  506. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  507. $backend->expects($this->once())
  508. ->method('deleteUser')
  509. ->willReturn(true);
  510. $config = $this->createMock(IConfig::class);
  511. $config->method('getSystemValue')
  512. ->willReturnArgument(1);
  513. $config->method('getSystemValueString')
  514. ->willReturnArgument(1);
  515. $config->method('getSystemValueBool')
  516. ->willReturnArgument(1);
  517. $config->method('getSystemValueInt')
  518. ->willReturnArgument(1);
  519. $userConfig = [];
  520. $config->expects(self::atLeast(2))
  521. ->method('setUserValue')
  522. ->willReturnCallback(function () {
  523. $userConfig[] = func_get_args();
  524. });
  525. $commentsManager = $this->createMock(ICommentsManager::class);
  526. $commentsManager->expects($this->once())
  527. ->method('deleteReferencesOfActor')
  528. ->willThrowException(new \Error('Test exception'));
  529. $this->overwriteService(\OCP\Comments\ICommentsManager::class, $commentsManager);
  530. $this->expectException(\Error::class);
  531. $user = $this->getMockBuilder(User::class)
  532. ->onlyMethods(['getHome'])
  533. ->setConstructorArgs(['foo', $backend, $this->dispatcher, null, $config])
  534. ->getMock();
  535. $user->expects(self::atLeastOnce())
  536. ->method('getHome')
  537. ->willReturn('/home/path');
  538. $user->delete();
  539. $this->assertEqualsCanonicalizing(
  540. [
  541. ['foo', 'core', 'deleted', 'true', null],
  542. ['foo', 'core', 'deleted.backup-home', '/home/path', null],
  543. ],
  544. $userConfig,
  545. );
  546. $this->restoreService(\OCP\Comments\ICommentsManager::class);
  547. }
  548. public function dataGetCloudId(): array {
  549. return [
  550. ['https://localhost:8888/nextcloud', 'foo@localhost:8888/nextcloud'],
  551. ['http://localhost:8888/nextcloud', 'foo@http://localhost:8888/nextcloud'],
  552. ];
  553. }
  554. /**
  555. * @dataProvider dataGetCloudId
  556. */
  557. public function testGetCloudId(string $absoluteUrl, string $cloudId): void {
  558. /** @var Backend|MockObject $backend */
  559. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  560. $urlGenerator = $this->createMock(IURLGenerator::class);
  561. $urlGenerator->method('getAbsoluteURL')
  562. ->withAnyParameters()
  563. ->willReturn($absoluteUrl);
  564. $user = new User('foo', $backend, $this->dispatcher, null, null, $urlGenerator);
  565. $this->assertEquals($cloudId, $user->getCloudId());
  566. }
  567. public function testSetEMailAddressEmpty(): void {
  568. /**
  569. * @var Backend | MockObject $backend
  570. */
  571. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  572. $test = $this;
  573. $hooksCalled = 0;
  574. /**
  575. * @param IUser $user
  576. * @param string $feature
  577. * @param string $value
  578. */
  579. $hook = function (IUser $user, $feature, $value) use ($test, &$hooksCalled) {
  580. $hooksCalled++;
  581. $test->assertEquals('eMailAddress', $feature);
  582. $test->assertEquals('', $value);
  583. };
  584. $emitter = new PublicEmitter();
  585. $emitter->listen('\OC\User', 'changeUser', $hook);
  586. $config = $this->createMock(IConfig::class);
  587. $config->expects($this->once())
  588. ->method('deleteUserValue')
  589. ->with(
  590. 'foo',
  591. 'settings',
  592. 'email'
  593. );
  594. $user = new User('foo', $backend, $this->dispatcher, $emitter, $config);
  595. $user->setEMailAddress('');
  596. }
  597. public function testSetEMailAddress(): void {
  598. /**
  599. * @var UserInterface | MockObject $backend
  600. */
  601. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  602. $test = $this;
  603. $hooksCalled = 0;
  604. /**
  605. * @param IUser $user
  606. * @param string $feature
  607. * @param string $value
  608. */
  609. $hook = function (IUser $user, $feature, $value) use ($test, &$hooksCalled) {
  610. $hooksCalled++;
  611. $test->assertEquals('eMailAddress', $feature);
  612. $test->assertEquals('foo@bar.com', $value);
  613. };
  614. $emitter = new PublicEmitter();
  615. $emitter->listen('\OC\User', 'changeUser', $hook);
  616. $config = $this->createMock(IConfig::class);
  617. $config->expects($this->once())
  618. ->method('setUserValue')
  619. ->with(
  620. 'foo',
  621. 'settings',
  622. 'email',
  623. 'foo@bar.com'
  624. );
  625. $user = new User('foo', $backend, $this->dispatcher, $emitter, $config);
  626. $user->setEMailAddress('foo@bar.com');
  627. }
  628. public function testSetEMailAddressNoChange(): void {
  629. /**
  630. * @var UserInterface | MockObject $backend
  631. */
  632. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  633. /** @var PublicEmitter|MockObject $emitter */
  634. $emitter = $this->createMock(PublicEmitter::class);
  635. $emitter->expects($this->never())
  636. ->method('emit');
  637. $dispatcher = $this->createMock(IEventDispatcher::class);
  638. $dispatcher->expects($this->never())
  639. ->method('dispatch');
  640. $config = $this->createMock(IConfig::class);
  641. $config->expects($this->any())
  642. ->method('getUserValue')
  643. ->willReturn('foo@bar.com');
  644. $config->expects($this->any())
  645. ->method('setUserValue');
  646. $user = new User('foo', $backend, $dispatcher, $emitter, $config);
  647. $user->setEMailAddress('foo@bar.com');
  648. }
  649. public function testSetQuota(): void {
  650. /**
  651. * @var UserInterface | MockObject $backend
  652. */
  653. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  654. $test = $this;
  655. $hooksCalled = 0;
  656. /**
  657. * @param IUser $user
  658. * @param string $feature
  659. * @param string $value
  660. */
  661. $hook = function (IUser $user, $feature, $value) use ($test, &$hooksCalled) {
  662. $hooksCalled++;
  663. $test->assertEquals('quota', $feature);
  664. $test->assertEquals('23 TB', $value);
  665. };
  666. $emitter = new PublicEmitter();
  667. $emitter->listen('\OC\User', 'changeUser', $hook);
  668. $config = $this->createMock(IConfig::class);
  669. $config->expects($this->once())
  670. ->method('setUserValue')
  671. ->with(
  672. 'foo',
  673. 'files',
  674. 'quota',
  675. '23 TB'
  676. );
  677. $user = new User('foo', $backend, $this->dispatcher, $emitter, $config);
  678. $user->setQuota('23 TB');
  679. }
  680. public function testGetDefaultUnlimitedQuota(): void {
  681. /**
  682. * @var UserInterface | MockObject $backend
  683. */
  684. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  685. /** @var PublicEmitter|MockObject $emitter */
  686. $emitter = $this->createMock(PublicEmitter::class);
  687. $emitter->expects($this->never())
  688. ->method('emit');
  689. $config = $this->createMock(IConfig::class);
  690. $user = new User('foo', $backend, $this->dispatcher, $emitter, $config);
  691. $userValueMap = [
  692. ['foo', 'files', 'quota', 'default', 'default'],
  693. ];
  694. $appValueMap = [
  695. ['files', 'default_quota', 'none', 'none'],
  696. // allow unlimited quota
  697. ['files', 'allow_unlimited_quota', '1', '1'],
  698. ];
  699. $config->method('getUserValue')
  700. ->will($this->returnValueMap($userValueMap));
  701. $config->method('getAppValue')
  702. ->will($this->returnValueMap($appValueMap));
  703. $quota = $user->getQuota();
  704. $this->assertEquals('none', $quota);
  705. }
  706. public function testGetDefaultUnlimitedQuotaForbidden(): void {
  707. /**
  708. * @var UserInterface | MockObject $backend
  709. */
  710. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  711. /** @var PublicEmitter|MockObject $emitter */
  712. $emitter = $this->createMock(PublicEmitter::class);
  713. $emitter->expects($this->never())
  714. ->method('emit');
  715. $config = $this->createMock(IConfig::class);
  716. $user = new User('foo', $backend, $this->dispatcher, $emitter, $config);
  717. $userValueMap = [
  718. ['foo', 'files', 'quota', 'default', 'default'],
  719. ];
  720. $appValueMap = [
  721. ['files', 'default_quota', 'none', 'none'],
  722. // do not allow unlimited quota
  723. ['files', 'allow_unlimited_quota', '1', '0'],
  724. ['files', 'quota_preset', '1 GB, 5 GB, 10 GB', '1 GB, 5 GB, 10 GB'],
  725. // expect seeing 1 GB used as fallback value
  726. ['files', 'default_quota', '1 GB', '1 GB'],
  727. ];
  728. $config->method('getUserValue')
  729. ->will($this->returnValueMap($userValueMap));
  730. $config->method('getAppValue')
  731. ->will($this->returnValueMap($appValueMap));
  732. $quota = $user->getQuota();
  733. $this->assertEquals('1 GB', $quota);
  734. }
  735. public function testSetQuotaAddressNoChange(): void {
  736. /**
  737. * @var UserInterface | MockObject $backend
  738. */
  739. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  740. /** @var PublicEmitter|MockObject $emitter */
  741. $emitter = $this->createMock(PublicEmitter::class);
  742. $emitter->expects($this->never())
  743. ->method('emit');
  744. $config = $this->createMock(IConfig::class);
  745. $config->expects($this->any())
  746. ->method('getUserValue')
  747. ->willReturn('23 TB');
  748. $config->expects($this->never())
  749. ->method('setUserValue');
  750. $user = new User('foo', $backend, $this->dispatcher, $emitter, $config);
  751. $user->setQuota('23 TB');
  752. }
  753. public function testGetLastLogin(): void {
  754. /**
  755. * @var Backend | MockObject $backend
  756. */
  757. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  758. $config = $this->createMock(IConfig::class);
  759. $config->method('getUserValue')
  760. ->willReturnCallback(function ($uid, $app, $key, $default) {
  761. if ($uid === 'foo' && $app === 'login' && $key === 'lastLogin') {
  762. return 42;
  763. } else {
  764. return $default;
  765. }
  766. });
  767. $user = new User('foo', $backend, $this->dispatcher, null, $config);
  768. $this->assertSame(42, $user->getLastLogin());
  769. }
  770. public function testSetEnabled(): void {
  771. /**
  772. * @var Backend | MockObject $backend
  773. */
  774. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  775. $config = $this->createMock(IConfig::class);
  776. $config->expects($this->once())
  777. ->method('setUserValue')
  778. ->with(
  779. $this->equalTo('foo'),
  780. $this->equalTo('core'),
  781. $this->equalTo('enabled'),
  782. 'true'
  783. );
  784. $user = new User('foo', $backend, $this->dispatcher, null, $config);
  785. $user->setEnabled(true);
  786. }
  787. public function testSetDisabled(): void {
  788. /**
  789. * @var Backend | MockObject $backend
  790. */
  791. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  792. $config = $this->createMock(IConfig::class);
  793. $config->expects($this->once())
  794. ->method('setUserValue')
  795. ->with(
  796. $this->equalTo('foo'),
  797. $this->equalTo('core'),
  798. $this->equalTo('enabled'),
  799. 'false'
  800. );
  801. $user = $this->getMockBuilder(User::class)
  802. ->setConstructorArgs([
  803. 'foo',
  804. $backend,
  805. $this->dispatcher,
  806. null,
  807. $config,
  808. ])
  809. ->setMethods(['isEnabled', 'triggerChange'])
  810. ->getMock();
  811. $user->expects($this->once())
  812. ->method('isEnabled')
  813. ->willReturn(true);
  814. $user->expects($this->once())
  815. ->method('triggerChange')
  816. ->with(
  817. 'enabled',
  818. false
  819. );
  820. $user->setEnabled(false);
  821. }
  822. public function testSetDisabledAlreadyDisabled(): void {
  823. /**
  824. * @var Backend | MockObject $backend
  825. */
  826. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  827. $config = $this->createMock(IConfig::class);
  828. $config->expects($this->never())
  829. ->method('setUserValue');
  830. $user = $this->getMockBuilder(User::class)
  831. ->setConstructorArgs([
  832. 'foo',
  833. $backend,
  834. $this->dispatcher,
  835. null,
  836. $config,
  837. ])
  838. ->setMethods(['isEnabled', 'triggerChange'])
  839. ->getMock();
  840. $user->expects($this->once())
  841. ->method('isEnabled')
  842. ->willReturn(false);
  843. $user->expects($this->never())
  844. ->method('triggerChange');
  845. $user->setEnabled(false);
  846. }
  847. public function testGetEMailAddress(): void {
  848. /**
  849. * @var Backend | MockObject $backend
  850. */
  851. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  852. $config = $this->createMock(IConfig::class);
  853. $config->method('getUserValue')
  854. ->willReturnCallback(function ($uid, $app, $key, $default) {
  855. if ($uid === 'foo' && $app === 'settings' && $key === 'email') {
  856. return 'foo@bar.com';
  857. } else {
  858. return $default;
  859. }
  860. });
  861. $user = new User('foo', $backend, $this->dispatcher, null, $config);
  862. $this->assertSame('foo@bar.com', $user->getEMailAddress());
  863. }
  864. }