UsersControllerTest.php 121 KB

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