userscontrollertest.php 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  1. <?php
  2. /**
  3. * @author Lukas Reschke
  4. * @copyright 2014-2015 Lukas Reschke lukas@owncloud.com
  5. *
  6. * This file is licensed under the Affero General Public License version 3 or
  7. * later.
  8. * See the COPYING-README file.
  9. */
  10. namespace OC\Settings\Controller;
  11. use \OC\Settings\Application;
  12. use OCP\AppFramework\Http;
  13. use OCP\AppFramework\Http\DataResponse;
  14. /**
  15. * @package OC\Settings\Controller
  16. */
  17. class UsersControllerTest extends \Test\TestCase {
  18. /** @var \OCP\AppFramework\IAppContainer */
  19. private $container;
  20. protected function setUp() {
  21. $app = new Application();
  22. $this->container = $app->getContainer();
  23. $this->container['AppName'] = 'settings';
  24. $this->container['GroupManager'] = $this->getMockBuilder('\OCP\IGroupManager')
  25. ->disableOriginalConstructor()->getMock();
  26. $this->container['UserManager'] = $this->getMockBuilder('\OCP\IUserManager')
  27. ->disableOriginalConstructor()->getMock();
  28. $this->container['UserSession'] = $this->getMockBuilder('\OC\User\Session')
  29. ->disableOriginalConstructor()->getMock();
  30. $this->container['L10N'] = $this->getMockBuilder('\OCP\IL10N')
  31. ->disableOriginalConstructor()->getMock();
  32. $this->container['SubAdminFactory'] = $this->getMockBuilder('\OC\Settings\Factory\SubAdminFactory')
  33. ->disableOriginalConstructor()->getMock();
  34. $this->container['Config'] = $this->getMockBuilder('\OCP\IConfig')
  35. ->disableOriginalConstructor()->getMock();
  36. $this->container['L10N']
  37. ->expects($this->any())
  38. ->method('t')
  39. ->will($this->returnCallback(function($text, $parameters = array()) {
  40. return vsprintf($text, $parameters);
  41. }));
  42. $this->container['Defaults'] = $this->getMockBuilder('\OC_Defaults')
  43. ->disableOriginalConstructor()->getMock();
  44. $this->container['Mailer'] = $this->getMockBuilder('\OCP\Mail\IMailer')
  45. ->disableOriginalConstructor()->getMock();
  46. $this->container['DefaultMailAddress'] = 'no-reply@owncloud.com';
  47. $this->container['Logger'] = $this->getMockBuilder('\OCP\ILogger')
  48. ->disableOriginalConstructor()->getMock();
  49. $this->container['URLGenerator'] = $this->getMockBuilder('\OCP\IURLGenerator')
  50. ->disableOriginalConstructor()->getMock();
  51. $this->container['OCP\\App\\IAppManager'] = $this->getMockBuilder('OCP\\App\\IAppManager')
  52. ->disableOriginalConstructor()->getMock();
  53. }
  54. public function testIndexAdmin() {
  55. $this->container['IsAdmin'] = true;
  56. $foo = $this->getMockBuilder('\OC\User\User')
  57. ->disableOriginalConstructor()->getMock();
  58. $foo
  59. ->expects($this->exactly(4))
  60. ->method('getUID')
  61. ->will($this->returnValue('foo'));
  62. $foo
  63. ->expects($this->once())
  64. ->method('getDisplayName')
  65. ->will($this->returnValue('M. Foo'));
  66. $foo
  67. ->method('getLastLogin')
  68. ->will($this->returnValue(500));
  69. $foo
  70. ->method('getHome')
  71. ->will($this->returnValue('/home/foo'));
  72. $foo
  73. ->expects($this->once())
  74. ->method('getBackendClassName')
  75. ->will($this->returnValue('OC_User_Database'));
  76. $admin = $this->getMockBuilder('\OC\User\User')
  77. ->disableOriginalConstructor()->getMock();
  78. $admin
  79. ->expects($this->exactly(4))
  80. ->method('getUID')
  81. ->will($this->returnValue('admin'));
  82. $admin
  83. ->expects($this->once())
  84. ->method('getDisplayName')
  85. ->will($this->returnValue('S. Admin'));
  86. $admin
  87. ->expects($this->once())
  88. ->method('getLastLogin')
  89. ->will($this->returnValue(12));
  90. $admin
  91. ->expects($this->once())
  92. ->method('getHome')
  93. ->will($this->returnValue('/home/admin'));
  94. $admin
  95. ->expects($this->once())
  96. ->method('getBackendClassName')
  97. ->will($this->returnValue('OC_User_Dummy'));
  98. $bar = $this->getMockBuilder('\OC\User\User')
  99. ->disableOriginalConstructor()->getMock();
  100. $bar
  101. ->expects($this->exactly(4))
  102. ->method('getUID')
  103. ->will($this->returnValue('bar'));
  104. $bar
  105. ->expects($this->once())
  106. ->method('getDisplayName')
  107. ->will($this->returnValue('B. Ar'));
  108. $bar
  109. ->method('getLastLogin')
  110. ->will($this->returnValue(3999));
  111. $bar
  112. ->method('getHome')
  113. ->will($this->returnValue('/home/bar'));
  114. $bar
  115. ->expects($this->once())
  116. ->method('getBackendClassName')
  117. ->will($this->returnValue('OC_User_Dummy'));
  118. $this->container['GroupManager']
  119. ->expects($this->once())
  120. ->method('displayNamesInGroup')
  121. ->with('gid', 'pattern')
  122. ->will($this->returnValue(array('foo' => 'M. Foo', 'admin' => 'S. Admin', 'bar' => 'B. Ar')));
  123. $this->container['GroupManager']
  124. ->expects($this->exactly(3))
  125. ->method('getUserGroupIds')
  126. ->will($this->onConsecutiveCalls(array('Users', 'Support'), array('admins', 'Support'), array('External Users')));
  127. $this->container['UserManager']
  128. ->expects($this->at(0))
  129. ->method('get')
  130. ->with('foo')
  131. ->will($this->returnValue($foo));
  132. $this->container['UserManager']
  133. ->expects($this->at(1))
  134. ->method('get')
  135. ->with('admin')
  136. ->will($this->returnValue($admin));
  137. $this->container['UserManager']
  138. ->expects($this->at(2))
  139. ->method('get')
  140. ->with('bar')
  141. ->will($this->returnValue($bar));
  142. $this->container['Config']
  143. ->expects($this->exactly(6))
  144. ->method('getUserValue')
  145. ->will($this->onConsecutiveCalls(1024, 'foo@bar.com',
  146. 404, 'admin@bar.com',
  147. 2323, 'bar@dummy.com'));
  148. $expectedResponse = new DataResponse(
  149. array(
  150. 0 => array(
  151. 'name' => 'foo',
  152. 'displayname' => 'M. Foo',
  153. 'groups' => array('Users', 'Support'),
  154. 'subadmin' => array(),
  155. 'quota' => 1024,
  156. 'storageLocation' => '/home/foo',
  157. 'lastLogin' => 500000,
  158. 'backend' => 'OC_User_Database',
  159. 'email' => 'foo@bar.com',
  160. 'isRestoreDisabled' => false,
  161. ),
  162. 1 => array(
  163. 'name' => 'admin',
  164. 'displayname' => 'S. Admin',
  165. 'groups' => array('admins', 'Support'),
  166. 'subadmin' => array(),
  167. 'quota' => 404,
  168. 'storageLocation' => '/home/admin',
  169. 'lastLogin' => 12000,
  170. 'backend' => 'OC_User_Dummy',
  171. 'email' => 'admin@bar.com',
  172. 'isRestoreDisabled' => false,
  173. ),
  174. 2 => array(
  175. 'name' => 'bar',
  176. 'displayname' => 'B. Ar',
  177. 'groups' => array('External Users'),
  178. 'subadmin' => array(),
  179. 'quota' => 2323,
  180. 'storageLocation' => '/home/bar',
  181. 'lastLogin' => 3999000,
  182. 'backend' => 'OC_User_Dummy',
  183. 'email' => 'bar@dummy.com',
  184. 'isRestoreDisabled' => false,
  185. ),
  186. )
  187. );
  188. $response = $this->container['UsersController']->index(0, 10, 'gid', 'pattern');
  189. $this->assertEquals($expectedResponse, $response);
  190. }
  191. public function testIndexSubAdmin() {
  192. $this->container['IsAdmin'] = false;
  193. $this->container['SubAdminFactory']
  194. ->expects($this->once())
  195. ->method('getSubAdminsOfGroups')
  196. ->with('username')
  197. ->will($this->returnValue(['SubGroup1', 'SubGroup2']));
  198. $user = $this->getMockBuilder('\OC\User\User')
  199. ->disableOriginalConstructor()->getMock();
  200. $user
  201. ->expects($this->once())
  202. ->method('getUID')
  203. ->will($this->returnValue('username'));
  204. $this->container['UserSession']
  205. ->expects($this->once())
  206. ->method('getUser')
  207. ->will($this->returnValue($user));
  208. $foo = $this->getMockBuilder('\OC\User\User')
  209. ->disableOriginalConstructor()->getMock();
  210. $foo
  211. ->expects($this->exactly(4))
  212. ->method('getUID')
  213. ->will($this->returnValue('foo'));
  214. $foo
  215. ->expects($this->once())
  216. ->method('getDisplayName')
  217. ->will($this->returnValue('M. Foo'));
  218. $foo
  219. ->method('getLastLogin')
  220. ->will($this->returnValue(500));
  221. $foo
  222. ->method('getHome')
  223. ->will($this->returnValue('/home/foo'));
  224. $foo
  225. ->expects($this->once())
  226. ->method('getBackendClassName')
  227. ->will($this->returnValue('OC_User_Database'));
  228. $admin = $this->getMockBuilder('\OC\User\User')
  229. ->disableOriginalConstructor()->getMock();
  230. $admin
  231. ->expects($this->exactly(4))
  232. ->method('getUID')
  233. ->will($this->returnValue('admin'));
  234. $admin
  235. ->expects($this->once())
  236. ->method('getDisplayName')
  237. ->will($this->returnValue('S. Admin'));
  238. $admin
  239. ->expects($this->once())
  240. ->method('getLastLogin')
  241. ->will($this->returnValue(12));
  242. $admin
  243. ->expects($this->once())
  244. ->method('getHome')
  245. ->will($this->returnValue('/home/admin'));
  246. $admin
  247. ->expects($this->once())
  248. ->method('getBackendClassName')
  249. ->will($this->returnValue('OC_User_Dummy'));
  250. $bar = $this->getMockBuilder('\OC\User\User')
  251. ->disableOriginalConstructor()->getMock();
  252. $bar
  253. ->expects($this->exactly(4))
  254. ->method('getUID')
  255. ->will($this->returnValue('bar'));
  256. $bar
  257. ->expects($this->once())
  258. ->method('getDisplayName')
  259. ->will($this->returnValue('B. Ar'));
  260. $bar
  261. ->method('getLastLogin')
  262. ->will($this->returnValue(3999));
  263. $bar
  264. ->method('getHome')
  265. ->will($this->returnValue('/home/bar'));
  266. $bar
  267. ->expects($this->once())
  268. ->method('getBackendClassName')
  269. ->will($this->returnValue('OC_User_Dummy'));
  270. $this->container['GroupManager']
  271. ->expects($this->at(0))
  272. ->method('displayNamesInGroup')
  273. ->with('SubGroup1', 'pattern')
  274. ->will($this->returnValue(['foo' => 'M. Foo', 'admin' => 'S. Admin']));
  275. $this->container['GroupManager']
  276. ->expects($this->at(1))
  277. ->method('displayNamesInGroup')
  278. ->with('SubGroup2', 'pattern')
  279. ->will($this->returnValue(['bar' => 'B. Ar']));
  280. $this->container['GroupManager']
  281. ->expects($this->exactly(3))
  282. ->method('getUserGroupIds')
  283. ->will($this->onConsecutiveCalls(
  284. ['SubGroup2', 'SubGroup1'],
  285. ['SubGroup2', 'Foo'],
  286. ['admin', 'SubGroup1', 'testGroup']
  287. ));
  288. $this->container['UserManager']
  289. ->expects($this->at(0))
  290. ->method('get')
  291. ->with('foo')
  292. ->will($this->returnValue($foo));
  293. $this->container['UserManager']
  294. ->expects($this->at(1))
  295. ->method('get')
  296. ->with('admin')
  297. ->will($this->returnValue($admin));
  298. $this->container['UserManager']
  299. ->expects($this->at(2))
  300. ->method('get')
  301. ->with('bar')
  302. ->will($this->returnValue($bar));
  303. $this->container['Config']
  304. ->expects($this->exactly(6))
  305. ->method('getUserValue')
  306. ->will($this->onConsecutiveCalls(
  307. 1024, 'foo@bar.com',
  308. 404, 'admin@bar.com',
  309. 2323, 'bar@dummy.com'
  310. ));
  311. $expectedResponse = new DataResponse(
  312. [
  313. 0 => [
  314. 'name' => 'foo',
  315. 'displayname' => 'M. Foo',
  316. 'groups' => ['SubGroup2', 'SubGroup1'],
  317. 'subadmin' => [],
  318. 'quota' => 1024,
  319. 'storageLocation' => '/home/foo',
  320. 'lastLogin' => 500000,
  321. 'backend' => 'OC_User_Database',
  322. 'email' => 'foo@bar.com',
  323. 'isRestoreDisabled' => false,
  324. ],
  325. 1 => [
  326. 'name' => 'admin',
  327. 'displayname' => 'S. Admin',
  328. 'groups' => ['SubGroup2'],
  329. 'subadmin' => [],
  330. 'quota' => 404,
  331. 'storageLocation' => '/home/admin',
  332. 'lastLogin' => 12000,
  333. 'backend' => 'OC_User_Dummy',
  334. 'email' => 'admin@bar.com',
  335. 'isRestoreDisabled' => false,
  336. ],
  337. 2 => [
  338. 'name' => 'bar',
  339. 'displayname' => 'B. Ar',
  340. 'groups' => ['SubGroup1'],
  341. 'subadmin' => [],
  342. 'quota' => 2323,
  343. 'storageLocation' => '/home/bar',
  344. 'lastLogin' => 3999000,
  345. 'backend' => 'OC_User_Dummy',
  346. 'email' => 'bar@dummy.com',
  347. 'isRestoreDisabled' => false,
  348. ],
  349. ]
  350. );
  351. $response = $this->container['UsersController']->index(0, 10, '', 'pattern');
  352. $this->assertEquals($expectedResponse, $response);
  353. }
  354. /**
  355. * TODO: Since the function uses the static OC_Subadmin class it can't be mocked
  356. * to test for subadmins. Thus the test always assumes you have admin permissions...
  357. */
  358. public function testIndexWithSearch() {
  359. $this->container['IsAdmin'] = true;
  360. $foo = $this->getMockBuilder('\OC\User\User')
  361. ->disableOriginalConstructor()->getMock();
  362. $foo
  363. ->expects($this->exactly(4))
  364. ->method('getUID')
  365. ->will($this->returnValue('foo'));
  366. $foo
  367. ->expects($this->once())
  368. ->method('getDisplayName')
  369. ->will($this->returnValue('M. Foo'));
  370. $foo
  371. ->method('getLastLogin')
  372. ->will($this->returnValue(500));
  373. $foo
  374. ->method('getHome')
  375. ->will($this->returnValue('/home/foo'));
  376. $foo
  377. ->expects($this->once())
  378. ->method('getBackendClassName')
  379. ->will($this->returnValue('OC_User_Database'));
  380. $admin = $this->getMockBuilder('\OC\User\User')
  381. ->disableOriginalConstructor()->getMock();
  382. $admin
  383. ->expects($this->exactly(4))
  384. ->method('getUID')
  385. ->will($this->returnValue('admin'));
  386. $admin
  387. ->expects($this->once())
  388. ->method('getDisplayName')
  389. ->will($this->returnValue('S. Admin'));
  390. $admin
  391. ->expects($this->once())
  392. ->method('getLastLogin')
  393. ->will($this->returnValue(12));
  394. $admin
  395. ->expects($this->once())
  396. ->method('getHome')
  397. ->will($this->returnValue('/home/admin'));
  398. $admin
  399. ->expects($this->once())
  400. ->method('getBackendClassName')
  401. ->will($this->returnValue('OC_User_Dummy'));
  402. $bar = $this->getMockBuilder('\OC\User\User')
  403. ->disableOriginalConstructor()->getMock();
  404. $bar
  405. ->expects($this->exactly(4))
  406. ->method('getUID')
  407. ->will($this->returnValue('bar'));
  408. $bar
  409. ->expects($this->once())
  410. ->method('getDisplayName')
  411. ->will($this->returnValue('B. Ar'));
  412. $bar
  413. ->method('getLastLogin')
  414. ->will($this->returnValue(3999));
  415. $bar
  416. ->method('getHome')
  417. ->will($this->returnValue('/home/bar'));
  418. $bar
  419. ->expects($this->once())
  420. ->method('getBackendClassName')
  421. ->will($this->returnValue('OC_User_Dummy'));
  422. $this->container['UserManager']
  423. ->expects($this->once())
  424. ->method('search')
  425. ->with('pattern', 10, 0)
  426. ->will($this->returnValue([$foo, $admin, $bar]));
  427. $this->container['GroupManager']
  428. ->expects($this->exactly(3))
  429. ->method('getUserGroupIds')
  430. ->will($this->onConsecutiveCalls(array('Users', 'Support'), array('admins', 'Support'), array('External Users')));
  431. $this->container['Config']
  432. ->expects($this->exactly(6))
  433. ->method('getUserValue')
  434. ->will($this->onConsecutiveCalls(1024, 'foo@bar.com',
  435. 404, 'admin@bar.com',
  436. 2323, 'bar@dummy.com'));
  437. $expectedResponse = new DataResponse(
  438. array(
  439. 0 => array(
  440. 'name' => 'foo',
  441. 'displayname' => 'M. Foo',
  442. 'groups' => array('Users', 'Support'),
  443. 'subadmin' => array(),
  444. 'quota' => 1024,
  445. 'storageLocation' => '/home/foo',
  446. 'lastLogin' => 500000,
  447. 'backend' => 'OC_User_Database',
  448. 'email' => 'foo@bar.com',
  449. 'isRestoreDisabled' => false,
  450. ),
  451. 1 => array(
  452. 'name' => 'admin',
  453. 'displayname' => 'S. Admin',
  454. 'groups' => array('admins', 'Support'),
  455. 'subadmin' => array(),
  456. 'quota' => 404,
  457. 'storageLocation' => '/home/admin',
  458. 'lastLogin' => 12000,
  459. 'backend' => 'OC_User_Dummy',
  460. 'email' => 'admin@bar.com',
  461. 'isRestoreDisabled' => false,
  462. ),
  463. 2 => array(
  464. 'name' => 'bar',
  465. 'displayname' => 'B. Ar',
  466. 'groups' => array('External Users'),
  467. 'subadmin' => array(),
  468. 'quota' => 2323,
  469. 'storageLocation' => '/home/bar',
  470. 'lastLogin' => 3999000,
  471. 'backend' => 'OC_User_Dummy',
  472. 'email' => 'bar@dummy.com',
  473. 'isRestoreDisabled' => false,
  474. ),
  475. )
  476. );
  477. $response = $this->container['UsersController']->index(0, 10, '', 'pattern');
  478. $this->assertEquals($expectedResponse, $response);
  479. }
  480. public function testIndexWithBackend() {
  481. $this->container['IsAdmin'] = true;
  482. $user = $this->getMockBuilder('\OC\User\User')
  483. ->disableOriginalConstructor()->getMock();
  484. $user
  485. ->expects($this->exactly(4))
  486. ->method('getUID')
  487. ->will($this->returnValue('foo'));
  488. $user
  489. ->expects($this->once())
  490. ->method('getDisplayName')
  491. ->will($this->returnValue('M. Foo'));
  492. $user
  493. ->method('getLastLogin')
  494. ->will($this->returnValue(500));
  495. $user
  496. ->method('getHome')
  497. ->will($this->returnValue('/home/foo'));
  498. $user
  499. ->expects($this->once())
  500. ->method('getBackendClassName')
  501. ->will($this->returnValue('OC_User_Database'));
  502. $this->container['UserManager']
  503. ->expects($this->once())
  504. ->method('getBackends')
  505. ->will($this->returnValue([new \OC_User_Dummy(), new \OC_User_Database()]));
  506. $this->container['UserManager']
  507. ->expects($this->once())
  508. ->method('clearBackends');
  509. $this->container['UserManager']
  510. ->expects($this->once())
  511. ->method('registerBackend')
  512. ->with(new \OC_User_Dummy());
  513. $this->container['UserManager']
  514. ->expects($this->once())
  515. ->method('search')
  516. ->with('')
  517. ->will($this->returnValue([$user]));
  518. $expectedResponse = new DataResponse(
  519. array(
  520. 0 => array(
  521. 'name' => 'foo',
  522. 'displayname' => 'M. Foo',
  523. 'groups' => null,
  524. 'subadmin' => array(),
  525. 'quota' => null,
  526. 'storageLocation' => '/home/foo',
  527. 'lastLogin' => 500000,
  528. 'backend' => 'OC_User_Database',
  529. 'email' => null,
  530. 'isRestoreDisabled' => false,
  531. )
  532. )
  533. );
  534. $response = $this->container['UsersController']->index(0, 10, '','', 'OC_User_Dummy');
  535. $this->assertEquals($expectedResponse, $response);
  536. }
  537. public function testIndexWithBackendNoUser() {
  538. $this->container['IsAdmin'] = true;
  539. $this->container['UserManager']
  540. ->expects($this->once())
  541. ->method('getBackends')
  542. ->will($this->returnValue([new \OC_User_Dummy(), new \OC_User_Database()]));
  543. $this->container['UserManager']
  544. ->expects($this->once())
  545. ->method('search')
  546. ->with('')
  547. ->will($this->returnValue([]));
  548. $expectedResponse = new DataResponse([]);
  549. $response = $this->container['UsersController']->index(0, 10, '','', 'OC_User_Dummy');
  550. $this->assertEquals($expectedResponse, $response);
  551. }
  552. public function testCreateSuccessfulWithoutGroupAdmin() {
  553. $this->container['IsAdmin'] = true;
  554. $user = $this->getMockBuilder('\OC\User\User')
  555. ->disableOriginalConstructor()->getMock();
  556. $user
  557. ->method('getHome')
  558. ->will($this->returnValue('/home/user'));
  559. $user
  560. ->method('getUID')
  561. ->will($this->returnValue('foo'));
  562. $user
  563. ->expects($this->once())
  564. ->method('getBackendClassName')
  565. ->will($this->returnValue('bar'));
  566. $this->container['UserManager']
  567. ->expects($this->once())
  568. ->method('createUser')
  569. ->will($this->onConsecutiveCalls($user));
  570. $expectedResponse = new DataResponse(
  571. array(
  572. 'name' => 'foo',
  573. 'groups' => null,
  574. 'storageLocation' => '/home/user',
  575. 'backend' => 'bar',
  576. 'lastLogin' => null,
  577. 'displayname' => null,
  578. 'quota' => null,
  579. 'subadmin' => array(),
  580. 'email' => null,
  581. 'isRestoreDisabled' => false,
  582. ),
  583. Http::STATUS_CREATED
  584. );
  585. $response = $this->container['UsersController']->create('foo', 'password', array());
  586. $this->assertEquals($expectedResponse, $response);
  587. }
  588. public function testCreateSuccessfulWithoutGroupSubAdmin() {
  589. $this->container['IsAdmin'] = false;
  590. $this->container['SubAdminFactory']
  591. ->expects($this->once())
  592. ->method('getSubAdminsOfGroups')
  593. ->with('username')
  594. ->will($this->returnValue(['SubGroup1', 'SubGroup2']));
  595. $user = $this->getMockBuilder('\OC\User\User')
  596. ->disableOriginalConstructor()->getMock();
  597. $user
  598. ->expects($this->once())
  599. ->method('getUID')
  600. ->will($this->returnValue('username'));
  601. $this->container['UserSession']
  602. ->expects($this->once())
  603. ->method('getUser')
  604. ->will($this->returnValue($user));
  605. $user = $this->getMockBuilder('\OC\User\User')
  606. ->disableOriginalConstructor()->getMock();
  607. $user
  608. ->method('getHome')
  609. ->will($this->returnValue('/home/user'));
  610. $user
  611. ->method('getHome')
  612. ->will($this->returnValue('/home/user'));
  613. $user
  614. ->method('getUID')
  615. ->will($this->returnValue('foo'));
  616. $user
  617. ->expects($this->once())
  618. ->method('getBackendClassName')
  619. ->will($this->returnValue('bar'));
  620. $subGroup1 = $this->getMockBuilder('\OCP\IGroup')
  621. ->disableOriginalConstructor()->getMock();
  622. $subGroup1
  623. ->expects($this->once())
  624. ->method('addUser')
  625. ->with($user);
  626. $subGroup2 = $this->getMockBuilder('\OCP\IGroup')
  627. ->disableOriginalConstructor()->getMock();
  628. $subGroup2
  629. ->expects($this->once())
  630. ->method('addUser')
  631. ->with($user);
  632. $this->container['UserManager']
  633. ->expects($this->once())
  634. ->method('createUser')
  635. ->will($this->onConsecutiveCalls($user));
  636. $this->container['GroupManager']
  637. ->expects($this->exactly(2))
  638. ->method('get')
  639. ->will($this->onConsecutiveCalls($subGroup1, $subGroup2));
  640. $this->container['GroupManager']
  641. ->expects($this->once())
  642. ->method('getUserGroupIds')
  643. ->with($user)
  644. ->will($this->onConsecutiveCalls(['SubGroup1', 'SubGroup2']));
  645. $expectedResponse = new DataResponse(
  646. array(
  647. 'name' => 'foo',
  648. 'groups' => ['SubGroup1', 'SubGroup2'],
  649. 'storageLocation' => '/home/user',
  650. 'backend' => 'bar',
  651. 'lastLogin' => null,
  652. 'displayname' => null,
  653. 'quota' => null,
  654. 'subadmin' => [],
  655. 'email' => null,
  656. 'isRestoreDisabled' => false,
  657. ),
  658. Http::STATUS_CREATED
  659. );
  660. $response = $this->container['UsersController']->create('foo', 'password');
  661. $this->assertEquals($expectedResponse, $response);
  662. }
  663. public function testCreateSuccessfulWithGroupAdmin() {
  664. $this->container['IsAdmin'] = true;
  665. $user = $this->getMockBuilder('\OC\User\User')
  666. ->disableOriginalConstructor()->getMock();
  667. $user
  668. ->method('getHome')
  669. ->will($this->returnValue('/home/user'));
  670. $user
  671. ->method('getHome')
  672. ->will($this->returnValue('/home/user'));
  673. $user
  674. ->method('getUID')
  675. ->will($this->returnValue('foo'));
  676. $user
  677. ->expects($this->once())
  678. ->method('getBackendClassName')
  679. ->will($this->returnValue('bar'));
  680. $existingGroup = $this->getMockBuilder('\OCP\IGroup')
  681. ->disableOriginalConstructor()->getMock();
  682. $existingGroup
  683. ->expects($this->once())
  684. ->method('addUser')
  685. ->with($user);
  686. $newGroup = $this->getMockBuilder('\OCP\IGroup')
  687. ->disableOriginalConstructor()->getMock();
  688. $newGroup
  689. ->expects($this->once())
  690. ->method('addUser')
  691. ->with($user);
  692. $this->container['UserManager']
  693. ->expects($this->once())
  694. ->method('createUser')
  695. ->will($this->onConsecutiveCalls($user));
  696. $this->container['GroupManager']
  697. ->expects($this->exactly(2))
  698. ->method('get')
  699. ->will($this->onConsecutiveCalls(null, $existingGroup));
  700. $this->container['GroupManager']
  701. ->expects($this->once())
  702. ->method('createGroup')
  703. ->with('NewGroup')
  704. ->will($this->onConsecutiveCalls($newGroup));
  705. $this->container['GroupManager']
  706. ->expects($this->once())
  707. ->method('getUserGroupIds')
  708. ->with($user)
  709. ->will($this->onConsecutiveCalls(array('NewGroup', 'ExistingGroup')));
  710. $expectedResponse = new DataResponse(
  711. array(
  712. 'name' => 'foo',
  713. 'groups' => array('NewGroup', 'ExistingGroup'),
  714. 'storageLocation' => '/home/user',
  715. 'backend' => 'bar',
  716. 'lastLogin' => null,
  717. 'displayname' => null,
  718. 'quota' => null,
  719. 'subadmin' => array(),
  720. 'email' => null,
  721. 'isRestoreDisabled' => false,
  722. ),
  723. Http::STATUS_CREATED
  724. );
  725. $response = $this->container['UsersController']->create('foo', 'password', array('NewGroup', 'ExistingGroup'));
  726. $this->assertEquals($expectedResponse, $response);
  727. }
  728. public function testCreateSuccessfulWithGroupSubAdmin() {
  729. $this->container['IsAdmin'] = false;
  730. $this->container['SubAdminFactory']
  731. ->expects($this->once())
  732. ->method('getSubAdminsOfGroups')
  733. ->with('username')
  734. ->will($this->returnValue(['SubGroup1', 'SubGroup2']));
  735. $user = $this->getMockBuilder('\OC\User\User')
  736. ->disableOriginalConstructor()->getMock();
  737. $user
  738. ->expects($this->once())
  739. ->method('getUID')
  740. ->will($this->returnValue('username'));
  741. $this->container['UserSession']
  742. ->expects($this->once())
  743. ->method('getUser')
  744. ->will($this->returnValue($user));
  745. $user = $this->getMockBuilder('\OC\User\User')
  746. ->disableOriginalConstructor()->getMock();
  747. $user
  748. ->method('getHome')
  749. ->will($this->returnValue('/home/user'));
  750. $user
  751. ->method('getHome')
  752. ->will($this->returnValue('/home/user'));
  753. $user
  754. ->method('getUID')
  755. ->will($this->returnValue('foo'));
  756. $user
  757. ->expects($this->once())
  758. ->method('getBackendClassName')
  759. ->will($this->returnValue('bar'));
  760. $subGroup1 = $this->getMockBuilder('\OCP\IGroup')
  761. ->disableOriginalConstructor()->getMock();
  762. $subGroup1
  763. ->expects($this->once())
  764. ->method('addUser')
  765. ->with($user);
  766. $subGroup2 = $this->getMockBuilder('\OCP\IGroup')
  767. ->disableOriginalConstructor()->getMock();
  768. $subGroup2
  769. ->expects($this->once())
  770. ->method('addUser')
  771. ->with($user);
  772. $this->container['UserManager']
  773. ->expects($this->once())
  774. ->method('createUser')
  775. ->will($this->onConsecutiveCalls($user));
  776. $this->container['GroupManager']
  777. ->expects($this->exactly(2))
  778. ->method('get')
  779. ->will($this->onConsecutiveCalls($subGroup1, $subGroup2));
  780. $this->container['GroupManager']
  781. ->expects($this->once())
  782. ->method('getUserGroupIds')
  783. ->with($user)
  784. ->will($this->onConsecutiveCalls(['SubGroup1']));
  785. $expectedResponse = new DataResponse(
  786. array(
  787. 'name' => 'foo',
  788. 'groups' => ['SubGroup1'],
  789. 'storageLocation' => '/home/user',
  790. 'backend' => 'bar',
  791. 'lastLogin' => null,
  792. 'displayname' => null,
  793. 'quota' => null,
  794. 'subadmin' => [],
  795. 'email' => null,
  796. 'isRestoreDisabled' => false,
  797. ),
  798. Http::STATUS_CREATED
  799. );
  800. $response = $this->container['UsersController']->create('foo', 'password', ['SubGroup1', 'ExistingGroup']);
  801. $this->assertEquals($expectedResponse, $response);
  802. }
  803. public function testCreateUnsuccessfulAdmin() {
  804. $this->container['IsAdmin'] = true;
  805. $this->container['UserManager']
  806. ->method('createUser')
  807. ->will($this->throwException(new \Exception()));
  808. $expectedResponse = new DataResponse(
  809. array(
  810. 'message' => 'Unable to create user.'
  811. ),
  812. Http::STATUS_FORBIDDEN
  813. );
  814. $response = $this->container['UsersController']->create('foo', 'password', array());
  815. $this->assertEquals($expectedResponse, $response);
  816. }
  817. public function testCreateUnsuccessfulSubAdmin() {
  818. $this->container['IsAdmin'] = false;
  819. $this->container['SubAdminFactory']
  820. ->expects($this->once())
  821. ->method('getSubAdminsOfGroups')
  822. ->with('username')
  823. ->will($this->returnValue(['SubGroup1', 'SubGroup2']));
  824. $user = $this->getMockBuilder('\OC\User\User')
  825. ->disableOriginalConstructor()->getMock();
  826. $user
  827. ->expects($this->once())
  828. ->method('getUID')
  829. ->will($this->returnValue('username'));
  830. $this->container['UserSession']
  831. ->expects($this->once())
  832. ->method('getUser')
  833. ->will($this->returnValue($user));
  834. $this->container['UserManager']
  835. ->method('createUser')
  836. ->will($this->throwException(new \Exception()));
  837. $expectedResponse = new DataResponse(
  838. [
  839. 'message' => 'Unable to create user.'
  840. ],
  841. Http::STATUS_FORBIDDEN
  842. );
  843. $response = $this->container['UsersController']->create('foo', 'password', array());
  844. $this->assertEquals($expectedResponse, $response);
  845. }
  846. public function testDestroySelfAdmin() {
  847. $this->container['IsAdmin'] = true;
  848. $user = $this->getMockBuilder('\OC\User\User')
  849. ->disableOriginalConstructor()->getMock();
  850. $user
  851. ->expects($this->once())
  852. ->method('getUID')
  853. ->will($this->returnValue('myself'));
  854. $this->container['UserSession']
  855. ->method('getUser')
  856. ->will($this->returnValue($user));
  857. $expectedResponse = new DataResponse(
  858. array(
  859. 'status' => 'error',
  860. 'data' => array(
  861. 'message' => 'Unable to delete user.'
  862. )
  863. ),
  864. Http::STATUS_FORBIDDEN
  865. );
  866. $response = $this->container['UsersController']->destroy('myself');
  867. $this->assertEquals($expectedResponse, $response);
  868. }
  869. public function testDestroySelfSubadmin() {
  870. $this->container['IsAdmin'] = false;
  871. $user = $this->getMockBuilder('\OC\User\User')
  872. ->disableOriginalConstructor()->getMock();
  873. $user
  874. ->expects($this->once())
  875. ->method('getUID')
  876. ->will($this->returnValue('myself'));
  877. $this->container['UserSession']
  878. ->method('getUser')
  879. ->will($this->returnValue($user));
  880. $expectedResponse = new DataResponse(
  881. array(
  882. 'status' => 'error',
  883. 'data' => array(
  884. 'message' => 'Unable to delete user.'
  885. )
  886. ),
  887. Http::STATUS_FORBIDDEN
  888. );
  889. $response = $this->container['UsersController']->destroy('myself');
  890. $this->assertEquals($expectedResponse, $response);
  891. }
  892. public function testDestroyAdmin() {
  893. $this->container['IsAdmin'] = true;
  894. $user = $this->getMockBuilder('\OC\User\User')
  895. ->disableOriginalConstructor()->getMock();
  896. $user
  897. ->expects($this->once())
  898. ->method('getUID')
  899. ->will($this->returnValue('Admin'));
  900. $toDeleteUser = $this->getMockBuilder('\OC\User\User')
  901. ->disableOriginalConstructor()->getMock();
  902. $toDeleteUser
  903. ->expects($this->once())
  904. ->method('delete')
  905. ->will($this->returnValue(true));
  906. $this->container['UserSession']
  907. ->method('getUser')
  908. ->will($this->returnValue($user));
  909. $this->container['UserManager']
  910. ->method('get')
  911. ->with('UserToDelete')
  912. ->will($this->returnValue($toDeleteUser));
  913. $expectedResponse = new DataResponse(
  914. array(
  915. 'status' => 'success',
  916. 'data' => array(
  917. 'username' => 'UserToDelete'
  918. )
  919. ),
  920. Http::STATUS_NO_CONTENT
  921. );
  922. $response = $this->container['UsersController']->destroy('UserToDelete');
  923. $this->assertEquals($expectedResponse, $response);
  924. }
  925. public function testDestroySubAdmin() {
  926. $this->container['IsAdmin'] = false;
  927. $this->container['SubAdminFactory']
  928. ->expects($this->once())
  929. ->method('isUserAccessible')
  930. ->with('myself', 'UserToDelete')
  931. ->will($this->returnValue(true));
  932. $user = $this->getMockBuilder('\OC\User\User')
  933. ->disableOriginalConstructor()->getMock();
  934. $user
  935. ->expects($this->once())
  936. ->method('getUID')
  937. ->will($this->returnValue('myself'));
  938. $this->container['UserSession']
  939. ->method('getUser')
  940. ->will($this->returnValue($user));
  941. $user = $this->getMockBuilder('\OC\User\User')
  942. ->disableOriginalConstructor()->getMock();
  943. $toDeleteUser = $this->getMockBuilder('\OC\User\User')
  944. ->disableOriginalConstructor()->getMock();
  945. $toDeleteUser
  946. ->expects($this->once())
  947. ->method('delete')
  948. ->will($this->returnValue(true));
  949. $this->container['UserSession']
  950. ->method('getUser')
  951. ->will($this->returnValue($user));
  952. $this->container['UserManager']
  953. ->method('get')
  954. ->with('UserToDelete')
  955. ->will($this->returnValue($toDeleteUser));
  956. $expectedResponse = new DataResponse(
  957. [
  958. 'status' => 'success',
  959. 'data' => [
  960. 'username' => 'UserToDelete'
  961. ]
  962. ],
  963. Http::STATUS_NO_CONTENT
  964. );
  965. $response = $this->container['UsersController']->destroy('UserToDelete');
  966. $this->assertEquals($expectedResponse, $response);
  967. }
  968. public function testDestroyUnsuccessfulAdmin() {
  969. $this->container['IsAdmin'] = true;
  970. $user = $this->getMockBuilder('\OC\User\User')
  971. ->disableOriginalConstructor()->getMock();
  972. $user
  973. ->expects($this->once())
  974. ->method('getUID')
  975. ->will($this->returnValue('Admin'));
  976. $toDeleteUser = $this->getMockBuilder('\OC\User\User')
  977. ->disableOriginalConstructor()->getMock();
  978. $toDeleteUser
  979. ->expects($this->once())
  980. ->method('delete')
  981. ->will($this->returnValue(false));
  982. $this->container['UserSession']
  983. ->method('getUser')
  984. ->will($this->returnValue($user));
  985. $this->container['UserManager']
  986. ->method('get')
  987. ->with('UserToDelete')
  988. ->will($this->returnValue($toDeleteUser));
  989. $expectedResponse = new DataResponse(
  990. array(
  991. 'status' => 'error',
  992. 'data' => array(
  993. 'message' => 'Unable to delete user.'
  994. )
  995. ),
  996. Http::STATUS_FORBIDDEN
  997. );
  998. $response = $this->container['UsersController']->destroy('UserToDelete');
  999. $this->assertEquals($expectedResponse, $response);
  1000. }
  1001. public function testDestroyUnsuccessfulSubAdmin() {
  1002. $this->container['IsAdmin'] = false;
  1003. $this->container['SubAdminFactory']
  1004. ->expects($this->once())
  1005. ->method('isUserAccessible')
  1006. ->with('myself', 'UserToDelete')
  1007. ->will($this->returnValue(true));
  1008. $user = $this->getMockBuilder('\OC\User\User')
  1009. ->disableOriginalConstructor()->getMock();
  1010. $user
  1011. ->expects($this->once())
  1012. ->method('getUID')
  1013. ->will($this->returnValue('myself'));
  1014. $this->container['UserSession']
  1015. ->method('getUser')
  1016. ->will($this->returnValue($user));
  1017. $toDeleteUser = $this->getMockBuilder('\OC\User\User')
  1018. ->disableOriginalConstructor()->getMock();
  1019. $toDeleteUser
  1020. ->expects($this->once())
  1021. ->method('delete')
  1022. ->will($this->returnValue(false));
  1023. $this->container['UserSession']
  1024. ->method('getUser')
  1025. ->will($this->returnValue($user));
  1026. $this->container['UserManager']
  1027. ->method('get')
  1028. ->with('UserToDelete')
  1029. ->will($this->returnValue($toDeleteUser));
  1030. $expectedResponse = new DataResponse(
  1031. [
  1032. 'status' => 'error',
  1033. 'data' => [
  1034. 'message' => 'Unable to delete user.'
  1035. ]
  1036. ],
  1037. Http::STATUS_FORBIDDEN
  1038. );
  1039. $response = $this->container['UsersController']->destroy('UserToDelete');
  1040. $this->assertEquals($expectedResponse, $response);
  1041. }
  1042. public function testDestroyNotAccessibleToSubAdmin() {
  1043. $this->container['IsAdmin'] = false;
  1044. $this->container['SubAdminFactory']
  1045. ->expects($this->once())
  1046. ->method('isUserAccessible')
  1047. ->with('myself', 'UserToDelete')
  1048. ->will($this->returnValue(false));
  1049. $user = $this->getMockBuilder('\OC\User\User')
  1050. ->disableOriginalConstructor()->getMock();
  1051. $user
  1052. ->expects($this->once())
  1053. ->method('getUID')
  1054. ->will($this->returnValue('myself'));
  1055. $this->container['UserSession']
  1056. ->method('getUser')
  1057. ->will($this->returnValue($user));
  1058. $toDeleteUser = $this->getMockBuilder('\OC\User\User')
  1059. ->disableOriginalConstructor()->getMock();
  1060. $this->container['UserSession']
  1061. ->method('getUser')
  1062. ->will($this->returnValue($user));
  1063. $this->container['UserManager']
  1064. ->method('get')
  1065. ->with('UserToDelete')
  1066. ->will($this->returnValue($toDeleteUser));
  1067. $expectedResponse = new DataResponse(
  1068. [
  1069. 'status' => 'error',
  1070. 'data' => [
  1071. 'message' => 'Authentication error'
  1072. ]
  1073. ],
  1074. Http::STATUS_FORBIDDEN
  1075. );
  1076. $response = $this->container['UsersController']->destroy('UserToDelete');
  1077. $this->assertEquals($expectedResponse, $response);
  1078. }
  1079. /**
  1080. * test if an invalid mail result in a failure response
  1081. */
  1082. public function testCreateUnsuccessfulWithInvalidEmailAdmin() {
  1083. $this->container['IsAdmin'] = true;
  1084. $expectedResponse = new DataResponse([
  1085. 'message' => 'Invalid mail address',
  1086. ],
  1087. Http::STATUS_UNPROCESSABLE_ENTITY
  1088. );
  1089. $response = $this->container['UsersController']->create('foo', 'password', [], 'invalidMailAdress');
  1090. $this->assertEquals($expectedResponse, $response);
  1091. }
  1092. /**
  1093. * test if a valid mail result in a successful mail send
  1094. */
  1095. public function testCreateSuccessfulWithValidEmailAdmin() {
  1096. $this->container['IsAdmin'] = true;
  1097. $message = $this->getMockBuilder('\OC\Mail\Message')
  1098. ->disableOriginalConstructor()->getMock();
  1099. $message
  1100. ->expects($this->at(0))
  1101. ->method('setTo')
  1102. ->with(['validMail@Adre.ss' => 'foo']);
  1103. $message
  1104. ->expects($this->at(1))
  1105. ->method('setSubject')
  1106. ->with('Your account was created');
  1107. $htmlBody = new Http\TemplateResponse(
  1108. 'settings',
  1109. 'email.new_user',
  1110. [
  1111. 'username' => 'foo',
  1112. 'url' => '',
  1113. ],
  1114. 'blank'
  1115. );
  1116. $message
  1117. ->expects($this->at(2))
  1118. ->method('setHtmlBody')
  1119. ->with($htmlBody->render());
  1120. $plainBody = new Http\TemplateResponse(
  1121. 'settings',
  1122. 'email.new_user_plain_text',
  1123. [
  1124. 'username' => 'foo',
  1125. 'url' => '',
  1126. ],
  1127. 'blank'
  1128. );
  1129. $message
  1130. ->expects($this->at(3))
  1131. ->method('setPlainBody')
  1132. ->with($plainBody->render());
  1133. $message
  1134. ->expects($this->at(4))
  1135. ->method('setFrom')
  1136. ->with(['no-reply@owncloud.com' => null]);
  1137. $this->container['Mailer']
  1138. ->expects($this->at(0))
  1139. ->method('validateMailAddress')
  1140. ->with('validMail@Adre.ss')
  1141. ->will($this->returnValue(true));
  1142. $this->container['Mailer']
  1143. ->expects($this->at(1))
  1144. ->method('createMessage')
  1145. ->will($this->returnValue($message));
  1146. $this->container['Mailer']
  1147. ->expects($this->at(2))
  1148. ->method('send')
  1149. ->with($message);
  1150. $user = $this->getMockBuilder('\OC\User\User')
  1151. ->disableOriginalConstructor()->getMock();
  1152. $user
  1153. ->method('getHome')
  1154. ->will($this->returnValue('/home/user'));
  1155. $user
  1156. ->method('getHome')
  1157. ->will($this->returnValue('/home/user'));
  1158. $user
  1159. ->method('getUID')
  1160. ->will($this->returnValue('foo'));
  1161. $user
  1162. ->expects($this->once())
  1163. ->method('getBackendClassName')
  1164. ->will($this->returnValue('bar'));
  1165. $this->container['UserManager']
  1166. ->expects($this->once())
  1167. ->method('createUser')
  1168. ->will($this->onConsecutiveCalls($user));
  1169. $response = $this->container['UsersController']->create('foo', 'password', [], 'validMail@Adre.ss');
  1170. $this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
  1171. }
  1172. private function mockUser($userId = 'foo', $displayName = 'M. Foo',
  1173. $lastLogin = 500, $home = '/home/foo', $backend = 'OC_User_Database') {
  1174. $user = $this->getMockBuilder('\OC\User\User')
  1175. ->disableOriginalConstructor()->getMock();
  1176. $user
  1177. ->expects($this->any())
  1178. ->method('getUID')
  1179. ->will($this->returnValue($userId));
  1180. $user
  1181. ->expects($this->once())
  1182. ->method('getDisplayName')
  1183. ->will($this->returnValue($displayName));
  1184. $user
  1185. ->method('getLastLogin')
  1186. ->will($this->returnValue($lastLogin));
  1187. $user
  1188. ->method('getHome')
  1189. ->will($this->returnValue($home));
  1190. $user
  1191. ->expects($this->once())
  1192. ->method('getBackendClassName')
  1193. ->will($this->returnValue($backend));
  1194. $result = [
  1195. 'name' => $userId,
  1196. 'displayname' => $displayName,
  1197. 'groups' => null,
  1198. 'subadmin' => array(),
  1199. 'quota' => null,
  1200. 'storageLocation' => $home,
  1201. 'lastLogin' => $lastLogin * 1000,
  1202. 'backend' => $backend,
  1203. 'email' => null,
  1204. 'isRestoreDisabled' => false,
  1205. ];
  1206. return [$user, $result];
  1207. }
  1208. public function testRestorePossibleWithoutEncryption() {
  1209. $this->container['IsAdmin'] = true;
  1210. list($user, $expectedResult) = $this->mockUser();
  1211. $result = \Test_Helper::invokePrivate($this->container['UsersController'], 'formatUserForIndex', [$user]);
  1212. $this->assertEquals($expectedResult, $result);
  1213. }
  1214. public function testRestorePossibleWithAdminAndUserRestore() {
  1215. $this->container['IsAdmin'] = true;
  1216. list($user, $expectedResult) = $this->mockUser();
  1217. $this->container['OCP\\App\\IAppManager']
  1218. ->expects($this->once())
  1219. ->method('isEnabledForUser')
  1220. ->with(
  1221. $this->equalTo('files_encryption')
  1222. )
  1223. ->will($this->returnValue(true));
  1224. $this->container['Config']
  1225. ->expects($this->once())
  1226. ->method('getAppValue')
  1227. ->with(
  1228. $this->equalTo('files_encryption'),
  1229. $this->equalTo('recoveryAdminEnabled'),
  1230. $this->anything()
  1231. )
  1232. ->will($this->returnValue('1'));
  1233. $this->container['Config']
  1234. ->expects($this->at(1))
  1235. ->method('getUserValue')
  1236. ->with(
  1237. $this->anything(),
  1238. $this->equalTo('files_encryption'),
  1239. $this->equalTo('recovery_enabled'),
  1240. $this->anything()
  1241. )
  1242. ->will($this->returnValue('1'));
  1243. $result = \Test_Helper::invokePrivate($this->container['UsersController'], 'formatUserForIndex', [$user]);
  1244. $this->assertEquals($expectedResult, $result);
  1245. }
  1246. public function testRestoreNotPossibleWithoutAdminRestore() {
  1247. $this->container['IsAdmin'] = true;
  1248. list($user, $expectedResult) = $this->mockUser();
  1249. $this->container['OCP\\App\\IAppManager']
  1250. ->method('isEnabledForUser')
  1251. ->with(
  1252. $this->equalTo('files_encryption')
  1253. )
  1254. ->will($this->returnValue(true));
  1255. $expectedResult['isRestoreDisabled'] = true;
  1256. $result = \Test_Helper::invokePrivate($this->container['UsersController'], 'formatUserForIndex', [$user]);
  1257. $this->assertEquals($expectedResult, $result);
  1258. }
  1259. public function testRestoreNotPossibleWithoutUserRestore() {
  1260. $this->container['IsAdmin'] = true;
  1261. list($user, $expectedResult) = $this->mockUser();
  1262. $this->container['OCP\\App\\IAppManager']
  1263. ->expects($this->once())
  1264. ->method('isEnabledForUser')
  1265. ->with(
  1266. $this->equalTo('files_encryption')
  1267. )
  1268. ->will($this->returnValue(true));
  1269. $this->container['Config']
  1270. ->expects($this->once())
  1271. ->method('getAppValue')
  1272. ->with(
  1273. $this->equalTo('files_encryption'),
  1274. $this->equalTo('recoveryAdminEnabled'),
  1275. $this->anything()
  1276. )
  1277. ->will($this->returnValue('1'));
  1278. $this->container['Config']
  1279. ->expects($this->at(1))
  1280. ->method('getUserValue')
  1281. ->with(
  1282. $this->anything(),
  1283. $this->equalTo('files_encryption'),
  1284. $this->equalTo('recovery_enabled'),
  1285. $this->anything()
  1286. )
  1287. ->will($this->returnValue('0'));
  1288. $expectedResult['isRestoreDisabled'] = true;
  1289. $result = \Test_Helper::invokePrivate($this->container['UsersController'], 'formatUserForIndex', [$user]);
  1290. $this->assertEquals($expectedResult, $result);
  1291. }
  1292. }