UsersControllerTest.php 120 KB

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