UsersControllerTest.php 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@owncloud.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author michag86 <micha_g@arcor.de>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. * @author Tom Needham <tom@owncloud.com>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OCA\Provisioning_API\Tests\Controller;
  32. use Exception;
  33. use OC\Accounts\AccountManager;
  34. use OC\Authentication\Token\RemoteWipe;
  35. use OC\Group\Manager;
  36. use OCA\FederatedFileSharing\AppInfo\Application;
  37. use OCA\FederatedFileSharing\FederatedShareProvider;
  38. use OCA\Provisioning_API\FederatedFileSharingFactory;
  39. use OCP\App\IAppManager;
  40. use OCP\Mail\IEMailTemplate;
  41. use OCA\Settings\Mailer\NewUserMailHelper;
  42. use OC\SubAdmin;
  43. use OCA\Provisioning_API\Controller\UsersController;
  44. use OCP\AppFramework\Http\DataResponse;
  45. use OCP\IConfig;
  46. use OCP\IGroup;
  47. use OCP\ILogger;
  48. use OCP\IL10N;
  49. use OCP\IRequest;
  50. use OCP\IUser;
  51. use OCP\IUserManager;
  52. use OCP\IUserSession;
  53. use OCP\L10N\IFactory;
  54. use OCP\Security\ISecureRandom;
  55. use OCP\UserInterface;
  56. use PHPUnit\Framework\MockObject\MockObject;
  57. use PHPUnit_Framework_MockObject_MockObject;
  58. use Test\TestCase;
  59. class UsersControllerTest extends TestCase {
  60. /** @var IUserManager|PHPUnit_Framework_MockObject_MockObject */
  61. protected $userManager;
  62. /** @var IConfig|PHPUnit_Framework_MockObject_MockObject */
  63. protected $config;
  64. /** @var IAppManager|PHPUnit_Framework_MockObject_MockObject */
  65. protected $appManager;
  66. /** @var Manager|PHPUnit_Framework_MockObject_MockObject */
  67. protected $groupManager;
  68. /** @var IUserSession|PHPUnit_Framework_MockObject_MockObject */
  69. protected $userSession;
  70. /** @var ILogger|PHPUnit_Framework_MockObject_MockObject */
  71. protected $logger;
  72. /** @var UsersController|PHPUnit_Framework_MockObject_MockObject */
  73. protected $api;
  74. /** @var AccountManager|PHPUnit_Framework_MockObject_MockObject */
  75. protected $accountManager;
  76. /** @var IRequest|PHPUnit_Framework_MockObject_MockObject */
  77. protected $request;
  78. /** @var IFactory|PHPUnit_Framework_MockObject_MockObject */
  79. private $l10nFactory;
  80. /** @var NewUserMailHelper|PHPUnit_Framework_MockObject_MockObject */
  81. private $newUserMailHelper;
  82. /** @var FederatedFileSharingFactory|\PHPUnit_Framework_MockObject_MockObject */
  83. private $federatedFileSharingFactory;
  84. /** @var ISecureRandom|\PHPUnit_Framework_MockObject_MockObject */
  85. private $secureRandom;
  86. /** @var RemoteWipe|MockObject */
  87. private $remoteWipe;
  88. protected function setUp() {
  89. parent::setUp();
  90. $this->userManager = $this->createMock(IUserManager::class);
  91. $this->config = $this->createMock(IConfig::class);
  92. $this->appManager = $this->createMock(IAppManager::class);
  93. $this->groupManager = $this->createMock(Manager::class);
  94. $this->userSession = $this->createMock(IUserSession::class);
  95. $this->logger = $this->createMock(ILogger::class);
  96. $this->request = $this->createMock(IRequest::class);
  97. $this->accountManager = $this->createMock(AccountManager::class);
  98. $this->l10nFactory = $this->createMock(IFactory::class);
  99. $this->newUserMailHelper = $this->createMock(NewUserMailHelper::class);
  100. $this->federatedFileSharingFactory = $this->createMock(FederatedFileSharingFactory::class);
  101. $this->secureRandom = $this->createMock(ISecureRandom::class);
  102. $this->remoteWipe = $this->createMock(RemoteWipe::class);
  103. $this->api = $this->getMockBuilder(UsersController::class)
  104. ->setConstructorArgs([
  105. 'provisioning_api',
  106. $this->request,
  107. $this->userManager,
  108. $this->config,
  109. $this->appManager,
  110. $this->groupManager,
  111. $this->userSession,
  112. $this->accountManager,
  113. $this->logger,
  114. $this->l10nFactory,
  115. $this->newUserMailHelper,
  116. $this->federatedFileSharingFactory,
  117. $this->secureRandom,
  118. $this->remoteWipe,
  119. ])
  120. ->setMethods(['fillStorageInfo'])
  121. ->getMock();
  122. }
  123. public function testGetUsersAsAdmin() {
  124. $loggedInUser = $this->getMockBuilder(IUser::class)
  125. ->disableOriginalConstructor()
  126. ->getMock();
  127. $loggedInUser
  128. ->expects($this->once())
  129. ->method('getUID')
  130. ->will($this->returnValue('admin'));
  131. $this->userSession
  132. ->expects($this->once())
  133. ->method('getUser')
  134. ->will($this->returnValue($loggedInUser));
  135. $this->groupManager
  136. ->expects($this->once())
  137. ->method('isAdmin')
  138. ->will($this->returnValue(true));
  139. $this->userManager
  140. ->expects($this->once())
  141. ->method('search')
  142. ->with('MyCustomSearch')
  143. ->will($this->returnValue(['Admin' => [], 'Foo' => [], 'Bar' => []]));
  144. $expected = ['users' => [
  145. 'Admin',
  146. 'Foo',
  147. 'Bar',
  148. ],
  149. ];
  150. $this->assertEquals($expected, $this->api->getUsers('MyCustomSearch')->getData());
  151. }
  152. public function testGetUsersAsSubAdmin() {
  153. $loggedInUser = $this->getMockBuilder(IUser::class)
  154. ->disableOriginalConstructor()
  155. ->getMock();
  156. $loggedInUser
  157. ->expects($this->once())
  158. ->method('getUID')
  159. ->will($this->returnValue('subadmin'));
  160. $this->userSession
  161. ->expects($this->once())
  162. ->method('getUser')
  163. ->will($this->returnValue($loggedInUser));
  164. $this->groupManager
  165. ->expects($this->once())
  166. ->method('isAdmin')
  167. ->will($this->returnValue(false));
  168. $firstGroup = $this->getMockBuilder('OCP\IGroup')
  169. ->disableOriginalConstructor()
  170. ->getMock();
  171. $firstGroup
  172. ->expects($this->once())
  173. ->method('getGID')
  174. ->will($this->returnValue('FirstGroup'));
  175. $secondGroup = $this->getMockBuilder('OCP\IGroup')
  176. ->disableOriginalConstructor()
  177. ->getMock();
  178. $secondGroup
  179. ->expects($this->once())
  180. ->method('getGID')
  181. ->will($this->returnValue('SecondGroup'));
  182. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  183. ->disableOriginalConstructor()->getMock();
  184. $subAdminManager
  185. ->expects($this->once())
  186. ->method('isSubAdmin')
  187. ->with($loggedInUser)
  188. ->will($this->returnValue(true));
  189. $subAdminManager
  190. ->expects($this->once())
  191. ->method('getSubAdminsGroups')
  192. ->with($loggedInUser)
  193. ->will($this->returnValue([$firstGroup, $secondGroup]));
  194. $this->groupManager
  195. ->expects($this->once())
  196. ->method('getSubAdmin')
  197. ->will($this->returnValue($subAdminManager));
  198. $this->groupManager
  199. ->expects($this->any())
  200. ->method('displayNamesInGroup')
  201. ->will($this->onConsecutiveCalls(['AnotherUserInTheFirstGroup' => []], ['UserInTheSecondGroup' => []]));
  202. $expected = [
  203. 'users' => [
  204. 'AnotherUserInTheFirstGroup',
  205. 'UserInTheSecondGroup',
  206. ],
  207. ];
  208. $this->assertEquals($expected, $this->api->getUsers('MyCustomSearch')->getData());
  209. }
  210. /**
  211. * @expectedException \OCP\AppFramework\OCS\OCSException
  212. * @expectedExceptionCode 102
  213. */
  214. public function testAddUserAlreadyExisting() {
  215. $this->userManager
  216. ->expects($this->once())
  217. ->method('userExists')
  218. ->with('AlreadyExistingUser')
  219. ->will($this->returnValue(true));
  220. $this->logger
  221. ->expects($this->once())
  222. ->method('error')
  223. ->with('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']);
  224. $loggedInUser = $this->getMockBuilder(IUser::class)
  225. ->disableOriginalConstructor()
  226. ->getMock();
  227. $loggedInUser
  228. ->expects($this->once())
  229. ->method('getUID')
  230. ->will($this->returnValue('adminUser'));
  231. $this->userSession
  232. ->expects($this->once())
  233. ->method('getUser')
  234. ->will($this->returnValue($loggedInUser));
  235. $this->groupManager
  236. ->expects($this->once())
  237. ->method('isAdmin')
  238. ->with('adminUser')
  239. ->willReturn(true);
  240. $this->api->addUser('AlreadyExistingUser', 'password', '', '', []);
  241. }
  242. /**
  243. * @expectedException \OCP\AppFramework\OCS\OCSException
  244. * @expectedExceptionCode 104
  245. * @expectedExceptionMessage group NonExistingGroup does not exist
  246. */
  247. public function testAddUserNonExistingGroup() {
  248. $this->userManager
  249. ->expects($this->once())
  250. ->method('userExists')
  251. ->with('NewUser')
  252. ->willReturn(false);
  253. $loggedInUser = $this->getMockBuilder(IUser::class)
  254. ->disableOriginalConstructor()
  255. ->getMock();
  256. $loggedInUser
  257. ->expects($this->once())
  258. ->method('getUID')
  259. ->will($this->returnValue('adminUser'));
  260. $this->userSession
  261. ->expects($this->once())
  262. ->method('getUser')
  263. ->will($this->returnValue($loggedInUser));
  264. $this->groupManager
  265. ->expects($this->once())
  266. ->method('isAdmin')
  267. ->with('adminUser')
  268. ->willReturn(true);
  269. $this->groupManager
  270. ->expects($this->once())
  271. ->method('groupExists')
  272. ->with('NonExistingGroup')
  273. ->willReturn(false);
  274. $this->api->addUser('NewUser', 'pass', '', '', ['NonExistingGroup']);
  275. }
  276. /**
  277. * @expectedException \OCP\AppFramework\OCS\OCSException
  278. * @expectedExceptionCode 104
  279. * @expectedExceptionMessage group NonExistingGroup does not exist
  280. */
  281. public function testAddUserExistingGroupNonExistingGroup() {
  282. $this->userManager
  283. ->expects($this->once())
  284. ->method('userExists')
  285. ->with('NewUser')
  286. ->willReturn(false);
  287. $loggedInUser = $this->getMockBuilder(IUser::class)
  288. ->disableOriginalConstructor()
  289. ->getMock();
  290. $loggedInUser
  291. ->expects($this->once())
  292. ->method('getUID')
  293. ->will($this->returnValue('adminUser'));
  294. $this->userSession
  295. ->expects($this->once())
  296. ->method('getUser')
  297. ->will($this->returnValue($loggedInUser));
  298. $this->groupManager
  299. ->expects($this->once())
  300. ->method('isAdmin')
  301. ->with('adminUser')
  302. ->willReturn(true);
  303. $this->groupManager
  304. ->expects($this->exactly(2))
  305. ->method('groupExists')
  306. ->withConsecutive(
  307. ['ExistingGroup'],
  308. ['NonExistingGroup']
  309. )
  310. ->will($this->returnValueMap([
  311. ['ExistingGroup', true],
  312. ['NonExistingGroup', false]
  313. ]));
  314. $this->api->addUser('NewUser', 'pass', '', '', ['ExistingGroup', 'NonExistingGroup']);
  315. }
  316. public function testAddUserSuccessful() {
  317. $this->userManager
  318. ->expects($this->once())
  319. ->method('userExists')
  320. ->with('NewUser')
  321. ->will($this->returnValue(false));
  322. $this->userManager
  323. ->expects($this->once())
  324. ->method('createUser')
  325. ->with('NewUser', 'PasswordOfTheNewUser');
  326. $this->logger
  327. ->expects($this->once())
  328. ->method('info')
  329. ->with('Successful addUser call with userid: NewUser', ['app' => 'ocs_api']);
  330. $loggedInUser = $this->getMockBuilder(IUser::class)
  331. ->disableOriginalConstructor()
  332. ->getMock();
  333. $loggedInUser
  334. ->expects($this->once())
  335. ->method('getUID')
  336. ->will($this->returnValue('adminUser'));
  337. $this->userSession
  338. ->expects($this->once())
  339. ->method('getUser')
  340. ->will($this->returnValue($loggedInUser));
  341. $this->groupManager
  342. ->expects($this->once())
  343. ->method('isAdmin')
  344. ->with('adminUser')
  345. ->willReturn(true);
  346. $this->assertTrue(key_exists(
  347. 'id',
  348. $this->api->addUser('NewUser', 'PasswordOfTheNewUser')->getData()
  349. ));
  350. }
  351. public function testAddUserSuccessfulWithDisplayName() {
  352. $api = $this->getMockBuilder('OCA\Provisioning_API\Controller\UsersController')
  353. ->setConstructorArgs([
  354. 'provisioning_api',
  355. $this->request,
  356. $this->userManager,
  357. $this->config,
  358. $this->appManager,
  359. $this->groupManager,
  360. $this->userSession,
  361. $this->accountManager,
  362. $this->logger,
  363. $this->l10nFactory,
  364. $this->newUserMailHelper,
  365. $this->federatedFileSharingFactory,
  366. $this->secureRandom,
  367. $this->remoteWipe
  368. ])
  369. ->setMethods(['editUser'])
  370. ->getMock();
  371. $this->userManager
  372. ->expects($this->once())
  373. ->method('userExists')
  374. ->with('NewUser')
  375. ->will($this->returnValue(false));
  376. $this->userManager
  377. ->expects($this->once())
  378. ->method('createUser')
  379. ->with('NewUser', 'PasswordOfTheNewUser');
  380. $this->logger
  381. ->expects($this->once())
  382. ->method('info')
  383. ->with('Successful addUser call with userid: NewUser', ['app' => 'ocs_api']);
  384. $loggedInUser = $this->getMockBuilder(IUser::class)
  385. ->disableOriginalConstructor()
  386. ->getMock();
  387. $loggedInUser
  388. ->expects($this->any())
  389. ->method('getUID')
  390. ->will($this->returnValue('adminUser'));
  391. $this->userSession
  392. ->expects($this->any())
  393. ->method('getUser')
  394. ->will($this->returnValue($loggedInUser));
  395. $this->groupManager
  396. ->expects($this->once())
  397. ->method('isAdmin')
  398. ->with('adminUser')
  399. ->willReturn(true);
  400. $api
  401. ->expects($this->once())
  402. ->method('editUser')
  403. ->with('NewUser', 'display', 'DisplayNameOfTheNewUser');
  404. $this->assertTrue(key_exists(
  405. 'id',
  406. $api->addUser('NewUser', 'PasswordOfTheNewUser', 'DisplayNameOfTheNewUser')->getData()
  407. ));
  408. }
  409. public function testAddUserSuccessfulGenerateUserID() {
  410. $this->config
  411. ->expects($this->any())
  412. ->method('getAppValue')
  413. ->willReturnCallback(function($appid, $key, $default) {
  414. if($key === 'newUser.generateUserID') {
  415. return 'yes';
  416. }
  417. return null;
  418. });
  419. $this->userManager
  420. ->expects($this->any())
  421. ->method('userExists')
  422. ->with($this->anything())
  423. ->will($this->returnValue(false));
  424. $this->userManager
  425. ->expects($this->once())
  426. ->method('createUser')
  427. ->with($this->anything(), 'PasswordOfTheNewUser');
  428. $this->logger
  429. ->expects($this->once())
  430. ->method('info')
  431. ->with($this->stringStartsWith('Successful addUser call with userid: '), ['app' => 'ocs_api']);
  432. $loggedInUser = $this->getMockBuilder(IUser::class)
  433. ->disableOriginalConstructor()
  434. ->getMock();
  435. $loggedInUser
  436. ->expects($this->once())
  437. ->method('getUID')
  438. ->will($this->returnValue('adminUser'));
  439. $this->userSession
  440. ->expects($this->once())
  441. ->method('getUser')
  442. ->will($this->returnValue($loggedInUser));
  443. $this->groupManager
  444. ->expects($this->once())
  445. ->method('isAdmin')
  446. ->with('adminUser')
  447. ->willReturn(true);
  448. $this->secureRandom->expects($this->any())
  449. ->method('generate')
  450. ->with(10)
  451. ->willReturnCallback(function() { return (string)rand(1000000000, 9999999999); });
  452. $this->assertTrue(key_exists(
  453. 'id',
  454. $this->api->addUser('', 'PasswordOfTheNewUser')->getData()
  455. ));
  456. }
  457. /**
  458. * @expectedException \OCP\AppFramework\OCS\OCSException
  459. * @expectedExceptionCode 111
  460. * @expectedExceptionMessage Could not create non-existing user id
  461. */
  462. public function testAddUserFailedToGenerateUserID() {
  463. $this->config
  464. ->expects($this->any())
  465. ->method('getAppValue')
  466. ->willReturnCallback(function($appid, $key, $default) {
  467. if($key === 'newUser.generateUserID') {
  468. return 'yes';
  469. }
  470. return null;
  471. });
  472. $this->userManager
  473. ->expects($this->any())
  474. ->method('userExists')
  475. ->with($this->anything())
  476. ->will($this->returnValue(true));
  477. $this->userManager
  478. ->expects($this->never())
  479. ->method('createUser');
  480. $loggedInUser = $this->getMockBuilder(IUser::class)
  481. ->disableOriginalConstructor()
  482. ->getMock();
  483. $loggedInUser
  484. ->expects($this->once())
  485. ->method('getUID')
  486. ->will($this->returnValue('adminUser'));
  487. $this->userSession
  488. ->expects($this->once())
  489. ->method('getUser')
  490. ->will($this->returnValue($loggedInUser));
  491. $this->groupManager
  492. ->expects($this->once())
  493. ->method('isAdmin')
  494. ->with('adminUser')
  495. ->willReturn(true);
  496. $this->api->addUser('', 'PasswordOfTheNewUser')->getData();
  497. }
  498. /**
  499. * @expectedException \OCP\AppFramework\OCS\OCSException
  500. * @expectedExceptionCode 110
  501. * @expectedExceptionMessage Required email address was not provided
  502. */
  503. public function testAddUserEmailRequired() {
  504. $this->config
  505. ->expects($this->any())
  506. ->method('getAppValue')
  507. ->willReturnCallback(function($appid, $key, $default) {
  508. if($key === 'newUser.requireEmail') {
  509. return 'yes';
  510. }
  511. return null;
  512. });
  513. $this->userManager
  514. ->expects($this->once())
  515. ->method('userExists')
  516. ->with('NewUser')
  517. ->will($this->returnValue(false));
  518. $this->userManager
  519. ->expects($this->never())
  520. ->method('createUser');
  521. $loggedInUser = $this->getMockBuilder(IUser::class)
  522. ->disableOriginalConstructor()
  523. ->getMock();
  524. $loggedInUser
  525. ->expects($this->once())
  526. ->method('getUID')
  527. ->will($this->returnValue('adminUser'));
  528. $this->userSession
  529. ->expects($this->once())
  530. ->method('getUser')
  531. ->will($this->returnValue($loggedInUser));
  532. $this->groupManager
  533. ->expects($this->once())
  534. ->method('isAdmin')
  535. ->with('adminUser')
  536. ->willReturn(true);
  537. $this->assertTrue(key_exists(
  538. 'id',
  539. $this->api->addUser('NewUser', 'PasswordOfTheNewUser')->getData()
  540. ));
  541. }
  542. public function testAddUserExistingGroup() {
  543. $this->userManager
  544. ->expects($this->once())
  545. ->method('userExists')
  546. ->with('NewUser')
  547. ->willReturn(false);
  548. $loggedInUser = $this->getMockBuilder(IUser::class)
  549. ->disableOriginalConstructor()
  550. ->getMock();
  551. $loggedInUser
  552. ->expects($this->once())
  553. ->method('getUID')
  554. ->will($this->returnValue('adminUser'));
  555. $this->userSession
  556. ->expects($this->once())
  557. ->method('getUser')
  558. ->will($this->returnValue($loggedInUser));
  559. $this->groupManager
  560. ->expects($this->once())
  561. ->method('isAdmin')
  562. ->with('adminUser')
  563. ->willReturn(true);
  564. $this->groupManager
  565. ->expects($this->once())
  566. ->method('groupExists')
  567. ->with('ExistingGroup')
  568. ->willReturn(true);
  569. $user = $this->getMockBuilder(IUser::class)
  570. ->disableOriginalConstructor()
  571. ->getMock();
  572. $this->userManager
  573. ->expects($this->once())
  574. ->method('createUser')
  575. ->with('NewUser', 'PasswordOfTheNewUser')
  576. ->willReturn($user);
  577. $group = $this->getMockBuilder('OCP\IGroup')
  578. ->disableOriginalConstructor()
  579. ->getMock();
  580. $group
  581. ->expects($this->once())
  582. ->method('addUser')
  583. ->with($user);
  584. $this->groupManager
  585. ->expects($this->once())
  586. ->method('get')
  587. ->with('ExistingGroup')
  588. ->willReturn($group);
  589. $this->logger
  590. ->expects($this->exactly(2))
  591. ->method('info')
  592. ->withConsecutive(
  593. ['Successful addUser call with userid: NewUser', ['app' => 'ocs_api']],
  594. ['Added userid NewUser to group ExistingGroup', ['app' => 'ocs_api']]
  595. );
  596. $this->assertTrue(key_exists(
  597. 'id',
  598. $this->api->addUser('NewUser', 'PasswordOfTheNewUser', '', '', ['ExistingGroup'])->getData()
  599. ));
  600. }
  601. /**
  602. * @expectedException \OCP\AppFramework\OCS\OCSException
  603. * @expectedExceptionCode 101
  604. * @expectedExceptionMessage Bad request
  605. */
  606. public function testAddUserUnsuccessful() {
  607. $exception = new Exception('User backend not found.');
  608. $this->userManager
  609. ->expects($this->once())
  610. ->method('userExists')
  611. ->with('NewUser')
  612. ->will($this->returnValue(false));
  613. $this->userManager
  614. ->expects($this->once())
  615. ->method('createUser')
  616. ->with('NewUser', 'PasswordOfTheNewUser')
  617. ->will($this->throwException($exception));
  618. $this->logger
  619. ->expects($this->once())
  620. ->method('logException')
  621. ->with($exception, [
  622. 'message' => 'Failed addUser attempt with exception.',
  623. 'level' => ILogger::ERROR,
  624. 'app' => 'ocs_api',
  625. ]);
  626. $loggedInUser = $this->getMockBuilder(IUser::class)
  627. ->disableOriginalConstructor()
  628. ->getMock();
  629. $loggedInUser
  630. ->expects($this->once())
  631. ->method('getUID')
  632. ->will($this->returnValue('adminUser'));
  633. $this->userSession
  634. ->expects($this->once())
  635. ->method('getUser')
  636. ->will($this->returnValue($loggedInUser));
  637. $this->groupManager
  638. ->expects($this->once())
  639. ->method('isAdmin')
  640. ->with('adminUser')
  641. ->willReturn(true);
  642. $this->api->addUser('NewUser', 'PasswordOfTheNewUser');
  643. }
  644. /**
  645. * @expectedException \OCP\AppFramework\OCS\OCSException
  646. * @expectedExceptionCode 106
  647. * @expectedExceptionMessage no group specified (required for subadmins)
  648. */
  649. public function testAddUserAsSubAdminNoGroup() {
  650. $loggedInUser = $this->getMockBuilder(IUser::class)
  651. ->disableOriginalConstructor()
  652. ->getMock();
  653. $loggedInUser
  654. ->expects($this->once())
  655. ->method('getUID')
  656. ->will($this->returnValue('regularUser'));
  657. $this->userSession
  658. ->expects($this->once())
  659. ->method('getUser')
  660. ->will($this->returnValue($loggedInUser));
  661. $this->groupManager
  662. ->expects($this->once())
  663. ->method('isAdmin')
  664. ->with('regularUser')
  665. ->willReturn(false);
  666. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  667. ->disableOriginalConstructor()->getMock();
  668. $this->groupManager
  669. ->expects($this->once())
  670. ->method('getSubAdmin')
  671. ->with()
  672. ->willReturn($subAdminManager);
  673. $this->api->addUser('NewUser', 'PasswordOfTheNewUser', '', '', []);
  674. }
  675. /**
  676. * @expectedException \OCP\AppFramework\OCS\OCSException
  677. * @expectedExceptionCode 105
  678. * @expectedExceptionMessage insufficient privileges for group ExistingGroup
  679. */
  680. public function testAddUserAsSubAdminValidGroupNotSubAdmin() {
  681. $loggedInUser = $this->getMockBuilder(IUser::class)
  682. ->disableOriginalConstructor()
  683. ->getMock();
  684. $loggedInUser
  685. ->expects($this->once())
  686. ->method('getUID')
  687. ->will($this->returnValue('regularUser'));
  688. $this->userSession
  689. ->expects($this->once())
  690. ->method('getUser')
  691. ->will($this->returnValue($loggedInUser));
  692. $this->groupManager
  693. ->expects($this->once())
  694. ->method('isAdmin')
  695. ->with('regularUser')
  696. ->willReturn(false);
  697. $existingGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  698. $this->groupManager
  699. ->expects($this->once())
  700. ->method('get')
  701. ->with('ExistingGroup')
  702. ->willReturn($existingGroup);
  703. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  704. ->disableOriginalConstructor()->getMock();
  705. $subAdminManager
  706. ->expects($this->once())
  707. ->method('isSubAdminOfGroup')
  708. ->with($loggedInUser, $existingGroup)
  709. ->willReturn(false);
  710. $this->groupManager
  711. ->expects($this->once())
  712. ->method('getSubAdmin')
  713. ->with()
  714. ->willReturn($subAdminManager);
  715. $this->groupManager
  716. ->expects($this->once())
  717. ->method('groupExists')
  718. ->with('ExistingGroup')
  719. ->willReturn(true);
  720. $this->api->addUser('NewUser', 'PasswordOfTheNewUser', '', '', ['ExistingGroup'])->getData();
  721. }
  722. public function testAddUserAsSubAdminExistingGroups() {
  723. $this->userManager
  724. ->expects($this->once())
  725. ->method('userExists')
  726. ->with('NewUser')
  727. ->willReturn(false);
  728. $loggedInUser = $this->getMockBuilder(IUser::class)
  729. ->disableOriginalConstructor()
  730. ->getMock();
  731. $loggedInUser
  732. ->expects($this->once())
  733. ->method('getUID')
  734. ->will($this->returnValue('subAdminUser'));
  735. $this->userSession
  736. ->expects($this->once())
  737. ->method('getUser')
  738. ->will($this->returnValue($loggedInUser));
  739. $this->groupManager
  740. ->expects($this->once())
  741. ->method('isAdmin')
  742. ->with('subAdminUser')
  743. ->willReturn(false);
  744. $this->groupManager
  745. ->expects($this->exactly(2))
  746. ->method('groupExists')
  747. ->withConsecutive(
  748. ['ExistingGroup1'],
  749. ['ExistingGroup2']
  750. )
  751. ->willReturn(true);
  752. $user = $this->getMockBuilder(IUser::class)
  753. ->disableOriginalConstructor()
  754. ->getMock();
  755. $this->userManager
  756. ->expects($this->once())
  757. ->method('createUser')
  758. ->with('NewUser', 'PasswordOfTheNewUser')
  759. ->willReturn($user);
  760. $existingGroup1 = $this->getMockBuilder('OCP\IGroup')
  761. ->disableOriginalConstructor()
  762. ->getMock();
  763. $existingGroup2 = $this->getMockBuilder('OCP\IGroup')
  764. ->disableOriginalConstructor()
  765. ->getMock();
  766. $existingGroup1
  767. ->expects($this->once())
  768. ->method('addUser')
  769. ->with($user);
  770. $existingGroup2
  771. ->expects($this->once())
  772. ->method('addUser')
  773. ->with($user);
  774. $this->groupManager
  775. ->expects($this->exactly(4))
  776. ->method('get')
  777. ->withConsecutive(
  778. ['ExistingGroup1'],
  779. ['ExistingGroup2'],
  780. ['ExistingGroup1'],
  781. ['ExistingGroup2']
  782. )
  783. ->will($this->returnValueMap([
  784. ['ExistingGroup1', $existingGroup1],
  785. ['ExistingGroup2', $existingGroup2]
  786. ]));
  787. $this->logger
  788. ->expects($this->exactly(3))
  789. ->method('info')
  790. ->withConsecutive(
  791. ['Successful addUser call with userid: NewUser', ['app' => 'ocs_api']],
  792. ['Added userid NewUser to group ExistingGroup1', ['app' => 'ocs_api']],
  793. ['Added userid NewUser to group ExistingGroup2', ['app' => 'ocs_api']]
  794. );
  795. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  796. ->disableOriginalConstructor()->getMock();
  797. $this->groupManager
  798. ->expects($this->once())
  799. ->method('getSubAdmin')
  800. ->willReturn($subAdminManager);
  801. $subAdminManager
  802. ->expects($this->exactly(2))
  803. ->method('isSubAdminOfGroup')
  804. ->withConsecutive(
  805. [$loggedInUser, $existingGroup1],
  806. [$loggedInUser, $existingGroup2]
  807. )
  808. ->willReturn(true);
  809. $this->assertTrue(key_exists(
  810. 'id',
  811. $this->api->addUser('NewUser', 'PasswordOfTheNewUser', '', '', ['ExistingGroup1', 'ExistingGroup2'])->getData()
  812. ));
  813. }
  814. /**
  815. * @expectedException \OCP\AppFramework\OCS\OCSException
  816. * @expectedExceptionCode 404
  817. * @expectedExceptionMessage User does not exist
  818. */
  819. public function testGetUserTargetDoesNotExist() {
  820. $loggedInUser = $this->getMockBuilder(IUser::class)
  821. ->disableOriginalConstructor()
  822. ->getMock();
  823. $this->userSession
  824. ->expects($this->once())
  825. ->method('getUser')
  826. ->will($this->returnValue($loggedInUser));
  827. $this->userManager
  828. ->expects($this->once())
  829. ->method('get')
  830. ->with('UserToGet')
  831. ->will($this->returnValue(null));
  832. $this->api->getUser('UserToGet');
  833. }
  834. public function testGetUserDataAsAdmin() {
  835. $group = $this->getMockBuilder(IGroup::class)
  836. ->disableOriginalConstructor()
  837. ->getMock();
  838. $loggedInUser = $this->getMockBuilder(IUser::class)
  839. ->disableOriginalConstructor()
  840. ->getMock();
  841. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  842. ->disableOriginalConstructor()
  843. ->getMock();
  844. $loggedInUser
  845. ->expects($this->once())
  846. ->method('getUID')
  847. ->will($this->returnValue('admin'));
  848. $targetUser = $this->getMockBuilder(IUser::class)
  849. ->disableOriginalConstructor()
  850. ->getMock();
  851. $targetUser->expects($this->once())
  852. ->method('getEMailAddress')
  853. ->willReturn('demo@nextcloud.com');
  854. $this->userSession
  855. ->expects($this->once())
  856. ->method('getUser')
  857. ->will($this->returnValue($loggedInUser));
  858. $this->userManager
  859. ->expects($this->exactly(2))
  860. ->method('get')
  861. ->with('UID')
  862. ->will($this->returnValue($targetUser));
  863. $this->groupManager
  864. ->expects($this->once())
  865. ->method('isAdmin')
  866. ->with('admin')
  867. ->will($this->returnValue(true));
  868. $this->groupManager
  869. ->expects($this->any())
  870. ->method('getUserGroups')
  871. ->willReturn([$group, $group, $group]);
  872. $this->groupManager
  873. ->expects($this->once())
  874. ->method('getSubAdmin')
  875. ->will($this->returnValue($subAdminManager));
  876. $subAdminManager
  877. ->expects($this->once())
  878. ->method('getSubAdminsGroups')
  879. ->willReturn([$group]);
  880. $group->expects($this->at(0))
  881. ->method('getGID')
  882. ->willReturn('group0');
  883. $group->expects($this->at(1))
  884. ->method('getGID')
  885. ->willReturn('group1');
  886. $group->expects($this->at(2))
  887. ->method('getGID')
  888. ->willReturn('group2');
  889. $group->expects($this->at(3))
  890. ->method('getGID')
  891. ->willReturn('group3');
  892. $this->accountManager->expects($this->any())->method('getUser')
  893. ->with($targetUser)
  894. ->willReturn(
  895. [
  896. AccountManager::PROPERTY_ADDRESS => ['value' => 'address'],
  897. AccountManager::PROPERTY_PHONE => ['value' => 'phone'],
  898. AccountManager::PROPERTY_TWITTER => ['value' => 'twitter'],
  899. AccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
  900. ]
  901. );
  902. $this->config
  903. ->expects($this->at(0))
  904. ->method('getUserValue')
  905. ->with('UID', 'core', 'enabled', 'true')
  906. ->will($this->returnValue('true'));
  907. $this->config
  908. ->expects($this->at(1))
  909. ->method('getUserValue')
  910. ->with('UID', 'core', 'lang')
  911. ->will($this->returnValue('de'));
  912. $this->api
  913. ->expects($this->once())
  914. ->method('fillStorageInfo')
  915. ->with('UID')
  916. ->will($this->returnValue(['DummyValue']));
  917. $backend = $this->createMock(UserInterface::class);
  918. $backend->expects($this->any())
  919. ->method('implementsActions')
  920. ->willReturn(true);
  921. $targetUser
  922. ->expects($this->once())
  923. ->method('getDisplayName')
  924. ->will($this->returnValue('Demo User'));
  925. $targetUser
  926. ->expects($this->once())
  927. ->method('getHome')
  928. ->will($this->returnValue('/var/www/newtcloud/data/UID'));
  929. $targetUser
  930. ->expects($this->once())
  931. ->method('getLastLogin')
  932. ->will($this->returnValue(1521191471));
  933. $targetUser
  934. ->expects($this->once())
  935. ->method('getBackendClassName')
  936. ->will($this->returnValue('Database'));
  937. $targetUser
  938. ->expects($this->once())
  939. ->method('getBackend')
  940. ->willReturn($backend);
  941. $targetUser
  942. ->expects($this->exactly(6))
  943. ->method('getUID')
  944. ->will($this->returnValue('UID'));
  945. $expected = [
  946. 'id' => 'UID',
  947. 'enabled' => true,
  948. 'storageLocation' => '/var/www/newtcloud/data/UID',
  949. 'lastLogin' => 1521191471000,
  950. 'backend' => 'Database',
  951. 'subadmin' => ['group3'],
  952. 'quota' => ['DummyValue'],
  953. 'email' => 'demo@nextcloud.com',
  954. 'displayname' => 'Demo User',
  955. 'phone' => 'phone',
  956. 'address' => 'address',
  957. 'website' => 'website',
  958. 'twitter' => 'twitter',
  959. 'groups' => ['group0', 'group1', 'group2'],
  960. 'language' => 'de',
  961. 'locale' => null,
  962. 'backendCapabilities' => [
  963. 'setDisplayName' => true,
  964. 'setPassword' => true,
  965. ]
  966. ];
  967. $this->assertEquals($expected, $this->invokePrivate($this->api, 'getUserData', ['UID']));
  968. }
  969. public function testGetUserDataAsSubAdminAndUserIsAccessible() {
  970. $loggedInUser = $this->getMockBuilder(IUser::class)
  971. ->disableOriginalConstructor()
  972. ->getMock();
  973. $loggedInUser
  974. ->expects($this->once())
  975. ->method('getUID')
  976. ->will($this->returnValue('subadmin'));
  977. $targetUser = $this->getMockBuilder(IUser::class)
  978. ->disableOriginalConstructor()
  979. ->getMock();
  980. $targetUser
  981. ->expects($this->once())
  982. ->method('getEMailAddress')
  983. ->willReturn('demo@nextcloud.com');
  984. $this->userSession
  985. ->expects($this->once())
  986. ->method('getUser')
  987. ->will($this->returnValue($loggedInUser));
  988. $this->userManager
  989. ->expects($this->exactly(2))
  990. ->method('get')
  991. ->with('UID')
  992. ->will($this->returnValue($targetUser));
  993. $this->groupManager
  994. ->expects($this->once())
  995. ->method('isAdmin')
  996. ->with('subadmin')
  997. ->will($this->returnValue(false));
  998. $this->groupManager
  999. ->expects($this->any())
  1000. ->method('getUserGroups')
  1001. ->willReturn([]);
  1002. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1003. ->disableOriginalConstructor()
  1004. ->getMock();
  1005. $subAdminManager
  1006. ->expects($this->once())
  1007. ->method('isUserAccessible')
  1008. ->with($loggedInUser, $targetUser)
  1009. ->will($this->returnValue(true));
  1010. $subAdminManager
  1011. ->expects($this->once())
  1012. ->method('getSubAdminsGroups')
  1013. ->willReturn([]);
  1014. $this->groupManager
  1015. ->expects($this->exactly(2))
  1016. ->method('getSubAdmin')
  1017. ->will($this->returnValue($subAdminManager));
  1018. $this->config
  1019. ->expects($this->at(0))
  1020. ->method('getUserValue')
  1021. ->with('UID', 'core', 'enabled', 'true')
  1022. ->will($this->returnValue('true'));
  1023. $this->config
  1024. ->expects($this->at(1))
  1025. ->method('getUserValue')
  1026. ->with('UID', 'core', 'lang')
  1027. ->will($this->returnValue('da'));
  1028. $this->api
  1029. ->expects($this->once())
  1030. ->method('fillStorageInfo')
  1031. ->with('UID')
  1032. ->will($this->returnValue(['DummyValue']));
  1033. $backend = $this->createMock(UserInterface::class);
  1034. $backend->expects($this->any())
  1035. ->method('implementsActions')
  1036. ->willReturn(true);
  1037. $targetUser
  1038. ->expects($this->once())
  1039. ->method('getDisplayName')
  1040. ->will($this->returnValue('Demo User'));
  1041. $targetUser
  1042. ->expects($this->once())
  1043. ->method('getHome')
  1044. ->will($this->returnValue('/var/www/newtcloud/data/UID'));
  1045. $targetUser
  1046. ->expects($this->once())
  1047. ->method('getLastLogin')
  1048. ->will($this->returnValue(1521191471));
  1049. $targetUser
  1050. ->expects($this->once())
  1051. ->method('getBackendClassName')
  1052. ->will($this->returnValue('Database'));
  1053. $targetUser
  1054. ->expects($this->once())
  1055. ->method('getBackend')
  1056. ->willReturn($backend);
  1057. $targetUser
  1058. ->expects($this->exactly(6))
  1059. ->method('getUID')
  1060. ->will($this->returnValue('UID'));
  1061. $this->accountManager->expects($this->any())->method('getUser')
  1062. ->with($targetUser)
  1063. ->willReturn(
  1064. [
  1065. AccountManager::PROPERTY_ADDRESS => ['value' => 'address'],
  1066. AccountManager::PROPERTY_PHONE => ['value' => 'phone'],
  1067. AccountManager::PROPERTY_TWITTER => ['value' => 'twitter'],
  1068. AccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
  1069. ]
  1070. );
  1071. $expected = [
  1072. 'id' => 'UID',
  1073. 'enabled' => true,
  1074. 'storageLocation' => '/var/www/newtcloud/data/UID',
  1075. 'lastLogin' => 1521191471000,
  1076. 'backend' => 'Database',
  1077. 'subadmin' => [],
  1078. 'quota' => ['DummyValue'],
  1079. 'email' => 'demo@nextcloud.com',
  1080. 'displayname' => 'Demo User',
  1081. 'phone' => 'phone',
  1082. 'address' => 'address',
  1083. 'website' => 'website',
  1084. 'twitter' => 'twitter',
  1085. 'groups' => [],
  1086. 'language' => 'da',
  1087. 'locale' => null,
  1088. 'backendCapabilities' => [
  1089. 'setDisplayName' => true,
  1090. 'setPassword' => true,
  1091. ]
  1092. ];
  1093. $this->assertEquals($expected, $this->invokePrivate($this->api, 'getUserData', ['UID']));
  1094. }
  1095. /**
  1096. * @expectedException \OCP\AppFramework\OCS\OCSException
  1097. * @expectedExceptionCode 997
  1098. */
  1099. public function testGetUserDataAsSubAdminAndUserIsNotAccessible() {
  1100. $loggedInUser = $this->getMockBuilder(IUser::class)
  1101. ->disableOriginalConstructor()
  1102. ->getMock();
  1103. $loggedInUser
  1104. ->expects($this->exactly(2))
  1105. ->method('getUID')
  1106. ->will($this->returnValue('subadmin'));
  1107. $targetUser = $this->getMockBuilder(IUser::class)
  1108. ->disableOriginalConstructor()
  1109. ->getMock();
  1110. $this->userSession
  1111. ->expects($this->once())
  1112. ->method('getUser')
  1113. ->will($this->returnValue($loggedInUser));
  1114. $this->userManager
  1115. ->expects($this->once())
  1116. ->method('get')
  1117. ->with('UserToGet')
  1118. ->will($this->returnValue($targetUser));
  1119. $this->groupManager
  1120. ->expects($this->once())
  1121. ->method('isAdmin')
  1122. ->with('subadmin')
  1123. ->will($this->returnValue(false));
  1124. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1125. ->disableOriginalConstructor()
  1126. ->getMock();
  1127. $subAdminManager
  1128. ->expects($this->once())
  1129. ->method('isUserAccessible')
  1130. ->with($loggedInUser, $targetUser)
  1131. ->will($this->returnValue(false));
  1132. $this->groupManager
  1133. ->expects($this->once())
  1134. ->method('getSubAdmin')
  1135. ->will($this->returnValue($subAdminManager));
  1136. $this->invokePrivate($this->api, 'getUser', ['UserToGet']);
  1137. }
  1138. public function testGetUserDataAsSubAdminSelfLookup() {
  1139. $loggedInUser = $this->getMockBuilder(IUser::class)
  1140. ->disableOriginalConstructor()
  1141. ->getMock();
  1142. $loggedInUser
  1143. ->expects($this->exactly(2))
  1144. ->method('getUID')
  1145. ->will($this->returnValue('UID'));
  1146. $targetUser = $this->getMockBuilder(IUser::class)
  1147. ->disableOriginalConstructor()
  1148. ->getMock();
  1149. $this->userSession
  1150. ->expects($this->once())
  1151. ->method('getUser')
  1152. ->will($this->returnValue($loggedInUser));
  1153. $this->userManager
  1154. ->expects($this->exactly(2))
  1155. ->method('get')
  1156. ->with('UID')
  1157. ->will($this->returnValue($targetUser));
  1158. $this->groupManager
  1159. ->expects($this->once())
  1160. ->method('isAdmin')
  1161. ->with('UID')
  1162. ->will($this->returnValue(false));
  1163. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1164. ->disableOriginalConstructor()
  1165. ->getMock();
  1166. $subAdminManager
  1167. ->expects($this->once())
  1168. ->method('isUserAccessible')
  1169. ->with($loggedInUser, $targetUser)
  1170. ->will($this->returnValue(false));
  1171. $subAdminManager
  1172. ->expects($this->once())
  1173. ->method('getSubAdminsGroups')
  1174. ->willReturn([]);
  1175. $this->groupManager
  1176. ->expects($this->exactly(2))
  1177. ->method('getSubAdmin')
  1178. ->will($this->returnValue($subAdminManager));
  1179. $this->groupManager
  1180. ->expects($this->any())
  1181. ->method('getUserGroups')
  1182. ->willReturn([]);
  1183. $this->api
  1184. ->expects($this->once())
  1185. ->method('fillStorageInfo')
  1186. ->with('UID')
  1187. ->will($this->returnValue(['DummyValue']));
  1188. $backend = $this->createMock(UserInterface::class);
  1189. $backend->expects($this->atLeastOnce())
  1190. ->method('implementsActions')
  1191. ->willReturn(false);
  1192. $targetUser
  1193. ->expects($this->once())
  1194. ->method('getDisplayName')
  1195. ->will($this->returnValue('Subadmin User'));
  1196. $targetUser
  1197. ->expects($this->once())
  1198. ->method('getEMailAddress')
  1199. ->will($this->returnValue('subadmin@nextcloud.com'));
  1200. $targetUser
  1201. ->expects($this->exactly(6))
  1202. ->method('getUID')
  1203. ->will($this->returnValue('UID'));
  1204. $targetUser
  1205. ->expects($this->once())
  1206. ->method('getHome')
  1207. ->will($this->returnValue('/var/www/newtcloud/data/UID'));
  1208. $targetUser
  1209. ->expects($this->once())
  1210. ->method('getLastLogin')
  1211. ->will($this->returnValue(1521191471));
  1212. $targetUser
  1213. ->expects($this->once())
  1214. ->method('getBackendClassName')
  1215. ->will($this->returnValue('Database'));
  1216. $targetUser
  1217. ->expects($this->once())
  1218. ->method('getBackend')
  1219. ->willReturn($backend);
  1220. $this->config
  1221. ->expects($this->at(0))
  1222. ->method('getUserValue')
  1223. ->with('UID', 'core', 'lang')
  1224. ->will($this->returnValue('ru'));
  1225. $this->accountManager->expects($this->any())->method('getUser')
  1226. ->with($targetUser)
  1227. ->willReturn(
  1228. [
  1229. AccountManager::PROPERTY_ADDRESS => ['value' => 'address'],
  1230. AccountManager::PROPERTY_PHONE => ['value' => 'phone'],
  1231. AccountManager::PROPERTY_TWITTER => ['value' => 'twitter'],
  1232. AccountManager::PROPERTY_WEBSITE => ['value' => 'website'],
  1233. ]
  1234. );
  1235. $expected = [
  1236. 'id' => 'UID',
  1237. 'storageLocation' => '/var/www/newtcloud/data/UID',
  1238. 'lastLogin' => 1521191471000,
  1239. 'backend' => 'Database',
  1240. 'subadmin' => [],
  1241. 'quota' => ['DummyValue'],
  1242. 'email' => 'subadmin@nextcloud.com',
  1243. 'displayname' => 'Subadmin User',
  1244. 'phone' => 'phone',
  1245. 'address' => 'address',
  1246. 'website' => 'website',
  1247. 'twitter' => 'twitter',
  1248. 'groups' => [],
  1249. 'language' => 'ru',
  1250. 'locale' => null,
  1251. 'backendCapabilities' => [
  1252. 'setDisplayName' => false,
  1253. 'setPassword' => false,
  1254. ]
  1255. ];
  1256. $this->assertEquals($expected, $this->invokePrivate($this->api, 'getUserData', ['UID']));
  1257. }
  1258. public function testEditUserRegularUserSelfEditChangeDisplayName() {
  1259. $loggedInUser = $this->getMockBuilder(IUser::class)
  1260. ->disableOriginalConstructor()
  1261. ->getMock();
  1262. $loggedInUser
  1263. ->expects($this->any())
  1264. ->method('getUID')
  1265. ->will($this->returnValue('UID'));
  1266. $targetUser = $this->getMockBuilder(IUser::class)
  1267. ->disableOriginalConstructor()
  1268. ->getMock();
  1269. $this->userSession
  1270. ->expects($this->once())
  1271. ->method('getUser')
  1272. ->will($this->returnValue($loggedInUser));
  1273. $this->userManager
  1274. ->expects($this->once())
  1275. ->method('get')
  1276. ->with('UserToEdit')
  1277. ->will($this->returnValue($targetUser));
  1278. $targetUser
  1279. ->expects($this->once())
  1280. ->method('setDisplayName')
  1281. ->with('NewDisplayName');
  1282. $targetUser
  1283. ->expects($this->any())
  1284. ->method('getUID')
  1285. ->will($this->returnValue('UID'));
  1286. $this->assertEquals([], $this->api->editUser('UserToEdit', 'display', 'NewDisplayName')->getData());
  1287. }
  1288. public function testEditUserRegularUserSelfEditChangeEmailValid() {
  1289. $loggedInUser = $this->getMockBuilder(IUser::class)
  1290. ->disableOriginalConstructor()
  1291. ->getMock();
  1292. $loggedInUser
  1293. ->expects($this->any())
  1294. ->method('getUID')
  1295. ->will($this->returnValue('UID'));
  1296. $targetUser = $this->getMockBuilder(IUser::class)
  1297. ->disableOriginalConstructor()
  1298. ->getMock();
  1299. $this->userSession
  1300. ->expects($this->once())
  1301. ->method('getUser')
  1302. ->will($this->returnValue($loggedInUser));
  1303. $this->userManager
  1304. ->expects($this->once())
  1305. ->method('get')
  1306. ->with('UserToEdit')
  1307. ->will($this->returnValue($targetUser));
  1308. $targetUser
  1309. ->expects($this->once())
  1310. ->method('setEMailAddress')
  1311. ->with('demo@nextcloud.com');
  1312. $targetUser
  1313. ->expects($this->any())
  1314. ->method('getUID')
  1315. ->will($this->returnValue('UID'));
  1316. $this->assertEquals([], $this->api->editUser('UserToEdit', 'email', 'demo@nextcloud.com')->getData());
  1317. }
  1318. /**
  1319. * @expectedException \OCP\AppFramework\OCS\OCSException
  1320. * @expectedExceptionCode 102
  1321. */
  1322. public function testEditUserRegularUserSelfEditChangeEmailInvalid() {
  1323. $loggedInUser = $this->getMockBuilder(IUser::class)
  1324. ->disableOriginalConstructor()
  1325. ->getMock();
  1326. $loggedInUser
  1327. ->expects($this->any())
  1328. ->method('getUID')
  1329. ->will($this->returnValue('UID'));
  1330. $targetUser = $this->getMockBuilder(IUser::class)
  1331. ->disableOriginalConstructor()
  1332. ->getMock();
  1333. $this->userSession
  1334. ->expects($this->once())
  1335. ->method('getUser')
  1336. ->will($this->returnValue($loggedInUser));
  1337. $this->userManager
  1338. ->expects($this->once())
  1339. ->method('get')
  1340. ->with('UserToEdit')
  1341. ->will($this->returnValue($targetUser));
  1342. $targetUser
  1343. ->expects($this->any())
  1344. ->method('getUID')
  1345. ->will($this->returnValue('UID'));
  1346. $this->api->editUser('UserToEdit', 'email', 'demo.org');
  1347. }
  1348. public function testEditUserRegularUserSelfEditChangePassword() {
  1349. $loggedInUser = $this->getMockBuilder(IUser::class)
  1350. ->disableOriginalConstructor()
  1351. ->getMock();
  1352. $loggedInUser
  1353. ->expects($this->any())
  1354. ->method('getUID')
  1355. ->will($this->returnValue('UID'));
  1356. $targetUser = $this->getMockBuilder(IUser::class)
  1357. ->disableOriginalConstructor()
  1358. ->getMock();
  1359. $this->userSession
  1360. ->expects($this->once())
  1361. ->method('getUser')
  1362. ->will($this->returnValue($loggedInUser));
  1363. $this->userManager
  1364. ->expects($this->once())
  1365. ->method('get')
  1366. ->with('UserToEdit')
  1367. ->will($this->returnValue($targetUser));
  1368. $targetUser
  1369. ->expects($this->once())
  1370. ->method('canChangePassword')
  1371. ->will($this->returnValue(true));
  1372. $targetUser
  1373. ->expects($this->once())
  1374. ->method('setPassword')
  1375. ->with('NewPassword');
  1376. $targetUser
  1377. ->expects($this->any())
  1378. ->method('getUID')
  1379. ->will($this->returnValue('UID'));
  1380. $this->assertEquals([], $this->api->editUser('UserToEdit', 'password', 'NewPassword')->getData());
  1381. }
  1382. /**
  1383. * @expectedException \OCP\AppFramework\OCS\OCSException
  1384. * @expectedExceptionCode 997
  1385. */
  1386. public function testEditUserRegularUserSelfEditChangeQuota() {
  1387. $loggedInUser = $this->getMockBuilder(IUser::class)
  1388. ->disableOriginalConstructor()
  1389. ->getMock();
  1390. $loggedInUser
  1391. ->expects($this->any())
  1392. ->method('getUID')
  1393. ->will($this->returnValue('UID'));
  1394. $targetUser = $this->getMockBuilder(IUser::class)
  1395. ->disableOriginalConstructor()
  1396. ->getMock();
  1397. $this->userSession
  1398. ->expects($this->once())
  1399. ->method('getUser')
  1400. ->will($this->returnValue($loggedInUser));
  1401. $this->userManager
  1402. ->expects($this->once())
  1403. ->method('get')
  1404. ->with('UserToEdit')
  1405. ->will($this->returnValue($targetUser));
  1406. $targetUser
  1407. ->expects($this->any())
  1408. ->method('getUID')
  1409. ->will($this->returnValue('UID'));
  1410. $this->api->editUser('UserToEdit', 'quota', 'NewQuota');
  1411. }
  1412. public function testEditUserAdminUserSelfEditChangeValidQuota() {
  1413. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1414. $loggedInUser
  1415. ->expects($this->any())
  1416. ->method('getUID')
  1417. ->will($this->returnValue('UID'));
  1418. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1419. $targetUser->expects($this->once())
  1420. ->method('setQuota')
  1421. ->with('2.9 MB');
  1422. $this->userSession
  1423. ->expects($this->once())
  1424. ->method('getUser')
  1425. ->will($this->returnValue($loggedInUser));
  1426. $this->userManager
  1427. ->expects($this->once())
  1428. ->method('get')
  1429. ->with('UserToEdit')
  1430. ->will($this->returnValue($targetUser));
  1431. $this->groupManager
  1432. ->expects($this->exactly(3))
  1433. ->method('isAdmin')
  1434. ->with('UID')
  1435. ->will($this->returnValue(true));
  1436. $targetUser
  1437. ->expects($this->any())
  1438. ->method('getUID')
  1439. ->will($this->returnValue('UID'));
  1440. $this->assertEquals([], $this->api->editUser('UserToEdit', 'quota', '3042824')->getData());
  1441. }
  1442. /**
  1443. * @expectedException \OCP\AppFramework\OCS\OCSException
  1444. * @expectedExceptionCode 103
  1445. * @expectedExceptionMessage Invalid quota value ABC
  1446. */
  1447. public function testEditUserAdminUserSelfEditChangeInvalidQuota() {
  1448. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1449. $loggedInUser
  1450. ->expects($this->any())
  1451. ->method('getUID')
  1452. ->will($this->returnValue('UID'));
  1453. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1454. $this->userSession
  1455. ->expects($this->once())
  1456. ->method('getUser')
  1457. ->will($this->returnValue($loggedInUser));
  1458. $this->userManager
  1459. ->expects($this->once())
  1460. ->method('get')
  1461. ->with('UserToEdit')
  1462. ->will($this->returnValue($targetUser));
  1463. $this->groupManager
  1464. ->expects($this->exactly(3))
  1465. ->method('isAdmin')
  1466. ->with('UID')
  1467. ->will($this->returnValue(true));
  1468. $targetUser
  1469. ->expects($this->any())
  1470. ->method('getUID')
  1471. ->will($this->returnValue('UID'));
  1472. $this->api->editUser('UserToEdit', 'quota', 'ABC');
  1473. }
  1474. public function testEditUserAdminUserEditChangeValidQuota() {
  1475. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1476. $loggedInUser
  1477. ->expects($this->any())
  1478. ->method('getUID')
  1479. ->will($this->returnValue('admin'));
  1480. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1481. $targetUser->expects($this->once())
  1482. ->method('setQuota')
  1483. ->with('2.9 MB');
  1484. $this->userSession
  1485. ->expects($this->once())
  1486. ->method('getUser')
  1487. ->will($this->returnValue($loggedInUser));
  1488. $this->userManager
  1489. ->expects($this->once())
  1490. ->method('get')
  1491. ->with('UserToEdit')
  1492. ->will($this->returnValue($targetUser));
  1493. $this->groupManager
  1494. ->expects($this->once())
  1495. ->method('isAdmin')
  1496. ->with('admin')
  1497. ->will($this->returnValue(true));
  1498. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1499. ->disableOriginalConstructor()
  1500. ->getMock();
  1501. $this->groupManager
  1502. ->expects($this->once())
  1503. ->method('getSubAdmin')
  1504. ->will($this->returnValue($subAdminManager));
  1505. $targetUser
  1506. ->expects($this->any())
  1507. ->method('getUID')
  1508. ->will($this->returnValue('UID'));
  1509. $this->assertEquals([], $this->api->editUser('UserToEdit', 'quota', '3042824')->getData());
  1510. }
  1511. public function testEditUserSelfEditChangeLanguage() {
  1512. $this->l10nFactory->expects($this->once())
  1513. ->method('findAvailableLanguages')
  1514. ->willReturn(['en', 'de', 'sv']);
  1515. $this->config->expects($this->any())
  1516. ->method('getSystemValue')
  1517. ->willReturnMap([
  1518. ['allow_user_to_change_display_name', true, true],
  1519. ['force_language', false, false],
  1520. ]);
  1521. $loggedInUser = $this->createMock(IUser::class);
  1522. $loggedInUser
  1523. ->expects($this->any())
  1524. ->method('getUID')
  1525. ->will($this->returnValue('UserToEdit'));
  1526. $targetUser = $this->createMock(IUser::class);
  1527. $this->config->expects($this->once())
  1528. ->method('setUserValue')
  1529. ->with('UserToEdit', 'core', 'lang', 'de');
  1530. $this->userSession
  1531. ->expects($this->once())
  1532. ->method('getUser')
  1533. ->will($this->returnValue($loggedInUser));
  1534. $this->userManager
  1535. ->expects($this->once())
  1536. ->method('get')
  1537. ->with('UserToEdit')
  1538. ->will($this->returnValue($targetUser));
  1539. $this->groupManager
  1540. ->expects($this->atLeastOnce())
  1541. ->method('isAdmin')
  1542. ->with('UserToEdit')
  1543. ->will($this->returnValue(false));
  1544. $targetUser
  1545. ->expects($this->any())
  1546. ->method('getUID')
  1547. ->will($this->returnValue('UserToEdit'));
  1548. $this->assertEquals([], $this->api->editUser('UserToEdit', 'language', 'de')->getData());
  1549. }
  1550. public function dataEditUserSelfEditChangeLanguageButForced() {
  1551. return [
  1552. ['de'],
  1553. [true],
  1554. ];
  1555. }
  1556. /**
  1557. * @dataProvider dataEditUserSelfEditChangeLanguageButForced
  1558. * @expectedException \OCP\AppFramework\OCS\OCSException
  1559. */
  1560. public function testEditUserSelfEditChangeLanguageButForced($forced) {
  1561. $this->config->expects($this->any())
  1562. ->method('getSystemValue')
  1563. ->willReturnMap([
  1564. ['allow_user_to_change_display_name', true, true],
  1565. ['force_language', false, $forced],
  1566. ]);
  1567. $loggedInUser = $this->createMock(IUser::class);
  1568. $loggedInUser
  1569. ->expects($this->any())
  1570. ->method('getUID')
  1571. ->will($this->returnValue('UserToEdit'));
  1572. $targetUser = $this->createMock(IUser::class);
  1573. $this->config->expects($this->never())
  1574. ->method('setUserValue');
  1575. $this->userSession
  1576. ->expects($this->once())
  1577. ->method('getUser')
  1578. ->will($this->returnValue($loggedInUser));
  1579. $this->userManager
  1580. ->expects($this->once())
  1581. ->method('get')
  1582. ->with('UserToEdit')
  1583. ->will($this->returnValue($targetUser));
  1584. $this->groupManager
  1585. ->expects($this->atLeastOnce())
  1586. ->method('isAdmin')
  1587. ->with('UserToEdit')
  1588. ->will($this->returnValue(false));
  1589. $targetUser
  1590. ->expects($this->any())
  1591. ->method('getUID')
  1592. ->will($this->returnValue('UserToEdit'));
  1593. $this->assertEquals([], $this->api->editUser('UserToEdit', 'language', 'de')->getData());
  1594. }
  1595. public function testEditUserAdminEditChangeLanguage() {
  1596. $this->l10nFactory->expects($this->once())
  1597. ->method('findAvailableLanguages')
  1598. ->willReturn(['en', 'de', 'sv']);
  1599. $loggedInUser = $this->createMock(IUser::class);
  1600. $loggedInUser
  1601. ->expects($this->any())
  1602. ->method('getUID')
  1603. ->will($this->returnValue('admin'));
  1604. $targetUser = $this->createMock(IUser::class);
  1605. $this->config->expects($this->once())
  1606. ->method('setUserValue')
  1607. ->with('UserToEdit', 'core', 'lang', 'de');
  1608. $this->userSession
  1609. ->expects($this->once())
  1610. ->method('getUser')
  1611. ->will($this->returnValue($loggedInUser));
  1612. $this->userManager
  1613. ->expects($this->once())
  1614. ->method('get')
  1615. ->with('UserToEdit')
  1616. ->will($this->returnValue($targetUser));
  1617. $this->groupManager
  1618. ->expects($this->once())
  1619. ->method('isAdmin')
  1620. ->with('admin')
  1621. ->will($this->returnValue(true));
  1622. $subAdminManager = $this->createMock(SubAdmin::class);
  1623. $this->groupManager
  1624. ->expects($this->once())
  1625. ->method('getSubAdmin')
  1626. ->will($this->returnValue($subAdminManager));
  1627. $targetUser
  1628. ->expects($this->any())
  1629. ->method('getUID')
  1630. ->will($this->returnValue('UserToEdit'));
  1631. $this->assertEquals([], $this->api->editUser('UserToEdit', 'language', 'de')->getData());
  1632. }
  1633. /**
  1634. * @dataProvider dataEditUserSelfEditChangeLanguageButForced
  1635. * @expectedException \OCP\AppFramework\OCS\OCSException
  1636. */
  1637. public function testEditUserAdminEditChangeLanguageInvalidLanguage() {
  1638. $this->l10nFactory->expects($this->once())
  1639. ->method('findAvailableLanguages')
  1640. ->willReturn(['en', 'de', 'sv']);
  1641. $loggedInUser = $this->createMock(IUser::class);
  1642. $loggedInUser
  1643. ->expects($this->any())
  1644. ->method('getUID')
  1645. ->will($this->returnValue('admin'));
  1646. $targetUser = $this->createMock(IUser::class);
  1647. $this->config->expects($this->never())
  1648. ->method('setUserValue');
  1649. $this->userSession
  1650. ->expects($this->once())
  1651. ->method('getUser')
  1652. ->will($this->returnValue($loggedInUser));
  1653. $this->userManager
  1654. ->expects($this->once())
  1655. ->method('get')
  1656. ->with('UserToEdit')
  1657. ->will($this->returnValue($targetUser));
  1658. $this->groupManager
  1659. ->expects($this->once())
  1660. ->method('isAdmin')
  1661. ->with('admin')
  1662. ->will($this->returnValue(true));
  1663. $subAdminManager = $this->createMock(SubAdmin::class);
  1664. $this->groupManager
  1665. ->expects($this->once())
  1666. ->method('getSubAdmin')
  1667. ->will($this->returnValue($subAdminManager));
  1668. $targetUser
  1669. ->expects($this->any())
  1670. ->method('getUID')
  1671. ->will($this->returnValue('UserToEdit'));
  1672. $this->assertEquals([], $this->api->editUser('UserToEdit', 'language', 'ru')->getData());
  1673. }
  1674. public function testEditUserSubadminUserAccessible() {
  1675. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1676. $loggedInUser
  1677. ->expects($this->any())
  1678. ->method('getUID')
  1679. ->will($this->returnValue('subadmin'));
  1680. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1681. $targetUser->expects($this->once())
  1682. ->method('setQuota')
  1683. ->with('2.9 MB');
  1684. $this->userSession
  1685. ->expects($this->once())
  1686. ->method('getUser')
  1687. ->will($this->returnValue($loggedInUser));
  1688. $this->userManager
  1689. ->expects($this->once())
  1690. ->method('get')
  1691. ->with('UserToEdit')
  1692. ->will($this->returnValue($targetUser));
  1693. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1694. ->disableOriginalConstructor()
  1695. ->getMock();
  1696. $subAdminManager
  1697. ->expects($this->once())
  1698. ->method('isUserAccessible')
  1699. ->with($loggedInUser, $targetUser)
  1700. ->will($this->returnValue(true));
  1701. $this->groupManager
  1702. ->expects($this->once())
  1703. ->method('getSubAdmin')
  1704. ->will($this->returnValue($subAdminManager));
  1705. $targetUser
  1706. ->expects($this->any())
  1707. ->method('getUID')
  1708. ->will($this->returnValue('UID'));
  1709. $this->assertEquals([], $this->api->editUser('UserToEdit', 'quota', '3042824')->getData());
  1710. }
  1711. /**
  1712. * @expectedException \OCP\AppFramework\OCS\OCSException
  1713. * @expectedExceptionCode 997
  1714. */
  1715. public function testEditUserSubadminUserInaccessible() {
  1716. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1717. $loggedInUser
  1718. ->expects($this->any())
  1719. ->method('getUID')
  1720. ->will($this->returnValue('subadmin'));
  1721. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1722. $this->userSession
  1723. ->expects($this->once())
  1724. ->method('getUser')
  1725. ->will($this->returnValue($loggedInUser));
  1726. $this->userManager
  1727. ->expects($this->once())
  1728. ->method('get')
  1729. ->with('UserToEdit')
  1730. ->will($this->returnValue($targetUser));
  1731. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1732. ->disableOriginalConstructor()
  1733. ->getMock();
  1734. $subAdminManager
  1735. ->expects($this->once())
  1736. ->method('isUserAccessible')
  1737. ->with($loggedInUser, $targetUser)
  1738. ->will($this->returnValue(false));
  1739. $this->groupManager
  1740. ->expects($this->once())
  1741. ->method('getSubAdmin')
  1742. ->will($this->returnValue($subAdminManager));
  1743. $targetUser
  1744. ->expects($this->any())
  1745. ->method('getUID')
  1746. ->will($this->returnValue('UID'));
  1747. $this->api->editUser('UserToEdit', 'quota', 'value');
  1748. }
  1749. /**
  1750. * @expectedException \OCP\AppFramework\OCS\OCSException
  1751. * @expectedExceptionCode 101
  1752. */
  1753. public function testDeleteUserNotExistingUser() {
  1754. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1755. $loggedInUser
  1756. ->expects($this->any())
  1757. ->method('getUID')
  1758. ->will($this->returnValue('UserToEdit'));
  1759. $this->userSession
  1760. ->expects($this->once())
  1761. ->method('getUser')
  1762. ->will($this->returnValue($loggedInUser));
  1763. $this->userManager
  1764. ->expects($this->once())
  1765. ->method('get')
  1766. ->with('UserToDelete')
  1767. ->will($this->returnValue(null));
  1768. $this->api->deleteUser('UserToDelete');
  1769. }
  1770. /**
  1771. * @expectedException \OCP\AppFramework\OCS\OCSException
  1772. * @expectedExceptionCode 101
  1773. */
  1774. public function testDeleteUserSelf() {
  1775. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1776. $loggedInUser
  1777. ->expects($this->any())
  1778. ->method('getUID')
  1779. ->will($this->returnValue('UID'));
  1780. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1781. $targetUser
  1782. ->expects($this->once())
  1783. ->method('getUID')
  1784. ->will($this->returnValue('UID'));
  1785. $this->userSession
  1786. ->expects($this->once())
  1787. ->method('getUser')
  1788. ->will($this->returnValue($loggedInUser));
  1789. $this->userManager
  1790. ->expects($this->once())
  1791. ->method('get')
  1792. ->with('UserToDelete')
  1793. ->will($this->returnValue($targetUser));
  1794. $this->api->deleteUser('UserToDelete');
  1795. }
  1796. public function testDeleteSuccessfulUserAsAdmin() {
  1797. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1798. $loggedInUser
  1799. ->expects($this->any())
  1800. ->method('getUID')
  1801. ->will($this->returnValue('admin'));
  1802. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1803. $targetUser
  1804. ->expects($this->once())
  1805. ->method('getUID')
  1806. ->will($this->returnValue('UID'));
  1807. $this->userSession
  1808. ->expects($this->once())
  1809. ->method('getUser')
  1810. ->will($this->returnValue($loggedInUser));
  1811. $this->userManager
  1812. ->expects($this->once())
  1813. ->method('get')
  1814. ->with('UserToDelete')
  1815. ->will($this->returnValue($targetUser));
  1816. $this->groupManager
  1817. ->expects($this->once())
  1818. ->method('isAdmin')
  1819. ->with('admin')
  1820. ->will($this->returnValue(true));
  1821. $targetUser
  1822. ->expects($this->once())
  1823. ->method('delete')
  1824. ->will($this->returnValue(true));
  1825. $this->assertEquals([], $this->api->deleteUser('UserToDelete')->getData());
  1826. }
  1827. /**
  1828. * @expectedException \OCP\AppFramework\OCS\OCSException
  1829. * @expectedExceptionCode 101
  1830. */
  1831. public function testDeleteUnsuccessfulUserAsAdmin() {
  1832. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1833. $loggedInUser
  1834. ->expects($this->any())
  1835. ->method('getUID')
  1836. ->will($this->returnValue('admin'));
  1837. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1838. $targetUser
  1839. ->expects($this->once())
  1840. ->method('getUID')
  1841. ->will($this->returnValue('UID'));
  1842. $this->userSession
  1843. ->expects($this->once())
  1844. ->method('getUser')
  1845. ->will($this->returnValue($loggedInUser));
  1846. $this->userManager
  1847. ->expects($this->once())
  1848. ->method('get')
  1849. ->with('UserToDelete')
  1850. ->will($this->returnValue($targetUser));
  1851. $this->groupManager
  1852. ->expects($this->once())
  1853. ->method('isAdmin')
  1854. ->with('admin')
  1855. ->will($this->returnValue(true));
  1856. $targetUser
  1857. ->expects($this->once())
  1858. ->method('delete')
  1859. ->will($this->returnValue(false));
  1860. $this->api->deleteUser('UserToDelete');
  1861. }
  1862. public function testDeleteSuccessfulUserAsSubadmin() {
  1863. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1864. $loggedInUser
  1865. ->expects($this->any())
  1866. ->method('getUID')
  1867. ->will($this->returnValue('subadmin'));
  1868. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1869. $targetUser
  1870. ->expects($this->once())
  1871. ->method('getUID')
  1872. ->will($this->returnValue('UID'));
  1873. $this->userSession
  1874. ->expects($this->once())
  1875. ->method('getUser')
  1876. ->will($this->returnValue($loggedInUser));
  1877. $this->userManager
  1878. ->expects($this->once())
  1879. ->method('get')
  1880. ->with('UserToDelete')
  1881. ->will($this->returnValue($targetUser));
  1882. $this->groupManager
  1883. ->expects($this->once())
  1884. ->method('isAdmin')
  1885. ->with('subadmin')
  1886. ->will($this->returnValue(false));
  1887. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1888. ->disableOriginalConstructor()->getMock();
  1889. $subAdminManager
  1890. ->expects($this->once())
  1891. ->method('isUserAccessible')
  1892. ->with($loggedInUser, $targetUser)
  1893. ->will($this->returnValue(true));
  1894. $this->groupManager
  1895. ->expects($this->once())
  1896. ->method('getSubAdmin')
  1897. ->will($this->returnValue($subAdminManager));
  1898. $targetUser
  1899. ->expects($this->once())
  1900. ->method('delete')
  1901. ->will($this->returnValue(true));
  1902. $this->assertEquals([], $this->api->deleteUser('UserToDelete')->getData());
  1903. }
  1904. /**
  1905. * @expectedException \OCP\AppFramework\OCS\OCSException
  1906. * @expectedExceptionCode 101
  1907. */
  1908. public function testDeleteUnsuccessfulUserAsSubadmin() {
  1909. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1910. $loggedInUser
  1911. ->expects($this->any())
  1912. ->method('getUID')
  1913. ->will($this->returnValue('subadmin'));
  1914. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1915. $targetUser
  1916. ->expects($this->once())
  1917. ->method('getUID')
  1918. ->will($this->returnValue('UID'));
  1919. $this->userSession
  1920. ->expects($this->once())
  1921. ->method('getUser')
  1922. ->will($this->returnValue($loggedInUser));
  1923. $this->userManager
  1924. ->expects($this->once())
  1925. ->method('get')
  1926. ->with('UserToDelete')
  1927. ->will($this->returnValue($targetUser));
  1928. $this->groupManager
  1929. ->expects($this->once())
  1930. ->method('isAdmin')
  1931. ->with('subadmin')
  1932. ->will($this->returnValue(false));
  1933. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1934. ->disableOriginalConstructor()->getMock();
  1935. $subAdminManager
  1936. ->expects($this->once())
  1937. ->method('isUserAccessible')
  1938. ->with($loggedInUser, $targetUser)
  1939. ->will($this->returnValue(true));
  1940. $this->groupManager
  1941. ->expects($this->once())
  1942. ->method('getSubAdmin')
  1943. ->will($this->returnValue($subAdminManager));
  1944. $targetUser
  1945. ->expects($this->once())
  1946. ->method('delete')
  1947. ->will($this->returnValue(false));
  1948. $this->api->deleteUser('UserToDelete');
  1949. }
  1950. /**
  1951. * @expectedException \OCP\AppFramework\OCS\OCSException
  1952. * @expectedExceptionCode 997
  1953. */
  1954. public function testDeleteUserAsSubAdminAndUserIsNotAccessible() {
  1955. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1956. $loggedInUser
  1957. ->expects($this->any())
  1958. ->method('getUID')
  1959. ->will($this->returnValue('subadmin'));
  1960. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1961. $targetUser
  1962. ->expects($this->once())
  1963. ->method('getUID')
  1964. ->will($this->returnValue('UID'));
  1965. $this->userSession
  1966. ->expects($this->once())
  1967. ->method('getUser')
  1968. ->will($this->returnValue($loggedInUser));
  1969. $this->userManager
  1970. ->expects($this->once())
  1971. ->method('get')
  1972. ->with('UserToDelete')
  1973. ->will($this->returnValue($targetUser));
  1974. $this->groupManager
  1975. ->expects($this->once())
  1976. ->method('isAdmin')
  1977. ->with('subadmin')
  1978. ->will($this->returnValue(false));
  1979. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  1980. ->disableOriginalConstructor()->getMock();
  1981. $subAdminManager
  1982. ->expects($this->once())
  1983. ->method('isUserAccessible')
  1984. ->with($loggedInUser, $targetUser)
  1985. ->will($this->returnValue(false));
  1986. $this->groupManager
  1987. ->expects($this->once())
  1988. ->method('getSubAdmin')
  1989. ->will($this->returnValue($subAdminManager));
  1990. $this->api->deleteUser('UserToDelete');
  1991. }
  1992. /**
  1993. * @expectedException \OCP\AppFramework\OCS\OCSException
  1994. * @expectedExceptionCode 998
  1995. */
  1996. public function testGetUsersGroupsTargetUserNotExisting() {
  1997. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  1998. $this->userSession
  1999. ->expects($this->once())
  2000. ->method('getUser')
  2001. ->will($this->returnValue($loggedInUser));
  2002. $this->api->getUsersGroups('UserToLookup');
  2003. }
  2004. public function testGetUsersGroupsSelfTargetted() {
  2005. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2006. $loggedInUser
  2007. ->expects($this->once())
  2008. ->method('getUID')
  2009. ->will($this->returnValue('UserToLookup'));
  2010. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2011. $targetUser
  2012. ->expects($this->once())
  2013. ->method('getUID')
  2014. ->will($this->returnValue('UserToLookup'));
  2015. $this->userSession
  2016. ->expects($this->once())
  2017. ->method('getUser')
  2018. ->will($this->returnValue($loggedInUser));
  2019. $this->userManager
  2020. ->expects($this->once())
  2021. ->method('get')
  2022. ->with('UserToLookup')
  2023. ->will($this->returnValue($targetUser));
  2024. $this->groupManager
  2025. ->expects($this->once())
  2026. ->method('getUserGroupIds')
  2027. ->with($targetUser)
  2028. ->will($this->returnValue(['DummyValue']));
  2029. $this->assertEquals(['groups' => ['DummyValue']], $this->api->getUsersGroups('UserToLookup')->getData());
  2030. }
  2031. public function testGetUsersGroupsForAdminUser() {
  2032. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2033. $loggedInUser
  2034. ->expects($this->exactly(2))
  2035. ->method('getUID')
  2036. ->will($this->returnValue('admin'));
  2037. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2038. $targetUser
  2039. ->expects($this->once())
  2040. ->method('getUID')
  2041. ->will($this->returnValue('UserToLookup'));
  2042. $this->userSession
  2043. ->expects($this->once())
  2044. ->method('getUser')
  2045. ->will($this->returnValue($loggedInUser));
  2046. $this->userManager
  2047. ->expects($this->once())
  2048. ->method('get')
  2049. ->with('UserToLookup')
  2050. ->will($this->returnValue($targetUser));
  2051. $this->groupManager
  2052. ->expects($this->once())
  2053. ->method('getUserGroupIds')
  2054. ->with($targetUser)
  2055. ->will($this->returnValue(['DummyValue']));
  2056. $this->groupManager
  2057. ->expects($this->once())
  2058. ->method('isAdmin')
  2059. ->with('admin')
  2060. ->will($this->returnValue(true));
  2061. $this->assertEquals(['groups' => ['DummyValue']], $this->api->getUsersGroups('UserToLookup')->getData());
  2062. }
  2063. public function testGetUsersGroupsForSubAdminUserAndUserIsAccessible() {
  2064. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2065. $loggedInUser
  2066. ->expects($this->exactly(2))
  2067. ->method('getUID')
  2068. ->will($this->returnValue('subadmin'));
  2069. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2070. $targetUser
  2071. ->expects($this->once())
  2072. ->method('getUID')
  2073. ->will($this->returnValue('UserToLookup'));
  2074. $this->userSession
  2075. ->expects($this->once())
  2076. ->method('getUser')
  2077. ->will($this->returnValue($loggedInUser));
  2078. $this->userManager
  2079. ->expects($this->once())
  2080. ->method('get')
  2081. ->with('UserToLookup')
  2082. ->will($this->returnValue($targetUser));
  2083. $this->groupManager
  2084. ->expects($this->once())
  2085. ->method('isAdmin')
  2086. ->with('subadmin')
  2087. ->will($this->returnValue(false));
  2088. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2089. ->disableOriginalConstructor()->getMock();
  2090. $subAdminManager
  2091. ->expects($this->once())
  2092. ->method('isUserAccessible')
  2093. ->with($loggedInUser, $targetUser)
  2094. ->will($this->returnValue(true));
  2095. $this->groupManager
  2096. ->expects($this->once())
  2097. ->method('getSubAdmin')
  2098. ->will($this->returnValue($subAdminManager));
  2099. $group1 = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2100. $group1
  2101. ->expects($this->any())
  2102. ->method('getGID')
  2103. ->will($this->returnValue('Group1'));
  2104. $group2 = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2105. $group2
  2106. ->expects($this->any())
  2107. ->method('getGID')
  2108. ->will($this->returnValue('Group2'));
  2109. $subAdminManager
  2110. ->expects($this->once())
  2111. ->method('getSubAdminsGroups')
  2112. ->with($loggedInUser)
  2113. ->will($this->returnValue([$group1, $group2]));
  2114. $this->groupManager
  2115. ->expects($this->any())
  2116. ->method('getUserGroupIds')
  2117. ->with($targetUser)
  2118. ->will($this->returnValue(['Group1']));
  2119. $this->assertEquals(['groups' => ['Group1']], $this->api->getUsersGroups('UserToLookup')->getData());
  2120. }
  2121. /**
  2122. * @expectedException \OCP\AppFramework\OCS\OCSException
  2123. * @expectedExceptionCode 997
  2124. */
  2125. public function testGetUsersGroupsForSubAdminUserAndUserIsInaccessible() {
  2126. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2127. $loggedInUser
  2128. ->expects($this->exactly(2))
  2129. ->method('getUID')
  2130. ->will($this->returnValue('subadmin'));
  2131. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2132. $targetUser
  2133. ->expects($this->once())
  2134. ->method('getUID')
  2135. ->will($this->returnValue('UserToLookup'));
  2136. $this->userSession
  2137. ->expects($this->once())
  2138. ->method('getUser')
  2139. ->will($this->returnValue($loggedInUser));
  2140. $this->userManager
  2141. ->expects($this->once())
  2142. ->method('get')
  2143. ->with('UserToLookup')
  2144. ->will($this->returnValue($targetUser));
  2145. $this->groupManager
  2146. ->expects($this->once())
  2147. ->method('isAdmin')
  2148. ->with('subadmin')
  2149. ->will($this->returnValue(false));
  2150. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2151. ->disableOriginalConstructor()->getMock();
  2152. $subAdminManager
  2153. ->expects($this->once())
  2154. ->method('isUserAccessible')
  2155. ->with($loggedInUser, $targetUser)
  2156. ->will($this->returnValue(false));
  2157. $this->groupManager
  2158. ->expects($this->once())
  2159. ->method('getSubAdmin')
  2160. ->will($this->returnValue($subAdminManager));
  2161. $this->groupManager
  2162. ->expects($this->any())
  2163. ->method('getUserGroupIds')
  2164. ->with($targetUser)
  2165. ->will($this->returnValue(['Group1']));
  2166. $this->api->getUsersGroups('UserToLookup');
  2167. }
  2168. /**
  2169. * @expectedException \OCP\AppFramework\OCS\OCSException
  2170. * @expectedExceptionCode 102
  2171. */
  2172. public function testAddToGroupWithTargetGroupNotExisting() {
  2173. $this->groupManager->expects($this->once())
  2174. ->method('get')
  2175. ->with('GroupToAddTo')
  2176. ->willReturn(null);
  2177. $this->api->addToGroup('TargetUser', 'GroupToAddTo');
  2178. }
  2179. /**
  2180. * @expectedException \OCP\AppFramework\OCS\OCSException
  2181. * @expectedExceptionCode 101
  2182. */
  2183. public function testAddToGroupWithNoGroupSpecified() {
  2184. $this->api->addToGroup('TargetUser');
  2185. }
  2186. /**
  2187. * @expectedException \OCP\AppFramework\OCS\OCSException
  2188. * @expectedExceptionCode 103
  2189. */
  2190. public function testAddToGroupWithTargetUserNotExisting() {
  2191. $targetGroup = $this->createMock(IGroup::class);
  2192. $this->groupManager->expects($this->once())
  2193. ->method('get')
  2194. ->with('GroupToAddTo')
  2195. ->willReturn($targetGroup);
  2196. $this->api->addToGroup('TargetUser', 'GroupToAddTo');
  2197. }
  2198. /**
  2199. * @expectedException \OCP\AppFramework\OCS\OCSException
  2200. * @expectedExceptionCode 104
  2201. */
  2202. public function testAddToGroupNoSubadmin() {
  2203. $targetUser = $this->createMock(IUser::class);
  2204. $loggedInUser = $this->createMock(IUser::class);
  2205. $loggedInUser->expects($this->once())
  2206. ->method('getUID')
  2207. ->willReturn('subadmin');
  2208. $targetGroup = $this->createMock(IGroup::class);
  2209. $targetGroup->expects($this->never())
  2210. ->method('addUser')
  2211. ->with($targetUser);
  2212. $this->groupManager->expects($this->once())
  2213. ->method('get')
  2214. ->with('GroupToAddTo')
  2215. ->willReturn($targetGroup);
  2216. $subAdminManager = $this->createMock(SubAdmin::class);
  2217. $subAdminManager->expects($this->once())
  2218. ->method('isSubAdminOfGroup')
  2219. ->with($loggedInUser, $targetGroup)
  2220. ->willReturn(false);
  2221. $this->groupManager->expects($this->once())
  2222. ->method('getSubAdmin')
  2223. ->willReturn($subAdminManager);
  2224. $this->groupManager->expects($this->once())
  2225. ->method('isAdmin')
  2226. ->with('subadmin')
  2227. ->willReturn(false);
  2228. $this->userManager->expects($this->once())
  2229. ->method('get')
  2230. ->with('TargetUser')
  2231. ->willReturn($targetUser);
  2232. $this->userSession->expects($this->once())
  2233. ->method('getUser')
  2234. ->willReturn($loggedInUser);
  2235. $this->api->addToGroup('TargetUser', 'GroupToAddTo');
  2236. }
  2237. public function testAddToGroupSuccessAsSubadmin() {
  2238. $targetUser = $this->createMock(IUser::class);
  2239. $loggedInUser = $this->createMock(IUser::class);
  2240. $loggedInUser->expects($this->once())
  2241. ->method('getUID')
  2242. ->willReturn('subadmin');
  2243. $targetGroup = $this->createMock(IGroup::class);
  2244. $targetGroup->expects($this->once())
  2245. ->method('addUser')
  2246. ->with($targetUser);
  2247. $this->groupManager->expects($this->once())
  2248. ->method('get')
  2249. ->with('GroupToAddTo')
  2250. ->willReturn($targetGroup);
  2251. $subAdminManager = $this->createMock(SubAdmin::class);
  2252. $subAdminManager->expects($this->once())
  2253. ->method('isSubAdminOfGroup')
  2254. ->with($loggedInUser, $targetGroup)
  2255. ->willReturn(true);
  2256. $this->groupManager->expects($this->once())
  2257. ->method('getSubAdmin')
  2258. ->willReturn($subAdminManager);
  2259. $this->groupManager->expects($this->once())
  2260. ->method('isAdmin')
  2261. ->with('subadmin')
  2262. ->willReturn(false);
  2263. $this->userManager->expects($this->once())
  2264. ->method('get')
  2265. ->with('TargetUser')
  2266. ->willReturn($targetUser);
  2267. $this->userSession->expects($this->once())
  2268. ->method('getUser')
  2269. ->willReturn($loggedInUser);
  2270. $this->assertEquals(new DataResponse(), $this->api->addToGroup('TargetUser', 'GroupToAddTo'));
  2271. }
  2272. public function testAddToGroupSuccessAsAdmin() {
  2273. $targetUser = $this->createMock(IUser::class);
  2274. $loggedInUser = $this->createMock(IUser::class);
  2275. $loggedInUser->expects($this->once())
  2276. ->method('getUID')
  2277. ->willReturn('admin');
  2278. $targetGroup = $this->createMock(IGroup::class);
  2279. $targetGroup->expects($this->once())
  2280. ->method('addUser')
  2281. ->with($targetUser);
  2282. $this->groupManager->expects($this->once())
  2283. ->method('get')
  2284. ->with('GroupToAddTo')
  2285. ->willReturn($targetGroup);
  2286. $subAdminManager = $this->createMock(SubAdmin::class);
  2287. $subAdminManager->expects($this->never())
  2288. ->method('isSubAdminOfGroup');
  2289. $this->groupManager->expects($this->once())
  2290. ->method('getSubAdmin')
  2291. ->willReturn($subAdminManager);
  2292. $this->groupManager->expects($this->once())
  2293. ->method('isAdmin')
  2294. ->with('admin')
  2295. ->willReturn(true);
  2296. $this->userManager->expects($this->once())
  2297. ->method('get')
  2298. ->with('TargetUser')
  2299. ->willReturn($targetUser);
  2300. $this->userSession->expects($this->once())
  2301. ->method('getUser')
  2302. ->willReturn($loggedInUser);
  2303. $this->assertEquals(new DataResponse(), $this->api->addToGroup('TargetUser', 'GroupToAddTo'));
  2304. }
  2305. /**
  2306. * @expectedException \OCP\AppFramework\OCS\OCSException
  2307. * @expectedExceptionCode 101
  2308. */
  2309. public function testRemoveFromGroupWithNoTargetGroup() {
  2310. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2311. $this->userSession
  2312. ->expects($this->once())
  2313. ->method('getUser')
  2314. ->will($this->returnValue($loggedInUser));
  2315. $this->api->removeFromGroup('TargetUser', '');
  2316. }
  2317. /**
  2318. * @expectedException \OCP\AppFramework\OCS\OCSException
  2319. * @expectedExceptionCode 101
  2320. */
  2321. public function testRemoveFromGroupWithEmptyTargetGroup() {
  2322. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2323. $this->userSession
  2324. ->expects($this->once())
  2325. ->method('getUser')
  2326. ->will($this->returnValue($loggedInUser));
  2327. $this->api->removeFromGroup('TargetUser', '');
  2328. }
  2329. /**
  2330. * @expectedException \OCP\AppFramework\OCS\OCSException
  2331. * @expectedExceptionCode 102
  2332. */
  2333. public function testRemoveFromGroupWithNotExistingTargetGroup() {
  2334. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2335. $this->userSession
  2336. ->expects($this->once())
  2337. ->method('getUser')
  2338. ->will($this->returnValue($loggedInUser));
  2339. $this->groupManager
  2340. ->expects($this->once())
  2341. ->method('get')
  2342. ->with('TargetGroup')
  2343. ->will($this->returnValue(null));
  2344. $this->api->removeFromGroup('TargetUser', 'TargetGroup');
  2345. }
  2346. /**
  2347. * @expectedException \OCP\AppFramework\OCS\OCSException
  2348. * @expectedExceptionCode 103
  2349. */
  2350. public function testRemoveFromGroupWithNotExistingTargetUser() {
  2351. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2352. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2353. $this->userSession
  2354. ->expects($this->once())
  2355. ->method('getUser')
  2356. ->will($this->returnValue($loggedInUser));
  2357. $this->groupManager
  2358. ->expects($this->once())
  2359. ->method('get')
  2360. ->with('TargetGroup')
  2361. ->will($this->returnValue($targetGroup));
  2362. $this->userManager
  2363. ->expects($this->once())
  2364. ->method('get')
  2365. ->with('TargetUser')
  2366. ->will($this->returnValue(null));
  2367. $this->api->removeFromGroup('TargetUser', 'TargetGroup');
  2368. }
  2369. /**
  2370. * @expectedException \OCP\AppFramework\OCS\OCSException
  2371. * @expectedExceptionCode 104
  2372. */
  2373. public function testRemoveFromGroupWithoutPermission() {
  2374. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2375. $loggedInUser
  2376. ->expects($this->once())
  2377. ->method('getUID')
  2378. ->will($this->returnValue('unauthorizedUser'));
  2379. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2380. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2381. $this->userSession
  2382. ->expects($this->once())
  2383. ->method('getUser')
  2384. ->will($this->returnValue($loggedInUser));
  2385. $this->groupManager
  2386. ->expects($this->once())
  2387. ->method('get')
  2388. ->with('TargetGroup')
  2389. ->will($this->returnValue($targetGroup));
  2390. $this->userManager
  2391. ->expects($this->once())
  2392. ->method('get')
  2393. ->with('TargetUser')
  2394. ->will($this->returnValue($targetUser));
  2395. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2396. ->disableOriginalConstructor()->getMock();
  2397. $this->groupManager
  2398. ->expects($this->once())
  2399. ->method('getSubAdmin')
  2400. ->will($this->returnValue($subAdminManager));
  2401. $this->groupManager
  2402. ->expects($this->once())
  2403. ->method('isAdmin')
  2404. ->with('unauthorizedUser')
  2405. ->will($this->returnValue(false));
  2406. $this->api->removeFromGroup('TargetUser', 'TargetGroup');
  2407. }
  2408. /**
  2409. * @expectedException \OCP\AppFramework\OCS\OCSException
  2410. * @expectedExceptionCode 105
  2411. * @expectedExceptionMessage Cannot remove yourself from the admin group
  2412. */
  2413. public function testRemoveFromGroupAsAdminFromAdmin() {
  2414. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2415. $loggedInUser
  2416. ->expects($this->any())
  2417. ->method('getUID')
  2418. ->will($this->returnValue('admin'));
  2419. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2420. $targetUser
  2421. ->expects($this->once())
  2422. ->method('getUID')
  2423. ->will($this->returnValue('admin'));
  2424. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2425. $targetGroup
  2426. ->expects($this->once())
  2427. ->method('getGID')
  2428. ->will($this->returnValue('admin'));
  2429. $this->userSession
  2430. ->expects($this->once())
  2431. ->method('getUser')
  2432. ->will($this->returnValue($loggedInUser));
  2433. $this->groupManager
  2434. ->expects($this->once())
  2435. ->method('get')
  2436. ->with('admin')
  2437. ->will($this->returnValue($targetGroup));
  2438. $this->userManager
  2439. ->expects($this->once())
  2440. ->method('get')
  2441. ->with('Admin')
  2442. ->will($this->returnValue($targetUser));
  2443. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2444. ->disableOriginalConstructor()->getMock();
  2445. $this->groupManager
  2446. ->expects($this->once())
  2447. ->method('getSubAdmin')
  2448. ->will($this->returnValue($subAdminManager));
  2449. $this->groupManager
  2450. ->expects($this->any())
  2451. ->method('isAdmin')
  2452. ->with('admin')
  2453. ->will($this->returnValue(true));
  2454. $this->api->removeFromGroup('Admin', 'admin');
  2455. }
  2456. /**
  2457. * @expectedException \OCP\AppFramework\OCS\OCSException
  2458. * @expectedExceptionCode 105
  2459. * @expectedExceptionMessage Cannot remove yourself from this group as you are a SubAdmin
  2460. */
  2461. public function testRemoveFromGroupAsSubAdminFromSubAdmin() {
  2462. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2463. $loggedInUser
  2464. ->expects($this->any())
  2465. ->method('getUID')
  2466. ->will($this->returnValue('subadmin'));
  2467. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2468. $targetUser
  2469. ->expects($this->once())
  2470. ->method('getUID')
  2471. ->will($this->returnValue('subadmin'));
  2472. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2473. $targetGroup
  2474. ->expects($this->any())
  2475. ->method('getGID')
  2476. ->will($this->returnValue('subadmin'));
  2477. $this->userSession
  2478. ->expects($this->once())
  2479. ->method('getUser')
  2480. ->will($this->returnValue($loggedInUser));
  2481. $this->groupManager
  2482. ->expects($this->once())
  2483. ->method('get')
  2484. ->with('subadmin')
  2485. ->will($this->returnValue($targetGroup));
  2486. $this->userManager
  2487. ->expects($this->once())
  2488. ->method('get')
  2489. ->with('SubAdmin')
  2490. ->will($this->returnValue($targetUser));
  2491. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2492. ->disableOriginalConstructor()->getMock();
  2493. $subAdminManager
  2494. ->expects($this->once())
  2495. ->method('isSubAdminOfGroup')
  2496. ->with($loggedInUser, $targetGroup)
  2497. ->will($this->returnValue(true));
  2498. $this->groupManager
  2499. ->expects($this->once())
  2500. ->method('getSubAdmin')
  2501. ->will($this->returnValue($subAdminManager));
  2502. $this->groupManager
  2503. ->expects($this->any())
  2504. ->method('isAdmin')
  2505. ->with('subadmin')
  2506. ->will($this->returnValue(false));
  2507. $this->api->removeFromGroup('SubAdmin', 'subadmin');
  2508. }
  2509. /**
  2510. * @expectedException \OCP\AppFramework\OCS\OCSException
  2511. * @expectedExceptionCode 105
  2512. * @expectedExceptionMessage Not viable to remove user from the last group you are SubAdmin of
  2513. */
  2514. public function testRemoveFromGroupAsSubAdminFromLastSubAdminGroup() {
  2515. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2516. $loggedInUser
  2517. ->expects($this->any())
  2518. ->method('getUID')
  2519. ->will($this->returnValue('subadmin'));
  2520. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2521. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2522. $targetGroup
  2523. ->expects($this->any())
  2524. ->method('getGID')
  2525. ->will($this->returnValue('subadmin'));
  2526. $this->userSession
  2527. ->expects($this->once())
  2528. ->method('getUser')
  2529. ->will($this->returnValue($loggedInUser));
  2530. $this->groupManager
  2531. ->expects($this->once())
  2532. ->method('get')
  2533. ->with('subadmin')
  2534. ->will($this->returnValue($targetGroup));
  2535. $this->userManager
  2536. ->expects($this->once())
  2537. ->method('get')
  2538. ->with('AnotherUser')
  2539. ->will($this->returnValue($targetUser));
  2540. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2541. ->disableOriginalConstructor()->getMock();
  2542. $subAdminManager
  2543. ->expects($this->once())
  2544. ->method('isSubAdminOfGroup')
  2545. ->with($loggedInUser, $targetGroup)
  2546. ->will($this->returnValue(true));
  2547. $this->groupManager
  2548. ->expects($this->once())
  2549. ->method('getSubAdmin')
  2550. ->will($this->returnValue($subAdminManager));
  2551. $subAdminManager
  2552. ->expects($this->once())
  2553. ->method('getSubAdminsGroups')
  2554. ->with($loggedInUser)
  2555. ->will($this->returnValue([$targetGroup]));
  2556. $this->groupManager
  2557. ->expects($this->any())
  2558. ->method('isAdmin')
  2559. ->with('subadmin')
  2560. ->will($this->returnValue(false));
  2561. $this->groupManager
  2562. ->expects($this->once())
  2563. ->method('getUserGroupIds')
  2564. ->with($targetUser)
  2565. ->willReturn(['subadmin', 'other group']);
  2566. $this->api->removeFromGroup('AnotherUser', 'subadmin');
  2567. }
  2568. public function testRemoveFromGroupSuccessful() {
  2569. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2570. $loggedInUser
  2571. ->expects($this->any())
  2572. ->method('getUID')
  2573. ->will($this->returnValue('admin'));
  2574. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2575. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2576. $this->userSession
  2577. ->expects($this->once())
  2578. ->method('getUser')
  2579. ->will($this->returnValue($loggedInUser));
  2580. $this->groupManager
  2581. ->expects($this->once())
  2582. ->method('get')
  2583. ->with('admin')
  2584. ->will($this->returnValue($targetGroup));
  2585. $this->userManager
  2586. ->expects($this->once())
  2587. ->method('get')
  2588. ->with('AnotherUser')
  2589. ->will($this->returnValue($targetUser));
  2590. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2591. ->disableOriginalConstructor()->getMock();
  2592. $this->groupManager
  2593. ->expects($this->once())
  2594. ->method('getSubAdmin')
  2595. ->will($this->returnValue($subAdminManager));
  2596. $this->groupManager
  2597. ->expects($this->any())
  2598. ->method('isAdmin')
  2599. ->with('admin')
  2600. ->will($this->returnValue(true));
  2601. $targetGroup
  2602. ->expects($this->once())
  2603. ->method('removeUser')
  2604. ->with($targetUser);
  2605. $this->assertEquals([], $this->api->removeFromGroup('AnotherUser', 'admin')->getData());
  2606. }
  2607. /**
  2608. * @expectedException \OCP\AppFramework\OCS\OCSException
  2609. * @expectedExceptionCode 101
  2610. * @expectedExceptionMessage User does not exist
  2611. */
  2612. public function testAddSubAdminWithNotExistingTargetUser() {
  2613. $this->userManager
  2614. ->expects($this->once())
  2615. ->method('get')
  2616. ->with('NotExistingUser')
  2617. ->will($this->returnValue(null));
  2618. $this->api->addSubAdmin('NotExistingUser', '');
  2619. }
  2620. /**
  2621. * @expectedException \OCP\AppFramework\OCS\OCSException
  2622. * @expectedExceptionCode 102
  2623. * @expectedExceptionMessage Group does not exist
  2624. */
  2625. public function testAddSubAdminWithNotExistingTargetGroup() {
  2626. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2627. $this->userManager
  2628. ->expects($this->once())
  2629. ->method('get')
  2630. ->with('ExistingUser')
  2631. ->will($this->returnValue($targetUser));
  2632. $this->groupManager
  2633. ->expects($this->once())
  2634. ->method('get')
  2635. ->with('NotExistingGroup')
  2636. ->will($this->returnValue(null));
  2637. $this->api->addSubAdmin('ExistingUser', 'NotExistingGroup');
  2638. }
  2639. /**
  2640. * @expectedException \OCP\AppFramework\OCS\OCSException
  2641. * @expectedExceptionCode 103
  2642. * @expectedExceptionMessage Cannot create subadmins for admin group
  2643. */
  2644. public function testAddSubAdminToAdminGroup() {
  2645. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2646. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2647. $targetGroup
  2648. ->expects($this->once())
  2649. ->method('getGID')
  2650. ->will($this->returnValue('admin'));
  2651. $this->userManager
  2652. ->expects($this->once())
  2653. ->method('get')
  2654. ->with('ExistingUser')
  2655. ->will($this->returnValue($targetUser));
  2656. $this->groupManager
  2657. ->expects($this->once())
  2658. ->method('get')
  2659. ->with('ADmiN')
  2660. ->will($this->returnValue($targetGroup));
  2661. $this->api->addSubAdmin('ExistingUser', 'ADmiN');
  2662. }
  2663. public function testAddSubAdminTwice() {
  2664. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2665. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2666. $this->userManager
  2667. ->expects($this->once())
  2668. ->method('get')
  2669. ->with('ExistingUser')
  2670. ->will($this->returnValue($targetUser));
  2671. $this->groupManager
  2672. ->expects($this->once())
  2673. ->method('get')
  2674. ->with('TargetGroup')
  2675. ->will($this->returnValue($targetGroup));
  2676. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2677. ->disableOriginalConstructor()->getMock();
  2678. $subAdminManager
  2679. ->expects($this->once())
  2680. ->method('isSubAdminOfGroup')
  2681. ->with($targetUser, $targetGroup)
  2682. ->will($this->returnValue(true));
  2683. $this->groupManager
  2684. ->expects($this->once())
  2685. ->method('getSubAdmin')
  2686. ->will($this->returnValue($subAdminManager));
  2687. $this->assertEquals([], $this->api->addSubAdmin('ExistingUser', 'TargetGroup')->getData());
  2688. }
  2689. public function testAddSubAdminSuccessful() {
  2690. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2691. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2692. $this->userManager
  2693. ->expects($this->once())
  2694. ->method('get')
  2695. ->with('ExistingUser')
  2696. ->will($this->returnValue($targetUser));
  2697. $this->groupManager
  2698. ->expects($this->once())
  2699. ->method('get')
  2700. ->with('TargetGroup')
  2701. ->will($this->returnValue($targetGroup));
  2702. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2703. ->disableOriginalConstructor()->getMock();
  2704. $subAdminManager
  2705. ->expects($this->once())
  2706. ->method('isSubAdminOfGroup')
  2707. ->with($targetUser, $targetGroup)
  2708. ->will($this->returnValue(false));
  2709. $subAdminManager
  2710. ->expects($this->once())
  2711. ->method('createSubAdmin')
  2712. ->with($targetUser, $targetGroup)
  2713. ->will($this->returnValue(true));
  2714. $this->groupManager
  2715. ->expects($this->once())
  2716. ->method('getSubAdmin')
  2717. ->will($this->returnValue($subAdminManager));
  2718. $this->assertEquals([], $this->api->addSubAdmin('ExistingUser', 'TargetGroup')->getData());
  2719. }
  2720. /**
  2721. * @expectedException \OCP\AppFramework\OCS\OCSException
  2722. * @expectedExceptionCode 101
  2723. * @expectedExceptionMessage User does not exist
  2724. */
  2725. public function testRemoveSubAdminNotExistingTargetUser() {
  2726. $this->userManager
  2727. ->expects($this->once())
  2728. ->method('get')
  2729. ->with('NotExistingUser')
  2730. ->will($this->returnValue(null));
  2731. $this->api->removeSubAdmin('NotExistingUser', 'GroupToDeleteFrom');
  2732. }
  2733. /**
  2734. * @expectedException \OCP\AppFramework\OCS\OCSException
  2735. * @expectedExceptionCode 101
  2736. * @expectedExceptionMessage Group does not exist
  2737. */
  2738. public function testRemoveSubAdminNotExistingTargetGroup() {
  2739. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2740. $this->userManager
  2741. ->expects($this->once())
  2742. ->method('get')
  2743. ->with('ExistingUser')
  2744. ->will($this->returnValue($targetUser));
  2745. $this->groupManager
  2746. ->expects($this->once())
  2747. ->method('get')
  2748. ->with('GroupToDeleteFrom')
  2749. ->will($this->returnValue(null));
  2750. $this->api->removeSubAdmin('ExistingUser', 'GroupToDeleteFrom');
  2751. }
  2752. /**
  2753. * @expectedException \OCP\AppFramework\OCS\OCSException
  2754. * @expectedExceptionCode 102
  2755. * @expectedExceptionMessage User is not a subadmin of this group
  2756. */
  2757. public function testRemoveSubAdminFromNotASubadmin() {
  2758. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2759. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2760. $this->userManager
  2761. ->expects($this->once())
  2762. ->method('get')
  2763. ->with('ExistingUser')
  2764. ->will($this->returnValue($targetUser));
  2765. $this->groupManager
  2766. ->expects($this->once())
  2767. ->method('get')
  2768. ->with('GroupToDeleteFrom')
  2769. ->will($this->returnValue($targetGroup));
  2770. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2771. ->disableOriginalConstructor()->getMock();
  2772. $subAdminManager
  2773. ->expects($this->once())
  2774. ->method('isSubAdminOfGroup')
  2775. ->with($targetUser, $targetGroup)
  2776. ->will($this->returnValue(false));
  2777. $this->groupManager
  2778. ->expects($this->once())
  2779. ->method('getSubAdmin')
  2780. ->will($this->returnValue($subAdminManager));
  2781. $this->api->removeSubAdmin('ExistingUser', 'GroupToDeleteFrom');
  2782. }
  2783. public function testRemoveSubAdminSuccessful() {
  2784. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2785. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2786. $this->userManager
  2787. ->expects($this->once())
  2788. ->method('get')
  2789. ->with('ExistingUser')
  2790. ->will($this->returnValue($targetUser));
  2791. $this->groupManager
  2792. ->expects($this->once())
  2793. ->method('get')
  2794. ->with('GroupToDeleteFrom')
  2795. ->will($this->returnValue($targetGroup));
  2796. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2797. ->disableOriginalConstructor()->getMock();
  2798. $subAdminManager
  2799. ->expects($this->once())
  2800. ->method('isSubAdminOfGroup')
  2801. ->with($targetUser, $targetGroup)
  2802. ->will($this->returnValue(true));
  2803. $subAdminManager
  2804. ->expects($this->once())
  2805. ->method('deleteSubAdmin')
  2806. ->with($targetUser, $targetGroup)
  2807. ->will($this->returnValue(true));
  2808. $this->groupManager
  2809. ->expects($this->once())
  2810. ->method('getSubAdmin')
  2811. ->will($this->returnValue($subAdminManager));
  2812. $this->assertEquals([], $this->api->removeSubAdmin('ExistingUser', 'GroupToDeleteFrom')->getData());
  2813. }
  2814. /**
  2815. * @expectedException \OCP\AppFramework\OCS\OCSException
  2816. * @expectedExceptionCode 404
  2817. * @expectedExceptionMessage User does not exist
  2818. */
  2819. public function testGetUserSubAdminGroupsNotExistingTargetUser() {
  2820. $this->userManager
  2821. ->expects($this->once())
  2822. ->method('get')
  2823. ->with('RequestedUser')
  2824. ->will($this->returnValue(null));
  2825. $this->api->getUserSubAdminGroups('RequestedUser');
  2826. }
  2827. public function testGetUserSubAdminGroupsWithGroups() {
  2828. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2829. $targetGroup = $this->getMockBuilder('\OCP\IGroup')->disableOriginalConstructor()->getMock();
  2830. $targetGroup
  2831. ->expects($this->once())
  2832. ->method('getGID')
  2833. ->will($this->returnValue('TargetGroup'));
  2834. $this->userManager
  2835. ->expects($this->once())
  2836. ->method('get')
  2837. ->with('RequestedUser')
  2838. ->will($this->returnValue($targetUser));
  2839. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  2840. ->disableOriginalConstructor()->getMock();
  2841. $subAdminManager
  2842. ->expects($this->once())
  2843. ->method('getSubAdminsGroups')
  2844. ->with($targetUser)
  2845. ->will($this->returnValue([$targetGroup]));
  2846. $this->groupManager
  2847. ->expects($this->once())
  2848. ->method('getSubAdmin')
  2849. ->will($this->returnValue($subAdminManager));
  2850. $this->assertEquals(['TargetGroup'], $this->api->getUserSubAdminGroups('RequestedUser')->getData());
  2851. }
  2852. public function testEnableUser() {
  2853. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2854. $targetUser->expects($this->once())
  2855. ->method('setEnabled')
  2856. ->with(true);
  2857. $this->userManager
  2858. ->expects($this->once())
  2859. ->method('get')
  2860. ->with('RequestedUser')
  2861. ->will($this->returnValue($targetUser));
  2862. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2863. $loggedInUser
  2864. ->expects($this->exactly(2))
  2865. ->method('getUID')
  2866. ->will($this->returnValue('admin'));
  2867. $this->userSession
  2868. ->expects($this->once())
  2869. ->method('getUser')
  2870. ->will($this->returnValue($loggedInUser));
  2871. $this->groupManager
  2872. ->expects($this->once())
  2873. ->method('isAdmin')
  2874. ->will($this->returnValue(true));
  2875. $this->assertEquals([], $this->api->enableUser('RequestedUser')->getData());
  2876. }
  2877. public function testDisableUser() {
  2878. $targetUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2879. $targetUser->expects($this->once())
  2880. ->method('setEnabled')
  2881. ->with(false);
  2882. $this->userManager
  2883. ->expects($this->once())
  2884. ->method('get')
  2885. ->with('RequestedUser')
  2886. ->will($this->returnValue($targetUser));
  2887. $loggedInUser = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  2888. $loggedInUser
  2889. ->expects($this->exactly(2))
  2890. ->method('getUID')
  2891. ->will($this->returnValue('admin'));
  2892. $this->userSession
  2893. ->expects($this->once())
  2894. ->method('getUser')
  2895. ->will($this->returnValue($loggedInUser));
  2896. $this->groupManager
  2897. ->expects($this->once())
  2898. ->method('isAdmin')
  2899. ->will($this->returnValue(true));
  2900. $this->assertEquals([], $this->api->disableUser('RequestedUser')->getData());
  2901. }
  2902. public function testGetCurrentUserLoggedIn() {
  2903. $user = $this->createMock(IUser::class);
  2904. $user->expects($this->once())->method('getUID')->willReturn('UID');
  2905. $this->userSession->expects($this->once())->method('getUser')
  2906. ->willReturn($user);
  2907. /** @var UsersController | PHPUnit_Framework_MockObject_MockObject $api */
  2908. $api = $this->getMockBuilder('OCA\Provisioning_API\Controller\UsersController')
  2909. ->setConstructorArgs([
  2910. 'provisioning_api',
  2911. $this->request,
  2912. $this->userManager,
  2913. $this->config,
  2914. $this->appManager,
  2915. $this->groupManager,
  2916. $this->userSession,
  2917. $this->accountManager,
  2918. $this->logger,
  2919. $this->l10nFactory,
  2920. $this->newUserMailHelper,
  2921. $this->federatedFileSharingFactory,
  2922. $this->secureRandom,
  2923. $this->remoteWipe,
  2924. ])
  2925. ->setMethods(['getUserData'])
  2926. ->getMock();
  2927. $api->expects($this->once())->method('getUserData')->with('UID')
  2928. ->willReturn(
  2929. [
  2930. 'id' => 'UID',
  2931. 'enabled' => 'true',
  2932. 'quota' => ['DummyValue'],
  2933. 'email' => 'demo@nextcloud.com',
  2934. 'displayname' => 'Demo User',
  2935. 'phone' => 'phone',
  2936. 'address' => 'address',
  2937. 'website' => 'website',
  2938. 'twitter' => 'twitter'
  2939. ]
  2940. );
  2941. $expected = [
  2942. 'id' => 'UID',
  2943. 'enabled' => 'true',
  2944. 'quota' => ['DummyValue'],
  2945. 'email' => 'demo@nextcloud.com',
  2946. 'phone' => 'phone',
  2947. 'address' => 'address',
  2948. 'website' => 'website',
  2949. 'twitter' => 'twitter',
  2950. 'display-name' => 'Demo User'
  2951. ];
  2952. $this->assertSame($expected, $api->getCurrentUser()->getData());
  2953. }
  2954. /**
  2955. * @expectedException \OCP\AppFramework\OCS\OCSException
  2956. */
  2957. public function testGetCurrentUserNotLoggedIn() {
  2958. $this->userSession->expects($this->once())->method('getUser')
  2959. ->willReturn(null);
  2960. $this->api->getCurrentUser();
  2961. }
  2962. public function testGetUser() {
  2963. /** @var UsersController | PHPUnit_Framework_MockObject_MockObject $api */
  2964. $api = $this->getMockBuilder('OCA\Provisioning_API\Controller\UsersController')
  2965. ->setConstructorArgs([
  2966. 'provisioning_api',
  2967. $this->request,
  2968. $this->userManager,
  2969. $this->config,
  2970. $this->appManager,
  2971. $this->groupManager,
  2972. $this->userSession,
  2973. $this->accountManager,
  2974. $this->logger,
  2975. $this->l10nFactory,
  2976. $this->newUserMailHelper,
  2977. $this->federatedFileSharingFactory,
  2978. $this->secureRandom,
  2979. $this->remoteWipe,
  2980. ])
  2981. ->setMethods(['getUserData'])
  2982. ->getMock();
  2983. $expected = [
  2984. 'id' => 'UID',
  2985. 'enabled' => 'true',
  2986. 'quota' => ['DummyValue'],
  2987. 'email' => 'demo@nextcloud.com',
  2988. 'phone' => 'phone',
  2989. 'address' => 'address',
  2990. 'website' => 'website',
  2991. 'twitter' => 'twitter',
  2992. 'displayname' => 'Demo User'
  2993. ];
  2994. $api->expects($this->once())->method('getUserData')
  2995. ->with('uid')
  2996. ->willReturn($expected);
  2997. $this->assertSame($expected, $api->getUser('uid')->getData());
  2998. }
  2999. /**
  3000. * @expectedException \OCP\AppFramework\OCS\OCSException
  3001. * @expectedExceptionCode 998
  3002. */
  3003. public function testResendWelcomeMessageWithNotExistingTargetUser() {
  3004. $this->userManager
  3005. ->expects($this->once())
  3006. ->method('get')
  3007. ->with('NotExistingUser')
  3008. ->will($this->returnValue(null));
  3009. $this->api->resendWelcomeMessage('NotExistingUser');
  3010. }
  3011. /**
  3012. * @expectedException \OCP\AppFramework\OCS\OCSException
  3013. * @expectedExceptionCode 997
  3014. */
  3015. public function testResendWelcomeMessageAsSubAdminAndUserIsNotAccessible() {
  3016. $loggedInUser = $this->getMockBuilder(IUser::class)
  3017. ->disableOriginalConstructor()
  3018. ->getMock();
  3019. $loggedInUser
  3020. ->expects($this->exactly(1))
  3021. ->method('getUID')
  3022. ->will($this->returnValue('subadmin'));
  3023. $targetUser = $this->getMockBuilder(IUser::class)
  3024. ->disableOriginalConstructor()
  3025. ->getMock();
  3026. $this->userSession
  3027. ->expects($this->once())
  3028. ->method('getUser')
  3029. ->will($this->returnValue($loggedInUser));
  3030. $this->userManager
  3031. ->expects($this->once())
  3032. ->method('get')
  3033. ->with('UserToGet')
  3034. ->will($this->returnValue($targetUser));
  3035. $this->groupManager
  3036. ->expects($this->once())
  3037. ->method('isAdmin')
  3038. ->with('subadmin')
  3039. ->will($this->returnValue(false));
  3040. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  3041. ->disableOriginalConstructor()
  3042. ->getMock();
  3043. $subAdminManager
  3044. ->expects($this->once())
  3045. ->method('isUserAccessible')
  3046. ->with($loggedInUser, $targetUser)
  3047. ->will($this->returnValue(false));
  3048. $this->groupManager
  3049. ->expects($this->once())
  3050. ->method('getSubAdmin')
  3051. ->will($this->returnValue($subAdminManager));
  3052. $this->api->resendWelcomeMessage('UserToGet');
  3053. }
  3054. /**
  3055. * @expectedException \OCP\AppFramework\OCS\OCSException
  3056. * @expectedExceptionCode 101
  3057. * @expectedExceptionMessage Email address not available
  3058. */
  3059. public function testResendWelcomeMessageNoEmail() {
  3060. $loggedInUser = $this->getMockBuilder(IUser::class)
  3061. ->disableOriginalConstructor()
  3062. ->getMock();
  3063. $targetUser = $this->getMockBuilder(IUser::class)
  3064. ->disableOriginalConstructor()
  3065. ->getMock();
  3066. $this->userSession
  3067. ->expects($this->once())
  3068. ->method('getUser')
  3069. ->will($this->returnValue($loggedInUser));
  3070. $this->userManager
  3071. ->expects($this->once())
  3072. ->method('get')
  3073. ->with('UserToGet')
  3074. ->will($this->returnValue($targetUser));
  3075. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  3076. ->disableOriginalConstructor()
  3077. ->getMock();
  3078. $subAdminManager
  3079. ->expects($this->once())
  3080. ->method('isUserAccessible')
  3081. ->with($loggedInUser, $targetUser)
  3082. ->will($this->returnValue(true));
  3083. $this->groupManager
  3084. ->expects($this->once())
  3085. ->method('getSubAdmin')
  3086. ->will($this->returnValue($subAdminManager));
  3087. $targetUser
  3088. ->expects($this->once())
  3089. ->method('getEmailAddress')
  3090. ->will($this->returnValue(''));
  3091. $this->api->resendWelcomeMessage('UserToGet');
  3092. }
  3093. /**
  3094. * @expectedException \OCP\AppFramework\OCS\OCSException
  3095. * @expectedExceptionCode 101
  3096. * @expectedExceptionMessage Email address not available
  3097. */
  3098. public function testResendWelcomeMessageNullEmail() {
  3099. $loggedInUser = $this->getMockBuilder(IUser::class)
  3100. ->disableOriginalConstructor()
  3101. ->getMock();
  3102. $targetUser = $this->getMockBuilder(IUser::class)
  3103. ->disableOriginalConstructor()
  3104. ->getMock();
  3105. $this->userSession
  3106. ->expects($this->once())
  3107. ->method('getUser')
  3108. ->will($this->returnValue($loggedInUser));
  3109. $this->userManager
  3110. ->expects($this->once())
  3111. ->method('get')
  3112. ->with('UserToGet')
  3113. ->will($this->returnValue($targetUser));
  3114. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  3115. ->disableOriginalConstructor()
  3116. ->getMock();
  3117. $subAdminManager
  3118. ->expects($this->once())
  3119. ->method('isUserAccessible')
  3120. ->with($loggedInUser, $targetUser)
  3121. ->will($this->returnValue(true));
  3122. $this->groupManager
  3123. ->expects($this->once())
  3124. ->method('getSubAdmin')
  3125. ->will($this->returnValue($subAdminManager));
  3126. $targetUser
  3127. ->expects($this->once())
  3128. ->method('getEmailAddress')
  3129. ->will($this->returnValue(null));
  3130. $this->api->resendWelcomeMessage('UserToGet');
  3131. }
  3132. public function testResendWelcomeMessageSuccess() {
  3133. $loggedInUser = $this->getMockBuilder(IUser::class)
  3134. ->disableOriginalConstructor()
  3135. ->getMock();
  3136. $targetUser = $this->getMockBuilder(IUser::class)
  3137. ->disableOriginalConstructor()
  3138. ->getMock();
  3139. $targetUser
  3140. ->method('getUID')
  3141. ->willReturn('user-id');
  3142. $this->userSession
  3143. ->expects($this->once())
  3144. ->method('getUser')
  3145. ->will($this->returnValue($loggedInUser));
  3146. $this->userManager
  3147. ->expects($this->once())
  3148. ->method('get')
  3149. ->with('UserToGet')
  3150. ->will($this->returnValue($targetUser));
  3151. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  3152. ->disableOriginalConstructor()
  3153. ->getMock();
  3154. $subAdminManager
  3155. ->expects($this->once())
  3156. ->method('isUserAccessible')
  3157. ->with($loggedInUser, $targetUser)
  3158. ->will($this->returnValue(true));
  3159. $this->groupManager
  3160. ->expects($this->once())
  3161. ->method('getSubAdmin')
  3162. ->will($this->returnValue($subAdminManager));
  3163. $targetUser
  3164. ->expects($this->once())
  3165. ->method('getEmailAddress')
  3166. ->will($this->returnValue('abc@example.org'));
  3167. $emailTemplate = $this->createMock(IEMailTemplate::class);
  3168. $this->newUserMailHelper
  3169. ->expects($this->at(0))
  3170. ->method('generateTemplate')
  3171. ->willReturn($emailTemplate);
  3172. $this->newUserMailHelper
  3173. ->expects($this->at(1))
  3174. ->method('sendMail')
  3175. ->with($targetUser, $emailTemplate);
  3176. $this->api->resendWelcomeMessage('UserToGet');
  3177. }
  3178. public function testResendWelcomeMessageSuccessWithFallbackLanguage() {
  3179. $loggedInUser = $this->getMockBuilder(IUser::class)
  3180. ->disableOriginalConstructor()
  3181. ->getMock();
  3182. $targetUser = $this->getMockBuilder(IUser::class)
  3183. ->disableOriginalConstructor()
  3184. ->getMock();
  3185. $targetUser
  3186. ->method('getUID')
  3187. ->willReturn('user-id');
  3188. $this->userSession
  3189. ->expects($this->once())
  3190. ->method('getUser')
  3191. ->will($this->returnValue($loggedInUser));
  3192. $this->userManager
  3193. ->expects($this->once())
  3194. ->method('get')
  3195. ->with('UserToGet')
  3196. ->will($this->returnValue($targetUser));
  3197. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  3198. ->disableOriginalConstructor()
  3199. ->getMock();
  3200. $subAdminManager
  3201. ->expects($this->once())
  3202. ->method('isUserAccessible')
  3203. ->with($loggedInUser, $targetUser)
  3204. ->will($this->returnValue(true));
  3205. $this->groupManager
  3206. ->expects($this->once())
  3207. ->method('getSubAdmin')
  3208. ->will($this->returnValue($subAdminManager));
  3209. $targetUser
  3210. ->expects($this->once())
  3211. ->method('getEmailAddress')
  3212. ->will($this->returnValue('abc@example.org'));
  3213. $l10n = $this->getMockBuilder(IL10N::class)
  3214. ->disableOriginalConstructor()
  3215. ->getMock();
  3216. $emailTemplate = $this->createMock(IEMailTemplate::class);
  3217. $this->newUserMailHelper
  3218. ->expects($this->at(0))
  3219. ->method('generateTemplate')
  3220. ->willReturn($emailTemplate);
  3221. $this->newUserMailHelper
  3222. ->expects($this->at(1))
  3223. ->method('sendMail')
  3224. ->with($targetUser, $emailTemplate);
  3225. $this->api->resendWelcomeMessage('UserToGet');
  3226. }
  3227. /**
  3228. * @expectedException \OCP\AppFramework\OCS\OCSException
  3229. * @expectedExceptionCode 102
  3230. * @expectedExceptionMessage Sending email failed
  3231. */
  3232. public function testResendWelcomeMessageFailed() {
  3233. $loggedInUser = $this->getMockBuilder(IUser::class)
  3234. ->disableOriginalConstructor()
  3235. ->getMock();
  3236. $targetUser = $this->getMockBuilder(IUser::class)
  3237. ->disableOriginalConstructor()
  3238. ->getMock();
  3239. $targetUser
  3240. ->method('getUID')
  3241. ->willReturn('user-id');
  3242. $this->userSession
  3243. ->expects($this->once())
  3244. ->method('getUser')
  3245. ->will($this->returnValue($loggedInUser));
  3246. $this->userManager
  3247. ->expects($this->once())
  3248. ->method('get')
  3249. ->with('UserToGet')
  3250. ->will($this->returnValue($targetUser));
  3251. $subAdminManager = $this->getMockBuilder('OC\SubAdmin')
  3252. ->disableOriginalConstructor()
  3253. ->getMock();
  3254. $subAdminManager
  3255. ->expects($this->once())
  3256. ->method('isUserAccessible')
  3257. ->with($loggedInUser, $targetUser)
  3258. ->will($this->returnValue(true));
  3259. $this->groupManager
  3260. ->expects($this->once())
  3261. ->method('getSubAdmin')
  3262. ->will($this->returnValue($subAdminManager));
  3263. $targetUser
  3264. ->expects($this->once())
  3265. ->method('getEmailAddress')
  3266. ->will($this->returnValue('abc@example.org'));
  3267. $emailTemplate = $this->createMock(IEMailTemplate::class);
  3268. $this->newUserMailHelper
  3269. ->expects($this->at(0))
  3270. ->method('generateTemplate')
  3271. ->willReturn($emailTemplate);
  3272. $this->newUserMailHelper
  3273. ->expects($this->at(1))
  3274. ->method('sendMail')
  3275. ->with($targetUser, $emailTemplate)
  3276. ->willThrowException(new \Exception());
  3277. $this->api->resendWelcomeMessage('UserToGet');
  3278. }
  3279. public function dataGetEditableFields() {
  3280. return [
  3281. [false, false, []],
  3282. [false, true, [
  3283. AccountManager::PROPERTY_PHONE,
  3284. AccountManager::PROPERTY_ADDRESS,
  3285. AccountManager::PROPERTY_WEBSITE,
  3286. AccountManager::PROPERTY_TWITTER,
  3287. ]],
  3288. [ true, false, [
  3289. AccountManager::PROPERTY_DISPLAYNAME,
  3290. AccountManager::PROPERTY_EMAIL,
  3291. ]],
  3292. [ true, true ,[
  3293. AccountManager::PROPERTY_DISPLAYNAME,
  3294. AccountManager::PROPERTY_EMAIL,
  3295. AccountManager::PROPERTY_PHONE,
  3296. AccountManager::PROPERTY_ADDRESS,
  3297. AccountManager::PROPERTY_WEBSITE,
  3298. AccountManager::PROPERTY_TWITTER,
  3299. ]]
  3300. ];
  3301. }
  3302. /**
  3303. * @dataProvider dataGetEditableFields
  3304. *
  3305. * @param bool $allowedToChangeDisplayName
  3306. * @param bool $federatedSharingEnabled
  3307. * @param array $expected
  3308. */
  3309. public function testGetEditableFields(bool $allowedToChangeDisplayName, bool $federatedSharingEnabled, array $expected) {
  3310. $this->config
  3311. ->method('getSystemValue')
  3312. ->with(
  3313. $this->equalTo('allow_user_to_change_display_name'),
  3314. $this->anything()
  3315. )->willReturn($allowedToChangeDisplayName);
  3316. $this->appManager
  3317. ->method('isEnabledForUser')
  3318. ->with($this->equalTo('federatedfilesharing'))
  3319. ->willReturn($federatedSharingEnabled);
  3320. $shareprovider = $this->createMock(FederatedShareProvider::class);
  3321. $shareprovider->method('isLookupServerUploadEnabled')->willReturn(true);
  3322. $federatedFileSharing = $this->createMock(Application::class);
  3323. $federatedFileSharing
  3324. ->method('getFederatedShareProvider')
  3325. ->willReturn($shareprovider);
  3326. $this->federatedFileSharingFactory
  3327. ->method('get')
  3328. ->willReturn($federatedFileSharing);
  3329. $expectedResp = new DataResponse($expected);
  3330. $this->assertEquals($expectedResp, $this->api->getEditableFields());
  3331. }
  3332. }