user.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. class User extends \PHPUnit_Framework_TestCase {
  11. public function testDisplayName() {
  12. /**
  13. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  14. */
  15. $backend = $this->getMock('\OC_User_Backend');
  16. $backend->expects($this->once())
  17. ->method('getDisplayName')
  18. ->with($this->equalTo('foo'))
  19. ->will($this->returnValue('Foo'));
  20. $backend->expects($this->any())
  21. ->method('implementsActions')
  22. ->with($this->equalTo(\OC_USER_BACKEND_GET_DISPLAYNAME))
  23. ->will($this->returnValue(true));
  24. $user = new \OC\User\User('foo', $backend);
  25. $this->assertEquals('Foo', $user->getDisplayName());
  26. }
  27. public function testDisplayNameNotSupported() {
  28. /**
  29. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  30. */
  31. $backend = $this->getMock('\OC_User_Backend');
  32. $backend->expects($this->never())
  33. ->method('getDisplayName');
  34. $backend->expects($this->any())
  35. ->method('implementsActions')
  36. ->with($this->equalTo(\OC_USER_BACKEND_GET_DISPLAYNAME))
  37. ->will($this->returnValue(false));
  38. $user = new \OC\User\User('foo', $backend);
  39. $this->assertEquals('foo', $user->getDisplayName());
  40. }
  41. public function testSetPassword() {
  42. /**
  43. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  44. */
  45. $backend = $this->getMock('\OC_User_Dummy');
  46. $backend->expects($this->once())
  47. ->method('setPassword')
  48. ->with($this->equalTo('foo'), $this->equalTo('bar'));
  49. $backend->expects($this->any())
  50. ->method('implementsActions')
  51. ->will($this->returnCallback(function ($actions) {
  52. if ($actions === \OC_USER_BACKEND_SET_PASSWORD) {
  53. return true;
  54. } else {
  55. return false;
  56. }
  57. }));
  58. $user = new \OC\User\User('foo', $backend);
  59. $this->assertTrue($user->setPassword('bar',''));
  60. }
  61. public function testSetPasswordNotSupported() {
  62. /**
  63. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  64. */
  65. $backend = $this->getMock('\OC_User_Dummy');
  66. $backend->expects($this->never())
  67. ->method('setPassword');
  68. $backend->expects($this->any())
  69. ->method('implementsActions')
  70. ->will($this->returnValue(false));
  71. $user = new \OC\User\User('foo', $backend);
  72. $this->assertFalse($user->setPassword('bar',''));
  73. }
  74. public function testDelete() {
  75. /**
  76. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  77. */
  78. $backend = $this->getMock('\OC_User_Dummy');
  79. $backend->expects($this->once())
  80. ->method('deleteUser')
  81. ->with($this->equalTo('foo'));
  82. $user = new \OC\User\User('foo', $backend);
  83. $this->assertTrue($user->delete());
  84. }
  85. public function testGetHome() {
  86. /**
  87. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  88. */
  89. $backend = $this->getMock('\OC_User_Dummy');
  90. $backend->expects($this->once())
  91. ->method('getHome')
  92. ->with($this->equalTo('foo'))
  93. ->will($this->returnValue('/home/foo'));
  94. $backend->expects($this->any())
  95. ->method('implementsActions')
  96. ->will($this->returnCallback(function ($actions) {
  97. if ($actions === \OC_USER_BACKEND_GET_HOME) {
  98. return true;
  99. } else {
  100. return false;
  101. }
  102. }));
  103. $user = new \OC\User\User('foo', $backend);
  104. $this->assertEquals('/home/foo', $user->getHome());
  105. }
  106. public function testGetHomeNotSupported() {
  107. /**
  108. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  109. */
  110. $backend = $this->getMock('\OC_User_Dummy');
  111. $backend->expects($this->never())
  112. ->method('getHome');
  113. $backend->expects($this->any())
  114. ->method('implementsActions')
  115. ->will($this->returnValue(false));
  116. $user = new \OC\User\User('foo', $backend);
  117. $this->assertEquals(\OC_Config::getValue("datadirectory", \OC::$SERVERROOT . "/data") . '/foo', $user->getHome());
  118. }
  119. public function testCanChangePassword() {
  120. /**
  121. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  122. */
  123. $backend = $this->getMock('\OC_User_Dummy');
  124. $backend->expects($this->any())
  125. ->method('implementsActions')
  126. ->will($this->returnCallback(function ($actions) {
  127. if ($actions === \OC_USER_BACKEND_SET_PASSWORD) {
  128. return true;
  129. } else {
  130. return false;
  131. }
  132. }));
  133. $user = new \OC\User\User('foo', $backend);
  134. $this->assertTrue($user->canChangePassword());
  135. }
  136. public function testCanChangePasswordNotSupported() {
  137. /**
  138. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  139. */
  140. $backend = $this->getMock('\OC_User_Dummy');
  141. $backend->expects($this->any())
  142. ->method('implementsActions')
  143. ->will($this->returnValue(false));
  144. $user = new \OC\User\User('foo', $backend);
  145. $this->assertFalse($user->canChangePassword());
  146. }
  147. public function testCanChangeDisplayName() {
  148. /**
  149. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  150. */
  151. $backend = $this->getMock('\OC_User_Dummy');
  152. $backend->expects($this->any())
  153. ->method('implementsActions')
  154. ->will($this->returnCallback(function ($actions) {
  155. if ($actions === \OC_USER_BACKEND_SET_DISPLAYNAME) {
  156. return true;
  157. } else {
  158. return false;
  159. }
  160. }));
  161. $user = new \OC\User\User('foo', $backend);
  162. $this->assertTrue($user->canChangeDisplayName());
  163. }
  164. public function testCanChangeDisplayNameNotSupported() {
  165. /**
  166. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  167. */
  168. $backend = $this->getMock('\OC_User_Dummy');
  169. $backend->expects($this->any())
  170. ->method('implementsActions')
  171. ->will($this->returnValue(false));
  172. $user = new \OC\User\User('foo', $backend);
  173. $this->assertFalse($user->canChangeDisplayName());
  174. }
  175. public function testSetDisplayNameSupported() {
  176. /**
  177. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  178. */
  179. $backend = $this->getMock('\OC_User_Database');
  180. $backend->expects($this->any())
  181. ->method('implementsActions')
  182. ->will($this->returnCallback(function ($actions) {
  183. if ($actions === \OC_USER_BACKEND_SET_DISPLAYNAME) {
  184. return true;
  185. } else {
  186. return false;
  187. }
  188. }));
  189. $backend->expects($this->once())
  190. ->method('setDisplayName')
  191. ->with('foo','Foo');
  192. $user = new \OC\User\User('foo', $backend);
  193. $this->assertTrue($user->setDisplayName('Foo'));
  194. $this->assertEquals('Foo',$user->getDisplayName());
  195. }
  196. public function testSetDisplayNameNotSupported() {
  197. /**
  198. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  199. */
  200. $backend = $this->getMock('\OC_User_Database');
  201. $backend->expects($this->any())
  202. ->method('implementsActions')
  203. ->will($this->returnCallback(function ($actions) {
  204. return false;
  205. }));
  206. $backend->expects($this->never())
  207. ->method('setDisplayName');
  208. $user = new \OC\User\User('foo', $backend);
  209. $this->assertFalse($user->setDisplayName('Foo'));
  210. $this->assertEquals('foo',$user->getDisplayName());
  211. }
  212. public function testSetPasswordHooks() {
  213. $hooksCalled = 0;
  214. $test = $this;
  215. /**
  216. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  217. */
  218. $backend = $this->getMock('\OC_User_Dummy');
  219. $backend->expects($this->once())
  220. ->method('setPassword');
  221. /**
  222. * @param \OC\User\User $user
  223. * @param string $password
  224. */
  225. $hook = function ($user, $password) use ($test, &$hooksCalled) {
  226. $hooksCalled++;
  227. $test->assertEquals('foo', $user->getUID());
  228. $test->assertEquals('bar', $password);
  229. };
  230. $emitter = new PublicEmitter();
  231. $emitter->listen('\OC\User', 'preSetPassword', $hook);
  232. $emitter->listen('\OC\User', 'postSetPassword', $hook);
  233. $backend->expects($this->any())
  234. ->method('implementsActions')
  235. ->will($this->returnCallback(function ($actions) {
  236. if ($actions === \OC_USER_BACKEND_SET_PASSWORD) {
  237. return true;
  238. } else {
  239. return false;
  240. }
  241. }));
  242. $user = new \OC\User\User('foo', $backend, $emitter);
  243. $user->setPassword('bar','');
  244. $this->assertEquals(2, $hooksCalled);
  245. }
  246. public function testDeleteHooks() {
  247. $hooksCalled = 0;
  248. $test = $this;
  249. /**
  250. * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
  251. */
  252. $backend = $this->getMock('\OC_User_Dummy');
  253. $backend->expects($this->once())
  254. ->method('deleteUser');
  255. /**
  256. * @param \OC\User\User $user
  257. */
  258. $hook = function ($user) use ($test, &$hooksCalled) {
  259. $hooksCalled++;
  260. $test->assertEquals('foo', $user->getUID());
  261. };
  262. $emitter = new PublicEmitter();
  263. $emitter->listen('\OC\User', 'preDelete', $hook);
  264. $emitter->listen('\OC\User', 'postDelete', $hook);
  265. $user = new \OC\User\User('foo', $backend, $emitter);
  266. $this->assertTrue($user->delete());
  267. $this->assertEquals(2, $hooksCalled);
  268. }
  269. }