1
0

UsersControllerTest.php 121 KB

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