UserTest.php 26 KB

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