user.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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\Hooks\PublicEmitter;
  10. /**
  11. * Class User
  12. *
  13. * @group DB
  14. *
  15. * @package Test\User
  16. */
  17. class User extends \Test\TestCase {
  18. public function testDisplayName() {
  19. /**
  20. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  21. */
  22. $backend = $this->getMock('\OC_User_Backend');
  23. $backend->expects($this->once())
  24. ->method('getDisplayName')
  25. ->with($this->equalTo('foo'))
  26. ->will($this->returnValue('Foo'));
  27. $backend->expects($this->any())
  28. ->method('implementsActions')
  29. ->with($this->equalTo(\OC_USER_BACKEND_GET_DISPLAYNAME))
  30. ->will($this->returnValue(true));
  31. $user = new \OC\User\User('foo', $backend);
  32. $this->assertEquals('Foo', $user->getDisplayName());
  33. }
  34. /**
  35. * if the display name contain whitespaces only, we expect the uid as result
  36. */
  37. public function testDisplayNameEmpty() {
  38. /**
  39. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  40. */
  41. $backend = $this->getMock('\OC_User_Backend');
  42. $backend->expects($this->once())
  43. ->method('getDisplayName')
  44. ->with($this->equalTo('foo'))
  45. ->will($this->returnValue(' '));
  46. $backend->expects($this->any())
  47. ->method('implementsActions')
  48. ->with($this->equalTo(\OC_USER_BACKEND_GET_DISPLAYNAME))
  49. ->will($this->returnValue(true));
  50. $user = new \OC\User\User('foo', $backend);
  51. $this->assertEquals('foo', $user->getDisplayName());
  52. }
  53. public function testDisplayNameNotSupported() {
  54. /**
  55. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  56. */
  57. $backend = $this->getMock('\OC_User_Backend');
  58. $backend->expects($this->never())
  59. ->method('getDisplayName');
  60. $backend->expects($this->any())
  61. ->method('implementsActions')
  62. ->with($this->equalTo(\OC_USER_BACKEND_GET_DISPLAYNAME))
  63. ->will($this->returnValue(false));
  64. $user = new \OC\User\User('foo', $backend);
  65. $this->assertEquals('foo', $user->getDisplayName());
  66. }
  67. public function testSetPassword() {
  68. /**
  69. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  70. */
  71. $backend = $this->getMock('\Test\Util\User\Dummy');
  72. $backend->expects($this->once())
  73. ->method('setPassword')
  74. ->with($this->equalTo('foo'), $this->equalTo('bar'));
  75. $backend->expects($this->any())
  76. ->method('implementsActions')
  77. ->will($this->returnCallback(function ($actions) {
  78. if ($actions === \OC_USER_BACKEND_SET_PASSWORD) {
  79. return true;
  80. } else {
  81. return false;
  82. }
  83. }));
  84. $user = new \OC\User\User('foo', $backend);
  85. $this->assertTrue($user->setPassword('bar',''));
  86. }
  87. public function testSetPasswordNotSupported() {
  88. /**
  89. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  90. */
  91. $backend = $this->getMock('\Test\Util\User\Dummy');
  92. $backend->expects($this->never())
  93. ->method('setPassword');
  94. $backend->expects($this->any())
  95. ->method('implementsActions')
  96. ->will($this->returnValue(false));
  97. $user = new \OC\User\User('foo', $backend);
  98. $this->assertFalse($user->setPassword('bar',''));
  99. }
  100. public function testChangeAvatarSupportedYes() {
  101. /**
  102. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  103. */
  104. require_once 'avataruserdummy.php';
  105. $backend = $this->getMock('Avatar_User_Dummy');
  106. $backend->expects($this->once())
  107. ->method('canChangeAvatar')
  108. ->with($this->equalTo('foo'))
  109. ->will($this->returnValue(true));
  110. $backend->expects($this->any())
  111. ->method('implementsActions')
  112. ->will($this->returnCallback(function ($actions) {
  113. if ($actions === \OC_USER_BACKEND_PROVIDE_AVATAR) {
  114. return true;
  115. } else {
  116. return false;
  117. }
  118. }));
  119. $user = new \OC\User\User('foo', $backend);
  120. $this->assertTrue($user->canChangeAvatar());
  121. }
  122. public function testChangeAvatarSupportedNo() {
  123. /**
  124. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  125. */
  126. require_once 'avataruserdummy.php';
  127. $backend = $this->getMock('Avatar_User_Dummy');
  128. $backend->expects($this->once())
  129. ->method('canChangeAvatar')
  130. ->with($this->equalTo('foo'))
  131. ->will($this->returnValue(false));
  132. $backend->expects($this->any())
  133. ->method('implementsActions')
  134. ->will($this->returnCallback(function ($actions) {
  135. if ($actions === \OC_USER_BACKEND_PROVIDE_AVATAR) {
  136. return true;
  137. } else {
  138. return false;
  139. }
  140. }));
  141. $user = new \OC\User\User('foo', $backend);
  142. $this->assertFalse($user->canChangeAvatar());
  143. }
  144. public function testChangeAvatarNotSupported() {
  145. /**
  146. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  147. */
  148. require_once 'avataruserdummy.php';
  149. $backend = $this->getMock('Avatar_User_Dummy');
  150. $backend->expects($this->never())
  151. ->method('canChangeAvatar');
  152. $backend->expects($this->any())
  153. ->method('implementsActions')
  154. ->will($this->returnCallback(function ($actions) {
  155. return false;
  156. }));
  157. $user = new \OC\User\User('foo', $backend);
  158. $this->assertTrue($user->canChangeAvatar());
  159. }
  160. public function testDelete() {
  161. /**
  162. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  163. */
  164. $backend = $this->getMock('\Test\Util\User\Dummy');
  165. $backend->expects($this->once())
  166. ->method('deleteUser')
  167. ->with($this->equalTo('foo'));
  168. $user = new \OC\User\User('foo', $backend);
  169. $this->assertTrue($user->delete());
  170. }
  171. public function testGetHome() {
  172. /**
  173. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  174. */
  175. $backend = $this->getMock('\Test\Util\User\Dummy');
  176. $backend->expects($this->once())
  177. ->method('getHome')
  178. ->with($this->equalTo('foo'))
  179. ->will($this->returnValue('/home/foo'));
  180. $backend->expects($this->any())
  181. ->method('implementsActions')
  182. ->will($this->returnCallback(function ($actions) {
  183. if ($actions === \OC_USER_BACKEND_GET_HOME) {
  184. return true;
  185. } else {
  186. return false;
  187. }
  188. }));
  189. $user = new \OC\User\User('foo', $backend);
  190. $this->assertEquals('/home/foo', $user->getHome());
  191. }
  192. public function testGetBackendClassName() {
  193. $user = new \OC\User\User('foo', new \Test\Util\User\Dummy());
  194. $this->assertEquals('Dummy', $user->getBackendClassName());
  195. $user = new \OC\User\User('foo', new \OC_User_Database());
  196. $this->assertEquals('Database', $user->getBackendClassName());
  197. }
  198. public function testGetHomeNotSupported() {
  199. /**
  200. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  201. */
  202. $backend = $this->getMock('\Test\Util\User\Dummy');
  203. $backend->expects($this->never())
  204. ->method('getHome');
  205. $backend->expects($this->any())
  206. ->method('implementsActions')
  207. ->will($this->returnValue(false));
  208. $allConfig = $this->getMockBuilder('\OCP\IConfig')
  209. ->disableOriginalConstructor()
  210. ->getMock();
  211. $allConfig->expects($this->any())
  212. ->method('getUserValue')
  213. ->will($this->returnValue(true));
  214. $allConfig->expects($this->any())
  215. ->method('getSystemValue')
  216. ->with($this->equalTo('datadirectory'))
  217. ->will($this->returnValue('arbitrary/path'));
  218. $user = new \OC\User\User('foo', $backend, null, $allConfig);
  219. $this->assertEquals('arbitrary/path/foo', $user->getHome());
  220. }
  221. public function testCanChangePassword() {
  222. /**
  223. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  224. */
  225. $backend = $this->getMock('\Test\Util\User\Dummy');
  226. $backend->expects($this->any())
  227. ->method('implementsActions')
  228. ->will($this->returnCallback(function ($actions) {
  229. if ($actions === \OC_USER_BACKEND_SET_PASSWORD) {
  230. return true;
  231. } else {
  232. return false;
  233. }
  234. }));
  235. $user = new \OC\User\User('foo', $backend);
  236. $this->assertTrue($user->canChangePassword());
  237. }
  238. public function testCanChangePasswordNotSupported() {
  239. /**
  240. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  241. */
  242. $backend = $this->getMock('\Test\Util\User\Dummy');
  243. $backend->expects($this->any())
  244. ->method('implementsActions')
  245. ->will($this->returnValue(false));
  246. $user = new \OC\User\User('foo', $backend);
  247. $this->assertFalse($user->canChangePassword());
  248. }
  249. public function testCanChangeDisplayName() {
  250. /**
  251. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  252. */
  253. $backend = $this->getMock('\Test\Util\User\Dummy');
  254. $backend->expects($this->any())
  255. ->method('implementsActions')
  256. ->will($this->returnCallback(function ($actions) {
  257. if ($actions === \OC_USER_BACKEND_SET_DISPLAYNAME) {
  258. return true;
  259. } else {
  260. return false;
  261. }
  262. }));
  263. $user = new \OC\User\User('foo', $backend);
  264. $this->assertTrue($user->canChangeDisplayName());
  265. }
  266. public function testCanChangeDisplayNameNotSupported() {
  267. /**
  268. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  269. */
  270. $backend = $this->getMock('\Test\Util\User\Dummy');
  271. $backend->expects($this->any())
  272. ->method('implementsActions')
  273. ->will($this->returnValue(false));
  274. $user = new \OC\User\User('foo', $backend);
  275. $this->assertFalse($user->canChangeDisplayName());
  276. }
  277. public function testSetDisplayNameSupported() {
  278. /**
  279. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  280. */
  281. $backend = $this->getMock('\OC_User_Database');
  282. $backend->expects($this->any())
  283. ->method('implementsActions')
  284. ->will($this->returnCallback(function ($actions) {
  285. if ($actions === \OC_USER_BACKEND_SET_DISPLAYNAME) {
  286. return true;
  287. } else {
  288. return false;
  289. }
  290. }));
  291. $backend->expects($this->once())
  292. ->method('setDisplayName')
  293. ->with('foo','Foo')
  294. ->willReturn(true);
  295. $user = new \OC\User\User('foo', $backend);
  296. $this->assertTrue($user->setDisplayName('Foo'));
  297. $this->assertEquals('Foo',$user->getDisplayName());
  298. }
  299. /**
  300. * don't allow display names containing whitespaces only
  301. */
  302. public function testSetDisplayNameEmpty() {
  303. /**
  304. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  305. */
  306. $backend = $this->getMock('\OC_User_Database');
  307. $backend->expects($this->any())
  308. ->method('implementsActions')
  309. ->will($this->returnCallback(function ($actions) {
  310. if ($actions === \OC_USER_BACKEND_SET_DISPLAYNAME) {
  311. return true;
  312. } else {
  313. return false;
  314. }
  315. }));
  316. $user = new \OC\User\User('foo', $backend);
  317. $this->assertFalse($user->setDisplayName(' '));
  318. $this->assertEquals('foo',$user->getDisplayName());
  319. }
  320. public function testSetDisplayNameNotSupported() {
  321. /**
  322. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  323. */
  324. $backend = $this->getMock('\OC_User_Database');
  325. $backend->expects($this->any())
  326. ->method('implementsActions')
  327. ->will($this->returnCallback(function ($actions) {
  328. return false;
  329. }));
  330. $backend->expects($this->never())
  331. ->method('setDisplayName');
  332. $user = new \OC\User\User('foo', $backend);
  333. $this->assertFalse($user->setDisplayName('Foo'));
  334. $this->assertEquals('foo',$user->getDisplayName());
  335. }
  336. public function testSetPasswordHooks() {
  337. $hooksCalled = 0;
  338. $test = $this;
  339. /**
  340. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  341. */
  342. $backend = $this->getMock('\Test\Util\User\Dummy');
  343. $backend->expects($this->once())
  344. ->method('setPassword');
  345. /**
  346. * @param \OC\User\User $user
  347. * @param string $password
  348. */
  349. $hook = function ($user, $password) use ($test, &$hooksCalled) {
  350. $hooksCalled++;
  351. $test->assertEquals('foo', $user->getUID());
  352. $test->assertEquals('bar', $password);
  353. };
  354. $emitter = new PublicEmitter();
  355. $emitter->listen('\OC\User', 'preSetPassword', $hook);
  356. $emitter->listen('\OC\User', 'postSetPassword', $hook);
  357. $backend->expects($this->any())
  358. ->method('implementsActions')
  359. ->will($this->returnCallback(function ($actions) {
  360. if ($actions === \OC_USER_BACKEND_SET_PASSWORD) {
  361. return true;
  362. } else {
  363. return false;
  364. }
  365. }));
  366. $user = new \OC\User\User('foo', $backend, $emitter);
  367. $user->setPassword('bar','');
  368. $this->assertEquals(2, $hooksCalled);
  369. }
  370. public function dataDeleteHooks() {
  371. return [
  372. [true, 2],
  373. [false, 1],
  374. ];
  375. }
  376. /**
  377. * @dataProvider dataDeleteHooks
  378. * @param bool $result
  379. * @param int $expectedHooks
  380. */
  381. public function testDeleteHooks($result, $expectedHooks) {
  382. $hooksCalled = 0;
  383. $test = $this;
  384. /**
  385. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  386. */
  387. $backend = $this->getMock('\Test\Util\User\Dummy');
  388. $backend->expects($this->once())
  389. ->method('deleteUser')
  390. ->willReturn($result);
  391. $emitter = new PublicEmitter();
  392. $user = new \OC\User\User('foo', $backend, $emitter);
  393. /**
  394. * @param \OC\User\User $user
  395. */
  396. $hook = function ($user) use ($test, &$hooksCalled) {
  397. $hooksCalled++;
  398. $test->assertEquals('foo', $user->getUID());
  399. };
  400. $emitter->listen('\OC\User', 'preDelete', $hook);
  401. $emitter->listen('\OC\User', 'postDelete', $hook);
  402. $config = $this->getMockBuilder('OCP\IConfig')->getMock();
  403. $commentsManager = $this->getMockBuilder('OCP\Comments\ICommentsManager')->getMock();
  404. $notificationManager = $this->getMockBuilder('OCP\Notification\IManager')->getMock();
  405. if ($result) {
  406. $config->expects($this->once())
  407. ->method('deleteAllUserValues')
  408. ->with('foo');
  409. $commentsManager->expects($this->once())
  410. ->method('deleteReferencesOfActor')
  411. ->with('users', 'foo');
  412. $commentsManager->expects($this->once())
  413. ->method('deleteReadMarksFromUser')
  414. ->with($user);
  415. $notification = $this->getMockBuilder('OCP\Notification\INotification')->getMock();
  416. $notification->expects($this->once())
  417. ->method('setUser')
  418. ->with('foo');
  419. $notificationManager->expects($this->once())
  420. ->method('createNotification')
  421. ->willReturn($notification);
  422. $notificationManager->expects($this->once())
  423. ->method('markProcessed')
  424. ->with($notification);
  425. } else {
  426. $config->expects($this->never())
  427. ->method('deleteAllUserValues');
  428. $commentsManager->expects($this->never())
  429. ->method('deleteReferencesOfActor');
  430. $commentsManager->expects($this->never())
  431. ->method('deleteReadMarksFromUser');
  432. $notificationManager->expects($this->never())
  433. ->method('createNotification');
  434. $notificationManager->expects($this->never())
  435. ->method('markProcessed');
  436. }
  437. $this->overwriteService('NotificationManager', $notificationManager);
  438. $this->overwriteService('CommentsManager', $commentsManager);
  439. $this->overwriteService('AllConfig', $config);
  440. $this->assertSame($result, $user->delete());
  441. $this->restoreService('AllConfig');
  442. $this->restoreService('CommentsManager');
  443. $this->restoreService('NotificationManager');
  444. $this->assertEquals($expectedHooks, $hooksCalled);
  445. }
  446. public function testGetCloudId() {
  447. /**
  448. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  449. */
  450. $backend = $this->getMock('\Test\Util\User\Dummy');
  451. $urlGenerator = $this->getMockBuilder('\OC\URLGenerator')
  452. ->setMethods(['getAbsoluteURL'])
  453. ->disableOriginalConstructor()->getMock();
  454. $urlGenerator
  455. ->expects($this->any())
  456. ->method('getAbsoluteURL')
  457. ->withAnyParameters()
  458. ->willReturn('http://localhost:8888/owncloud');
  459. $user = new \OC\User\User('foo', $backend, null, null, $urlGenerator);
  460. $this->assertEquals("foo@localhost:8888/owncloud", $user->getCloudId());
  461. }
  462. }