UserTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\User_LDAP\Tests\User;
  26. use OCA\User_LDAP\FilesystemHelper;
  27. use OCA\User_LDAP\ILDAPWrapper;
  28. use OCA\User_LDAP\LogWrapper;
  29. use OCA\User_LDAP\User\IUserTools;
  30. use OCA\User_LDAP\User\User;
  31. use OCP\IAvatar;
  32. use OCP\IAvatarManager;
  33. use OCP\IConfig;
  34. use OCP\IDBConnection;
  35. use OCP\Image;
  36. use OCP\IUser;
  37. use OCP\IUserManager;
  38. /**
  39. * Class UserTest
  40. *
  41. * @group DB
  42. *
  43. * @package OCA\User_LDAP\Tests\User
  44. */
  45. class UserTest extends \Test\TestCase {
  46. private function getTestInstances() {
  47. $access = $this->createMock(IUserTools::class);
  48. $config = $this->createMock(IConfig::class);
  49. $filesys = $this->createMock(FilesystemHelper::class);
  50. $log = $this->createMock(LogWrapper::class);
  51. $avaMgr = $this->createMock(IAvatarManager::class);
  52. $image = $this->createMock(Image::class);
  53. $dbc = $this->createMock(IDBConnection::class);
  54. $userMgr = $this->createMock(IUserManager::class);
  55. return array($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr);
  56. }
  57. private function getAdvancedMocks($cfMock, $fsMock, $logMock, $avaMgr, $dbc, $userMgr = null) {
  58. static $conMethods;
  59. static $accMethods;
  60. static $umMethods;
  61. if(is_null($conMethods) || is_null($accMethods)) {
  62. $conMethods = get_class_methods('\OCA\User_LDAP\Connection');
  63. $accMethods = get_class_methods('\OCA\User_LDAP\Access');
  64. //getConnection shall not be replaced
  65. unset($accMethods[array_search('getConnection', $accMethods)]);
  66. $umMethods = get_class_methods('\OCA\User_LDAP\User\Manager');
  67. }
  68. $lw = $this->createMock(ILDAPWrapper::class);
  69. $im = $this->createMock(Image::class);
  70. if (is_null($userMgr)) {
  71. $userMgr = $this->createMock(IUserManager::class);
  72. }
  73. $um = $this->getMockBuilder('\OCA\User_LDAP\User\Manager')
  74. ->setMethods($umMethods)
  75. ->setConstructorArgs([$cfMock, $fsMock, $logMock, $avaMgr, $im, $dbc, $userMgr])
  76. ->getMock();
  77. $helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig());
  78. $connector = $this->getMockBuilder('\OCA\User_LDAP\Connection')
  79. ->setMethods($conMethods)
  80. ->setConstructorArgs([$lw, null, null])
  81. ->getMock();
  82. $access = $this->getMockBuilder('\OCA\User_LDAP\Access')
  83. ->setMethods($accMethods)
  84. ->setConstructorArgs([$connector, $lw, $um, $helper])
  85. ->getMock();
  86. return array($access, $connector);
  87. }
  88. public function testGetDNandUsername() {
  89. list($access, $config, $filesys, $image, $log, $avaMgr, $db, $userMgr) =
  90. $this->getTestInstances();
  91. $uid = 'alice';
  92. $dn = 'uid=alice,dc=foo,dc=bar';
  93. $user = new User(
  94. $uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr, $userMgr);
  95. $this->assertSame($dn, $user->getDN());
  96. $this->assertSame($uid, $user->getUsername());
  97. }
  98. public function testUpdateEmailProvided() {
  99. list($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  100. $this->getTestInstances();
  101. list($access, $connection) =
  102. $this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc, $userMgr);
  103. $connection->expects($this->once())
  104. ->method('__get')
  105. ->with($this->equalTo('ldapEmailAttribute'))
  106. ->will($this->returnValue('email'));
  107. $access->expects($this->once())
  108. ->method('readAttribute')
  109. ->with($this->equalTo('uid=alice,dc=foo,dc=bar'),
  110. $this->equalTo('email'))
  111. ->will($this->returnValue(array('alice@foo.bar')));
  112. $uid = 'alice';
  113. $dn = 'uid=alice,dc=foo,dc=bar';
  114. $uuser = $this->getMockBuilder('\OCP\IUser')
  115. ->disableOriginalConstructor()
  116. ->getMock();
  117. $uuser->expects($this->once())
  118. ->method('setEMailAddress')
  119. ->with('alice@foo.bar');
  120. /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject $userMgr */
  121. $userMgr->expects($this->any())
  122. ->method('get')
  123. ->willReturn($uuser);
  124. $user = new User(
  125. $uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr, $userMgr);
  126. $user->updateEmail();
  127. }
  128. public function testUpdateEmailNotProvided() {
  129. list($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  130. $this->getTestInstances();
  131. list($access, $connection) =
  132. $this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
  133. $connection->expects($this->once())
  134. ->method('__get')
  135. ->with($this->equalTo('ldapEmailAttribute'))
  136. ->will($this->returnValue('email'));
  137. $access->expects($this->once())
  138. ->method('readAttribute')
  139. ->with($this->equalTo('uid=alice,dc=foo,dc=bar'),
  140. $this->equalTo('email'))
  141. ->will($this->returnValue(false));
  142. $config->expects($this->never())
  143. ->method('setUserValue');
  144. $uid = 'alice';
  145. $dn = 'uid=alice,dc=foo,dc=bar';
  146. $user = new User(
  147. $uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr, $userMgr);
  148. $user->updateEmail();
  149. }
  150. public function testUpdateEmailNotConfigured() {
  151. list($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  152. $this->getTestInstances();
  153. list($access, $connection) =
  154. $this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
  155. $connection->expects($this->once())
  156. ->method('__get')
  157. ->with($this->equalTo('ldapEmailAttribute'))
  158. ->will($this->returnValue(''));
  159. $access->expects($this->never())
  160. ->method('readAttribute');
  161. $config->expects($this->never())
  162. ->method('setUserValue');
  163. $uid = 'alice';
  164. $dn = 'uid=alice,dc=foo,dc=bar';
  165. $user = new User(
  166. $uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr, $userMgr);
  167. $user->updateEmail();
  168. }
  169. public function testUpdateQuotaAllProvided() {
  170. list($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  171. $this->getTestInstances();
  172. list($access, $connection) =
  173. $this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
  174. $connection->expects($this->at(0))
  175. ->method('__get')
  176. ->with($this->equalTo('ldapQuotaDefault'))
  177. ->will($this->returnValue('23 GB'));
  178. $connection->expects($this->at(1))
  179. ->method('__get')
  180. ->with($this->equalTo('ldapQuotaAttribute'))
  181. ->will($this->returnValue('myquota'));
  182. $connection->expects($this->exactly(2))
  183. ->method('__get');
  184. $access->expects($this->once())
  185. ->method('readAttribute')
  186. ->with($this->equalTo('uid=alice,dc=foo,dc=bar'),
  187. $this->equalTo('myquota'))
  188. ->will($this->returnValue(array('42 GB')));
  189. $user = $this->createMock(IUser::class);
  190. $user->expects($this->once())
  191. ->method('setQuota')
  192. ->with('42 GB');
  193. $userMgr->expects($this->once())
  194. ->method('get')
  195. ->with('alice')
  196. ->will($this->returnValue($user));
  197. $uid = 'alice';
  198. $dn = 'uid=alice,dc=foo,dc=bar';
  199. $user = new User(
  200. $uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr, $userMgr);
  201. $user->updateQuota();
  202. }
  203. public function testUpdateQuotaDefaultProvided() {
  204. list($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  205. $this->getTestInstances();
  206. list($access, $connection) =
  207. $this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
  208. $connection->expects($this->at(0))
  209. ->method('__get')
  210. ->with($this->equalTo('ldapQuotaDefault'))
  211. ->will($this->returnValue('25 GB'));
  212. $connection->expects($this->at(1))
  213. ->method('__get')
  214. ->with($this->equalTo('ldapQuotaAttribute'))
  215. ->will($this->returnValue('myquota'));
  216. $connection->expects($this->exactly(2))
  217. ->method('__get');
  218. $access->expects($this->once())
  219. ->method('readAttribute')
  220. ->with($this->equalTo('uid=alice,dc=foo,dc=bar'),
  221. $this->equalTo('myquota'))
  222. ->will($this->returnValue(false));
  223. $user = $this->createMock(IUser::class);
  224. $user->expects($this->once())
  225. ->method('setQuota')
  226. ->with('25 GB');
  227. $userMgr->expects($this->once())
  228. ->method('get')
  229. ->with('alice')
  230. ->will($this->returnValue($user));
  231. $uid = 'alice';
  232. $dn = 'uid=alice,dc=foo,dc=bar';
  233. $user = new User(
  234. $uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr, $userMgr);
  235. $user->updateQuota();
  236. }
  237. public function testUpdateQuotaIndividualProvided() {
  238. list($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  239. $this->getTestInstances();
  240. list($access, $connection) =
  241. $this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
  242. $connection->expects($this->at(0))
  243. ->method('__get')
  244. ->with($this->equalTo('ldapQuotaDefault'))
  245. ->will($this->returnValue(''));
  246. $connection->expects($this->at(1))
  247. ->method('__get')
  248. ->with($this->equalTo('ldapQuotaAttribute'))
  249. ->will($this->returnValue('myquota'));
  250. $connection->expects($this->exactly(2))
  251. ->method('__get');
  252. $access->expects($this->once())
  253. ->method('readAttribute')
  254. ->with($this->equalTo('uid=alice,dc=foo,dc=bar'),
  255. $this->equalTo('myquota'))
  256. ->will($this->returnValue(array('27 GB')));
  257. $user = $this->createMock(IUser::class);
  258. $user->expects($this->once())
  259. ->method('setQuota')
  260. ->with('27 GB');
  261. $userMgr->expects($this->once())
  262. ->method('get')
  263. ->with('alice')
  264. ->will($this->returnValue($user));
  265. $uid = 'alice';
  266. $dn = 'uid=alice,dc=foo,dc=bar';
  267. $user = new User(
  268. $uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr, $userMgr);
  269. $user->updateQuota();
  270. }
  271. public function testUpdateQuotaNoneProvided() {
  272. list($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  273. $this->getTestInstances();
  274. list($access, $connection) =
  275. $this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
  276. $connection->expects($this->at(0))
  277. ->method('__get')
  278. ->with($this->equalTo('ldapQuotaDefault'))
  279. ->will($this->returnValue(''));
  280. $connection->expects($this->at(1))
  281. ->method('__get')
  282. ->with($this->equalTo('ldapQuotaAttribute'))
  283. ->will($this->returnValue('myquota'));
  284. $connection->expects($this->exactly(2))
  285. ->method('__get');
  286. $access->expects($this->once())
  287. ->method('readAttribute')
  288. ->with($this->equalTo('uid=alice,dc=foo,dc=bar'),
  289. $this->equalTo('myquota'))
  290. ->will($this->returnValue(false));
  291. $config->expects($this->never())
  292. ->method('setUserValue');
  293. $uid = 'alice';
  294. $dn = 'uid=alice,dc=foo,dc=bar';
  295. $user = new User(
  296. $uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr, $userMgr);
  297. $user->updateQuota();
  298. }
  299. public function testUpdateQuotaNoneConfigured() {
  300. list($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  301. $this->getTestInstances();
  302. list($access, $connection) =
  303. $this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
  304. $connection->expects($this->at(0))
  305. ->method('__get')
  306. ->with($this->equalTo('ldapQuotaDefault'))
  307. ->will($this->returnValue(''));
  308. $connection->expects($this->at(1))
  309. ->method('__get')
  310. ->with($this->equalTo('ldapQuotaAttribute'))
  311. ->will($this->returnValue(''));
  312. $connection->expects($this->exactly(2))
  313. ->method('__get');
  314. $access->expects($this->never())
  315. ->method('readAttribute');
  316. $config->expects($this->never())
  317. ->method('setUserValue');
  318. $uid = 'alice';
  319. $dn = 'uid=alice,dc=foo,dc=bar';
  320. $user = new User(
  321. $uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr, $userMgr);
  322. $user->updateQuota();
  323. }
  324. public function testUpdateQuotaFromValue() {
  325. list($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  326. $this->getTestInstances();
  327. list($access, $connection) =
  328. $this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
  329. $readQuota = '19 GB';
  330. $connection->expects($this->at(0))
  331. ->method('__get')
  332. ->with($this->equalTo('ldapQuotaDefault'))
  333. ->will($this->returnValue(''));
  334. $connection->expects($this->once(1))
  335. ->method('__get')
  336. ->with($this->equalTo('ldapQuotaDefault'))
  337. ->will($this->returnValue(null));
  338. $access->expects($this->never())
  339. ->method('readAttribute');
  340. $user = $this->createMock(IUser::class);
  341. $user->expects($this->once())
  342. ->method('setQuota')
  343. ->with($readQuota);
  344. $userMgr->expects($this->once())
  345. ->method('get')
  346. ->with('alice')
  347. ->will($this->returnValue($user));
  348. $uid = 'alice';
  349. $dn = 'uid=alice,dc=foo,dc=bar';
  350. $user = new User(
  351. $uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr, $userMgr);
  352. $user->updateQuota($readQuota);
  353. }
  354. //the testUpdateAvatar series also implicitely tests getAvatarImage
  355. public function testUpdateAvatarJpegPhotoProvided() {
  356. list($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  357. $this->getTestInstances();
  358. list($access, $connection) =
  359. $this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
  360. $access->expects($this->once())
  361. ->method('readAttribute')
  362. ->with($this->equalTo('uid=alice,dc=foo,dc=bar'),
  363. $this->equalTo('jpegPhoto'))
  364. ->will($this->returnValue(array('this is a photo')));
  365. $image->expects($this->once())
  366. ->method('valid')
  367. ->will($this->returnValue(true));
  368. $image->expects($this->once())
  369. ->method('width')
  370. ->will($this->returnValue(128));
  371. $image->expects($this->once())
  372. ->method('height')
  373. ->will($this->returnValue(128));
  374. $image->expects($this->once())
  375. ->method('centerCrop')
  376. ->will($this->returnValue(true));
  377. $filesys->expects($this->once())
  378. ->method('isLoaded')
  379. ->will($this->returnValue(true));
  380. $avatar = $this->createMock(IAvatar::class);
  381. $avatar->expects($this->once())
  382. ->method('set')
  383. ->with($this->isInstanceOf($image));
  384. $avaMgr->expects($this->once())
  385. ->method('getAvatar')
  386. ->with($this->equalTo('alice'))
  387. ->will($this->returnValue($avatar));
  388. $uid = 'alice';
  389. $dn = 'uid=alice,dc=foo,dc=bar';
  390. $user = new User(
  391. $uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr, $userMgr);
  392. $user->updateAvatar();
  393. }
  394. public function testUpdateAvatarThumbnailPhotoProvided() {
  395. list($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  396. $this->getTestInstances();
  397. list($access, $connection) =
  398. $this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
  399. $access->expects($this->at(0))
  400. ->method('readAttribute')
  401. ->with($this->equalTo('uid=alice,dc=foo,dc=bar'),
  402. $this->equalTo('jpegPhoto'))
  403. ->will($this->returnValue(false));
  404. $access->expects($this->at(1))
  405. ->method('readAttribute')
  406. ->with($this->equalTo('uid=alice,dc=foo,dc=bar'),
  407. $this->equalTo('thumbnailPhoto'))
  408. ->will($this->returnValue(array('this is a photo')));
  409. $access->expects($this->exactly(2))
  410. ->method('readAttribute');
  411. $image->expects($this->once())
  412. ->method('valid')
  413. ->will($this->returnValue(true));
  414. $image->expects($this->once())
  415. ->method('width')
  416. ->will($this->returnValue(128));
  417. $image->expects($this->once())
  418. ->method('height')
  419. ->will($this->returnValue(128));
  420. $image->expects($this->once())
  421. ->method('centerCrop')
  422. ->will($this->returnValue(true));
  423. $filesys->expects($this->once())
  424. ->method('isLoaded')
  425. ->will($this->returnValue(true));
  426. $avatar = $this->createMock(IAvatar::class);
  427. $avatar->expects($this->once())
  428. ->method('set')
  429. ->with($this->isInstanceOf($image));
  430. $avaMgr->expects($this->once())
  431. ->method('getAvatar')
  432. ->with($this->equalTo('alice'))
  433. ->will($this->returnValue($avatar));
  434. $uid = 'alice';
  435. $dn = 'uid=alice,dc=foo,dc=bar';
  436. $user = new User(
  437. $uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr, $userMgr);
  438. $user->updateAvatar();
  439. }
  440. public function testUpdateAvatarNotProvided() {
  441. list($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  442. $this->getTestInstances();
  443. list($access, $connection) =
  444. $this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
  445. $access->expects($this->at(0))
  446. ->method('readAttribute')
  447. ->with($this->equalTo('uid=alice,dc=foo,dc=bar'),
  448. $this->equalTo('jpegPhoto'))
  449. ->will($this->returnValue(false));
  450. $access->expects($this->at(1))
  451. ->method('readAttribute')
  452. ->with($this->equalTo('uid=alice,dc=foo,dc=bar'),
  453. $this->equalTo('thumbnailPhoto'))
  454. ->will($this->returnValue(false));
  455. $access->expects($this->exactly(2))
  456. ->method('readAttribute');
  457. $image->expects($this->never())
  458. ->method('valid');
  459. $image->expects($this->never())
  460. ->method('width');
  461. $image->expects($this->never())
  462. ->method('height');
  463. $image->expects($this->never())
  464. ->method('centerCrop');
  465. $filesys->expects($this->never())
  466. ->method('isLoaded');
  467. $avaMgr->expects($this->never())
  468. ->method('getAvatar');
  469. $uid = 'alice';
  470. $dn = 'uid=alice,dc=foo,dc=bar';
  471. $user = new User(
  472. $uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr, $userMgr);
  473. $user->updateAvatar();
  474. }
  475. public function testUpdateBeforeFirstLogin() {
  476. list($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  477. $this->getTestInstances();
  478. list($access, $connection) =
  479. $this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
  480. $config->expects($this->at(0))
  481. ->method('getUserValue')
  482. ->with($this->equalTo('alice'), $this->equalTo('user_ldap'),
  483. $this->equalTo(User::USER_PREFKEY_FIRSTLOGIN),
  484. $this->equalTo(0))
  485. ->will($this->returnValue(0));
  486. $config->expects($this->at(1))
  487. ->method('getUserValue')
  488. ->with($this->equalTo('alice'), $this->equalTo('user_ldap'),
  489. $this->equalTo(User::USER_PREFKEY_LASTREFRESH),
  490. $this->equalTo(0))
  491. ->will($this->returnValue(0));
  492. $config->expects($this->exactly(2))
  493. ->method('getUserValue');
  494. $config->expects($this->never())
  495. ->method('setUserValue');
  496. $uid = 'alice';
  497. $dn = 'uid=alice,dc=foo,dc=bar';
  498. $user = new User(
  499. $uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr, $userMgr);
  500. $user->update();
  501. }
  502. public function testUpdateAfterFirstLogin() {
  503. list($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  504. $this->getTestInstances();
  505. list($access, $connection) =
  506. $this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
  507. $config->expects($this->at(0))
  508. ->method('getUserValue')
  509. ->with($this->equalTo('alice'), $this->equalTo('user_ldap'),
  510. $this->equalTo(User::USER_PREFKEY_FIRSTLOGIN),
  511. $this->equalTo(0))
  512. ->will($this->returnValue(1));
  513. $config->expects($this->at(1))
  514. ->method('getUserValue')
  515. ->with($this->equalTo('alice'), $this->equalTo('user_ldap'),
  516. $this->equalTo(User::USER_PREFKEY_LASTREFRESH),
  517. $this->equalTo(0))
  518. ->will($this->returnValue(0));
  519. $config->expects($this->exactly(2))
  520. ->method('getUserValue');
  521. $config->expects($this->once())
  522. ->method('setUserValue')
  523. ->with($this->equalTo('alice'), $this->equalTo('user_ldap'),
  524. $this->equalTo(User::USER_PREFKEY_LASTREFRESH),
  525. $this->anything())
  526. ->will($this->returnValue(true));
  527. $uid = 'alice';
  528. $dn = 'uid=alice,dc=foo,dc=bar';
  529. $user = new User(
  530. $uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr, $userMgr);
  531. $user->update();
  532. }
  533. public function testUpdateNoRefresh() {
  534. list($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  535. $this->getTestInstances();
  536. list($access, $connection) =
  537. $this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
  538. $config->expects($this->at(0))
  539. ->method('getUserValue')
  540. ->with($this->equalTo('alice'), $this->equalTo('user_ldap'),
  541. $this->equalTo(User::USER_PREFKEY_FIRSTLOGIN),
  542. $this->equalTo(0))
  543. ->will($this->returnValue(1));
  544. $config->expects($this->at(1))
  545. ->method('getUserValue')
  546. ->with($this->equalTo('alice'), $this->equalTo('user_ldap'),
  547. $this->equalTo(User::USER_PREFKEY_LASTREFRESH),
  548. $this->equalTo(0))
  549. ->will($this->returnValue(time()));
  550. $config->expects($this->exactly(2))
  551. ->method('getUserValue');
  552. $config->expects($this->never())
  553. ->method('setUserValue');
  554. $uid = 'alice';
  555. $dn = 'uid=alice,dc=foo,dc=bar';
  556. $user = new User(
  557. $uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr, $userMgr);
  558. $user->update();
  559. }
  560. public function testMarkLogin() {
  561. list($access, $config, $filesys, $image, $log, $avaMgr, $db, $userMgr) =
  562. $this->getTestInstances();
  563. $config->expects($this->once())
  564. ->method('setUserValue')
  565. ->with($this->equalTo('alice'),
  566. $this->equalTo('user_ldap'),
  567. $this->equalTo(User::USER_PREFKEY_FIRSTLOGIN),
  568. $this->equalTo(1))
  569. ->will($this->returnValue(true));
  570. $uid = 'alice';
  571. $dn = 'uid=alice,dc=foo,dc=bar';
  572. $user = new User(
  573. $uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr, $userMgr);
  574. $user->markLogin();
  575. }
  576. public function testGetAvatarImageProvided() {
  577. list($access, $config, $filesys, $image, $log, $avaMgr, $db, $userMgr) =
  578. $this->getTestInstances();
  579. $access->expects($this->once())
  580. ->method('readAttribute')
  581. ->with($this->equalTo('uid=alice,dc=foo,dc=bar'),
  582. $this->equalTo('jpegPhoto'))
  583. ->will($this->returnValue(array('this is a photo')));
  584. $uid = 'alice';
  585. $dn = 'uid=alice,dc=foo,dc=bar';
  586. $user = new User(
  587. $uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr, $userMgr);
  588. $photo = $user->getAvatarImage();
  589. $this->assertSame('this is a photo', $photo);
  590. //make sure readAttribute is not called again but the already fetched
  591. //photo is returned
  592. $photo = $user->getAvatarImage();
  593. }
  594. public function testProcessAttributes() {
  595. list(, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  596. $this->getTestInstances();
  597. list($access, $connection) =
  598. $this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
  599. $uid = 'alice';
  600. $dn = 'uid=alice';
  601. $requiredMethods = array(
  602. 'markRefreshTime',
  603. 'updateQuota',
  604. 'updateEmail',
  605. 'composeAndStoreDisplayName',
  606. 'storeLDAPUserName',
  607. 'getHomePath',
  608. 'updateAvatar'
  609. );
  610. $userMock = $this->getMockBuilder('OCA\User_LDAP\User\User')
  611. ->setConstructorArgs(array($uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr, $userMgr))
  612. ->setMethods($requiredMethods)
  613. ->getMock();
  614. $connection->setConfiguration(array(
  615. 'homeFolderNamingRule' => 'homeDirectory'
  616. ));
  617. $connection->expects($this->any())
  618. ->method('__get')
  619. //->will($this->returnArgument(0));
  620. ->will($this->returnCallback(function($name) {
  621. if($name === 'homeFolderNamingRule') {
  622. return 'attr:homeDirectory';
  623. }
  624. return $name;
  625. }));
  626. $record = array(
  627. strtolower($connection->ldapQuotaAttribute) => array('4096'),
  628. strtolower($connection->ldapEmailAttribute) => array('alice@wonderland.org'),
  629. strtolower($connection->ldapUserDisplayName) => array('Aaaaalice'),
  630. 'uid' => array($uid),
  631. 'homedirectory' => array('Alice\'s Folder'),
  632. 'memberof' => array('cn=groupOne', 'cn=groupTwo'),
  633. 'jpegphoto' => array('here be an image')
  634. );
  635. foreach($requiredMethods as $method) {
  636. $userMock->expects($this->once())
  637. ->method($method);
  638. }
  639. $userMock->processAttributes($record);
  640. \OC_Hook::emit('OC_User', 'post_login', array('uid' => $uid));
  641. }
  642. public function emptyHomeFolderAttributeValueProvider() {
  643. return array(
  644. 'empty' => array(''),
  645. 'prefixOnly' => array('attr:'),
  646. );
  647. }
  648. /**
  649. * @dataProvider emptyHomeFolderAttributeValueProvider
  650. */
  651. public function testGetHomePathNotConfigured($attributeValue) {
  652. list($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  653. $this->getTestInstances();
  654. list($access, $connection) =
  655. $this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
  656. $connection->expects($this->any())
  657. ->method('__get')
  658. ->with($this->equalTo('homeFolderNamingRule'))
  659. ->will($this->returnValue($attributeValue));
  660. $access->expects($this->never())
  661. ->method('readAttribute');
  662. $config->expects($this->never())
  663. ->method('getAppValue');
  664. $uid = 'alice';
  665. $dn = 'uid=alice,dc=foo,dc=bar';
  666. $user = new User(
  667. $uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr, $userMgr);
  668. $path = $user->getHomePath();
  669. $this->assertSame($path, false);
  670. }
  671. public function testGetHomePathConfiguredNotAvailableAllowed() {
  672. list($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  673. $this->getTestInstances();
  674. list($access, $connection) =
  675. $this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc);
  676. $connection->expects($this->any())
  677. ->method('__get')
  678. ->with($this->equalTo('homeFolderNamingRule'))
  679. ->will($this->returnValue('attr:foobar'));
  680. $access->expects($this->once())
  681. ->method('readAttribute')
  682. ->will($this->returnValue(false));
  683. // asks for "enforce_home_folder_naming_rule"
  684. $config->expects($this->once())
  685. ->method('getAppValue')
  686. ->will($this->returnValue(false));
  687. $uid = 'alice';
  688. $dn = 'uid=alice,dc=foo,dc=bar';
  689. $user = new User(
  690. $uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr, $userMgr);
  691. $path = $user->getHomePath();
  692. $this->assertSame($path, false);
  693. }
  694. /**
  695. * @expectedException \Exception
  696. */
  697. public function testGetHomePathConfiguredNotAvailableNotAllowed() {
  698. list($access, $config, $filesys, $image, $log, $avaMgr, $dbc, $userMgr) =
  699. $this->getTestInstances();
  700. list($access, $connection) =
  701. $this->getAdvancedMocks($config, $filesys, $log, $avaMgr, $dbc, $userMgr);
  702. $connection->expects($this->any())
  703. ->method('__get')
  704. ->with($this->equalTo('homeFolderNamingRule'))
  705. ->will($this->returnValue('attr:foobar'));
  706. $access->expects($this->once())
  707. ->method('readAttribute')
  708. ->will($this->returnValue(false));
  709. // asks for "enforce_home_folder_naming_rule"
  710. $config->expects($this->once())
  711. ->method('getAppValue')
  712. ->will($this->returnValue(true));
  713. $uid = 'alice';
  714. $dn = 'uid=alice,dc=foo,dc=bar';
  715. $user = new User(
  716. $uid, $dn, $access, $config, $filesys, $image, $log, $avaMgr, $userMgr);
  717. $user->getHomePath();
  718. }
  719. public function displayNameProvider() {
  720. return [
  721. ['Roland Deschain', '', 'Roland Deschain'],
  722. ['Roland Deschain', null, 'Roland Deschain'],
  723. ['Roland Deschain', 'gunslinger@darktower.com', 'Roland Deschain (gunslinger@darktower.com)'],
  724. ];
  725. }
  726. /**
  727. * @dataProvider displayNameProvider
  728. */
  729. public function testComposeAndStoreDisplayName($part1, $part2, $expected) {
  730. list($access, $config, $filesys, $image, $log, $avaMgr, , $userMgr) =
  731. $this->getTestInstances();
  732. $config->expects($this->once())
  733. ->method('setUserValue');
  734. $user = new User(
  735. 'user', 'cn=user', $access, $config, $filesys, $image, $log, $avaMgr, $userMgr);
  736. $displayName = $user->composeAndStoreDisplayName($part1, $part2);
  737. $this->assertSame($expected, $displayName);
  738. }
  739. }