1
0

UserTest.php 26 KB

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