1
0

UserTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\User;
  9. use OC\AllConfig;
  10. use OC\Files\Mount\ObjectHomeMountProvider;
  11. use OC\Hooks\PublicEmitter;
  12. use OC\User\User;
  13. use OCP\Comments\ICommentsManager;
  14. use OCP\Files\Storage\IStorageFactory;
  15. use OCP\IConfig;
  16. use OCP\IURLGenerator;
  17. use OCP\IUser;
  18. use OCP\Notification\IManager as INotificationManager;
  19. use OCP\Notification\INotification;
  20. use OCP\UserInterface;
  21. use PHPUnit\Framework\MockObject\MockObject;
  22. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  23. use Test\TestCase;
  24. /**
  25. * Class UserTest
  26. *
  27. * @group DB
  28. *
  29. * @package Test\User
  30. */
  31. class UserTest extends TestCase {
  32. /** @var EventDispatcherInterface|MockObject */
  33. protected $dispatcher;
  34. protected function setUp(): void {
  35. parent::setUp();
  36. $this->dispatcher = $this->createMock(EventDispatcherInterface::class);
  37. }
  38. public function testDisplayName() {
  39. /**
  40. * @var \OC\User\Backend | MockObject $backend
  41. */
  42. $backend = $this->createMock(\OC\User\Backend::class);
  43. $backend->expects($this->once())
  44. ->method('getDisplayName')
  45. ->with($this->equalTo('foo'))
  46. ->willReturn('Foo');
  47. $backend->expects($this->any())
  48. ->method('implementsActions')
  49. ->with($this->equalTo(\OC\User\Backend::GET_DISPLAYNAME))
  50. ->willReturn(true);
  51. $user = new User('foo', $backend, $this->dispatcher);
  52. $this->assertEquals('Foo', $user->getDisplayName());
  53. }
  54. /**
  55. * if the display name contain whitespaces only, we expect the uid as result
  56. */
  57. public function testDisplayNameEmpty() {
  58. /**
  59. * @var \OC\User\Backend | MockObject $backend
  60. */
  61. $backend = $this->createMock(\OC\User\Backend::class);
  62. $backend->expects($this->once())
  63. ->method('getDisplayName')
  64. ->with($this->equalTo('foo'))
  65. ->willReturn(' ');
  66. $backend->expects($this->any())
  67. ->method('implementsActions')
  68. ->with($this->equalTo(\OC\User\Backend::GET_DISPLAYNAME))
  69. ->willReturn(true);
  70. $user = new User('foo', $backend, $this->dispatcher);
  71. $this->assertEquals('foo', $user->getDisplayName());
  72. }
  73. public function testDisplayNameNotSupported() {
  74. /**
  75. * @var \OC\User\Backend | MockObject $backend
  76. */
  77. $backend = $this->createMock(\OC\User\Backend::class);
  78. $backend->expects($this->never())
  79. ->method('getDisplayName');
  80. $backend->expects($this->any())
  81. ->method('implementsActions')
  82. ->with($this->equalTo(\OC\User\Backend::GET_DISPLAYNAME))
  83. ->willReturn(false);
  84. $user = new User('foo', $backend, $this->dispatcher);
  85. $this->assertEquals('foo', $user->getDisplayName());
  86. }
  87. public function testSetPassword() {
  88. /**
  89. * @var Backend | MockObject $backend
  90. */
  91. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  92. $backend->expects($this->once())
  93. ->method('setPassword')
  94. ->with($this->equalTo('foo'), $this->equalTo('bar'));
  95. $backend->expects($this->any())
  96. ->method('implementsActions')
  97. ->willReturnCallback(function ($actions) {
  98. if ($actions === \OC\User\Backend::SET_PASSWORD) {
  99. return true;
  100. } else {
  101. return false;
  102. }
  103. });
  104. $user = new User('foo', $backend, $this->dispatcher);
  105. $this->assertTrue($user->setPassword('bar', ''));
  106. }
  107. public function testSetPasswordNotSupported() {
  108. /**
  109. * @var Backend | MockObject $backend
  110. */
  111. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  112. $backend->expects($this->never())
  113. ->method('setPassword');
  114. $backend->expects($this->any())
  115. ->method('implementsActions')
  116. ->willReturn(false);
  117. $user = new User('foo', $backend, $this->dispatcher);
  118. $this->assertFalse($user->setPassword('bar', ''));
  119. }
  120. public function testChangeAvatarSupportedYes() {
  121. /**
  122. * @var Backend | MockObject $backend
  123. */
  124. $backend = $this->createMock(AvatarUserDummy::class);
  125. $backend->expects($this->once())
  126. ->method('canChangeAvatar')
  127. ->with($this->equalTo('foo'))
  128. ->willReturn(true);
  129. $backend->expects($this->any())
  130. ->method('implementsActions')
  131. ->willReturnCallback(function ($actions) {
  132. if ($actions === \OC\User\Backend::PROVIDE_AVATAR) {
  133. return true;
  134. } else {
  135. return false;
  136. }
  137. });
  138. $user = new User('foo', $backend, $this->dispatcher);
  139. $this->assertTrue($user->canChangeAvatar());
  140. }
  141. public function testChangeAvatarSupportedNo() {
  142. /**
  143. * @var Backend | MockObject $backend
  144. */
  145. $backend = $this->createMock(AvatarUserDummy::class);
  146. $backend->expects($this->once())
  147. ->method('canChangeAvatar')
  148. ->with($this->equalTo('foo'))
  149. ->willReturn(false);
  150. $backend->expects($this->any())
  151. ->method('implementsActions')
  152. ->willReturnCallback(function ($actions) {
  153. if ($actions === \OC\User\Backend::PROVIDE_AVATAR) {
  154. return true;
  155. } else {
  156. return false;
  157. }
  158. });
  159. $user = new User('foo', $backend, $this->dispatcher);
  160. $this->assertFalse($user->canChangeAvatar());
  161. }
  162. public function testChangeAvatarNotSupported() {
  163. /**
  164. * @var Backend | MockObject $backend
  165. */
  166. $backend = $this->createMock(AvatarUserDummy::class);
  167. $backend->expects($this->never())
  168. ->method('canChangeAvatar');
  169. $backend->expects($this->any())
  170. ->method('implementsActions')
  171. ->willReturn(false);
  172. $user = new User('foo', $backend, $this->dispatcher);
  173. $this->assertTrue($user->canChangeAvatar());
  174. }
  175. public function testDelete() {
  176. /**
  177. * @var Backend | MockObject $backend
  178. */
  179. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  180. $backend->expects($this->once())
  181. ->method('deleteUser')
  182. ->with($this->equalTo('foo'));
  183. $user = new User('foo', $backend, $this->dispatcher);
  184. $this->assertTrue($user->delete());
  185. }
  186. public function testDeleteWithDifferentHome() {
  187. /** @var ObjectHomeMountProvider $homeProvider */
  188. $homeProvider = \OC::$server->get(ObjectHomeMountProvider::class);
  189. $user = $this->createMock(IUser::class);
  190. $user->method('getUID')
  191. ->willReturn('foo');
  192. if ($homeProvider->getHomeMountForUser($user, $this->createMock(IStorageFactory::class)) !== null) {
  193. $this->markTestSkipped("Skipping test for non local home storage");
  194. }
  195. /**
  196. * @var Backend | MockObject $backend
  197. */
  198. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  199. $backend->expects($this->once())
  200. ->method('implementsActions')
  201. ->willReturnCallback(function ($actions) {
  202. if ($actions === \OC\User\Backend::GET_HOME) {
  203. return true;
  204. } else {
  205. return false;
  206. }
  207. });
  208. // important: getHome MUST be called before deleteUser because
  209. // once the user is deleted, getHome implementations might not
  210. // return anything
  211. $backend->expects($this->once())
  212. ->method('getHome')
  213. ->with($this->equalTo('foo'))
  214. ->willReturn('/home/foo');
  215. $backend->expects($this->once())
  216. ->method('deleteUser')
  217. ->with($this->equalTo('foo'));
  218. $user = new User('foo', $backend, $this->dispatcher);
  219. $this->assertTrue($user->delete());
  220. }
  221. public function testGetHome() {
  222. /**
  223. * @var Backend | MockObject $backend
  224. */
  225. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  226. $backend->expects($this->once())
  227. ->method('getHome')
  228. ->with($this->equalTo('foo'))
  229. ->willReturn('/home/foo');
  230. $backend->expects($this->any())
  231. ->method('implementsActions')
  232. ->willReturnCallback(function ($actions) {
  233. if ($actions === \OC\User\Backend::GET_HOME) {
  234. return true;
  235. } else {
  236. return false;
  237. }
  238. });
  239. $user = new User('foo', $backend, $this->dispatcher);
  240. $this->assertEquals('/home/foo', $user->getHome());
  241. }
  242. public function testGetBackendClassName() {
  243. $user = new User('foo', new \Test\Util\User\Dummy(), $this->dispatcher);
  244. $this->assertEquals('Dummy', $user->getBackendClassName());
  245. $user = new User('foo', new \OC\User\Database(), $this->dispatcher);
  246. $this->assertEquals('Database', $user->getBackendClassName());
  247. }
  248. public function testGetHomeNotSupported() {
  249. /**
  250. * @var Backend | MockObject $backend
  251. */
  252. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  253. $backend->expects($this->never())
  254. ->method('getHome');
  255. $backend->expects($this->any())
  256. ->method('implementsActions')
  257. ->willReturn(false);
  258. $allConfig = $this->getMockBuilder(IConfig::class)
  259. ->disableOriginalConstructor()
  260. ->getMock();
  261. $allConfig->expects($this->any())
  262. ->method('getUserValue')
  263. ->willReturn(true);
  264. $allConfig->expects($this->any())
  265. ->method('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->dispatcher);
  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. $this->dispatcher->expects($this->never())
  596. ->method('dispatch');
  597. $config = $this->createMock(IConfig::class);
  598. $config->expects($this->any())
  599. ->method('getUserValue')
  600. ->willReturn('foo@bar.com');
  601. $config->expects($this->any())
  602. ->method('setUserValue');
  603. $user = new User('foo', $backend, $this->dispatcher, $emitter, $config);
  604. $user->setEMailAddress('foo@bar.com');
  605. }
  606. public function testSetQuota() {
  607. /**
  608. * @var UserInterface | MockObject $backend
  609. */
  610. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  611. $test = $this;
  612. $hooksCalled = 0;
  613. /**
  614. * @param IUser $user
  615. * @param string $feature
  616. * @param string $value
  617. */
  618. $hook = function (IUser $user, $feature, $value) use ($test, &$hooksCalled) {
  619. $hooksCalled++;
  620. $test->assertEquals('quota', $feature);
  621. $test->assertEquals('23 TB', $value);
  622. };
  623. $emitter = new PublicEmitter();
  624. $emitter->listen('\OC\User', 'changeUser', $hook);
  625. $config = $this->createMock(IConfig::class);
  626. $config->expects($this->once())
  627. ->method('setUserValue')
  628. ->with(
  629. 'foo',
  630. 'files',
  631. 'quota',
  632. '23 TB'
  633. );
  634. $user = new User('foo', $backend, $this->dispatcher, $emitter, $config);
  635. $user->setQuota('23 TB');
  636. }
  637. public function testGetDefaultUnlimitedQuota() {
  638. /**
  639. * @var UserInterface | MockObject $backend
  640. */
  641. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  642. /** @var PublicEmitter|MockObject $emitter */
  643. $emitter = $this->createMock(PublicEmitter::class);
  644. $emitter->expects($this->never())
  645. ->method('emit');
  646. $config = $this->createMock(IConfig::class);
  647. $user = new User('foo', $backend, $this->dispatcher, $emitter, $config);
  648. $userValueMap = [
  649. ['foo', 'files', 'quota', 'default', 'default'],
  650. ];
  651. $appValueMap = [
  652. ['files', 'default_quota', 'none', 'none'],
  653. // allow unlimited quota
  654. ['files', 'allow_unlimited_quota', '1', '1'],
  655. ];
  656. $config->method('getUserValue')
  657. ->will($this->returnValueMap($userValueMap));
  658. $config->method('getAppValue')
  659. ->will($this->returnValueMap($appValueMap));
  660. $quota = $user->getQuota();
  661. $this->assertEquals('none', $quota);
  662. }
  663. public function testGetDefaultUnlimitedQuotaForbidden() {
  664. /**
  665. * @var UserInterface | MockObject $backend
  666. */
  667. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  668. /** @var PublicEmitter|MockObject $emitter */
  669. $emitter = $this->createMock(PublicEmitter::class);
  670. $emitter->expects($this->never())
  671. ->method('emit');
  672. $config = $this->createMock(IConfig::class);
  673. $user = new User('foo', $backend, $this->dispatcher, $emitter, $config);
  674. $userValueMap = [
  675. ['foo', 'files', 'quota', 'default', 'default'],
  676. ];
  677. $appValueMap = [
  678. ['files', 'default_quota', 'none', 'none'],
  679. // do not allow unlimited quota
  680. ['files', 'allow_unlimited_quota', '1', '0'],
  681. ['files', 'quota_preset', '1 GB, 5 GB, 10 GB', '1 GB, 5 GB, 10 GB'],
  682. // expect seeing 1 GB used as fallback value
  683. ['files', 'default_quota', '1 GB', '1 GB'],
  684. ];
  685. $config->method('getUserValue')
  686. ->will($this->returnValueMap($userValueMap));
  687. $config->method('getAppValue')
  688. ->will($this->returnValueMap($appValueMap));
  689. $quota = $user->getQuota();
  690. $this->assertEquals('1 GB', $quota);
  691. }
  692. public function testSetQuotaAddressNoChange() {
  693. /**
  694. * @var UserInterface | MockObject $backend
  695. */
  696. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  697. /** @var PublicEmitter|MockObject $emitter */
  698. $emitter = $this->createMock(PublicEmitter::class);
  699. $emitter->expects($this->never())
  700. ->method('emit');
  701. $config = $this->createMock(IConfig::class);
  702. $config->expects($this->any())
  703. ->method('getUserValue')
  704. ->willReturn('23 TB');
  705. $config->expects($this->never())
  706. ->method('setUserValue');
  707. $user = new User('foo', $backend, $this->dispatcher, $emitter, $config);
  708. $user->setQuota('23 TB');
  709. }
  710. public function testGetLastLogin() {
  711. /**
  712. * @var Backend | MockObject $backend
  713. */
  714. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  715. $config = $this->createMock(IConfig::class);
  716. $config->method('getUserValue')
  717. ->willReturnCallback(function ($uid, $app, $key, $default) {
  718. if ($uid === 'foo' && $app === 'login' && $key === 'lastLogin') {
  719. return 42;
  720. } else {
  721. return $default;
  722. }
  723. });
  724. $user = new User('foo', $backend, $this->dispatcher, null, $config);
  725. $this->assertSame(42, $user->getLastLogin());
  726. }
  727. public function testSetEnabled() {
  728. /**
  729. * @var Backend | MockObject $backend
  730. */
  731. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  732. $config = $this->createMock(IConfig::class);
  733. $config->expects($this->once())
  734. ->method('setUserValue')
  735. ->with(
  736. $this->equalTo('foo'),
  737. $this->equalTo('core'),
  738. $this->equalTo('enabled'),
  739. 'true'
  740. );
  741. $user = new User('foo', $backend, $this->dispatcher, null, $config);
  742. $user->setEnabled(true);
  743. }
  744. public function testSetDisabled() {
  745. /**
  746. * @var Backend | MockObject $backend
  747. */
  748. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  749. $config = $this->createMock(IConfig::class);
  750. $config->expects($this->once())
  751. ->method('setUserValue')
  752. ->with(
  753. $this->equalTo('foo'),
  754. $this->equalTo('core'),
  755. $this->equalTo('enabled'),
  756. 'false'
  757. );
  758. $user = $this->getMockBuilder(User::class)
  759. ->setConstructorArgs([
  760. 'foo',
  761. $backend,
  762. $this->dispatcher,
  763. null,
  764. $config,
  765. ])
  766. ->setMethods(['isEnabled', 'triggerChange'])
  767. ->getMock();
  768. $user->expects($this->once())
  769. ->method('isEnabled')
  770. ->willReturn(true);
  771. $user->expects($this->once())
  772. ->method('triggerChange')
  773. ->with(
  774. 'enabled',
  775. false
  776. );
  777. $user->setEnabled(false);
  778. }
  779. public function testSetDisabledAlreadyDisabled() {
  780. /**
  781. * @var Backend | MockObject $backend
  782. */
  783. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  784. $config = $this->createMock(IConfig::class);
  785. $config->expects($this->never())
  786. ->method('setUserValue');
  787. $user = $this->getMockBuilder(User::class)
  788. ->setConstructorArgs([
  789. 'foo',
  790. $backend,
  791. $this->dispatcher,
  792. null,
  793. $config,
  794. ])
  795. ->setMethods(['isEnabled', 'triggerChange'])
  796. ->getMock();
  797. $user->expects($this->once())
  798. ->method('isEnabled')
  799. ->willReturn(false);
  800. $user->expects($this->never())
  801. ->method('triggerChange');
  802. $user->setEnabled(false);
  803. }
  804. public function testGetEMailAddress() {
  805. /**
  806. * @var Backend | MockObject $backend
  807. */
  808. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  809. $config = $this->createMock(IConfig::class);
  810. $config->method('getUserValue')
  811. ->willReturnCallback(function ($uid, $app, $key, $default) {
  812. if ($uid === 'foo' && $app === 'settings' && $key === 'email') {
  813. return 'foo@bar.com';
  814. } else {
  815. return $default;
  816. }
  817. });
  818. $user = new User('foo', $backend, $this->dispatcher, null, $config);
  819. $this->assertSame('foo@bar.com', $user->getEMailAddress());
  820. }
  821. }