1
0

UserTest.php 27 KB

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