User_LDAPTest.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin McCorkell <robin@mccorkell.me.uk>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Roger Szabo <roger.szabo@web.de>
  14. * @author Thomas Müller <thomas.mueller@tmit.eu>
  15. * @author Vinicius Cubas Brand <vinicius@eita.org.br>
  16. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. namespace OCA\User_LDAP\Tests;
  33. use OC\HintException;
  34. use OC\User\Backend;
  35. use OC\User\Session;
  36. use OCA\User_LDAP\Access;
  37. use OCA\User_LDAP\Connection;
  38. use OCA\User_LDAP\Mapping\AbstractMapping;
  39. use OCA\User_LDAP\Mapping\UserMapping;
  40. use OCA\User_LDAP\User\Manager;
  41. use OCA\User_LDAP\User\OfflineUser;
  42. use OCA\User_LDAP\User\User;
  43. use OCA\User_LDAP\User_LDAP;
  44. use OCA\User_LDAP\User_LDAP as UserLDAP;
  45. use OCA\User_LDAP\UserPluginManager;
  46. use OCP\IConfig;
  47. use OCP\IUser;
  48. use OCP\Notification\IManager as INotificationManager;
  49. use Test\TestCase;
  50. /**
  51. * Class Test_User_Ldap_Direct
  52. *
  53. * @group DB
  54. *
  55. * @package OCA\User_LDAP\Tests
  56. */
  57. class User_LDAPTest extends TestCase {
  58. /** @var User_LDAP */
  59. protected $backend;
  60. /** @var Access|\PHPUnit_Framework_MockObject_MockObject */
  61. protected $access;
  62. /** @var OfflineUser|\PHPUnit_Framework_MockObject_MockObject */
  63. protected $offlineUser;
  64. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  65. protected $config;
  66. /** @var INotificationManager|\PHPUnit_Framework_MockObject_MockObject */
  67. protected $notificationManager;
  68. /** @var Session|\PHPUnit_Framework_MockObject_MockObject */
  69. protected $session;
  70. /** @var UserPluginManager|\PHPUnit_Framework_MockObject_MockObject */
  71. protected $pluginManager;
  72. /** @var Connection|\PHPUnit_Framework_MockObject_MockObject */
  73. protected $connection;
  74. /** @var Manager|\PHPUnit_Framework_MockObject_MockObject */
  75. protected $userManager;
  76. protected function setUp(): void {
  77. parent::setUp();
  78. \OC_User::clearBackends();
  79. \OC::$server->getGroupManager()->clearBackends();
  80. $this->connection = $this->createMock(Connection::class);
  81. $this->userManager = $this->createMock(Manager::class);
  82. $this->access = $this->createMock(Access::class);
  83. $this->access->connection = $this->connection;
  84. $this->access->userManager = $this->userManager;
  85. $this->config = $this->createMock(IConfig::class);
  86. $this->notificationManager = $this->createMock(INotificationManager::class);
  87. // Cannot use IUserSession because of private listen() methods
  88. $this->session = $this->createMock(Session::class);
  89. $this->pluginManager = $this->createMock(UserPluginManager::class);
  90. $this->backend = new User_LDAP(
  91. $this->access,
  92. $this->config,
  93. $this->notificationManager,
  94. $this->session,
  95. $this->pluginManager
  96. );
  97. }
  98. private function prepareMockForUserExists() {
  99. $this->access->expects($this->any())
  100. ->method('username2dn')
  101. ->willReturnCallback(function($uid) {
  102. switch ($uid) {
  103. case 'gunslinger':
  104. return 'dnOfRoland,dc=test';
  105. break;
  106. case 'formerUser':
  107. return 'dnOfFormerUser,dc=test';
  108. break;
  109. case 'newyorker':
  110. return 'dnOfNewYorker,dc=test';
  111. break;
  112. case 'ladyofshadows':
  113. return 'dnOfLadyOfShadows,dc=test';
  114. break;
  115. default:
  116. return false;
  117. }
  118. });
  119. $this->access->method('fetchUsersByLoginName')
  120. ->willReturn([]);
  121. }
  122. /**
  123. * Prepares the Access mock for checkPassword tests
  124. * @param bool $noDisplayName
  125. * @return void
  126. */
  127. private function prepareAccessForCheckPassword($noDisplayName = false) {
  128. $this->connection->expects($this->any())
  129. ->method('__get')
  130. ->willReturnCallback(function($name) {
  131. if($name === 'ldapLoginFilter') {
  132. return '%uid';
  133. }
  134. return null;
  135. });
  136. $this->access->expects($this->any())
  137. ->method('fetchListOfUsers')
  138. ->willReturnCallback(function($filter) {
  139. if($filter === 'roland') {
  140. return [['dn' => ['dnOfRoland,dc=test']]];
  141. }
  142. return [];
  143. });
  144. $this->access->expects($this->any())
  145. ->method('fetchUsersByLoginName')
  146. ->willReturnCallback(function($uid) {
  147. if($uid === 'roland') {
  148. return [['dn' => ['dnOfRoland,dc=test']]];
  149. }
  150. return [];
  151. });
  152. $retVal = 'gunslinger';
  153. if($noDisplayName === true) {
  154. $retVal = false;
  155. }
  156. $this->access->expects($this->any())
  157. ->method('dn2username')
  158. ->with($this->equalTo('dnOfRoland,dc=test'))
  159. ->willReturn($retVal);
  160. $this->access->expects($this->any())
  161. ->method('stringResemblesDN')
  162. ->with($this->equalTo('dnOfRoland,dc=test'))
  163. ->willReturn(true);
  164. $this->access->expects($this->any())
  165. ->method('areCredentialsValid')
  166. ->willReturnCallback(function($dn, $pwd) {
  167. if($pwd === 'dt19') {
  168. return true;
  169. }
  170. return false;
  171. });
  172. }
  173. public function testCheckPasswordUidReturn() {
  174. $user = $this->createMock(User::class);
  175. $user->expects($this->any())
  176. ->method('getUsername')
  177. ->willReturn('gunslinger');
  178. $this->prepareAccessForCheckPassword();
  179. $this->userManager->expects($this->any())
  180. ->method('get')
  181. ->willReturn($user);
  182. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  183. \OC_User::useBackend($backend);
  184. $result = $backend->checkPassword('roland', 'dt19');
  185. $this->assertEquals('gunslinger', $result);
  186. }
  187. public function testCheckPasswordWrongPassword() {
  188. $this->prepareAccessForCheckPassword();
  189. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  190. \OC_User::useBackend($backend);
  191. $result = $backend->checkPassword('roland', 'wrong');
  192. $this->assertFalse($result);
  193. }
  194. public function testCheckPasswordWrongUser() {
  195. $this->prepareAccessForCheckPassword();
  196. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  197. \OC_User::useBackend($backend);
  198. $result = $backend->checkPassword('mallory', 'evil');
  199. $this->assertFalse($result);
  200. }
  201. public function testCheckPasswordNoDisplayName() {
  202. $this->prepareAccessForCheckPassword(true);
  203. $this->prepareAccessForCheckPassword();
  204. $this->userManager->expects($this->atLeastOnce())
  205. ->method('get')
  206. ->willReturn(null);
  207. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  208. \OC_User::useBackend($backend);
  209. $result = $backend->checkPassword('roland', 'dt19');
  210. $this->assertFalse($result);
  211. }
  212. public function testCheckPasswordPublicAPI() {
  213. $user = $this->createMock(User::class);
  214. $user->expects($this->any())
  215. ->method('getUsername')
  216. ->willReturn('gunslinger');
  217. $this->prepareAccessForCheckPassword();
  218. $this->userManager->expects($this->any())
  219. ->method('get')
  220. ->willReturn($user);
  221. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  222. \OC_User::useBackend($backend);
  223. $user = \OC::$server->getUserManager()->checkPassword('roland', 'dt19');
  224. $result = false;
  225. if ($user !== false) {
  226. $result = $user->getUID();
  227. }
  228. $this->assertEquals('gunslinger', $result);
  229. }
  230. public function testCheckPasswordPublicAPIWrongPassword() {
  231. $this->prepareAccessForCheckPassword();
  232. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  233. \OC_User::useBackend($backend);
  234. $user = \OC::$server->getUserManager()->checkPassword('roland', 'wrong');
  235. $result = false;
  236. if ($user !== false) {
  237. $result = $user->getUID();
  238. }
  239. $this->assertFalse($result);
  240. }
  241. public function testCheckPasswordPublicAPIWrongUser() {
  242. $this->prepareAccessForCheckPassword();
  243. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  244. \OC_User::useBackend($backend);
  245. $user = \OC::$server->getUserManager()->checkPassword('mallory', 'evil');
  246. $result = false;
  247. if ($user !== false) {
  248. $result = $user->getUID();
  249. }
  250. $this->assertFalse($result);
  251. }
  252. public function testDeleteUserCancel() {
  253. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  254. $result = $backend->deleteUser('notme');
  255. $this->assertFalse($result);
  256. }
  257. public function testDeleteUserSuccess() {
  258. $uid = 'jeremy';
  259. $home = '/var/vhome/jdings/';
  260. $mapping = $this->createMock(UserMapping::class);
  261. $mapping->expects($this->once())
  262. ->method('unmap')
  263. ->willReturn(true);
  264. $this->access->expects($this->once())
  265. ->method('getUserMapper')
  266. ->willReturn($mapping);
  267. $this->connection->expects($this->any())
  268. ->method('getConnectionResource')
  269. ->willReturn('this is an ldap link');
  270. $this->config->expects($this->any())
  271. ->method('getUserValue')
  272. ->with($uid, 'user_ldap', 'isDeleted')
  273. ->willReturn('1');
  274. $offlineUser = $this->createMock(OfflineUser::class);
  275. $offlineUser->expects($this->once())
  276. ->method('getHomePath')
  277. ->willReturn($home);
  278. $this->userManager->expects($this->atLeastOnce())
  279. ->method('get')
  280. ->willReturn($offlineUser);
  281. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  282. $result = $backend->deleteUser($uid);
  283. $this->assertTrue($result);
  284. /** @noinspection PhpUnhandledExceptionInspection */
  285. $this->assertSame($backend->getHome($uid), $home);
  286. }
  287. public function testDeleteUserWithPlugin() {
  288. $this->pluginManager->expects($this->once())
  289. ->method('canDeleteUser')
  290. ->willReturn(true);
  291. $this->pluginManager->expects($this->once())
  292. ->method('deleteUser')
  293. ->with('uid')
  294. ->willReturn(true);
  295. $this->config->expects($this->once())
  296. ->method('getUserValue')
  297. ->with('uid', 'user_ldap', 'isDeleted', 0)
  298. ->willReturn(1);
  299. $mapper = $this->createMock(UserMapping::class);
  300. $mapper->expects($this->once())
  301. ->method('unmap')
  302. ->with('uid');
  303. $this->access->expects($this->atLeastOnce())
  304. ->method('getUserMapper')
  305. ->willReturn($mapper);
  306. $this->userManager->expects($this->once())
  307. ->method('invalidate')
  308. ->with('uid');
  309. $this->assertEquals(true, $this->backend->deleteUser('uid'));
  310. }
  311. /**
  312. * Prepares the Access mock for getUsers tests
  313. */
  314. private function prepareAccessForGetUsers() {
  315. $this->access->expects($this->once())
  316. ->method('escapeFilterPart')
  317. ->willReturnCallback(function($search) {
  318. return $search;
  319. });
  320. $this->access->expects($this->any())
  321. ->method('getFilterPartForUserSearch')
  322. ->willReturnCallback(function($search) {
  323. return $search;
  324. });
  325. $this->access->expects($this->any())
  326. ->method('combineFilterWithAnd')
  327. ->willReturnCallback(function($param) {
  328. return $param[2];
  329. });
  330. $this->access->expects($this->any())
  331. ->method('fetchListOfUsers')
  332. ->willReturnCallback(function($search, $a, $l, $o) {
  333. $users = ['gunslinger', 'newyorker', 'ladyofshadows'];
  334. if(empty($search)) {
  335. $result = $users;
  336. } else {
  337. $result = [];
  338. foreach($users as $user) {
  339. if(stripos($user, $search) !== false) {
  340. $result[] = $user;
  341. }
  342. }
  343. }
  344. if(!is_null($l) || !is_null($o)) {
  345. $result = array_slice($result, $o, $l);
  346. }
  347. return $result;
  348. });
  349. $this->access->expects($this->any())
  350. ->method('nextcloudUserNames')
  351. ->willReturnArgument(0);
  352. $this->access->method('fetchUsersByLoginName')
  353. ->willReturn([]);
  354. }
  355. public function testGetUsersNoParam() {
  356. $this->prepareAccessForGetUsers();
  357. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  358. $result = $backend->getUsers();
  359. $this->assertEquals(3, count($result));
  360. }
  361. public function testGetUsersLimitOffset() {
  362. $this->prepareAccessForGetUsers();
  363. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  364. $result = $backend->getUsers('', 1, 2);
  365. $this->assertEquals(1, count($result));
  366. }
  367. public function testGetUsersLimitOffset2() {
  368. $this->prepareAccessForGetUsers();
  369. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  370. $result = $backend->getUsers('', 2, 1);
  371. $this->assertEquals(2, count($result));
  372. }
  373. public function testGetUsersSearchWithResult() {
  374. $this->prepareAccessForGetUsers();
  375. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  376. $result = $backend->getUsers('yo');
  377. $this->assertEquals(2, count($result));
  378. }
  379. public function testGetUsersSearchEmptyResult() {
  380. $this->prepareAccessForGetUsers();
  381. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  382. $result = $backend->getUsers('nix');
  383. $this->assertEquals(0, count($result));
  384. }
  385. private function getUsers($search = '', $limit = null, $offset = null) {
  386. $users = \OC::$server->getUserManager()->search($search, $limit, $offset);
  387. $uids = array_map(function(IUser $user) {
  388. return $user->getUID();
  389. }, $users);
  390. return $uids;
  391. }
  392. public function testGetUsersViaAPINoParam() {
  393. $this->prepareAccessForGetUsers();
  394. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  395. \OC_User::useBackend($backend);
  396. $result = $this->getUsers();
  397. $this->assertEquals(3, count($result));
  398. }
  399. public function testGetUsersViaAPILimitOffset() {
  400. $this->prepareAccessForGetUsers();
  401. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  402. \OC_User::useBackend($backend);
  403. $result = $this->getUsers('', 1, 2);
  404. $this->assertEquals(1, count($result));
  405. }
  406. public function testGetUsersViaAPILimitOffset2() {
  407. $this->prepareAccessForGetUsers();
  408. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  409. \OC_User::useBackend($backend);
  410. $result = $this->getUsers('', 2, 1);
  411. $this->assertEquals(2, count($result));
  412. }
  413. public function testGetUsersViaAPISearchWithResult() {
  414. $this->prepareAccessForGetUsers();
  415. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  416. \OC_User::useBackend($backend);
  417. $result = $this->getUsers('yo');
  418. $this->assertEquals(2, count($result));
  419. }
  420. public function testGetUsersViaAPISearchEmptyResult() {
  421. $this->prepareAccessForGetUsers();
  422. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  423. \OC_User::useBackend($backend);
  424. $result = $this->getUsers('nix');
  425. $this->assertEquals(0, count($result));
  426. }
  427. public function testUserExists() {
  428. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  429. $this->prepareMockForUserExists();
  430. $user = $this->createMock(User::class);
  431. $this->userManager->expects($this->atLeastOnce())
  432. ->method('get')
  433. ->willReturn($user);
  434. $this->access->expects($this->any())
  435. ->method('getUserMapper')
  436. ->willReturn($this->createMock(UserMapping::class));
  437. //test for existing user
  438. /** @noinspection PhpUnhandledExceptionInspection */
  439. $result = $backend->userExists('gunslinger');
  440. $this->assertTrue($result);
  441. }
  442. public function testUserExistsForDeleted() {
  443. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  444. $this->prepareMockForUserExists();
  445. $mapper = $this->createMock(UserMapping::class);
  446. $mapper->expects($this->any())
  447. ->method('getUUIDByDN')
  448. ->with('dnOfFormerUser,dc=test')
  449. ->willReturn('45673458748');
  450. $this->access->expects($this->any())
  451. ->method('getUserMapper')
  452. ->willReturn($mapper);
  453. $user = $this->createMock(User::class);
  454. $this->userManager->expects($this->atLeastOnce())
  455. ->method('get')
  456. ->willReturn($user);
  457. //test for deleted user – always returns true as long as we have the user in DB
  458. $this->assertTrue($backend->userExists('formerUser'));
  459. }
  460. public function testUserExistsForNeverExisting() {
  461. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  462. $this->prepareMockForUserExists();
  463. $this->access->expects($this->any())
  464. ->method('readAttribute')
  465. ->willReturnCallback(function($dn) {
  466. if($dn === 'dnOfRoland,dc=test') {
  467. return [];
  468. }
  469. return false;
  470. });
  471. //test for never-existing user
  472. /** @noinspection PhpUnhandledExceptionInspection */
  473. $result = $backend->userExists('mallory');
  474. $this->assertFalse($result);
  475. }
  476. public function testUserExistsPublicAPI() {
  477. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  478. $this->prepareMockForUserExists();
  479. \OC_User::useBackend($backend);
  480. $user = $this->createMock(User::class);
  481. $user->expects($this->any())
  482. ->method('getDN')
  483. ->willReturn('dnOfRoland,dc=test');
  484. $this->access->expects($this->any())
  485. ->method('readAttribute')
  486. ->willReturnCallback(function($dn) {
  487. if($dn === 'dnOfRoland,dc=test') {
  488. return [];
  489. }
  490. return false;
  491. });
  492. $this->userManager->expects($this->atLeastOnce())
  493. ->method('get')
  494. ->willReturn($user);
  495. $this->access->expects($this->any())
  496. ->method('getUserMapper')
  497. ->willReturn($this->createMock(UserMapping::class));
  498. //test for existing user
  499. $result = \OC::$server->getUserManager()->userExists('gunslinger');
  500. $this->assertTrue($result);
  501. }
  502. public function testDeleteUserExisting() {
  503. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  504. //we do not support deleting existing users at all
  505. $result = $backend->deleteUser('gunslinger');
  506. $this->assertFalse($result);
  507. }
  508. public function testGetHomeAbsolutePath() {
  509. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  510. $this->prepareMockForUserExists();
  511. $this->connection->expects($this->any())
  512. ->method('__get')
  513. ->willReturnCallback(function($name) {
  514. if($name === 'homeFolderNamingRule') {
  515. return 'attr:testAttribute';
  516. }
  517. return null;
  518. });
  519. $this->access->expects($this->any())
  520. ->method('readAttribute')
  521. ->willReturnCallback(function($dn, $attr) {
  522. switch ($dn) {
  523. case 'dnOfRoland,dc=test':
  524. if($attr === 'testAttribute') {
  525. return ['/tmp/rolandshome/'];
  526. }
  527. return [];
  528. break;
  529. default:
  530. return false;
  531. }
  532. });
  533. $user = $this->createMock(User::class);
  534. $user->expects($this->any())
  535. ->method('getUsername')
  536. ->willReturn('gunslinger');
  537. $user->expects($this->any())
  538. ->method('getDN')
  539. ->willReturn('dnOfRoland,dc=test');
  540. $user->expects($this->any())
  541. ->method('getHomePath')
  542. ->willReturn('/tmp/rolandshome/');
  543. $this->userManager->expects($this->atLeastOnce())
  544. ->method('get')
  545. ->willReturn($user);
  546. //absolute path
  547. /** @noinspection PhpUnhandledExceptionInspection */
  548. $result = $backend->getHome('gunslinger');
  549. $this->assertEquals('/tmp/rolandshome/', $result);
  550. }
  551. public function testGetHomeRelative() {
  552. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  553. $this->prepareMockForUserExists();
  554. $dataDir = \OC::$server->getConfig()->getSystemValue(
  555. 'datadirectory', \OC::$SERVERROOT.'/data');
  556. $this->connection->expects($this->any())
  557. ->method('__get')
  558. ->willReturnCallback(function($name) {
  559. if($name === 'homeFolderNamingRule') {
  560. return 'attr:testAttribute';
  561. }
  562. return null;
  563. });
  564. $this->access->expects($this->any())
  565. ->method('readAttribute')
  566. ->willReturnCallback(function($dn, $attr) {
  567. switch ($dn) {
  568. case 'dnOfLadyOfShadows,dc=test':
  569. if($attr === 'testAttribute') {
  570. return ['susannah/'];
  571. }
  572. return [];
  573. break;
  574. default:
  575. return false;
  576. }
  577. });
  578. $user = $this->createMock(User::class);
  579. $user->expects($this->any())
  580. ->method('getUsername')
  581. ->willReturn('ladyofshadows');
  582. $user->expects($this->any())
  583. ->method('getDN')
  584. ->willReturn('dnOfLadyOfShadows,dc=test');
  585. $user->expects($this->any())
  586. ->method('getHomePath')
  587. ->willReturn($dataDir.'/susannah/');
  588. $this->userManager->expects($this->atLeastOnce())
  589. ->method('get')
  590. ->willReturn($user);
  591. /** @noinspection PhpUnhandledExceptionInspection */
  592. $result = $backend->getHome('ladyofshadows');
  593. $this->assertEquals($dataDir.'/susannah/', $result);
  594. }
  595. public function testGetHomeNoPath() {
  596. $this->expectException(\Exception::class);
  597. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  598. $this->prepareMockForUserExists();
  599. $this->connection->expects($this->any())
  600. ->method('__get')
  601. ->willReturnCallback(function($name) {
  602. if($name === 'homeFolderNamingRule') {
  603. return 'attr:testAttribute';
  604. }
  605. return null;
  606. });
  607. $this->access->expects($this->any())
  608. ->method('readAttribute')
  609. ->willReturnCallback(function($dn, $attr) {
  610. switch ($dn) {
  611. default:
  612. return false;
  613. }
  614. });
  615. $this->access->connection->expects($this->any())
  616. ->method('getFromCache')
  617. ->willReturnCallback(function($key) {
  618. if($key === 'userExistsnewyorker') {
  619. return true;
  620. }
  621. return null;
  622. });
  623. $user = $this->createMock(User::class);
  624. $user->expects($this->any())
  625. ->method('getUsername')
  626. ->willReturn('newyorker');
  627. $user->expects($this->any())
  628. ->method('getHomePath')
  629. ->willThrowException(new \Exception());
  630. $this->userManager->expects($this->atLeastOnce())
  631. ->method('get')
  632. ->willReturn($user);
  633. //no path at all – triggers OC default behaviour
  634. $result = $backend->getHome('newyorker');
  635. $this->assertFalse($result);
  636. }
  637. public function testGetHomeDeletedUser() {
  638. $uid = 'newyorker';
  639. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  640. $this->prepareMockForUserExists();
  641. $this->connection->expects($this->any())
  642. ->method('__get')
  643. ->willReturnCallback(function($name) {
  644. if($name === 'homeFolderNamingRule') {
  645. return 'attr:testAttribute';
  646. }
  647. return null;
  648. });
  649. $this->access->expects($this->any())
  650. ->method('readAttribute')
  651. ->willReturn([]);
  652. $userMapper = $this->createMock(UserMapping::class);
  653. $this->access->expects($this->any())
  654. ->method('getUserMapper')
  655. ->willReturn($userMapper);
  656. $this->config->expects($this->any())
  657. ->method('getUserValue')
  658. ->willReturn(true);
  659. $offlineUser = $this->createMock(OfflineUser::class);
  660. $offlineUser->expects($this->atLeastOnce())
  661. ->method('getHomePath')
  662. ->willReturn('');
  663. $this->userManager->expects($this->atLeastOnce())
  664. ->method('get')
  665. ->willReturn($offlineUser);
  666. $result = $backend->getHome($uid);
  667. $this->assertFalse($result);
  668. }
  669. public function testGetHomeWithPlugin() {
  670. $this->pluginManager->expects($this->once())
  671. ->method('implementsActions')
  672. ->with(Backend::GET_HOME)
  673. ->willReturn(true);
  674. $this->pluginManager->expects($this->once())
  675. ->method('getHome')
  676. ->with('uid')
  677. ->willReturn('result');
  678. $this->connection->expects($this->any())
  679. ->method('getFromCache')
  680. ->willReturnCallback(function($uid) {
  681. return true;
  682. });
  683. /** @noinspection PhpUnhandledExceptionInspection */
  684. $this->assertEquals($this->backend->getHome('uid'),'result');
  685. }
  686. private function prepareAccessForGetDisplayName() {
  687. $this->connection->expects($this->any())
  688. ->method('__get')
  689. ->willReturnCallback(function($name) {
  690. if($name === 'ldapUserDisplayName') {
  691. return 'displayname';
  692. }
  693. return null;
  694. });
  695. $this->access->expects($this->any())
  696. ->method('readAttribute')
  697. ->willReturnCallback(function($dn, $attr) {
  698. switch ($dn) {
  699. case 'dnOfRoland,dc=test':
  700. if($attr === 'displayname') {
  701. return ['Roland Deschain'];
  702. }
  703. return [];
  704. break;
  705. default:
  706. return false;
  707. }
  708. });
  709. $this->access->method('fetchUsersByLoginName')
  710. ->willReturn([]);
  711. }
  712. public function testGetDisplayName() {
  713. $this->prepareAccessForGetDisplayName();
  714. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  715. $this->prepareMockForUserExists();
  716. $this->connection->expects($this->any())
  717. ->method('getConnectionResource')
  718. ->willReturnCallback(function() {
  719. return true;
  720. });
  721. $user1 = $this->createMock(User::class);
  722. $user1->expects($this->once())
  723. ->method('composeAndStoreDisplayName')
  724. ->willReturn('Roland Deschain');
  725. $user1->expects($this->any())
  726. ->method('getDN')
  727. ->willReturn('dnOfRoland,dc=test');
  728. $user2 = $this->createMock(User::class);
  729. $user2->expects($this->never())
  730. ->method('composeAndStoreDisplayName');
  731. $user2->expects($this->any())
  732. ->method('getDN')
  733. ->willReturn('another DN');
  734. $mapper = $this->createMock(UserMapping::class);
  735. $mapper->expects($this->any())
  736. ->method('getUUIDByDN')
  737. ->willReturnCallback(function($dn) { return $dn; });
  738. $this->userManager->expects($this->any())
  739. ->method('get')
  740. ->willReturnCallback(function($uid) use ($user1, $user2) {
  741. if($uid === 'gunslinger') {
  742. return $user1;
  743. } else if($uid === 'newyorker') {
  744. return $user2;
  745. }
  746. return null;
  747. });
  748. $this->access->expects($this->any())
  749. ->method('getUserMapper')
  750. ->willReturn($mapper);
  751. $this->access->expects($this->any())
  752. ->method('getUserDnByUuid')
  753. ->willReturnCallback(function($uuid) { return $uuid . '1'; });
  754. //with displayName
  755. $result = $backend->getDisplayName('gunslinger');
  756. $this->assertEquals('Roland Deschain', $result);
  757. //empty displayname retrieved
  758. $result = $backend->getDisplayName('newyorker');
  759. $this->assertEquals(null, $result);
  760. }
  761. public function testGetDisplayNamePublicAPI() {
  762. $this->access->expects($this->any())
  763. ->method('username2dn')
  764. ->willReturnCallback(function($uid) {
  765. switch ($uid) {
  766. case 'gunslinger':
  767. return 'dnOfRoland,dc=test';
  768. break;
  769. case 'formerUser':
  770. return 'dnOfFormerUser,dc=test';
  771. break;
  772. case 'newyorker':
  773. return 'dnOfNewYorker,dc=test';
  774. break;
  775. case 'ladyofshadows':
  776. return 'dnOfLadyOfShadows,dc=test';
  777. break;
  778. default:
  779. return false;
  780. }
  781. });
  782. $this->prepareAccessForGetDisplayName();
  783. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  784. $this->prepareMockForUserExists();
  785. $this->connection->expects($this->any())
  786. ->method('getConnectionResource')
  787. ->willReturnCallback(function() {
  788. return true;
  789. });
  790. \OC_User::useBackend($backend);
  791. $user1 = $this->createMock(User::class);
  792. $user1->expects($this->once())
  793. ->method('composeAndStoreDisplayName')
  794. ->willReturn('Roland Deschain');
  795. $user1->expects($this->any())
  796. ->method('getDN')
  797. ->willReturn('dnOfRoland,dc=test');
  798. $user2 = $this->createMock(User::class);
  799. $user2->expects($this->never())
  800. ->method('composeAndStoreDisplayName');
  801. $user2->expects($this->any())
  802. ->method('getDN')
  803. ->willReturn('another DN');
  804. $mapper = $this->createMock(UserMapping::class);
  805. $mapper->expects($this->any())
  806. ->method('getUUIDByDN')
  807. ->willReturnCallback(function($dn) { return $dn; });
  808. $this->userManager->expects($this->any())
  809. ->method('get')
  810. ->willReturnCallback(function($uid) use ($user1, $user2) {
  811. if($uid === 'gunslinger') {
  812. return $user1;
  813. } else if($uid === 'newyorker') {
  814. return $user2;
  815. }
  816. return null;
  817. });
  818. $this->access->expects($this->any())
  819. ->method('getUserMapper')
  820. ->willReturn($mapper);
  821. $this->access->expects($this->any())
  822. ->method('getUserDnByUuid')
  823. ->willReturnCallback(function($uuid) { return $uuid . '1'; });
  824. //with displayName
  825. $result = \OC::$server->getUserManager()->get('gunslinger')->getDisplayName();
  826. $this->assertEquals('Roland Deschain', $result);
  827. //empty displayname retrieved
  828. $result = \OC::$server->getUserManager()->get('newyorker') === null ? 'newyorker' : \OC::$server->getUserManager()->get('newyorker')->getDisplayName();
  829. $this->assertEquals('newyorker', $result);
  830. }
  831. public function testGetDisplayNameWithPlugin() {
  832. $this->pluginManager->expects($this->once())
  833. ->method('implementsActions')
  834. ->with(Backend::GET_DISPLAYNAME)
  835. ->willReturn(true);
  836. $this->pluginManager->expects($this->once())
  837. ->method('getDisplayName')
  838. ->with('uid')
  839. ->willReturn('result');
  840. $this->assertEquals($this->backend->getDisplayName('uid'),'result');
  841. }
  842. //no test for getDisplayNames, because it just invokes getUsers and
  843. //getDisplayName
  844. public function testCountUsers() {
  845. $this->access->expects($this->once())
  846. ->method('countUsers')
  847. ->willReturn(5);
  848. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  849. $result = $backend->countUsers();
  850. $this->assertEquals(5, $result);
  851. }
  852. public function testCountUsersFailing() {
  853. $this->access->expects($this->once())
  854. ->method('countUsers')
  855. ->willReturn(false);
  856. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  857. $result = $backend->countUsers();
  858. $this->assertFalse($result);
  859. }
  860. public function testCountUsersWithPlugin() {
  861. $this->pluginManager->expects($this->once())
  862. ->method('implementsActions')
  863. ->with(Backend::COUNT_USERS)
  864. ->willReturn(true);
  865. $this->pluginManager->expects($this->once())
  866. ->method('countUsers')
  867. ->willReturn(42);
  868. $this->assertEquals($this->backend->countUsers(),42);
  869. }
  870. public function testLoginName2UserNameSuccess() {
  871. $loginName = 'Alice';
  872. $username = 'alice';
  873. $dn = 'uid=alice,dc=what,dc=ever';
  874. $this->access->expects($this->once())
  875. ->method('fetchUsersByLoginName')
  876. ->with($this->equalTo($loginName))
  877. ->willReturn([['dn' => [$dn]]]);
  878. $this->access->expects($this->any())
  879. ->method('stringResemblesDN')
  880. ->with($this->equalTo($dn))
  881. ->willReturn(true);
  882. $this->access->expects($this->any())
  883. ->method('dn2username')
  884. ->with($this->equalTo($dn))
  885. ->willReturn($username);
  886. $this->connection->expects($this->exactly(2))
  887. ->method('getFromCache')
  888. ->with($this->equalTo('loginName2UserName-'.$loginName))
  889. ->willReturnOnConsecutiveCalls(null, $username);
  890. $this->connection->expects($this->once())
  891. ->method('writeToCache')
  892. ->with($this->equalTo('loginName2UserName-'.$loginName), $this->equalTo($username));
  893. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  894. $user = $this->createMock(User::class);
  895. $user->expects($this->any())
  896. ->method('getUsername')
  897. ->willReturn('alice');
  898. $this->userManager->expects($this->atLeastOnce())
  899. ->method('get')
  900. ->with($dn)
  901. ->willReturn($user);
  902. $name = $backend->loginName2UserName($loginName);
  903. $this->assertSame($username, $name);
  904. // and once again to verify that caching works
  905. $backend->loginName2UserName($loginName);
  906. }
  907. public function testLoginName2UserNameNoUsersOnLDAP() {
  908. $loginName = 'Loki';
  909. $this->access->expects($this->once())
  910. ->method('fetchUsersByLoginName')
  911. ->with($this->equalTo($loginName))
  912. ->willReturn([]);
  913. $this->access->expects($this->never())
  914. ->method('stringResemblesDN');
  915. $this->access->expects($this->never())
  916. ->method('dn2username');
  917. $this->connection->expects($this->exactly(2))
  918. ->method('getFromCache')
  919. ->with($this->equalTo('loginName2UserName-'.$loginName))
  920. ->willReturnOnConsecutiveCalls(null, false);
  921. $this->connection->expects($this->once())
  922. ->method('writeToCache')
  923. ->with($this->equalTo('loginName2UserName-'.$loginName), false);
  924. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  925. $name = $backend->loginName2UserName($loginName);
  926. $this->assertSame(false, $name);
  927. // and once again to verify that caching works
  928. $backend->loginName2UserName($loginName);
  929. }
  930. public function testLoginName2UserNameOfflineUser() {
  931. $loginName = 'Alice';
  932. $dn = 'uid=alice,dc=what,dc=ever';
  933. $offlineUser = $this->getMockBuilder(OfflineUser::class)
  934. ->disableOriginalConstructor()
  935. ->getMock();
  936. $this->access->expects($this->once())
  937. ->method('fetchUsersByLoginName')
  938. ->with($this->equalTo($loginName))
  939. ->willReturn([['dn' => [$dn]]]);
  940. $this->connection->expects($this->exactly(2))
  941. ->method('getFromCache')
  942. ->with($this->equalTo('loginName2UserName-'.$loginName))
  943. ->willReturnOnConsecutiveCalls(null, false);
  944. $this->connection->expects($this->once())
  945. ->method('writeToCache')
  946. ->with($this->equalTo('loginName2UserName-'.$loginName), $this->equalTo(false));
  947. $this->userManager->expects($this->any())
  948. ->method('get')
  949. ->with($dn)
  950. ->willReturn($offlineUser);
  951. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  952. $name = $backend->loginName2UserName($loginName);
  953. $this->assertSame(false, $name);
  954. // and once again to verify that caching works
  955. $backend->loginName2UserName($loginName);
  956. }
  957. /**
  958. * Prepares the Access mock for setPassword tests
  959. *
  960. * @param bool $enablePasswordChange
  961. */
  962. private function prepareAccessForSetPassword($enablePasswordChange = true) {
  963. $this->connection->expects($this->any())
  964. ->method('__get')
  965. ->willReturnCallback(function($name) use (&$enablePasswordChange) {
  966. if($name === 'ldapLoginFilter') {
  967. return '%uid';
  968. }
  969. if($name === 'turnOnPasswordChange') {
  970. return $enablePasswordChange?1:0;
  971. }
  972. return null;
  973. });
  974. $this->connection->expects($this->any())
  975. ->method('getFromCache')
  976. ->willReturnCallback(function($uid) {
  977. if($uid === 'userExists'.'roland') {
  978. return true;
  979. }
  980. return null;
  981. });
  982. $this->access->expects($this->any())
  983. ->method('fetchListOfUsers')
  984. ->willReturnCallback(function($filter) {
  985. if($filter === 'roland') {
  986. return [['dn' => ['dnOfRoland,dc=test']]];
  987. }
  988. return [];
  989. });
  990. $this->access->expects($this->any())
  991. ->method('fetchUsersByLoginName')
  992. ->willReturnCallback(function($uid) {
  993. if($uid === 'roland') {
  994. return [['dn' => ['dnOfRoland,dc=test']]];
  995. }
  996. return [];
  997. });
  998. $this->access->expects($this->any())
  999. ->method('dn2username')
  1000. ->with($this->equalTo('dnOfRoland,dc=test'))
  1001. ->willReturn('roland');
  1002. $this->access->expects($this->any())
  1003. ->method('stringResemblesDN')
  1004. ->with($this->equalTo('dnOfRoland,dc=test'))
  1005. ->willReturn(true);
  1006. $this->access->expects($this->any())
  1007. ->method('setPassword')
  1008. ->willReturnCallback(function($uid, $password) {
  1009. if(strlen($password) <= 5) {
  1010. throw new HintException('Password fails quality checking policy', '', 19);
  1011. }
  1012. return true;
  1013. });
  1014. }
  1015. public function testSetPasswordInvalid() {
  1016. $this->expectException(\OC\HintException::class);
  1017. $this->expectExceptionMessage('Password fails quality checking policy');
  1018. $this->prepareAccessForSetPassword($this->access);
  1019. $this->userManager->expects($this->atLeastOnce())
  1020. ->method('get')
  1021. ->willReturn($this->createMock(User::class));
  1022. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  1023. \OC_User::useBackend($backend);
  1024. $this->assertTrue(\OC_User::setPassword('roland', 'dt'));
  1025. }
  1026. public function testSetPasswordValid() {
  1027. $this->prepareAccessForSetPassword($this->access);
  1028. $this->userManager->expects($this->any())
  1029. ->method('get')
  1030. ->willReturn($this->createMock(User::class));
  1031. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  1032. $this->userManager->expects($this->any())
  1033. ->method('get')
  1034. ->willReturn($this->createMock(User::class));
  1035. \OC_User::useBackend($backend);
  1036. $this->assertTrue(\OC_User::setPassword('roland', 'dt12234$'));
  1037. }
  1038. public function testSetPasswordValidDisabled() {
  1039. $this->userManager->expects($this->any())
  1040. ->method('get')
  1041. ->willReturn($this->createMock(User::class));
  1042. $this->prepareAccessForSetPassword(false);
  1043. $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager);
  1044. \OC_User::useBackend($backend);
  1045. $this->assertFalse(\OC_User::setPassword('roland', 'dt12234$'));
  1046. }
  1047. public function testSetPasswordWithInvalidUser() {
  1048. $this->expectException(\Exception::class);
  1049. $this->expectExceptionMessage('LDAP setPassword: Could not get user object for uid NotExistingUser. Maybe the LDAP entry has no set display name attribute?');
  1050. $this->userManager
  1051. ->expects($this->once())
  1052. ->method('get')
  1053. ->with('NotExistingUser')
  1054. ->willReturn(null);
  1055. $this->backend->setPassword('NotExistingUser', 'Password');
  1056. }
  1057. public function testSetPasswordWithUsernameFalse() {
  1058. $user = $this->createMock(User::class);
  1059. $user
  1060. ->expects($this->once())
  1061. ->method('getUsername')
  1062. ->willReturn(false);
  1063. $this->userManager
  1064. ->expects($this->once())
  1065. ->method('get')
  1066. ->with('NotExistingUser')
  1067. ->willReturn($user);
  1068. /** @noinspection PhpUnhandledExceptionInspection */
  1069. $this->assertFalse($this->backend->setPassword('NotExistingUser', 'Password'));
  1070. }
  1071. public function testSetPasswordWithPlugin() {
  1072. $this->pluginManager->expects($this->once())
  1073. ->method('implementsActions')
  1074. ->with(Backend::SET_PASSWORD)
  1075. ->willReturn(true);
  1076. $this->pluginManager->expects($this->once())
  1077. ->method('setPassword')
  1078. ->with('uid','password')
  1079. ->willReturn('result');
  1080. /** @noinspection PhpUnhandledExceptionInspection */
  1081. $this->assertEquals($this->backend->setPassword('uid', 'password'),'result');
  1082. }
  1083. public function avatarDataProvider() {
  1084. return [
  1085. [ 'validImageData', false ],
  1086. [ 'corruptImageData', true ],
  1087. [ false, true]
  1088. ];
  1089. }
  1090. /** @dataProvider avatarDataProvider */
  1091. public function testCanChangeAvatar($imageData, $expected) {
  1092. $isValidImage = strpos((string)$imageData, 'valid') === 0;
  1093. $user = $this->createMock(User::class);
  1094. $user->expects($this->once())
  1095. ->method('getAvatarImage')
  1096. ->willReturn($imageData);
  1097. $user->expects($this->atMost(1))
  1098. ->method('updateAvatar')
  1099. ->willReturn($isValidImage);
  1100. $this->userManager->expects($this->atLeastOnce())
  1101. ->method('get')
  1102. ->willReturn($user);
  1103. /** @noinspection PhpUnhandledExceptionInspection */
  1104. $this->assertSame($expected, $this->backend->canChangeAvatar('uid'));
  1105. }
  1106. public function testCanChangeAvatarWithPlugin() {
  1107. $this->pluginManager->expects($this->once())
  1108. ->method('implementsActions')
  1109. ->with(Backend::PROVIDE_AVATAR)
  1110. ->willReturn(true);
  1111. $this->pluginManager->expects($this->once())
  1112. ->method('canChangeAvatar')
  1113. ->with('uid')
  1114. ->willReturn('result');
  1115. $this->assertEquals($this->backend->canChangeAvatar('uid'),'result');
  1116. }
  1117. public function testSetDisplayNameWithPlugin() {
  1118. $newDisplayName = 'J. Baker';
  1119. $this->pluginManager->expects($this->once())
  1120. ->method('implementsActions')
  1121. ->with(Backend::SET_DISPLAYNAME)
  1122. ->willReturn(true);
  1123. $this->pluginManager->expects($this->once())
  1124. ->method('setDisplayName')
  1125. ->with('uid', $newDisplayName)
  1126. ->willReturn($newDisplayName);
  1127. $this->access->expects($this->once())
  1128. ->method('cacheUserDisplayName');
  1129. $this->assertEquals($newDisplayName, $this->backend->setDisplayName('uid', $newDisplayName));
  1130. }
  1131. public function testSetDisplayNameErrorWithPlugin() {
  1132. $this->expectException(\OC\HintException::class);
  1133. $newDisplayName = 'J. Baker';
  1134. $this->pluginManager->expects($this->once())
  1135. ->method('implementsActions')
  1136. ->with(Backend::SET_DISPLAYNAME)
  1137. ->willReturn(true);
  1138. $this->pluginManager->expects($this->once())
  1139. ->method('setDisplayName')
  1140. ->with('uid', $newDisplayName)
  1141. ->willThrowException(new HintException('something happned'));
  1142. $this->access->expects($this->never())
  1143. ->method('cacheUserDisplayName');
  1144. $this->backend->setDisplayName('uid', $newDisplayName);
  1145. }
  1146. public function testSetDisplayNameFailing() {
  1147. $this->pluginManager->expects($this->once())
  1148. ->method('implementsActions')
  1149. ->with(Backend::SET_DISPLAYNAME)
  1150. ->willReturn(false);
  1151. $this->access->expects($this->never())
  1152. ->method('cacheUserDisplayName');
  1153. $this->assertFalse($this->backend->setDisplayName('uid', 'displayName'));
  1154. }
  1155. public function testCreateUserWithPlugin() {
  1156. $uid = 'alien6372';
  1157. $uuid = '123-2345-36756-123-2345234-4431';
  1158. $pwd = 'passwørd';
  1159. $this->pluginManager->expects($this->once())
  1160. ->method('implementsActions')
  1161. ->with(Backend::CREATE_USER)
  1162. ->willReturn(true);
  1163. $this->pluginManager->expects($this->once())
  1164. ->method('createUser')
  1165. ->with($uid, $pwd)
  1166. ->willReturn('result');
  1167. $this->access->expects($this->atLeastOnce())
  1168. ->method('getUUID')
  1169. ->willReturn($uuid);
  1170. $this->access->expects($this->once())
  1171. ->method('mapAndAnnounceIfApplicable')
  1172. ->with($this->isInstanceOf(AbstractMapping::class), $this->anything(), $uid, $uuid, true);
  1173. $this->access->expects($this->any())
  1174. ->method('getUserMapper')
  1175. ->willReturn($this->createMock(AbstractMapping::class));
  1176. $this->assertEquals($this->backend->createUser($uid, $pwd),true);
  1177. }
  1178. public function testCreateUserFailing() {
  1179. $this->pluginManager->expects($this->once())
  1180. ->method('implementsActions')
  1181. ->with(Backend::CREATE_USER)
  1182. ->willReturn(false);
  1183. $this->assertFalse($this->backend->createUser('uid', 'password'));
  1184. }
  1185. public function actionProvider() {
  1186. return [
  1187. [ 'ldapUserAvatarRule', 'default', Backend::PROVIDE_AVATAR, true] ,
  1188. [ 'ldapUserAvatarRule', 'data:selfiePhoto', Backend::PROVIDE_AVATAR, true],
  1189. [ 'ldapUserAvatarRule', 'none', Backend::PROVIDE_AVATAR, false],
  1190. [ 'turnOnPasswordChange', 0, Backend::SET_PASSWORD, false],
  1191. [ 'turnOnPasswordChange', 1, Backend::SET_PASSWORD, true],
  1192. ];
  1193. }
  1194. /**
  1195. * @dataProvider actionProvider
  1196. */
  1197. public function testImplementsAction($configurable, $value, $actionCode, $expected) {
  1198. $this->pluginManager->expects($this->once())
  1199. ->method('getImplementedActions')
  1200. ->willReturn(0);
  1201. $this->connection->expects($this->any())
  1202. ->method('__get')
  1203. ->willReturnMap([
  1204. [$configurable, $value],
  1205. ]);
  1206. $this->assertSame($expected, $this->backend->implementsActions($actionCode));
  1207. }
  1208. }