UsersControllerTest.php 64 KB

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