1
0

UsersControllerTest.php 123 KB

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