user.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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\Hooks\PublicEmitter;
  11. class User extends \PHPUnit_Framework_TestCase {
  12. public function testDisplayName() {
  13. /**
  14. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  15. */
  16. $backend = $this->getMock('\OC_User_Backend');
  17. $backend->expects($this->once())
  18. ->method('getDisplayName')
  19. ->with($this->equalTo('foo'))
  20. ->will($this->returnValue('Foo'));
  21. $backend->expects($this->any())
  22. ->method('implementsActions')
  23. ->with($this->equalTo(\OC_USER_BACKEND_GET_DISPLAYNAME))
  24. ->will($this->returnValue(true));
  25. $user = new \OC\User\User('foo', $backend);
  26. $this->assertEquals('Foo', $user->getDisplayName());
  27. }
  28. /**
  29. * if the display name contain whitespaces only, we expect the uid as result
  30. */
  31. public function testDisplayNameEmpty() {
  32. /**
  33. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  34. */
  35. $backend = $this->getMock('\OC_User_Backend');
  36. $backend->expects($this->once())
  37. ->method('getDisplayName')
  38. ->with($this->equalTo('foo'))
  39. ->will($this->returnValue(' '));
  40. $backend->expects($this->any())
  41. ->method('implementsActions')
  42. ->with($this->equalTo(\OC_USER_BACKEND_GET_DISPLAYNAME))
  43. ->will($this->returnValue(true));
  44. $user = new \OC\User\User('foo', $backend);
  45. $this->assertEquals('foo', $user->getDisplayName());
  46. }
  47. public function testDisplayNameNotSupported() {
  48. /**
  49. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  50. */
  51. $backend = $this->getMock('\OC_User_Backend');
  52. $backend->expects($this->never())
  53. ->method('getDisplayName');
  54. $backend->expects($this->any())
  55. ->method('implementsActions')
  56. ->with($this->equalTo(\OC_USER_BACKEND_GET_DISPLAYNAME))
  57. ->will($this->returnValue(false));
  58. $user = new \OC\User\User('foo', $backend);
  59. $this->assertEquals('foo', $user->getDisplayName());
  60. }
  61. public function testSetPassword() {
  62. /**
  63. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  64. */
  65. $backend = $this->getMock('\OC_User_Dummy');
  66. $backend->expects($this->once())
  67. ->method('setPassword')
  68. ->with($this->equalTo('foo'), $this->equalTo('bar'));
  69. $backend->expects($this->any())
  70. ->method('implementsActions')
  71. ->will($this->returnCallback(function ($actions) {
  72. if ($actions === \OC_USER_BACKEND_SET_PASSWORD) {
  73. return true;
  74. } else {
  75. return false;
  76. }
  77. }));
  78. $user = new \OC\User\User('foo', $backend);
  79. $this->assertTrue($user->setPassword('bar',''));
  80. }
  81. public function testSetPasswordNotSupported() {
  82. /**
  83. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  84. */
  85. $backend = $this->getMock('\OC_User_Dummy');
  86. $backend->expects($this->never())
  87. ->method('setPassword');
  88. $backend->expects($this->any())
  89. ->method('implementsActions')
  90. ->will($this->returnValue(false));
  91. $user = new \OC\User\User('foo', $backend);
  92. $this->assertFalse($user->setPassword('bar',''));
  93. }
  94. public function testChangeAvatarSupportedYes() {
  95. /**
  96. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  97. */
  98. require_once 'avataruserdummy.php';
  99. $backend = $this->getMock('Avatar_User_Dummy');
  100. $backend->expects($this->once())
  101. ->method('canChangeAvatar')
  102. ->with($this->equalTo('foo'))
  103. ->will($this->returnValue(true));
  104. $backend->expects($this->any())
  105. ->method('implementsActions')
  106. ->will($this->returnCallback(function ($actions) {
  107. if ($actions === \OC_USER_BACKEND_PROVIDE_AVATAR) {
  108. return true;
  109. } else {
  110. return false;
  111. }
  112. }));
  113. $user = new \OC\User\User('foo', $backend);
  114. $this->assertTrue($user->canChangeAvatar());
  115. }
  116. public function testChangeAvatarSupportedNo() {
  117. /**
  118. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  119. */
  120. require_once 'avataruserdummy.php';
  121. $backend = $this->getMock('Avatar_User_Dummy');
  122. $backend->expects($this->once())
  123. ->method('canChangeAvatar')
  124. ->with($this->equalTo('foo'))
  125. ->will($this->returnValue(false));
  126. $backend->expects($this->any())
  127. ->method('implementsActions')
  128. ->will($this->returnCallback(function ($actions) {
  129. if ($actions === \OC_USER_BACKEND_PROVIDE_AVATAR) {
  130. return true;
  131. } else {
  132. return false;
  133. }
  134. }));
  135. $user = new \OC\User\User('foo', $backend);
  136. $this->assertFalse($user->canChangeAvatar());
  137. }
  138. public function testChangeAvatarNotSupported() {
  139. /**
  140. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  141. */
  142. require_once 'avataruserdummy.php';
  143. $backend = $this->getMock('Avatar_User_Dummy');
  144. $backend->expects($this->never())
  145. ->method('canChangeAvatar');
  146. $backend->expects($this->any())
  147. ->method('implementsActions')
  148. ->will($this->returnCallback(function ($actions) {
  149. return false;
  150. }));
  151. $user = new \OC\User\User('foo', $backend);
  152. $this->assertTrue($user->canChangeAvatar());
  153. }
  154. public function testDelete() {
  155. /**
  156. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  157. */
  158. $backend = $this->getMock('\OC_User_Dummy');
  159. $backend->expects($this->once())
  160. ->method('deleteUser')
  161. ->with($this->equalTo('foo'));
  162. $user = new \OC\User\User('foo', $backend);
  163. $this->assertTrue($user->delete());
  164. }
  165. public function testGetHome() {
  166. /**
  167. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  168. */
  169. $backend = $this->getMock('\OC_User_Dummy');
  170. $backend->expects($this->once())
  171. ->method('getHome')
  172. ->with($this->equalTo('foo'))
  173. ->will($this->returnValue('/home/foo'));
  174. $backend->expects($this->any())
  175. ->method('implementsActions')
  176. ->will($this->returnCallback(function ($actions) {
  177. if ($actions === \OC_USER_BACKEND_GET_HOME) {
  178. return true;
  179. } else {
  180. return false;
  181. }
  182. }));
  183. $user = new \OC\User\User('foo', $backend);
  184. $this->assertEquals('/home/foo', $user->getHome());
  185. }
  186. public function testGetHomeNotSupported() {
  187. /**
  188. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  189. */
  190. $backend = $this->getMock('\OC_User_Dummy');
  191. $backend->expects($this->never())
  192. ->method('getHome');
  193. $backend->expects($this->any())
  194. ->method('implementsActions')
  195. ->will($this->returnValue(false));
  196. $allConfig = new AllConfig();
  197. $user = new \OC\User\User('foo', $backend, null, $allConfig);
  198. $this->assertEquals(\OC_Config::getValue("datadirectory", \OC::$SERVERROOT . "/data") . '/foo', $user->getHome());
  199. }
  200. public function testCanChangePassword() {
  201. /**
  202. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  203. */
  204. $backend = $this->getMock('\OC_User_Dummy');
  205. $backend->expects($this->any())
  206. ->method('implementsActions')
  207. ->will($this->returnCallback(function ($actions) {
  208. if ($actions === \OC_USER_BACKEND_SET_PASSWORD) {
  209. return true;
  210. } else {
  211. return false;
  212. }
  213. }));
  214. $user = new \OC\User\User('foo', $backend);
  215. $this->assertTrue($user->canChangePassword());
  216. }
  217. public function testCanChangePasswordNotSupported() {
  218. /**
  219. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  220. */
  221. $backend = $this->getMock('\OC_User_Dummy');
  222. $backend->expects($this->any())
  223. ->method('implementsActions')
  224. ->will($this->returnValue(false));
  225. $user = new \OC\User\User('foo', $backend);
  226. $this->assertFalse($user->canChangePassword());
  227. }
  228. public function testCanChangeDisplayName() {
  229. /**
  230. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  231. */
  232. $backend = $this->getMock('\OC_User_Dummy');
  233. $backend->expects($this->any())
  234. ->method('implementsActions')
  235. ->will($this->returnCallback(function ($actions) {
  236. if ($actions === \OC_USER_BACKEND_SET_DISPLAYNAME) {
  237. return true;
  238. } else {
  239. return false;
  240. }
  241. }));
  242. $user = new \OC\User\User('foo', $backend);
  243. $this->assertTrue($user->canChangeDisplayName());
  244. }
  245. public function testCanChangeDisplayNameNotSupported() {
  246. /**
  247. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  248. */
  249. $backend = $this->getMock('\OC_User_Dummy');
  250. $backend->expects($this->any())
  251. ->method('implementsActions')
  252. ->will($this->returnValue(false));
  253. $user = new \OC\User\User('foo', $backend);
  254. $this->assertFalse($user->canChangeDisplayName());
  255. }
  256. public function testSetDisplayNameSupported() {
  257. /**
  258. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  259. */
  260. $backend = $this->getMock('\OC_User_Database');
  261. $backend->expects($this->any())
  262. ->method('implementsActions')
  263. ->will($this->returnCallback(function ($actions) {
  264. if ($actions === \OC_USER_BACKEND_SET_DISPLAYNAME) {
  265. return true;
  266. } else {
  267. return false;
  268. }
  269. }));
  270. $backend->expects($this->once())
  271. ->method('setDisplayName')
  272. ->with('foo','Foo');
  273. $user = new \OC\User\User('foo', $backend);
  274. $this->assertTrue($user->setDisplayName('Foo'));
  275. $this->assertEquals('Foo',$user->getDisplayName());
  276. }
  277. /**
  278. * don't allow display names containing whitespaces only
  279. */
  280. public function testSetDisplayNameEmpty() {
  281. /**
  282. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  283. */
  284. $backend = $this->getMock('\OC_User_Database');
  285. $backend->expects($this->any())
  286. ->method('implementsActions')
  287. ->will($this->returnCallback(function ($actions) {
  288. if ($actions === \OC_USER_BACKEND_SET_DISPLAYNAME) {
  289. return true;
  290. } else {
  291. return false;
  292. }
  293. }));
  294. $user = new \OC\User\User('foo', $backend);
  295. $this->assertFalse($user->setDisplayName(' '));
  296. $this->assertEquals('foo',$user->getDisplayName());
  297. }
  298. public function testSetDisplayNameNotSupported() {
  299. /**
  300. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  301. */
  302. $backend = $this->getMock('\OC_User_Database');
  303. $backend->expects($this->any())
  304. ->method('implementsActions')
  305. ->will($this->returnCallback(function ($actions) {
  306. return false;
  307. }));
  308. $backend->expects($this->never())
  309. ->method('setDisplayName');
  310. $user = new \OC\User\User('foo', $backend);
  311. $this->assertFalse($user->setDisplayName('Foo'));
  312. $this->assertEquals('foo',$user->getDisplayName());
  313. }
  314. public function testSetPasswordHooks() {
  315. $hooksCalled = 0;
  316. $test = $this;
  317. /**
  318. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  319. */
  320. $backend = $this->getMock('\OC_User_Dummy');
  321. $backend->expects($this->once())
  322. ->method('setPassword');
  323. /**
  324. * @param \OC\User\User $user
  325. * @param string $password
  326. */
  327. $hook = function ($user, $password) use ($test, &$hooksCalled) {
  328. $hooksCalled++;
  329. $test->assertEquals('foo', $user->getUID());
  330. $test->assertEquals('bar', $password);
  331. };
  332. $emitter = new PublicEmitter();
  333. $emitter->listen('\OC\User', 'preSetPassword', $hook);
  334. $emitter->listen('\OC\User', 'postSetPassword', $hook);
  335. $backend->expects($this->any())
  336. ->method('implementsActions')
  337. ->will($this->returnCallback(function ($actions) {
  338. if ($actions === \OC_USER_BACKEND_SET_PASSWORD) {
  339. return true;
  340. } else {
  341. return false;
  342. }
  343. }));
  344. $user = new \OC\User\User('foo', $backend, $emitter);
  345. $user->setPassword('bar','');
  346. $this->assertEquals(2, $hooksCalled);
  347. }
  348. public function testDeleteHooks() {
  349. $hooksCalled = 0;
  350. $test = $this;
  351. /**
  352. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  353. */
  354. $backend = $this->getMock('\OC_User_Dummy');
  355. $backend->expects($this->once())
  356. ->method('deleteUser');
  357. /**
  358. * @param \OC\User\User $user
  359. */
  360. $hook = function ($user) use ($test, &$hooksCalled) {
  361. $hooksCalled++;
  362. $test->assertEquals('foo', $user->getUID());
  363. };
  364. $emitter = new PublicEmitter();
  365. $emitter->listen('\OC\User', 'preDelete', $hook);
  366. $emitter->listen('\OC\User', 'postDelete', $hook);
  367. $user = new \OC\User\User('foo', $backend, $emitter);
  368. $this->assertTrue($user->delete());
  369. $this->assertEquals(2, $hooksCalled);
  370. }
  371. }