UsersControllerTest.php 121 KB

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