UserTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. use OC\User\Database;
  11. /**
  12. * Class UserTest
  13. *
  14. * @group DB
  15. *
  16. * @package Test\User
  17. */
  18. class UserTest extends \Test\TestCase {
  19. public function testDisplayName() {
  20. /**
  21. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  22. */
  23. $backend = $this->createMock(\OC\User\Backend::class);
  24. $backend->expects($this->once())
  25. ->method('getDisplayName')
  26. ->with($this->equalTo('foo'))
  27. ->will($this->returnValue('Foo'));
  28. $backend->expects($this->any())
  29. ->method('implementsActions')
  30. ->with($this->equalTo(\OC\User\Backend::GET_DISPLAYNAME))
  31. ->will($this->returnValue(true));
  32. $user = new \OC\User\User('foo', $backend);
  33. $this->assertEquals('Foo', $user->getDisplayName());
  34. }
  35. /**
  36. * if the display name contain whitespaces only, we expect the uid as result
  37. */
  38. public function testDisplayNameEmpty() {
  39. /**
  40. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_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. ->will($this->returnValue(' '));
  47. $backend->expects($this->any())
  48. ->method('implementsActions')
  49. ->with($this->equalTo(\OC\User\Backend::GET_DISPLAYNAME))
  50. ->will($this->returnValue(true));
  51. $user = new \OC\User\User('foo', $backend);
  52. $this->assertEquals('foo', $user->getDisplayName());
  53. }
  54. public function testDisplayNameNotSupported() {
  55. /**
  56. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  57. */
  58. $backend = $this->createMock(\OC\User\Backend::class);
  59. $backend->expects($this->never())
  60. ->method('getDisplayName');
  61. $backend->expects($this->any())
  62. ->method('implementsActions')
  63. ->with($this->equalTo(\OC\User\Backend::GET_DISPLAYNAME))
  64. ->will($this->returnValue(false));
  65. $user = new \OC\User\User('foo', $backend);
  66. $this->assertEquals('foo', $user->getDisplayName());
  67. }
  68. public function testSetPassword() {
  69. /**
  70. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  71. */
  72. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  73. $backend->expects($this->once())
  74. ->method('setPassword')
  75. ->with($this->equalTo('foo'), $this->equalTo('bar'));
  76. $backend->expects($this->any())
  77. ->method('implementsActions')
  78. ->will($this->returnCallback(function ($actions) {
  79. if ($actions === \OC\User\Backend::SET_PASSWORD) {
  80. return true;
  81. } else {
  82. return false;
  83. }
  84. }));
  85. $user = new \OC\User\User('foo', $backend);
  86. $this->assertTrue($user->setPassword('bar',''));
  87. }
  88. public function testSetPasswordNotSupported() {
  89. /**
  90. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  91. */
  92. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  93. $backend->expects($this->never())
  94. ->method('setPassword');
  95. $backend->expects($this->any())
  96. ->method('implementsActions')
  97. ->will($this->returnValue(false));
  98. $user = new \OC\User\User('foo', $backend);
  99. $this->assertFalse($user->setPassword('bar',''));
  100. }
  101. public function testChangeAvatarSupportedYes() {
  102. /**
  103. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  104. */
  105. $backend = $this->createMock(AvatarUserDummy::class);
  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. $backend = $this->createMock(AvatarUserDummy::class);
  127. $backend->expects($this->once())
  128. ->method('canChangeAvatar')
  129. ->with($this->equalTo('foo'))
  130. ->will($this->returnValue(false));
  131. $backend->expects($this->any())
  132. ->method('implementsActions')
  133. ->will($this->returnCallback(function ($actions) {
  134. if ($actions === \OC\User\Backend::PROVIDE_AVATAR) {
  135. return true;
  136. } else {
  137. return false;
  138. }
  139. }));
  140. $user = new \OC\User\User('foo', $backend);
  141. $this->assertFalse($user->canChangeAvatar());
  142. }
  143. public function testChangeAvatarNotSupported() {
  144. /**
  145. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  146. */
  147. $backend = $this->createMock(AvatarUserDummy::class);
  148. $backend->expects($this->never())
  149. ->method('canChangeAvatar');
  150. $backend->expects($this->any())
  151. ->method('implementsActions')
  152. ->will($this->returnCallback(function ($actions) {
  153. return false;
  154. }));
  155. $user = new \OC\User\User('foo', $backend);
  156. $this->assertTrue($user->canChangeAvatar());
  157. }
  158. public function testDelete() {
  159. /**
  160. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  161. */
  162. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  163. $backend->expects($this->once())
  164. ->method('deleteUser')
  165. ->with($this->equalTo('foo'));
  166. $user = new \OC\User\User('foo', $backend);
  167. $this->assertTrue($user->delete());
  168. }
  169. public function testGetHome() {
  170. /**
  171. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  172. */
  173. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  174. $backend->expects($this->once())
  175. ->method('getHome')
  176. ->with($this->equalTo('foo'))
  177. ->will($this->returnValue('/home/foo'));
  178. $backend->expects($this->any())
  179. ->method('implementsActions')
  180. ->will($this->returnCallback(function ($actions) {
  181. if ($actions === \OC\User\Backend::GET_HOME) {
  182. return true;
  183. } else {
  184. return false;
  185. }
  186. }));
  187. $user = new \OC\User\User('foo', $backend);
  188. $this->assertEquals('/home/foo', $user->getHome());
  189. }
  190. public function testGetBackendClassName() {
  191. $user = new \OC\User\User('foo', new \Test\Util\User\Dummy());
  192. $this->assertEquals('Dummy', $user->getBackendClassName());
  193. $user = new \OC\User\User('foo', new \OC\User\Database());
  194. $this->assertEquals('Database', $user->getBackendClassName());
  195. }
  196. public function testGetHomeNotSupported() {
  197. /**
  198. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  199. */
  200. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  201. $backend->expects($this->never())
  202. ->method('getHome');
  203. $backend->expects($this->any())
  204. ->method('implementsActions')
  205. ->will($this->returnValue(false));
  206. $allConfig = $this->getMockBuilder('\OCP\IConfig')
  207. ->disableOriginalConstructor()
  208. ->getMock();
  209. $allConfig->expects($this->any())
  210. ->method('getUserValue')
  211. ->will($this->returnValue(true));
  212. $allConfig->expects($this->any())
  213. ->method('getSystemValue')
  214. ->with($this->equalTo('datadirectory'))
  215. ->will($this->returnValue('arbitrary/path'));
  216. $user = new \OC\User\User('foo', $backend, null, $allConfig);
  217. $this->assertEquals('arbitrary/path/foo', $user->getHome());
  218. }
  219. public function testCanChangePassword() {
  220. /**
  221. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  222. */
  223. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  224. $backend->expects($this->any())
  225. ->method('implementsActions')
  226. ->will($this->returnCallback(function ($actions) {
  227. if ($actions === \OC\User\Backend::SET_PASSWORD) {
  228. return true;
  229. } else {
  230. return false;
  231. }
  232. }));
  233. $user = new \OC\User\User('foo', $backend);
  234. $this->assertTrue($user->canChangePassword());
  235. }
  236. public function testCanChangePasswordNotSupported() {
  237. /**
  238. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  239. */
  240. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  241. $backend->expects($this->any())
  242. ->method('implementsActions')
  243. ->will($this->returnValue(false));
  244. $user = new \OC\User\User('foo', $backend);
  245. $this->assertFalse($user->canChangePassword());
  246. }
  247. public function testCanChangeDisplayName() {
  248. /**
  249. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  250. */
  251. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  252. $backend->expects($this->any())
  253. ->method('implementsActions')
  254. ->will($this->returnCallback(function ($actions) {
  255. if ($actions === \OC\User\Backend::SET_DISPLAYNAME) {
  256. return true;
  257. } else {
  258. return false;
  259. }
  260. }));
  261. $user = new \OC\User\User('foo', $backend);
  262. $this->assertTrue($user->canChangeDisplayName());
  263. }
  264. public function testCanChangeDisplayNameNotSupported() {
  265. /**
  266. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  267. */
  268. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  269. $backend->expects($this->any())
  270. ->method('implementsActions')
  271. ->will($this->returnValue(false));
  272. $user = new \OC\User\User('foo', $backend);
  273. $this->assertFalse($user->canChangeDisplayName());
  274. }
  275. public function testSetDisplayNameSupported() {
  276. /**
  277. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  278. */
  279. $backend = $this->createMock(Database::class);
  280. $backend->expects($this->any())
  281. ->method('implementsActions')
  282. ->will($this->returnCallback(function ($actions) {
  283. if ($actions === \OC\User\Backend::SET_DISPLAYNAME) {
  284. return true;
  285. } else {
  286. return false;
  287. }
  288. }));
  289. $backend->expects($this->once())
  290. ->method('setDisplayName')
  291. ->with('foo','Foo')
  292. ->willReturn(true);
  293. $user = new \OC\User\User('foo', $backend);
  294. $this->assertTrue($user->setDisplayName('Foo'));
  295. $this->assertEquals('Foo',$user->getDisplayName());
  296. }
  297. /**
  298. * don't allow display names containing whitespaces only
  299. */
  300. public function testSetDisplayNameEmpty() {
  301. /**
  302. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  303. */
  304. $backend = $this->createMock(Database::class);
  305. $backend->expects($this->any())
  306. ->method('implementsActions')
  307. ->will($this->returnCallback(function ($actions) {
  308. if ($actions === \OC\User\Backend::SET_DISPLAYNAME) {
  309. return true;
  310. } else {
  311. return false;
  312. }
  313. }));
  314. $user = new \OC\User\User('foo', $backend);
  315. $this->assertFalse($user->setDisplayName(' '));
  316. $this->assertEquals('foo',$user->getDisplayName());
  317. }
  318. public function testSetDisplayNameNotSupported() {
  319. /**
  320. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  321. */
  322. $backend = $this->createMock(Database::class);
  323. $backend->expects($this->any())
  324. ->method('implementsActions')
  325. ->will($this->returnCallback(function ($actions) {
  326. return false;
  327. }));
  328. $backend->expects($this->never())
  329. ->method('setDisplayName');
  330. $user = new \OC\User\User('foo', $backend);
  331. $this->assertFalse($user->setDisplayName('Foo'));
  332. $this->assertEquals('foo',$user->getDisplayName());
  333. }
  334. public function testSetPasswordHooks() {
  335. $hooksCalled = 0;
  336. $test = $this;
  337. /**
  338. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  339. */
  340. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  341. $backend->expects($this->once())
  342. ->method('setPassword');
  343. /**
  344. * @param \OC\User\User $user
  345. * @param string $password
  346. */
  347. $hook = function ($user, $password) use ($test, &$hooksCalled) {
  348. $hooksCalled++;
  349. $test->assertEquals('foo', $user->getUID());
  350. $test->assertEquals('bar', $password);
  351. };
  352. $emitter = new PublicEmitter();
  353. $emitter->listen('\OC\User', 'preSetPassword', $hook);
  354. $emitter->listen('\OC\User', 'postSetPassword', $hook);
  355. $backend->expects($this->any())
  356. ->method('implementsActions')
  357. ->will($this->returnCallback(function ($actions) {
  358. if ($actions === \OC\User\Backend::SET_PASSWORD) {
  359. return true;
  360. } else {
  361. return false;
  362. }
  363. }));
  364. $user = new \OC\User\User('foo', $backend, $emitter);
  365. $user->setPassword('bar','');
  366. $this->assertEquals(2, $hooksCalled);
  367. }
  368. public function testDeleteHooks() {
  369. $hooksCalled = 0;
  370. $test = $this;
  371. /**
  372. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  373. */
  374. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  375. $backend->expects($this->once())
  376. ->method('deleteUser');
  377. /**
  378. * @param \OC\User\User $user
  379. */
  380. $hook = function ($user) use ($test, &$hooksCalled) {
  381. $hooksCalled++;
  382. $test->assertEquals('foo', $user->getUID());
  383. };
  384. $emitter = new PublicEmitter();
  385. $emitter->listen('\OC\User', 'preDelete', $hook);
  386. $emitter->listen('\OC\User', 'postDelete', $hook);
  387. $user = new \OC\User\User('foo', $backend, $emitter);
  388. $this->assertTrue($user->delete());
  389. $this->assertEquals(2, $hooksCalled);
  390. }
  391. public function testGetCloudId() {
  392. /**
  393. * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  394. */
  395. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  396. $urlGenerator = $this->getMockBuilder('\OC\URLGenerator')
  397. ->setMethods(['getAbsoluteURL'])
  398. ->disableOriginalConstructor()->getMock();
  399. $urlGenerator
  400. ->expects($this->any())
  401. ->method('getAbsoluteURL')
  402. ->withAnyParameters()
  403. ->willReturn('http://localhost:8888/owncloud');
  404. $user = new \OC\User\User('foo', $backend, null, null, $urlGenerator);
  405. $this->assertEquals("foo@localhost:8888/owncloud", $user->getCloudId());
  406. }
  407. }