UsersControllerTest.php 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080
  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 Tests\Settings\Controller;
  11. use OC\Group\Manager;
  12. use OC\Settings\Controller\UsersController;
  13. use OCP\App\IAppManager;
  14. use OCP\AppFramework\Http;
  15. use OCP\AppFramework\Http\DataResponse;
  16. use OCP\IAvatar;
  17. use OCP\IAvatarManager;
  18. use OCP\IConfig;
  19. use OCP\IGroupManager;
  20. use OCP\IL10N;
  21. use OCP\ILogger;
  22. use OCP\IRequest;
  23. use OCP\IURLGenerator;
  24. use OCP\IUser;
  25. use OCP\IUserManager;
  26. use OCP\IUserSession;
  27. use OCP\Mail\IMailer;
  28. /**
  29. * @group DB
  30. *
  31. * @package Tests\Settings\Controller
  32. */
  33. class UsersControllerTest extends \Test\TestCase {
  34. /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
  35. private $groupManager;
  36. /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
  37. private $userManager;
  38. /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
  39. private $userSession;
  40. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  41. private $config;
  42. /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
  43. private $logger;
  44. /** @var \OC_Defaults|\PHPUnit_Framework_MockObject_MockObject */
  45. private $defaults;
  46. /** @var IMailer|\PHPUnit_Framework_MockObject_MockObject */
  47. private $mailer;
  48. /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
  49. private $urlGenerator;
  50. /** @var IAppManager|\PHPUnit_Framework_MockObject_MockObject */
  51. private $appManager;
  52. /** @var IAvatarManager|\PHPUnit_Framework_MockObject_MockObject */
  53. private $avatarManager;
  54. /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
  55. private $l;
  56. protected function setUp() {
  57. parent::setUp();
  58. $this->groupManager = $this->createMock(Manager::class);
  59. $this->userManager = $this->createMock(IUserManager::class);
  60. $this->userSession = $this->createMock(IUserSession::class);
  61. $this->config = $this->createMock(IConfig::class);
  62. $this->logger = $this->createMock(ILogger::class);
  63. $this->defaults = $this->createMock(\OC_Defaults::class);
  64. $this->mailer = $this->createMock(IMailer::class);
  65. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  66. $this->appManager = $this->createMock(IAppManager::class);
  67. $this->avatarManager = $this->createMock(IAvatarManager::class);
  68. $this->l = $this->createMock(IL10N::class);
  69. $this->l->method('t')
  70. ->will($this->returnCallback(function ($text, $parameters = []) {
  71. return vsprintf($text, $parameters);
  72. }));
  73. /*
  74. * Set default avatar behaviour for whole test suite
  75. */
  76. $avatarExists = $this->createMock(IAvatar::class);
  77. $avatarExists->method('exists')->willReturn(true);
  78. $avatarNotExists = $this->createMock(IAvatar::class);
  79. $avatarNotExists->method('exists')->willReturn(false);
  80. $this->avatarManager->method('getAvatar')
  81. ->will($this->returnValueMap([
  82. ['foo', $avatarExists],
  83. ['bar', $avatarExists],
  84. ['admin', $avatarNotExists],
  85. ]));
  86. $this->config->method('getSystemValue')
  87. ->with('enable_avatars', true)
  88. ->willReturn(true);
  89. }
  90. /**
  91. * @param bool $isAdmin
  92. * @return UsersController
  93. */
  94. protected function getController($isAdmin) {
  95. return new UsersController(
  96. 'settings',
  97. $this->createMock(IRequest::class),
  98. $this->userManager,
  99. $this->groupManager,
  100. $this->userSession,
  101. $this->config,
  102. $isAdmin,
  103. $this->l,
  104. $this->logger,
  105. $this->defaults,
  106. $this->mailer,
  107. 'no-reply@owncloud.com',
  108. $this->urlGenerator,
  109. $this->appManager,
  110. $this->avatarManager
  111. );
  112. }
  113. public function testIndexAdmin() {
  114. $controller = $this->getController(true);
  115. $foo = $this->getMockBuilder('\OC\User\User')
  116. ->disableOriginalConstructor()->getMock();
  117. $foo
  118. ->expects($this->exactly(2))
  119. ->method('getUID')
  120. ->will($this->returnValue('foo'));
  121. $foo
  122. ->expects($this->once())
  123. ->method('getDisplayName')
  124. ->will($this->returnValue('M. Foo'));
  125. $foo
  126. ->expects($this->once())
  127. ->method('getEMailAddress')
  128. ->will($this->returnValue('foo@bar.com'));
  129. $foo
  130. ->expects($this->once())
  131. ->method('getQuota')
  132. ->will($this->returnValue('1024'));
  133. $foo
  134. ->method('getLastLogin')
  135. ->will($this->returnValue(500));
  136. $foo
  137. ->method('getHome')
  138. ->will($this->returnValue('/home/foo'));
  139. $foo
  140. ->expects($this->once())
  141. ->method('getBackendClassName')
  142. ->will($this->returnValue('OC_User_Database'));
  143. $admin = $this->getMockBuilder('\OC\User\User')
  144. ->disableOriginalConstructor()->getMock();
  145. $admin
  146. ->expects($this->exactly(2))
  147. ->method('getUID')
  148. ->will($this->returnValue('admin'));
  149. $admin
  150. ->expects($this->once())
  151. ->method('getDisplayName')
  152. ->will($this->returnValue('S. Admin'));
  153. $admin
  154. ->expects($this->once())
  155. ->method('getEMailAddress')
  156. ->will($this->returnValue('admin@bar.com'));
  157. $admin
  158. ->expects($this->once())
  159. ->method('getQuota')
  160. ->will($this->returnValue('404'));
  161. $admin
  162. ->expects($this->once())
  163. ->method('getLastLogin')
  164. ->will($this->returnValue(12));
  165. $admin
  166. ->expects($this->once())
  167. ->method('getHome')
  168. ->will($this->returnValue('/home/admin'));
  169. $admin
  170. ->expects($this->once())
  171. ->method('getBackendClassName')
  172. ->will($this->returnValue('\Test\Util\User\Dummy'));
  173. $bar = $this->getMockBuilder('\OC\User\User')
  174. ->disableOriginalConstructor()->getMock();
  175. $bar
  176. ->expects($this->exactly(2))
  177. ->method('getUID')
  178. ->will($this->returnValue('bar'));
  179. $bar
  180. ->expects($this->once())
  181. ->method('getDisplayName')
  182. ->will($this->returnValue('B. Ar'));
  183. $bar
  184. ->expects($this->once())
  185. ->method('getEMailAddress')
  186. ->will($this->returnValue('bar@dummy.com'));
  187. $bar
  188. ->expects($this->once())
  189. ->method('getQuota')
  190. ->will($this->returnValue('2323'));
  191. $bar
  192. ->method('getLastLogin')
  193. ->will($this->returnValue(3999));
  194. $bar
  195. ->method('getHome')
  196. ->will($this->returnValue('/home/bar'));
  197. $bar
  198. ->expects($this->once())
  199. ->method('getBackendClassName')
  200. ->will($this->returnValue('\Test\Util\User\Dummy'));
  201. $this->groupManager
  202. ->expects($this->once())
  203. ->method('displayNamesInGroup')
  204. ->with('gid', 'pattern')
  205. ->will($this->returnValue(array('foo' => 'M. Foo', 'admin' => 'S. Admin', 'bar' => 'B. Ar')));
  206. $this->groupManager
  207. ->expects($this->exactly(3))
  208. ->method('getUserGroupIds')
  209. ->will($this->onConsecutiveCalls(array('Users', 'Support'), array('admins', 'Support'), array('External Users')));
  210. $this->userManager
  211. ->expects($this->at(0))
  212. ->method('get')
  213. ->with('foo')
  214. ->will($this->returnValue($foo));
  215. $this->userManager
  216. ->expects($this->at(1))
  217. ->method('get')
  218. ->with('admin')
  219. ->will($this->returnValue($admin));
  220. $this->userManager
  221. ->expects($this->at(2))
  222. ->method('get')
  223. ->with('bar')
  224. ->will($this->returnValue($bar));
  225. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  226. ->disableOriginalConstructor()
  227. ->getMock();
  228. $subadmin
  229. ->expects($this->any())
  230. ->method('getSubAdminsGroups')
  231. ->with($foo)
  232. ->will($this->returnValue([]));
  233. $subadmin
  234. ->expects($this->any())
  235. ->method('getSubAdminsGroups')
  236. ->with($admin)
  237. ->will($this->returnValue([]));
  238. $subadmin
  239. ->expects($this->any())
  240. ->method('getSubAdminsGroups')
  241. ->with($bar)
  242. ->will($this->returnValue([]));
  243. $this->groupManager
  244. ->expects($this->any())
  245. ->method('getSubAdmin')
  246. ->will($this->returnValue($subadmin));
  247. $expectedResponse = new DataResponse(
  248. array(
  249. 0 => array(
  250. 'name' => 'foo',
  251. 'displayname' => 'M. Foo',
  252. 'groups' => array('Users', 'Support'),
  253. 'subadmin' => array(),
  254. 'quota' => 1024,
  255. 'storageLocation' => '/home/foo',
  256. 'lastLogin' => 500000,
  257. 'backend' => 'OC_User_Database',
  258. 'email' => 'foo@bar.com',
  259. 'isRestoreDisabled' => false,
  260. 'isAvatarAvailable' => true,
  261. ),
  262. 1 => array(
  263. 'name' => 'admin',
  264. 'displayname' => 'S. Admin',
  265. 'groups' => array('admins', 'Support'),
  266. 'subadmin' => array(),
  267. 'quota' => 404,
  268. 'storageLocation' => '/home/admin',
  269. 'lastLogin' => 12000,
  270. 'backend' => '\Test\Util\User\Dummy',
  271. 'email' => 'admin@bar.com',
  272. 'isRestoreDisabled' => false,
  273. 'isAvatarAvailable' => false,
  274. ),
  275. 2 => array(
  276. 'name' => 'bar',
  277. 'displayname' => 'B. Ar',
  278. 'groups' => array('External Users'),
  279. 'subadmin' => array(),
  280. 'quota' => 2323,
  281. 'storageLocation' => '/home/bar',
  282. 'lastLogin' => 3999000,
  283. 'backend' => '\Test\Util\User\Dummy',
  284. 'email' => 'bar@dummy.com',
  285. 'isRestoreDisabled' => false,
  286. 'isAvatarAvailable' => true,
  287. ),
  288. )
  289. );
  290. $response = $controller->index(0, 10, 'gid', 'pattern');
  291. $this->assertEquals($expectedResponse, $response);
  292. }
  293. public function testIndexSubAdmin() {
  294. $controller = $this->getController(false);
  295. $user = $this->getMockBuilder('\OC\User\User')
  296. ->disableOriginalConstructor()->getMock();
  297. $this->userSession
  298. ->expects($this->once())
  299. ->method('getUser')
  300. ->will($this->returnValue($user));
  301. $foo = $this->getMockBuilder('\OC\User\User')
  302. ->disableOriginalConstructor()->getMock();
  303. $foo
  304. ->expects($this->exactly(2))
  305. ->method('getUID')
  306. ->will($this->returnValue('foo'));
  307. $foo
  308. ->expects($this->once())
  309. ->method('getDisplayName')
  310. ->will($this->returnValue('M. Foo'));
  311. $foo
  312. ->expects($this->once())
  313. ->method('getEMailAddress')
  314. ->will($this->returnValue('foo@bar.com'));
  315. $foo
  316. ->expects($this->once())
  317. ->method('getQuota')
  318. ->will($this->returnValue('1024'));
  319. $foo
  320. ->method('getLastLogin')
  321. ->will($this->returnValue(500));
  322. $foo
  323. ->method('getHome')
  324. ->will($this->returnValue('/home/foo'));
  325. $foo
  326. ->expects($this->once())
  327. ->method('getBackendClassName')
  328. ->will($this->returnValue('OC_User_Database'));
  329. $admin = $this->getMockBuilder('\OC\User\User')
  330. ->disableOriginalConstructor()->getMock();
  331. $admin
  332. ->expects($this->exactly(2))
  333. ->method('getUID')
  334. ->will($this->returnValue('admin'));
  335. $admin
  336. ->expects($this->once())
  337. ->method('getDisplayName')
  338. ->will($this->returnValue('S. Admin'));
  339. $admin
  340. ->expects($this->once())
  341. ->method('getEMailAddress')
  342. ->will($this->returnValue('admin@bar.com'));
  343. $admin
  344. ->expects($this->once())
  345. ->method('getQuota')
  346. ->will($this->returnValue('404'));
  347. $admin
  348. ->expects($this->once())
  349. ->method('getLastLogin')
  350. ->will($this->returnValue(12));
  351. $admin
  352. ->expects($this->once())
  353. ->method('getHome')
  354. ->will($this->returnValue('/home/admin'));
  355. $admin
  356. ->expects($this->once())
  357. ->method('getBackendClassName')
  358. ->will($this->returnValue('\Test\Util\User\Dummy'));
  359. $bar = $this->getMockBuilder('\OC\User\User')
  360. ->disableOriginalConstructor()->getMock();
  361. $bar
  362. ->expects($this->exactly(2))
  363. ->method('getUID')
  364. ->will($this->returnValue('bar'));
  365. $bar
  366. ->expects($this->once())
  367. ->method('getDisplayName')
  368. ->will($this->returnValue('B. Ar'));
  369. $bar
  370. ->expects($this->once())
  371. ->method('getEMailAddress')
  372. ->will($this->returnValue('bar@dummy.com'));
  373. $bar
  374. ->expects($this->once())
  375. ->method('getQuota')
  376. ->will($this->returnValue('2323'));
  377. $bar
  378. ->method('getLastLogin')
  379. ->will($this->returnValue(3999));
  380. $bar
  381. ->method('getHome')
  382. ->will($this->returnValue('/home/bar'));
  383. $bar
  384. ->expects($this->once())
  385. ->method('getBackendClassName')
  386. ->will($this->returnValue('\Test\Util\User\Dummy'));
  387. $this->groupManager
  388. ->expects($this->at(2))
  389. ->method('displayNamesInGroup')
  390. ->with('SubGroup2', 'pattern')
  391. ->will($this->returnValue(['foo' => 'M. Foo', 'admin' => 'S. Admin']));
  392. $this->groupManager
  393. ->expects($this->at(1))
  394. ->method('displayNamesInGroup')
  395. ->with('SubGroup1', 'pattern')
  396. ->will($this->returnValue(['bar' => 'B. Ar']));
  397. $this->groupManager
  398. ->expects($this->exactly(3))
  399. ->method('getUserGroupIds')
  400. ->will($this->onConsecutiveCalls(
  401. ['admin', 'SubGroup1', 'testGroup'],
  402. ['SubGroup2', 'SubGroup1'],
  403. ['SubGroup2', 'Foo']
  404. ));
  405. $this->userManager
  406. ->expects($this->at(0))
  407. ->method('get')
  408. ->with('bar')
  409. ->will($this->returnValue($bar));
  410. $this->userManager
  411. ->expects($this->at(1))
  412. ->method('get')
  413. ->with('foo')
  414. ->will($this->returnValue($foo));
  415. $this->userManager
  416. ->expects($this->at(2))
  417. ->method('get')
  418. ->with('admin')
  419. ->will($this->returnValue($admin));
  420. $subgroup1 = $this->getMockBuilder('\OCP\IGroup')
  421. ->disableOriginalConstructor()
  422. ->getMock();
  423. $subgroup1->expects($this->any())
  424. ->method('getGID')
  425. ->will($this->returnValue('SubGroup1'));
  426. $subgroup2 = $this->getMockBuilder('\OCP\IGroup')
  427. ->disableOriginalConstructor()
  428. ->getMock();
  429. $subgroup2->expects($this->any())
  430. ->method('getGID')
  431. ->will($this->returnValue('SubGroup2'));
  432. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  433. ->disableOriginalConstructor()
  434. ->getMock();
  435. $subadmin
  436. ->expects($this->at(0))
  437. ->method('getSubAdminsGroups')
  438. ->will($this->returnValue([$subgroup1, $subgroup2]));
  439. $subadmin
  440. ->expects($this->any())
  441. ->method('getSubAdminsGroups')
  442. ->will($this->returnValue([]));
  443. $this->groupManager
  444. ->expects($this->any())
  445. ->method('getSubAdmin')
  446. ->will($this->returnValue($subadmin));
  447. $expectedResponse = new DataResponse(
  448. [
  449. 0 => [
  450. 'name' => 'bar',
  451. 'displayname' => 'B. Ar',
  452. 'groups' => ['SubGroup1'],
  453. 'subadmin' => [],
  454. 'quota' => 2323,
  455. 'storageLocation' => '/home/bar',
  456. 'lastLogin' => 3999000,
  457. 'backend' => '\Test\Util\User\Dummy',
  458. 'email' => 'bar@dummy.com',
  459. 'isRestoreDisabled' => false,
  460. 'isAvatarAvailable' => true,
  461. ],
  462. 1=> [
  463. 'name' => 'foo',
  464. 'displayname' => 'M. Foo',
  465. 'groups' => ['SubGroup2', 'SubGroup1'],
  466. 'subadmin' => [],
  467. 'quota' => 1024,
  468. 'storageLocation' => '/home/foo',
  469. 'lastLogin' => 500000,
  470. 'backend' => 'OC_User_Database',
  471. 'email' => 'foo@bar.com',
  472. 'isRestoreDisabled' => false,
  473. 'isAvatarAvailable' => true,
  474. ],
  475. 2 => [
  476. 'name' => 'admin',
  477. 'displayname' => 'S. Admin',
  478. 'groups' => ['SubGroup2'],
  479. 'subadmin' => [],
  480. 'quota' => 404,
  481. 'storageLocation' => '/home/admin',
  482. 'lastLogin' => 12000,
  483. 'backend' => '\Test\Util\User\Dummy',
  484. 'email' => 'admin@bar.com',
  485. 'isRestoreDisabled' => false,
  486. 'isAvatarAvailable' => false,
  487. ],
  488. ]
  489. );
  490. $response = $controller->index(0, 10, '', 'pattern');
  491. $this->assertEquals($expectedResponse, $response);
  492. }
  493. /**
  494. * TODO: Since the function uses the static OC_Subadmin class it can't be mocked
  495. * to test for subadmins. Thus the test always assumes you have admin permissions...
  496. */
  497. public function testIndexWithSearch() {
  498. $controller = $this->getController(true);
  499. $foo = $this->getMockBuilder('\OC\User\User')
  500. ->disableOriginalConstructor()->getMock();
  501. $foo
  502. ->expects($this->exactly(2))
  503. ->method('getUID')
  504. ->will($this->returnValue('foo'));
  505. $foo
  506. ->expects($this->once())
  507. ->method('getDisplayName')
  508. ->will($this->returnValue('M. Foo'));
  509. $foo
  510. ->expects($this->once())
  511. ->method('getEMailAddress')
  512. ->will($this->returnValue('foo@bar.com'));
  513. $foo
  514. ->expects($this->once())
  515. ->method('getQuota')
  516. ->will($this->returnValue('1024'));
  517. $foo
  518. ->method('getLastLogin')
  519. ->will($this->returnValue(500));
  520. $foo
  521. ->method('getHome')
  522. ->will($this->returnValue('/home/foo'));
  523. $foo
  524. ->expects($this->once())
  525. ->method('getBackendClassName')
  526. ->will($this->returnValue('OC_User_Database'));
  527. $admin = $this->getMockBuilder('\OC\User\User')
  528. ->disableOriginalConstructor()->getMock();
  529. $admin
  530. ->expects($this->exactly(2))
  531. ->method('getUID')
  532. ->will($this->returnValue('admin'));
  533. $admin
  534. ->expects($this->once())
  535. ->method('getDisplayName')
  536. ->will($this->returnValue('S. Admin'));
  537. $admin
  538. ->expects($this->once())
  539. ->method('getEMailAddress')
  540. ->will($this->returnValue('admin@bar.com'));
  541. $admin
  542. ->expects($this->once())
  543. ->method('getQuota')
  544. ->will($this->returnValue('404'));
  545. $admin
  546. ->expects($this->once())
  547. ->method('getLastLogin')
  548. ->will($this->returnValue(12));
  549. $admin
  550. ->expects($this->once())
  551. ->method('getHome')
  552. ->will($this->returnValue('/home/admin'));
  553. $admin
  554. ->expects($this->once())
  555. ->method('getBackendClassName')
  556. ->will($this->returnValue('\Test\Util\User\Dummy'));
  557. $bar = $this->getMockBuilder('\OC\User\User')
  558. ->disableOriginalConstructor()->getMock();
  559. $bar
  560. ->expects($this->exactly(2))
  561. ->method('getUID')
  562. ->will($this->returnValue('bar'));
  563. $bar
  564. ->expects($this->once())
  565. ->method('getDisplayName')
  566. ->will($this->returnValue('B. Ar'));
  567. $bar
  568. ->expects($this->once())
  569. ->method('getEMailAddress')
  570. ->will($this->returnValue('bar@dummy.com'));
  571. $bar
  572. ->expects($this->once())
  573. ->method('getQuota')
  574. ->will($this->returnValue('2323'));
  575. $bar
  576. ->method('getLastLogin')
  577. ->will($this->returnValue(3999));
  578. $bar
  579. ->method('getHome')
  580. ->will($this->returnValue('/home/bar'));
  581. $bar
  582. ->expects($this->once())
  583. ->method('getBackendClassName')
  584. ->will($this->returnValue('\Test\Util\User\Dummy'));
  585. $this->userManager
  586. ->expects($this->once())
  587. ->method('search')
  588. ->with('pattern', 10, 0)
  589. ->will($this->returnValue([$foo, $admin, $bar]));
  590. $this->groupManager
  591. ->expects($this->exactly(3))
  592. ->method('getUserGroupIds')
  593. ->will($this->onConsecutiveCalls(array('Users', 'Support'), array('admins', 'Support'), array('External Users')));
  594. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  595. ->disableOriginalConstructor()
  596. ->getMock();
  597. $subadmin->expects($this->any())
  598. ->method('getSubAdminsGroups')
  599. ->will($this->returnValue([]));
  600. $this->groupManager
  601. ->expects($this->any())
  602. ->method('getSubAdmin')
  603. ->will($this->returnValue($subadmin));
  604. $expectedResponse = new DataResponse(
  605. array(
  606. 0 => array(
  607. 'name' => 'foo',
  608. 'displayname' => 'M. Foo',
  609. 'groups' => array('Users', 'Support'),
  610. 'subadmin' => array(),
  611. 'quota' => 1024,
  612. 'storageLocation' => '/home/foo',
  613. 'lastLogin' => 500000,
  614. 'backend' => 'OC_User_Database',
  615. 'email' => 'foo@bar.com',
  616. 'isRestoreDisabled' => false,
  617. 'isAvatarAvailable' => true,
  618. ),
  619. 1 => array(
  620. 'name' => 'admin',
  621. 'displayname' => 'S. Admin',
  622. 'groups' => array('admins', 'Support'),
  623. 'subadmin' => array(),
  624. 'quota' => 404,
  625. 'storageLocation' => '/home/admin',
  626. 'lastLogin' => 12000,
  627. 'backend' => '\Test\Util\User\Dummy',
  628. 'email' => 'admin@bar.com',
  629. 'isRestoreDisabled' => false,
  630. 'isAvatarAvailable' => false,
  631. ),
  632. 2 => array(
  633. 'name' => 'bar',
  634. 'displayname' => 'B. Ar',
  635. 'groups' => array('External Users'),
  636. 'subadmin' => array(),
  637. 'quota' => 2323,
  638. 'storageLocation' => '/home/bar',
  639. 'lastLogin' => 3999000,
  640. 'backend' => '\Test\Util\User\Dummy',
  641. 'email' => 'bar@dummy.com',
  642. 'isRestoreDisabled' => false,
  643. 'isAvatarAvailable' => true,
  644. ),
  645. )
  646. );
  647. $response = $controller->index(0, 10, '', 'pattern');
  648. $this->assertEquals($expectedResponse, $response);
  649. }
  650. public function testIndexWithBackend() {
  651. $controller = $this->getController(true);
  652. $user = $this->getMockBuilder('\OC\User\User')
  653. ->disableOriginalConstructor()->getMock();
  654. $user
  655. ->expects($this->exactly(2))
  656. ->method('getUID')
  657. ->will($this->returnValue('foo'));
  658. $user
  659. ->expects($this->once())
  660. ->method('getDisplayName')
  661. ->will($this->returnValue('M. Foo'));
  662. $user
  663. ->expects($this->once())
  664. ->method('getEMailAddress')
  665. ->will($this->returnValue(null));
  666. $user
  667. ->expects($this->once())
  668. ->method('getQuota')
  669. ->will($this->returnValue('none'));
  670. $user
  671. ->method('getLastLogin')
  672. ->will($this->returnValue(500));
  673. $user
  674. ->method('getHome')
  675. ->will($this->returnValue('/home/foo'));
  676. $user
  677. ->expects($this->once())
  678. ->method('getBackendClassName')
  679. ->will($this->returnValue('OC_User_Database'));
  680. $this->userManager
  681. ->expects($this->once())
  682. ->method('getBackends')
  683. ->will($this->returnValue([new \Test\Util\User\Dummy(), new \OC\User\Database()]));
  684. $this->userManager
  685. ->expects($this->once())
  686. ->method('clearBackends');
  687. $this->userManager
  688. ->expects($this->once())
  689. ->method('search')
  690. ->with('')
  691. ->will($this->returnValue([$user]));
  692. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  693. ->disableOriginalConstructor()
  694. ->getMock();
  695. $subadmin->expects($this->once())
  696. ->method('getSubAdminsGroups')
  697. ->will($this->returnValue([]));
  698. $this->groupManager
  699. ->expects($this->any())
  700. ->method('getSubAdmin')
  701. ->will($this->returnValue($subadmin));
  702. $expectedResponse = new DataResponse(
  703. array(
  704. 0 => array(
  705. 'name' => 'foo',
  706. 'displayname' => 'M. Foo',
  707. 'groups' => null,
  708. 'subadmin' => array(),
  709. 'quota' => 'none',
  710. 'storageLocation' => '/home/foo',
  711. 'lastLogin' => 500000,
  712. 'backend' => 'OC_User_Database',
  713. 'email' => null,
  714. 'isRestoreDisabled' => false,
  715. 'isAvatarAvailable' => true,
  716. )
  717. )
  718. );
  719. $response = $controller->index(0, 10, '','', '\Test\Util\User\Dummy');
  720. $this->assertEquals($expectedResponse, $response);
  721. }
  722. public function testIndexWithBackendNoUser() {
  723. $controller = $this->getController(true);
  724. $this->userManager
  725. ->expects($this->once())
  726. ->method('getBackends')
  727. ->will($this->returnValue([new \Test\Util\User\Dummy(), new \OC\User\Database()]));
  728. $this->userManager
  729. ->expects($this->once())
  730. ->method('search')
  731. ->with('')
  732. ->will($this->returnValue([]));
  733. $expectedResponse = new DataResponse([]);
  734. $response = $controller->index(0, 10, '','', '\Test\Util\User\Dummy');
  735. $this->assertEquals($expectedResponse, $response);
  736. }
  737. public function testCreateSuccessfulWithoutGroupAdmin() {
  738. $controller = $this->getController(true);
  739. $user = $this->getMockBuilder('\OC\User\User')
  740. ->disableOriginalConstructor()->getMock();
  741. $user
  742. ->method('getHome')
  743. ->will($this->returnValue('/home/user'));
  744. $user
  745. ->method('getUID')
  746. ->will($this->returnValue('foo'));
  747. $user
  748. ->expects($this->once())
  749. ->method('getBackendClassName')
  750. ->will($this->returnValue('bar'));
  751. $this->userManager
  752. ->expects($this->once())
  753. ->method('createUser')
  754. ->will($this->onConsecutiveCalls($user));
  755. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  756. ->disableOriginalConstructor()
  757. ->getMock();
  758. $subadmin
  759. ->expects($this->any())
  760. ->method('getSubAdminsGroups')
  761. ->with($user)
  762. ->will($this->returnValue([]));
  763. $this->groupManager
  764. ->expects($this->any())
  765. ->method('getSubAdmin')
  766. ->will($this->returnValue($subadmin));
  767. $expectedResponse = new DataResponse(
  768. array(
  769. 'name' => 'foo',
  770. 'groups' => null,
  771. 'storageLocation' => '/home/user',
  772. 'backend' => 'bar',
  773. 'lastLogin' => null,
  774. 'displayname' => null,
  775. 'quota' => null,
  776. 'subadmin' => array(),
  777. 'email' => null,
  778. 'isRestoreDisabled' => false,
  779. 'isAvatarAvailable' => true,
  780. ),
  781. Http::STATUS_CREATED
  782. );
  783. $response = $controller->create('foo', 'password', array());
  784. $this->assertEquals($expectedResponse, $response);
  785. }
  786. public function testCreateSuccessfulWithoutGroupSubAdmin() {
  787. $controller = $this->getController(false);
  788. $user = $this->getMockBuilder('\OC\User\User')
  789. ->disableOriginalConstructor()->getMock();
  790. $this->userSession
  791. ->expects($this->once())
  792. ->method('getUser')
  793. ->will($this->returnValue($user));
  794. $newUser = $this->getMockBuilder('\OC\User\User')
  795. ->disableOriginalConstructor()->getMock();
  796. $newUser
  797. ->method('getUID')
  798. ->will($this->returnValue('foo'));
  799. $newUser
  800. ->method('getHome')
  801. ->will($this->returnValue('/home/user'));
  802. $newUser
  803. ->method('getHome')
  804. ->will($this->returnValue('/home/user'));
  805. $newUser
  806. ->expects($this->once())
  807. ->method('getBackendClassName')
  808. ->will($this->returnValue('bar'));
  809. $user = $this->getMockBuilder('\OC\User\User')
  810. ->disableOriginalConstructor()->getMock();
  811. $subGroup1 = $this->getMockBuilder('\OCP\IGroup')
  812. ->disableOriginalConstructor()->getMock();
  813. $subGroup1
  814. ->expects($this->once())
  815. ->method('addUser')
  816. ->with($newUser);
  817. $subGroup2 = $this->getMockBuilder('\OCP\IGroup')
  818. ->disableOriginalConstructor()->getMock();
  819. $subGroup2
  820. ->expects($this->once())
  821. ->method('addUser')
  822. ->with($newUser);
  823. $this->userManager
  824. ->expects($this->once())
  825. ->method('createUser')
  826. ->will($this->returnValue($newUser));
  827. $this->groupManager
  828. ->expects($this->exactly(2))
  829. ->method('get')
  830. ->will($this->onConsecutiveCalls($subGroup1, $subGroup2));
  831. $this->groupManager
  832. ->expects($this->once())
  833. ->method('getUserGroupIds')
  834. ->with($user)
  835. ->will($this->onConsecutiveCalls(['SubGroup1', 'SubGroup2']));
  836. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  837. ->disableOriginalConstructor()
  838. ->getMock();
  839. $subadmin
  840. ->expects($this->at(0))
  841. ->method('getSubAdminsGroups')
  842. ->will($this->returnValue([$subGroup1, $subGroup2]));
  843. $subadmin
  844. ->expects($this->at(1))
  845. ->method('getSubAdminsGroups')
  846. ->will($this->returnValue([]));
  847. $this->groupManager
  848. ->expects($this->any())
  849. ->method('getSubAdmin')
  850. ->will($this->returnValue($subadmin));
  851. $expectedResponse = new DataResponse(
  852. array(
  853. 'name' => 'foo',
  854. 'groups' => ['SubGroup1', 'SubGroup2'],
  855. 'storageLocation' => '/home/user',
  856. 'backend' => 'bar',
  857. 'lastLogin' => 0,
  858. 'displayname' => null,
  859. 'quota' => null,
  860. 'subadmin' => [],
  861. 'email' => null,
  862. 'isRestoreDisabled' => false,
  863. 'isAvatarAvailable' => true,
  864. ),
  865. Http::STATUS_CREATED
  866. );
  867. $response = $controller->create('foo', 'password');
  868. $this->assertEquals($expectedResponse, $response);
  869. }
  870. public function testCreateSuccessfulWithGroupAdmin() {
  871. $controller = $this->getController(true);
  872. $user = $this->getMockBuilder('\OC\User\User')
  873. ->disableOriginalConstructor()->getMock();
  874. $user
  875. ->method('getHome')
  876. ->will($this->returnValue('/home/user'));
  877. $user
  878. ->method('getHome')
  879. ->will($this->returnValue('/home/user'));
  880. $user
  881. ->method('getUID')
  882. ->will($this->returnValue('foo'));
  883. $user
  884. ->expects($this->once())
  885. ->method('getBackendClassName')
  886. ->will($this->returnValue('bar'));
  887. $existingGroup = $this->getMockBuilder('\OCP\IGroup')
  888. ->disableOriginalConstructor()->getMock();
  889. $existingGroup
  890. ->expects($this->once())
  891. ->method('addUser')
  892. ->with($user);
  893. $newGroup = $this->getMockBuilder('\OCP\IGroup')
  894. ->disableOriginalConstructor()->getMock();
  895. $newGroup
  896. ->expects($this->once())
  897. ->method('addUser')
  898. ->with($user);
  899. $this->userManager
  900. ->expects($this->once())
  901. ->method('createUser')
  902. ->will($this->onConsecutiveCalls($user));
  903. $this->groupManager
  904. ->expects($this->exactly(2))
  905. ->method('get')
  906. ->will($this->onConsecutiveCalls(null, $existingGroup));
  907. $this->groupManager
  908. ->expects($this->once())
  909. ->method('createGroup')
  910. ->with('NewGroup')
  911. ->will($this->onConsecutiveCalls($newGroup));
  912. $this->groupManager
  913. ->expects($this->once())
  914. ->method('getUserGroupIds')
  915. ->with($user)
  916. ->will($this->onConsecutiveCalls(array('NewGroup', 'ExistingGroup')));
  917. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  918. ->disableOriginalConstructor()
  919. ->getMock();
  920. $subadmin
  921. ->expects($this->once())
  922. ->method('getSubAdminsGroups')
  923. ->with($user)
  924. ->will($this->returnValue([]));
  925. $this->groupManager
  926. ->expects($this->any())
  927. ->method('getSubAdmin')
  928. ->will($this->returnValue($subadmin));
  929. $expectedResponse = new DataResponse(
  930. array(
  931. 'name' => 'foo',
  932. 'groups' => array('NewGroup', 'ExistingGroup'),
  933. 'storageLocation' => '/home/user',
  934. 'backend' => 'bar',
  935. 'lastLogin' => null,
  936. 'displayname' => null,
  937. 'quota' => null,
  938. 'subadmin' => array(),
  939. 'email' => null,
  940. 'isRestoreDisabled' => false,
  941. 'isAvatarAvailable' => true,
  942. ),
  943. Http::STATUS_CREATED
  944. );
  945. $response = $controller->create('foo', 'password', array('NewGroup', 'ExistingGroup'));
  946. $this->assertEquals($expectedResponse, $response);
  947. }
  948. public function testCreateSuccessfulWithGroupSubAdmin() {
  949. $controller = $this->getController(false);
  950. $user = $this->getMockBuilder('\OC\User\User')
  951. ->disableOriginalConstructor()->getMock();
  952. $this->userSession
  953. ->expects($this->once())
  954. ->method('getUser')
  955. ->will($this->returnValue($user));
  956. $user = $this->getMockBuilder('\OC\User\User')
  957. ->disableOriginalConstructor()->getMock();
  958. $newUser = $this->getMockBuilder('\OC\User\User')
  959. ->disableOriginalConstructor()->getMock();
  960. $newUser
  961. ->method('getHome')
  962. ->will($this->returnValue('/home/user'));
  963. $newUser
  964. ->method('getHome')
  965. ->will($this->returnValue('/home/user'));
  966. $newUser
  967. ->method('getUID')
  968. ->will($this->returnValue('foo'));
  969. $newUser
  970. ->expects($this->once())
  971. ->method('getBackendClassName')
  972. ->will($this->returnValue('bar'));
  973. $subGroup1 = $this->getMockBuilder('\OCP\IGroup')
  974. ->disableOriginalConstructor()->getMock();
  975. $subGroup1
  976. ->expects($this->any())
  977. ->method('getGID')
  978. ->will($this->returnValue('SubGroup1'));
  979. $subGroup1
  980. ->expects($this->once())
  981. ->method('addUser')
  982. ->with($user);
  983. $this->userManager
  984. ->expects($this->once())
  985. ->method('createUser')
  986. ->will($this->returnValue($newUser));
  987. $this->groupManager
  988. ->expects($this->at(0))
  989. ->method('get')
  990. ->with('SubGroup1')
  991. ->will($this->returnValue($subGroup1));
  992. $this->groupManager
  993. ->expects($this->at(4))
  994. ->method('get')
  995. ->with('SubGroup1')
  996. ->will($this->returnValue($subGroup1));
  997. $this->groupManager
  998. ->expects($this->once())
  999. ->method('getUserGroupIds')
  1000. ->with($user)
  1001. ->will($this->onConsecutiveCalls(['SubGroup1']));
  1002. $this->groupManager
  1003. ->expects($this->once())
  1004. ->method('getUserGroupIds')
  1005. ->with($newUser)
  1006. ->will($this->onConsecutiveCalls(['SubGroup1']));
  1007. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  1008. ->disableOriginalConstructor()
  1009. ->getMock();
  1010. $subadmin->expects($this->at(1))
  1011. ->method('getSubAdminsGroups')
  1012. ->with($user)
  1013. ->will($this->returnValue([$subGroup1]));
  1014. $subadmin->expects($this->at(2))
  1015. ->method('getSubAdminsGroups')
  1016. ->with($newUser)
  1017. ->will($this->returnValue([]));
  1018. $this->groupManager
  1019. ->expects($this->any())
  1020. ->method('getSubAdmin')
  1021. ->will($this->returnValue($subadmin));
  1022. $expectedResponse = new DataResponse(
  1023. array(
  1024. 'name' => 'foo',
  1025. 'groups' => ['SubGroup1'],
  1026. 'storageLocation' => '/home/user',
  1027. 'backend' => 'bar',
  1028. 'lastLogin' => 0,
  1029. 'displayname' => null,
  1030. 'quota' => null,
  1031. 'subadmin' => [],
  1032. 'email' => null,
  1033. 'isRestoreDisabled' => false,
  1034. 'isAvatarAvailable' => true,
  1035. ),
  1036. Http::STATUS_CREATED
  1037. );
  1038. $response = $controller->create('foo', 'password', ['SubGroup1', 'ExistingGroup']);
  1039. $this->assertEquals($expectedResponse, $response);
  1040. }
  1041. public function testCreateUnsuccessfulAdmin() {
  1042. $controller = $this->getController(true);
  1043. $this->userManager
  1044. ->method('createUser')
  1045. ->will($this->throwException(new \Exception()));
  1046. $expectedResponse = new DataResponse(
  1047. array(
  1048. 'message' => 'Unable to create user.'
  1049. ),
  1050. Http::STATUS_FORBIDDEN
  1051. );
  1052. $response = $controller->create('foo', 'password', array());
  1053. $this->assertEquals($expectedResponse, $response);
  1054. }
  1055. public function testCreateUnsuccessfulSubAdmin() {
  1056. $controller = $this->getController(false);
  1057. $user = $this->getMockBuilder('\OC\User\User')
  1058. ->disableOriginalConstructor()->getMock();
  1059. $user
  1060. ->expects($this->any())
  1061. ->method('getUID')
  1062. ->will($this->returnValue('username'));
  1063. $this->userSession
  1064. ->expects($this->once())
  1065. ->method('getUser')
  1066. ->will($this->returnValue($user));
  1067. $this->userManager
  1068. ->method('createUser')
  1069. ->will($this->throwException(new \Exception()));
  1070. $subgroup1 = $this->getMockBuilder('\OCP\IGroup')
  1071. ->disableOriginalConstructor()
  1072. ->getMock();
  1073. $subgroup1->expects($this->once())
  1074. ->method('getGID')
  1075. ->will($this->returnValue('SubGroup1'));
  1076. $subgroup2 = $this->getMockBuilder('\OCP\IGroup')
  1077. ->disableOriginalConstructor()
  1078. ->getMock();
  1079. $subgroup2->expects($this->once())
  1080. ->method('getGID')
  1081. ->will($this->returnValue('SubGroup2'));
  1082. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  1083. ->disableOriginalConstructor()
  1084. ->getMock();
  1085. $subadmin->expects($this->once())
  1086. ->method('getSubAdminsGroups')
  1087. ->with($user)
  1088. ->will($this->returnValue([$subgroup1, $subgroup2]));
  1089. $this->groupManager
  1090. ->expects($this->any())
  1091. ->method('getSubAdmin')
  1092. ->will($this->returnValue($subadmin));
  1093. $expectedResponse = new DataResponse(
  1094. [
  1095. 'message' => 'Unable to create user.'
  1096. ],
  1097. Http::STATUS_FORBIDDEN
  1098. );
  1099. $response = $controller->create('foo', 'password', array());
  1100. $this->assertEquals($expectedResponse, $response);
  1101. }
  1102. public function testDestroySelfAdmin() {
  1103. $controller = $this->getController(true);
  1104. $user = $this->getMockBuilder('\OC\User\User')
  1105. ->disableOriginalConstructor()->getMock();
  1106. $user
  1107. ->expects($this->once())
  1108. ->method('getUID')
  1109. ->will($this->returnValue('myself'));
  1110. $this->userSession
  1111. ->method('getUser')
  1112. ->will($this->returnValue($user));
  1113. $expectedResponse = new DataResponse(
  1114. array(
  1115. 'status' => 'error',
  1116. 'data' => array(
  1117. 'message' => 'Unable to delete user.'
  1118. )
  1119. ),
  1120. Http::STATUS_FORBIDDEN
  1121. );
  1122. $response = $controller->destroy('myself');
  1123. $this->assertEquals($expectedResponse, $response);
  1124. }
  1125. public function testDestroySelfSubadmin() {
  1126. $controller = $this->getController(false);
  1127. $user = $this->getMockBuilder('\OC\User\User')
  1128. ->disableOriginalConstructor()->getMock();
  1129. $user
  1130. ->expects($this->once())
  1131. ->method('getUID')
  1132. ->will($this->returnValue('myself'));
  1133. $this->userSession
  1134. ->method('getUser')
  1135. ->will($this->returnValue($user));
  1136. $expectedResponse = new DataResponse(
  1137. array(
  1138. 'status' => 'error',
  1139. 'data' => array(
  1140. 'message' => 'Unable to delete user.'
  1141. )
  1142. ),
  1143. Http::STATUS_FORBIDDEN
  1144. );
  1145. $response = $controller->destroy('myself');
  1146. $this->assertEquals($expectedResponse, $response);
  1147. }
  1148. public function testDestroyAdmin() {
  1149. $controller = $this->getController(true);
  1150. $user = $this->getMockBuilder('\OC\User\User')
  1151. ->disableOriginalConstructor()->getMock();
  1152. $user
  1153. ->expects($this->once())
  1154. ->method('getUID')
  1155. ->will($this->returnValue('Admin'));
  1156. $toDeleteUser = $this->getMockBuilder('\OC\User\User')
  1157. ->disableOriginalConstructor()->getMock();
  1158. $toDeleteUser
  1159. ->expects($this->once())
  1160. ->method('delete')
  1161. ->will($this->returnValue(true));
  1162. $this->userSession
  1163. ->method('getUser')
  1164. ->will($this->returnValue($user));
  1165. $this->userManager
  1166. ->method('get')
  1167. ->with('UserToDelete')
  1168. ->will($this->returnValue($toDeleteUser));
  1169. $expectedResponse = new DataResponse(
  1170. array(
  1171. 'status' => 'success',
  1172. 'data' => array(
  1173. 'username' => 'UserToDelete'
  1174. )
  1175. ),
  1176. Http::STATUS_NO_CONTENT
  1177. );
  1178. $response = $controller->destroy('UserToDelete');
  1179. $this->assertEquals($expectedResponse, $response);
  1180. }
  1181. public function testDestroySubAdmin() {
  1182. $controller = $this->getController(false);
  1183. $user = $this->getMockBuilder('\OC\User\User')
  1184. ->disableOriginalConstructor()->getMock();
  1185. $user
  1186. ->expects($this->once())
  1187. ->method('getUID')
  1188. ->will($this->returnValue('myself'));
  1189. $this->userSession
  1190. ->method('getUser')
  1191. ->will($this->returnValue($user));
  1192. $user = $this->getMockBuilder('\OC\User\User')
  1193. ->disableOriginalConstructor()->getMock();
  1194. $toDeleteUser = $this->getMockBuilder('\OC\User\User')
  1195. ->disableOriginalConstructor()->getMock();
  1196. $toDeleteUser
  1197. ->expects($this->once())
  1198. ->method('delete')
  1199. ->will($this->returnValue(true));
  1200. $this->userSession
  1201. ->method('getUser')
  1202. ->will($this->returnValue($user));
  1203. $this->userManager
  1204. ->method('get')
  1205. ->with('UserToDelete')
  1206. ->will($this->returnValue($toDeleteUser));
  1207. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  1208. ->disableOriginalConstructor()
  1209. ->getMock();
  1210. $subadmin->expects($this->once())
  1211. ->method('isUserAccessible')
  1212. ->with($user, $toDeleteUser)
  1213. ->will($this->returnValue(true));
  1214. $this->groupManager
  1215. ->expects($this->any())
  1216. ->method('getSubAdmin')
  1217. ->will($this->returnValue($subadmin));
  1218. $expectedResponse = new DataResponse(
  1219. [
  1220. 'status' => 'success',
  1221. 'data' => [
  1222. 'username' => 'UserToDelete'
  1223. ]
  1224. ],
  1225. Http::STATUS_NO_CONTENT
  1226. );
  1227. $response = $controller->destroy('UserToDelete');
  1228. $this->assertEquals($expectedResponse, $response);
  1229. }
  1230. public function testDestroyUnsuccessfulAdmin() {
  1231. $controller = $this->getController(true);
  1232. $user = $this->getMockBuilder('\OC\User\User')
  1233. ->disableOriginalConstructor()->getMock();
  1234. $user
  1235. ->expects($this->once())
  1236. ->method('getUID')
  1237. ->will($this->returnValue('Admin'));
  1238. $toDeleteUser = $this->getMockBuilder('\OC\User\User')
  1239. ->disableOriginalConstructor()->getMock();
  1240. $toDeleteUser
  1241. ->expects($this->once())
  1242. ->method('delete')
  1243. ->will($this->returnValue(false));
  1244. $this->userSession
  1245. ->method('getUser')
  1246. ->will($this->returnValue($user));
  1247. $this->userManager
  1248. ->method('get')
  1249. ->with('UserToDelete')
  1250. ->will($this->returnValue($toDeleteUser));
  1251. $expectedResponse = new DataResponse(
  1252. array(
  1253. 'status' => 'error',
  1254. 'data' => array(
  1255. 'message' => 'Unable to delete user.'
  1256. )
  1257. ),
  1258. Http::STATUS_FORBIDDEN
  1259. );
  1260. $response = $controller->destroy('UserToDelete');
  1261. $this->assertEquals($expectedResponse, $response);
  1262. }
  1263. public function testDestroyUnsuccessfulSubAdmin() {
  1264. $controller = $this->getController(false);
  1265. $user = $this->getMockBuilder('\OC\User\User')
  1266. ->disableOriginalConstructor()->getMock();
  1267. $user
  1268. ->expects($this->once())
  1269. ->method('getUID')
  1270. ->will($this->returnValue('myself'));
  1271. $this->userSession
  1272. ->method('getUser')
  1273. ->will($this->returnValue($user));
  1274. $toDeleteUser = $this->getMockBuilder('\OC\User\User')
  1275. ->disableOriginalConstructor()->getMock();
  1276. $toDeleteUser
  1277. ->expects($this->once())
  1278. ->method('delete')
  1279. ->will($this->returnValue(false));
  1280. $this->userSession
  1281. ->method('getUser')
  1282. ->will($this->returnValue($user));
  1283. $this->userManager
  1284. ->method('get')
  1285. ->with('UserToDelete')
  1286. ->will($this->returnValue($toDeleteUser));
  1287. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  1288. ->disableOriginalConstructor()
  1289. ->getMock();
  1290. $subadmin->expects($this->once())
  1291. ->method('isUserAccessible')
  1292. ->with($user, $toDeleteUser)
  1293. ->will($this->returnValue(true));
  1294. $this->groupManager
  1295. ->expects($this->any())
  1296. ->method('getSubAdmin')
  1297. ->will($this->returnValue($subadmin));
  1298. $expectedResponse = new DataResponse(
  1299. [
  1300. 'status' => 'error',
  1301. 'data' => [
  1302. 'message' => 'Unable to delete user.'
  1303. ]
  1304. ],
  1305. Http::STATUS_FORBIDDEN
  1306. );
  1307. $response = $controller->destroy('UserToDelete');
  1308. $this->assertEquals($expectedResponse, $response);
  1309. }
  1310. public function testDestroyNotAccessibleToSubAdmin() {
  1311. $controller = $this->getController(false);
  1312. $user = $this->getMockBuilder('\OC\User\User')
  1313. ->disableOriginalConstructor()->getMock();
  1314. $user
  1315. ->expects($this->once())
  1316. ->method('getUID')
  1317. ->will($this->returnValue('myself'));
  1318. $this->userSession
  1319. ->method('getUser')
  1320. ->will($this->returnValue($user));
  1321. $toDeleteUser = $this->getMockBuilder('\OC\User\User')
  1322. ->disableOriginalConstructor()->getMock();
  1323. $this->userSession
  1324. ->method('getUser')
  1325. ->will($this->returnValue($user));
  1326. $this->userManager
  1327. ->method('get')
  1328. ->with('UserToDelete')
  1329. ->will($this->returnValue($toDeleteUser));
  1330. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  1331. ->disableOriginalConstructor()
  1332. ->getMock();
  1333. $subadmin->expects($this->once())
  1334. ->method('isUserAccessible')
  1335. ->with($user, $toDeleteUser)
  1336. ->will($this->returnValue(false));
  1337. $this->groupManager
  1338. ->expects($this->any())
  1339. ->method('getSubAdmin')
  1340. ->will($this->returnValue($subadmin));
  1341. $expectedResponse = new DataResponse(
  1342. [
  1343. 'status' => 'error',
  1344. 'data' => [
  1345. 'message' => 'Authentication error'
  1346. ]
  1347. ],
  1348. Http::STATUS_FORBIDDEN
  1349. );
  1350. $response = $controller->destroy('UserToDelete');
  1351. $this->assertEquals($expectedResponse, $response);
  1352. }
  1353. /**
  1354. * test if an invalid mail result in a failure response
  1355. */
  1356. public function testCreateUnsuccessfulWithInvalidEmailAdmin() {
  1357. $controller = $this->getController(true);
  1358. $expectedResponse = new DataResponse([
  1359. 'message' => 'Invalid mail address',
  1360. ],
  1361. Http::STATUS_UNPROCESSABLE_ENTITY
  1362. );
  1363. $response = $controller->create('foo', 'password', [], 'invalidMailAdress');
  1364. $this->assertEquals($expectedResponse, $response);
  1365. }
  1366. /**
  1367. * test if a valid mail result in a successful mail send
  1368. */
  1369. public function testCreateSuccessfulWithValidEmailAdmin() {
  1370. $controller = $this->getController(true);
  1371. $message = $this->getMockBuilder('\OC\Mail\Message')
  1372. ->disableOriginalConstructor()->getMock();
  1373. $message
  1374. ->expects($this->at(0))
  1375. ->method('setTo')
  1376. ->with(['validMail@Adre.ss' => 'foo']);
  1377. $message
  1378. ->expects($this->at(1))
  1379. ->method('setSubject')
  1380. ->with('Your account was created');
  1381. $htmlBody = new Http\TemplateResponse(
  1382. 'settings',
  1383. 'email.new_user',
  1384. [
  1385. 'username' => 'foo',
  1386. 'url' => '',
  1387. ],
  1388. 'blank'
  1389. );
  1390. $message
  1391. ->expects($this->at(2))
  1392. ->method('setHtmlBody')
  1393. ->with($htmlBody->render());
  1394. $plainBody = new Http\TemplateResponse(
  1395. 'settings',
  1396. 'email.new_user_plain_text',
  1397. [
  1398. 'username' => 'foo',
  1399. 'url' => '',
  1400. ],
  1401. 'blank'
  1402. );
  1403. $message
  1404. ->expects($this->at(3))
  1405. ->method('setPlainBody')
  1406. ->with($plainBody->render());
  1407. $message
  1408. ->expects($this->at(4))
  1409. ->method('setFrom')
  1410. ->with(['no-reply@owncloud.com' => null]);
  1411. $this->mailer
  1412. ->expects($this->at(0))
  1413. ->method('validateMailAddress')
  1414. ->with('validMail@Adre.ss')
  1415. ->will($this->returnValue(true));
  1416. $this->mailer
  1417. ->expects($this->at(1))
  1418. ->method('createMessage')
  1419. ->will($this->returnValue($message));
  1420. $this->mailer
  1421. ->expects($this->at(2))
  1422. ->method('send')
  1423. ->with($message);
  1424. $user = $this->getMockBuilder('\OC\User\User')
  1425. ->disableOriginalConstructor()->getMock();
  1426. $user
  1427. ->method('getHome')
  1428. ->will($this->returnValue('/home/user'));
  1429. $user
  1430. ->method('getHome')
  1431. ->will($this->returnValue('/home/user'));
  1432. $user
  1433. ->method('getUID')
  1434. ->will($this->returnValue('foo'));
  1435. $user
  1436. ->expects($this->once())
  1437. ->method('getBackendClassName')
  1438. ->will($this->returnValue('bar'));
  1439. $this->userManager
  1440. ->expects($this->once())
  1441. ->method('createUser')
  1442. ->will($this->onConsecutiveCalls($user));
  1443. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  1444. ->disableOriginalConstructor()
  1445. ->getMock();
  1446. $subadmin->expects($this->once())
  1447. ->method('getSubAdminsGroups')
  1448. ->with($user)
  1449. ->will($this->returnValue([]));
  1450. $this->groupManager
  1451. ->expects($this->any())
  1452. ->method('getSubAdmin')
  1453. ->will($this->returnValue($subadmin));
  1454. $response = $controller->create('foo', 'password', [], 'validMail@Adre.ss');
  1455. $this->assertEquals(Http::STATUS_CREATED, $response->getStatus());
  1456. }
  1457. private function mockUser($userId = 'foo', $displayName = 'M. Foo',
  1458. $lastLogin = 500, $home = '/home/foo', $backend = 'OC_User_Database') {
  1459. $user = $this->getMockBuilder('\OC\User\User')
  1460. ->disableOriginalConstructor()->getMock();
  1461. $user
  1462. ->expects($this->any())
  1463. ->method('getUID')
  1464. ->will($this->returnValue($userId));
  1465. $user
  1466. ->expects($this->once())
  1467. ->method('getDisplayName')
  1468. ->will($this->returnValue($displayName));
  1469. $user
  1470. ->method('getLastLogin')
  1471. ->will($this->returnValue($lastLogin));
  1472. $user
  1473. ->method('getHome')
  1474. ->will($this->returnValue($home));
  1475. $user
  1476. ->expects($this->once())
  1477. ->method('getBackendClassName')
  1478. ->will($this->returnValue($backend));
  1479. $result = [
  1480. 'name' => $userId,
  1481. 'displayname' => $displayName,
  1482. 'groups' => null,
  1483. 'subadmin' => array(),
  1484. 'quota' => null,
  1485. 'storageLocation' => $home,
  1486. 'lastLogin' => $lastLogin * 1000,
  1487. 'backend' => $backend,
  1488. 'email' => null,
  1489. 'isRestoreDisabled' => false,
  1490. 'isAvatarAvailable' => true,
  1491. ];
  1492. return [$user, $result];
  1493. }
  1494. public function testRestorePossibleWithoutEncryption() {
  1495. $controller = $this->getController(true);
  1496. list($user, $expectedResult) = $this->mockUser();
  1497. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  1498. ->disableOriginalConstructor()
  1499. ->getMock();
  1500. $subadmin->expects($this->once())
  1501. ->method('getSubAdminsGroups')
  1502. ->with($user)
  1503. ->will($this->returnValue([]));
  1504. $this->groupManager
  1505. ->expects($this->any())
  1506. ->method('getSubAdmin')
  1507. ->will($this->returnValue($subadmin));
  1508. $result = self::invokePrivate($controller, 'formatUserForIndex', [$user]);
  1509. $this->assertEquals($expectedResult, $result);
  1510. }
  1511. public function testRestorePossibleWithAdminAndUserRestore() {
  1512. list($user, $expectedResult) = $this->mockUser();
  1513. $this->appManager
  1514. ->expects($this->once())
  1515. ->method('isEnabledForUser')
  1516. ->with(
  1517. $this->equalTo('encryption')
  1518. )
  1519. ->will($this->returnValue(true));
  1520. $this->config
  1521. ->expects($this->once())
  1522. ->method('getAppValue')
  1523. ->with(
  1524. $this->equalTo('encryption'),
  1525. $this->equalTo('recoveryAdminEnabled'),
  1526. $this->anything()
  1527. )
  1528. ->will($this->returnValue('1'));
  1529. $this->config
  1530. ->expects($this->at(1))
  1531. ->method('getUserValue')
  1532. ->with(
  1533. $this->anything(),
  1534. $this->equalTo('encryption'),
  1535. $this->equalTo('recoveryEnabled'),
  1536. $this->anything()
  1537. )
  1538. ->will($this->returnValue('1'));
  1539. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  1540. ->disableOriginalConstructor()
  1541. ->getMock();
  1542. $subadmin->expects($this->once())
  1543. ->method('getSubAdminsGroups')
  1544. ->with($user)
  1545. ->will($this->returnValue([]));
  1546. $this->groupManager
  1547. ->expects($this->any())
  1548. ->method('getSubAdmin')
  1549. ->will($this->returnValue($subadmin));
  1550. $controller = $this->getController(true);
  1551. $result = self::invokePrivate($controller, 'formatUserForIndex', [$user]);
  1552. $this->assertEquals($expectedResult, $result);
  1553. }
  1554. public function testRestoreNotPossibleWithoutAdminRestore() {
  1555. list($user, $expectedResult) = $this->mockUser();
  1556. $this->appManager
  1557. ->method('isEnabledForUser')
  1558. ->with(
  1559. $this->equalTo('encryption')
  1560. )
  1561. ->will($this->returnValue(true));
  1562. $expectedResult['isRestoreDisabled'] = true;
  1563. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  1564. ->disableOriginalConstructor()
  1565. ->getMock();
  1566. $subadmin->expects($this->once())
  1567. ->method('getSubAdminsGroups')
  1568. ->with($user)
  1569. ->will($this->returnValue([]));
  1570. $this->groupManager
  1571. ->expects($this->any())
  1572. ->method('getSubAdmin')
  1573. ->will($this->returnValue($subadmin));
  1574. $controller = $this->getController(true);
  1575. $result = self::invokePrivate($controller, 'formatUserForIndex', [$user]);
  1576. $this->assertEquals($expectedResult, $result);
  1577. }
  1578. public function testRestoreNotPossibleWithoutUserRestore() {
  1579. list($user, $expectedResult) = $this->mockUser();
  1580. $this->appManager
  1581. ->expects($this->once())
  1582. ->method('isEnabledForUser')
  1583. ->with(
  1584. $this->equalTo('encryption')
  1585. )
  1586. ->will($this->returnValue(true));
  1587. $this->config
  1588. ->expects($this->once())
  1589. ->method('getAppValue')
  1590. ->with(
  1591. $this->equalTo('encryption'),
  1592. $this->equalTo('recoveryAdminEnabled'),
  1593. $this->anything()
  1594. )
  1595. ->will($this->returnValue('1'));
  1596. $this->config
  1597. ->expects($this->at(1))
  1598. ->method('getUserValue')
  1599. ->with(
  1600. $this->anything(),
  1601. $this->equalTo('encryption'),
  1602. $this->equalTo('recoveryEnabled'),
  1603. $this->anything()
  1604. )
  1605. ->will($this->returnValue('0'));
  1606. $expectedResult['isRestoreDisabled'] = true;
  1607. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  1608. ->disableOriginalConstructor()
  1609. ->getMock();
  1610. $subadmin->expects($this->once())
  1611. ->method('getSubAdminsGroups')
  1612. ->with($user)
  1613. ->will($this->returnValue([]));
  1614. $this->groupManager
  1615. ->expects($this->any())
  1616. ->method('getSubAdmin')
  1617. ->will($this->returnValue($subadmin));
  1618. $controller = $this->getController(true);
  1619. $result = self::invokePrivate($controller, 'formatUserForIndex', [$user]);
  1620. $this->assertEquals($expectedResult, $result);
  1621. }
  1622. public function testNoAvatar() {
  1623. $controller = $this->getController(true);
  1624. list($user, $expectedResult) = $this->mockUser();
  1625. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  1626. ->disableOriginalConstructor()
  1627. ->getMock();
  1628. $subadmin->expects($this->once())
  1629. ->method('getSubAdminsGroups')
  1630. ->with($user)
  1631. ->will($this->returnValue([]));
  1632. $this->groupManager
  1633. ->expects($this->any())
  1634. ->method('getSubAdmin')
  1635. ->will($this->returnValue($subadmin));
  1636. $this->avatarManager
  1637. ->method('getAvatar')
  1638. ->will($this->throwException(new \OCP\Files\NotFoundException()));
  1639. $expectedResult['isAvatarAvailable'] = false;
  1640. $result = self::invokePrivate($controller, 'formatUserForIndex', [$user]);
  1641. $this->assertEquals($expectedResult, $result);
  1642. }
  1643. /**
  1644. * @return array
  1645. */
  1646. public function setEmailAddressData() {
  1647. return [
  1648. /* mailAddress, isValid, expectsUpdate, canChangeDisplayName, responseCode */
  1649. [ '', true, true, true, Http::STATUS_OK ],
  1650. [ 'foo@local', true, true, true, Http::STATUS_OK],
  1651. [ 'foo@bar@local', false, false, true, Http::STATUS_UNPROCESSABLE_ENTITY],
  1652. [ 'foo@local', true, false, false, Http::STATUS_FORBIDDEN],
  1653. ];
  1654. }
  1655. /**
  1656. * @dataProvider setEmailAddressData
  1657. *
  1658. * @param string $mailAddress
  1659. * @param bool $isValid
  1660. * @param bool $expectsUpdate
  1661. * @param bool $expectsDelete
  1662. */
  1663. public function testSetEmailAddress($mailAddress, $isValid, $expectsUpdate, $canChangeDisplayName, $responseCode) {
  1664. $controller = $this->getController(true);
  1665. $user = $this->getMockBuilder('\OC\User\User')
  1666. ->disableOriginalConstructor()->getMock();
  1667. $user
  1668. ->expects($this->any())
  1669. ->method('getUID')
  1670. ->will($this->returnValue('foo'));
  1671. $user
  1672. ->expects($this->any())
  1673. ->method('canChangeDisplayName')
  1674. ->will($this->returnValue($canChangeDisplayName));
  1675. $user
  1676. ->expects($expectsUpdate ? $this->once() : $this->never())
  1677. ->method('setEMailAddress')
  1678. ->with(
  1679. $this->equalTo($mailAddress)
  1680. );
  1681. $this->userSession
  1682. ->expects($this->atLeastOnce())
  1683. ->method('getUser')
  1684. ->will($this->returnValue($user));
  1685. $this->mailer
  1686. ->expects($this->any())
  1687. ->method('validateMailAddress')
  1688. ->with($mailAddress)
  1689. ->willReturn($isValid);
  1690. if ($isValid) {
  1691. $user->expects($this->atLeastOnce())
  1692. ->method('canChangeDisplayName')
  1693. ->willReturn(true);
  1694. $this->userManager
  1695. ->expects($this->atLeastOnce())
  1696. ->method('get')
  1697. ->with('foo')
  1698. ->will($this->returnValue($user));
  1699. }
  1700. $response = $controller->setMailAddress($user->getUID(), $mailAddress);
  1701. $this->assertSame($responseCode, $response->getStatus());
  1702. }
  1703. public function testStatsAdmin() {
  1704. $controller = $this->getController(true);
  1705. $this->userManager
  1706. ->expects($this->at(0))
  1707. ->method('countUsers')
  1708. ->will($this->returnValue([128, 44]));
  1709. $expectedResponse = new DataResponse(
  1710. [
  1711. 'totalUsers' => 172
  1712. ]
  1713. );
  1714. $response = $controller->stats();
  1715. $this->assertEquals($expectedResponse, $response);
  1716. }
  1717. /**
  1718. * Tests that the subadmin stats return unique users, even
  1719. * when a user appears in several groups.
  1720. */
  1721. public function testStatsSubAdmin() {
  1722. $controller = $this->getController(false);
  1723. $user = $this->getMockBuilder('\OC\User\User')
  1724. ->disableOriginalConstructor()->getMock();
  1725. $this->userSession
  1726. ->expects($this->once())
  1727. ->method('getUser')
  1728. ->will($this->returnValue($user));
  1729. $group1 = $this->getMockBuilder('\OC\Group\Group')
  1730. ->disableOriginalConstructor()->getMock();
  1731. $group1
  1732. ->expects($this->once())
  1733. ->method('getUsers')
  1734. ->will($this->returnValue(['foo' => 'M. Foo', 'admin' => 'S. Admin']));
  1735. $group2 = $this->getMockBuilder('\OC\Group\Group')
  1736. ->disableOriginalConstructor()->getMock();
  1737. $group2
  1738. ->expects($this->once())
  1739. ->method('getUsers')
  1740. ->will($this->returnValue(['bar' => 'B. Ar']));
  1741. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  1742. ->disableOriginalConstructor()
  1743. ->getMock();
  1744. $subadmin
  1745. ->expects($this->at(0))
  1746. ->method('getSubAdminsGroups')
  1747. ->will($this->returnValue([$group1, $group2]));
  1748. $this->groupManager
  1749. ->expects($this->any())
  1750. ->method('getSubAdmin')
  1751. ->will($this->returnValue($subadmin));
  1752. $expectedResponse = new DataResponse(
  1753. [
  1754. 'totalUsers' => 3
  1755. ]
  1756. );
  1757. $response = $controller->stats();
  1758. $this->assertEquals($expectedResponse, $response);
  1759. }
  1760. public function testSetDisplayNameNull() {
  1761. $user = $this->createMock(IUser::class);
  1762. $user->method('getUID')->willReturn('userName');
  1763. $this->userSession
  1764. ->expects($this->once())
  1765. ->method('getUser')
  1766. ->willReturn($user);
  1767. $expectedResponse = new DataResponse(
  1768. [
  1769. 'status' => 'error',
  1770. 'data' => [
  1771. 'message' => 'Authentication error',
  1772. ],
  1773. ]
  1774. );
  1775. $controller = $this->getController(true);
  1776. $response = $controller->setDisplayName(null, 'displayName');
  1777. $this->assertEquals($expectedResponse, $response);
  1778. }
  1779. public function dataSetDisplayName() {
  1780. $data = [];
  1781. $user1 = $this->createMock(IUser::class);
  1782. $user1->method('getUID')->willReturn('user1');
  1783. $user1->method('canChangeDisplayName')->willReturn(true);
  1784. $data[] = [$user1, $user1, false, false, true];
  1785. $user1 = $this->createMock(IUser::class);
  1786. $user1->method('getUID')->willReturn('user1');
  1787. $user1->method('canChangeDisplayName')->willReturn(false);
  1788. $data[] = [$user1, $user1, false, false, false];
  1789. $user1 = $this->createMock(IUser::class);
  1790. $user1->method('getUID')->willReturn('user1');
  1791. $user2 = $this->createMock(IUser::class);
  1792. $user2->method('getUID')->willReturn('user2');
  1793. $user2->method('canChangeDisplayName')->willReturn(true);
  1794. $data[] = [$user1, $user2, false, false, false];
  1795. $user1 = $this->createMock(IUser::class);
  1796. $user1->method('getUID')->willReturn('user1');
  1797. $user2 = $this->createMock(IUser::class);
  1798. $user2->method('getUID')->willReturn('user2');
  1799. $user2->method('canChangeDisplayName')->willReturn(true);
  1800. $data[] = [$user1, $user2, true, false, true];
  1801. $user1 = $this->createMock(IUser::class);
  1802. $user1->method('getUID')->willReturn('user1');
  1803. $user2 = $this->createMock(IUser::class);
  1804. $user2->method('getUID')->willReturn('user2');
  1805. $user2->method('canChangeDisplayName')->willReturn(true);
  1806. $data[] = [$user1, $user2, false, true, true];
  1807. return $data;
  1808. }
  1809. /**
  1810. * @dataProvider dataSetDisplayName
  1811. *
  1812. * @param IUser|\PHPUnit_Framework_MockObject_MockObject $currentUser
  1813. * @param IUser|\PHPUnit_Framework_MockObject_MockObject $editUser
  1814. * @param bool $isAdmin
  1815. * @param bool $isSubAdmin
  1816. * @param bool $valid
  1817. */
  1818. public function testSetDisplayName($currentUser, $editUser, $isAdmin, $isSubAdmin, $valid) {
  1819. $this->userSession
  1820. ->expects($this->once())
  1821. ->method('getUser')
  1822. ->willReturn($currentUser);
  1823. $this->userManager
  1824. ->expects($this->once())
  1825. ->method('get')
  1826. ->with($editUser->getUID())
  1827. ->willReturn($editUser);
  1828. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  1829. ->disableOriginalConstructor()
  1830. ->getMock();
  1831. $subadmin
  1832. ->method('isUserAccessible')
  1833. ->with($currentUser, $editUser)
  1834. ->willReturn($isSubAdmin);
  1835. $this->groupManager
  1836. ->method('getSubAdmin')
  1837. ->willReturn($subadmin);
  1838. $this->groupManager
  1839. ->method('isAdmin')
  1840. ->with($currentUser->getUID())
  1841. ->willReturn($isAdmin);
  1842. if ($valid === true) {
  1843. $editUser->expects($this->once())
  1844. ->method('setDisplayName')
  1845. ->with('newDisplayName')
  1846. ->willReturn(true);
  1847. $expectedResponse = new DataResponse(
  1848. [
  1849. 'status' => 'success',
  1850. 'data' => [
  1851. 'message' => 'Your full name has been changed.',
  1852. 'username' => $editUser->getUID(),
  1853. 'displayName' => 'newDisplayName',
  1854. ],
  1855. ]
  1856. );
  1857. } else {
  1858. $editUser->expects($this->never())->method('setDisplayName');
  1859. $expectedResponse = new DataResponse(
  1860. [
  1861. 'status' => 'error',
  1862. 'data' => [
  1863. 'message' => 'Authentication error',
  1864. ],
  1865. ]
  1866. );
  1867. }
  1868. $controller = $this->getController(true);
  1869. $response = $controller->setDisplayName($editUser->getUID(), 'newDisplayName');
  1870. $this->assertEquals($expectedResponse, $response);
  1871. }
  1872. public function testSetDisplayNameFails() {
  1873. /** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
  1874. $user = $this->createMock(IUser::class);
  1875. $user->method('canChangeDisplayname')->willReturn(true);
  1876. $user->method('getUID')->willReturn('user');
  1877. $user->expects($this->once())
  1878. ->method('setDisplayName')
  1879. ->with('newDisplayName')
  1880. ->willReturn(false);
  1881. $user->method('getDisplayName')->willReturn('oldDisplayName');
  1882. $this->userSession
  1883. ->expects($this->once())
  1884. ->method('getUser')
  1885. ->willReturn($user);
  1886. $this->userManager
  1887. ->expects($this->once())
  1888. ->method('get')
  1889. ->with($user->getUID())
  1890. ->willReturn($user);
  1891. $subadmin = $this->getMockBuilder('\OC\SubAdmin')
  1892. ->disableOriginalConstructor()
  1893. ->getMock();
  1894. $subadmin
  1895. ->method('isUserAccessible')
  1896. ->with($user, $user)
  1897. ->willReturn(false);
  1898. $this->groupManager
  1899. ->method('getSubAdmin')
  1900. ->willReturn($subadmin);
  1901. $this->groupManager
  1902. ->expects($this->once())
  1903. ->method('isAdmin')
  1904. ->with($user->getUID())
  1905. ->willReturn(false);
  1906. $expectedResponse = new DataResponse(
  1907. [
  1908. 'status' => 'error',
  1909. 'data' => [
  1910. 'message' => 'Unable to change full name',
  1911. 'displayName' => 'oldDisplayName',
  1912. ],
  1913. ]
  1914. );
  1915. $controller = $this->getController(true);
  1916. $response = $controller->setDisplayName($user->getUID(), 'newDisplayName');
  1917. $this->assertEquals($expectedResponse, $response);
  1918. }
  1919. }