ManagerTest.php 158 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test\Share20;
  8. use DateTimeZone;
  9. use OC\Files\Mount\MoveableMount;
  10. use OC\Files\Utils\PathHelper;
  11. use OC\KnownUser\KnownUserService;
  12. use OC\Share20\DefaultShareProvider;
  13. use OC\Share20\Exception;
  14. use OC\Share20\Manager;
  15. use OC\Share20\Share;
  16. use OC\Share20\ShareDisableChecker;
  17. use OCP\EventDispatcher\Event;
  18. use OCP\EventDispatcher\IEventDispatcher;
  19. use OCP\Files\File;
  20. use OCP\Files\Folder;
  21. use OCP\Files\IRootFolder;
  22. use OCP\Files\Mount\IMountManager;
  23. use OCP\Files\Mount\IMountPoint;
  24. use OCP\Files\Node;
  25. use OCP\Files\Storage\IStorage;
  26. use OCP\HintException;
  27. use OCP\IConfig;
  28. use OCP\IDateTimeZone;
  29. use OCP\IGroup;
  30. use OCP\IGroupManager;
  31. use OCP\IL10N;
  32. use OCP\IServerContainer;
  33. use OCP\IURLGenerator;
  34. use OCP\IUser;
  35. use OCP\IUserManager;
  36. use OCP\IUserSession;
  37. use OCP\L10N\IFactory;
  38. use OCP\Mail\IMailer;
  39. use OCP\Security\Events\ValidatePasswordPolicyEvent;
  40. use OCP\Security\IHasher;
  41. use OCP\Security\ISecureRandom;
  42. use OCP\Share\Events\BeforeShareCreatedEvent;
  43. use OCP\Share\Events\BeforeShareDeletedEvent;
  44. use OCP\Share\Events\ShareCreatedEvent;
  45. use OCP\Share\Events\ShareDeletedEvent;
  46. use OCP\Share\Events\ShareDeletedFromSelfEvent;
  47. use OCP\Share\Exceptions\AlreadySharedException;
  48. use OCP\Share\Exceptions\GenericShareException;
  49. use OCP\Share\Exceptions\ShareNotFound;
  50. use OCP\Share\IManager;
  51. use OCP\Share\IProviderFactory;
  52. use OCP\Share\IShare;
  53. use OCP\Share\IShareProvider;
  54. use PHPUnit\Framework\MockObject\MockBuilder;
  55. use PHPUnit\Framework\MockObject\MockObject;
  56. use Psr\Log\LoggerInterface;
  57. /**
  58. * Class ManagerTest
  59. *
  60. * @package Test\Share20
  61. * @group DB
  62. */
  63. class ManagerTest extends \Test\TestCase {
  64. /** @var Manager */
  65. protected $manager;
  66. /** @var LoggerInterface|MockObject */
  67. protected $logger;
  68. /** @var IConfig|MockObject */
  69. protected $config;
  70. /** @var ISecureRandom|MockObject */
  71. protected $secureRandom;
  72. /** @var IHasher|MockObject */
  73. protected $hasher;
  74. /** @var IShareProvider|MockObject */
  75. protected $defaultProvider;
  76. /** @var IMountManager|MockObject */
  77. protected $mountManager;
  78. /** @var IGroupManager|MockObject */
  79. protected $groupManager;
  80. /** @var IL10N|MockObject */
  81. protected $l;
  82. /** @var IFactory|MockObject */
  83. protected $l10nFactory;
  84. /** @var DummyFactory */
  85. protected $factory;
  86. /** @var IUserManager|MockObject */
  87. protected $userManager;
  88. /** @var IRootFolder | MockObject */
  89. protected $rootFolder;
  90. /** @var IEventDispatcher|MockObject */
  91. protected $dispatcher;
  92. /** @var IMailer|MockObject */
  93. protected $mailer;
  94. /** @var IURLGenerator|MockObject */
  95. protected $urlGenerator;
  96. /** @var \OC_Defaults|MockObject */
  97. protected $defaults;
  98. /** @var IUserSession|MockObject */
  99. protected $userSession;
  100. /** @var KnownUserService|MockObject */
  101. protected $knownUserService;
  102. /** @var ShareDisableChecker|MockObject */
  103. protected $shareDisabledChecker;
  104. private DateTimeZone $timezone;
  105. /** @var IDateTimeZone|MockObject */
  106. protected $dateTimeZone;
  107. protected function setUp(): void {
  108. $this->logger = $this->createMock(LoggerInterface::class);
  109. $this->config = $this->createMock(IConfig::class);
  110. $this->secureRandom = $this->createMock(ISecureRandom::class);
  111. $this->hasher = $this->createMock(IHasher::class);
  112. $this->mountManager = $this->createMock(IMountManager::class);
  113. $this->groupManager = $this->createMock(IGroupManager::class);
  114. $this->userManager = $this->createMock(IUserManager::class);
  115. $this->rootFolder = $this->createMock(IRootFolder::class);
  116. $this->mailer = $this->createMock(IMailer::class);
  117. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  118. $this->defaults = $this->createMock(\OC_Defaults::class);
  119. $this->dispatcher = $this->createMock(IEventDispatcher::class);
  120. $this->userSession = $this->createMock(IUserSession::class);
  121. $this->knownUserService = $this->createMock(KnownUserService::class);
  122. $this->shareDisabledChecker = new ShareDisableChecker($this->config, $this->userManager, $this->groupManager);
  123. $this->dateTimeZone = $this->createMock(IDateTimeZone::class);
  124. $this->timezone = new \DateTimeZone('Pacific/Auckland');
  125. $this->dateTimeZone->method('getTimeZone')->willReturnCallback(fn () => $this->timezone);
  126. $this->l10nFactory = $this->createMock(IFactory::class);
  127. $this->l = $this->createMock(IL10N::class);
  128. $this->l->method('t')
  129. ->willReturnCallback(function ($text, $parameters = []) {
  130. return vsprintf($text, $parameters);
  131. });
  132. $this->l->method('n')
  133. ->willReturnCallback(function ($singular, $plural, $count, $parameters = []) {
  134. return vsprintf(str_replace('%n', $count, ($count === 1) ? $singular : $plural), $parameters);
  135. });
  136. $this->l10nFactory->method('get')->willReturn($this->l);
  137. $this->factory = new DummyFactory(\OC::$server);
  138. $this->manager = $this->createManager($this->factory);
  139. $this->defaultProvider = $this->createMock(DefaultShareProvider::class);
  140. $this->defaultProvider->method('identifier')->willReturn('default');
  141. $this->factory->setProvider($this->defaultProvider);
  142. }
  143. private function createManager(IProviderFactory $factory): Manager {
  144. return new Manager(
  145. $this->logger,
  146. $this->config,
  147. $this->secureRandom,
  148. $this->hasher,
  149. $this->mountManager,
  150. $this->groupManager,
  151. $this->l10nFactory,
  152. $factory,
  153. $this->userManager,
  154. $this->rootFolder,
  155. $this->mailer,
  156. $this->urlGenerator,
  157. $this->defaults,
  158. $this->dispatcher,
  159. $this->userSession,
  160. $this->knownUserService,
  161. $this->shareDisabledChecker,
  162. $this->dateTimeZone,
  163. );
  164. }
  165. /**
  166. * @return MockBuilder
  167. */
  168. private function createManagerMock() {
  169. return $this->getMockBuilder(Manager::class)
  170. ->setConstructorArgs([
  171. $this->logger,
  172. $this->config,
  173. $this->secureRandom,
  174. $this->hasher,
  175. $this->mountManager,
  176. $this->groupManager,
  177. $this->l10nFactory,
  178. $this->factory,
  179. $this->userManager,
  180. $this->rootFolder,
  181. $this->mailer,
  182. $this->urlGenerator,
  183. $this->defaults,
  184. $this->dispatcher,
  185. $this->userSession,
  186. $this->knownUserService,
  187. $this->shareDisabledChecker,
  188. $this->dateTimeZone,
  189. ]);
  190. }
  191. private function createFolderMock(string $folderPath): MockObject&Folder {
  192. $folder = $this->createMock(Folder::class);
  193. $folder->method('getPath')->willReturn($folderPath);
  194. $folder->method('getRelativePath')->willReturnCallback(
  195. fn (string $path): ?string => PathHelper::getRelativePath($folderPath, $path)
  196. );
  197. return $folder;
  198. }
  199. public function testDeleteNoShareId(): void {
  200. $this->expectException(\InvalidArgumentException::class);
  201. $share = $this->manager->newShare();
  202. $this->manager->deleteShare($share);
  203. }
  204. public function dataTestDelete() {
  205. $user = $this->createMock(IUser::class);
  206. $user->method('getUID')->willReturn('sharedWithUser');
  207. $group = $this->createMock(IGroup::class);
  208. $group->method('getGID')->willReturn('sharedWithGroup');
  209. return [
  210. [IShare::TYPE_USER, 'sharedWithUser'],
  211. [IShare::TYPE_GROUP, 'sharedWithGroup'],
  212. [IShare::TYPE_LINK, ''],
  213. [IShare::TYPE_REMOTE, 'foo@bar.com'],
  214. ];
  215. }
  216. /**
  217. * @dataProvider dataTestDelete
  218. */
  219. public function testDelete($shareType, $sharedWith): void {
  220. $manager = $this->createManagerMock()
  221. ->setMethods(['getShareById', 'deleteChildren', 'promoteReshares'])
  222. ->getMock();
  223. $manager->method('deleteChildren')->willReturn([]);
  224. $path = $this->createMock(File::class);
  225. $path->method('getId')->willReturn(1);
  226. $share = $this->manager->newShare();
  227. $share->setId(42)
  228. ->setProviderId('prov')
  229. ->setShareType($shareType)
  230. ->setSharedWith($sharedWith)
  231. ->setSharedBy('sharedBy')
  232. ->setNode($path)
  233. ->setTarget('myTarget');
  234. $manager->expects($this->once())->method('deleteChildren')->with($share);
  235. $manager->expects($this->once())->method('promoteReshares')->with($share);
  236. $this->defaultProvider
  237. ->expects($this->once())
  238. ->method('delete')
  239. ->with($share);
  240. $this->dispatcher->expects($this->exactly(2))
  241. ->method('dispatchTyped')
  242. ->withConsecutive(
  243. [
  244. $this->callBack(function (BeforeShareDeletedEvent $e) use ($share) {
  245. return $e->getShare() === $share;
  246. })],
  247. [
  248. $this->callBack(function (ShareDeletedEvent $e) use ($share) {
  249. return $e->getShare() === $share;
  250. })]
  251. );
  252. $manager->deleteShare($share);
  253. }
  254. public function testDeleteLazyShare(): void {
  255. $manager = $this->createManagerMock()
  256. ->setMethods(['getShareById', 'deleteChildren', 'promoteReshares'])
  257. ->getMock();
  258. $manager->method('deleteChildren')->willReturn([]);
  259. $share = $this->manager->newShare();
  260. $share->setId(42)
  261. ->setProviderId('prov')
  262. ->setShareType(IShare::TYPE_USER)
  263. ->setSharedWith('sharedWith')
  264. ->setSharedBy('sharedBy')
  265. ->setShareOwner('shareOwner')
  266. ->setTarget('myTarget')
  267. ->setNodeId(1)
  268. ->setNodeType('file');
  269. $this->rootFolder->expects($this->never())->method($this->anything());
  270. $manager->expects($this->once())->method('deleteChildren')->with($share);
  271. $manager->expects($this->once())->method('promoteReshares')->with($share);
  272. $this->defaultProvider
  273. ->expects($this->once())
  274. ->method('delete')
  275. ->with($share);
  276. $this->dispatcher->expects($this->exactly(2))
  277. ->method('dispatchTyped')
  278. ->withConsecutive(
  279. [
  280. $this->callBack(function (BeforeShareDeletedEvent $e) use ($share) {
  281. return $e->getShare() === $share;
  282. })],
  283. [
  284. $this->callBack(function (ShareDeletedEvent $e) use ($share) {
  285. return $e->getShare() === $share;
  286. })]
  287. );
  288. $manager->deleteShare($share);
  289. }
  290. public function testDeleteNested(): void {
  291. $manager = $this->createManagerMock()
  292. ->setMethods(['getShareById', 'promoteReshares'])
  293. ->getMock();
  294. $path = $this->createMock(File::class);
  295. $path->method('getId')->willReturn(1);
  296. $share1 = $this->manager->newShare();
  297. $share1->setId(42)
  298. ->setProviderId('prov')
  299. ->setShareType(IShare::TYPE_USER)
  300. ->setSharedWith('sharedWith1')
  301. ->setSharedBy('sharedBy1')
  302. ->setNode($path)
  303. ->setTarget('myTarget1');
  304. $share2 = $this->manager->newShare();
  305. $share2->setId(43)
  306. ->setProviderId('prov')
  307. ->setShareType(IShare::TYPE_GROUP)
  308. ->setSharedWith('sharedWith2')
  309. ->setSharedBy('sharedBy2')
  310. ->setNode($path)
  311. ->setTarget('myTarget2')
  312. ->setParent(42);
  313. $share3 = $this->manager->newShare();
  314. $share3->setId(44)
  315. ->setProviderId('prov')
  316. ->setShareType(IShare::TYPE_LINK)
  317. ->setSharedBy('sharedBy3')
  318. ->setNode($path)
  319. ->setTarget('myTarget3')
  320. ->setParent(43);
  321. $this->defaultProvider
  322. ->method('getChildren')
  323. ->willReturnMap([
  324. [$share1, [$share2]],
  325. [$share2, [$share3]],
  326. [$share3, []],
  327. ]);
  328. $this->defaultProvider
  329. ->method('delete')
  330. ->withConsecutive([$share3], [$share2], [$share1]);
  331. $this->dispatcher->expects($this->exactly(6))
  332. ->method('dispatchTyped')
  333. ->withConsecutive(
  334. [
  335. $this->callBack(function (BeforeShareDeletedEvent $e) use ($share1) {
  336. return $e->getShare()->getId() === $share1->getId();
  337. })
  338. ],
  339. [
  340. $this->callBack(function (BeforeShareDeletedEvent $e) use ($share2) {
  341. return $e->getShare()->getId() === $share2->getId();
  342. })
  343. ],
  344. [
  345. $this->callBack(function (BeforeShareDeletedEvent $e) use ($share3) {
  346. return $e->getShare()->getId() === $share3->getId();
  347. })
  348. ],
  349. [
  350. $this->callBack(function (ShareDeletedEvent $e) use ($share3) {
  351. return $e->getShare()->getId() === $share3->getId();
  352. })
  353. ],
  354. [
  355. $this->callBack(function (ShareDeletedEvent $e) use ($share2) {
  356. return $e->getShare()->getId() === $share2->getId();
  357. })
  358. ],
  359. [
  360. $this->callBack(function (ShareDeletedEvent $e) use ($share1) {
  361. return $e->getShare()->getId() === $share1->getId();
  362. })
  363. ],
  364. );
  365. $manager->deleteShare($share1);
  366. }
  367. public function testDeleteFromSelf(): void {
  368. $manager = $this->createManagerMock()
  369. ->setMethods(['getShareById'])
  370. ->getMock();
  371. $recipientId = 'unshareFrom';
  372. $share = $this->manager->newShare();
  373. $share->setId(42)
  374. ->setProviderId('prov')
  375. ->setShareType(IShare::TYPE_USER)
  376. ->setSharedWith('sharedWith')
  377. ->setSharedBy('sharedBy')
  378. ->setShareOwner('shareOwner')
  379. ->setTarget('myTarget')
  380. ->setNodeId(1)
  381. ->setNodeType('file');
  382. $this->defaultProvider
  383. ->expects($this->once())
  384. ->method('deleteFromSelf')
  385. ->with($share, $recipientId);
  386. $this->dispatcher->expects($this->once())
  387. ->method('dispatchTyped')
  388. ->with(
  389. $this->callBack(function (ShareDeletedFromSelfEvent $e) use ($share) {
  390. return $e->getShare() === $share;
  391. })
  392. );
  393. $manager->deleteFromSelf($share, $recipientId);
  394. }
  395. public function testDeleteChildren(): void {
  396. $manager = $this->createManagerMock()
  397. ->setMethods(['deleteShare'])
  398. ->getMock();
  399. $share = $this->createMock(IShare::class);
  400. $share->method('getShareType')->willReturn(IShare::TYPE_USER);
  401. $child1 = $this->createMock(IShare::class);
  402. $child1->method('getShareType')->willReturn(IShare::TYPE_USER);
  403. $child2 = $this->createMock(IShare::class);
  404. $child2->method('getShareType')->willReturn(IShare::TYPE_USER);
  405. $child3 = $this->createMock(IShare::class);
  406. $child3->method('getShareType')->willReturn(IShare::TYPE_USER);
  407. $shares = [
  408. $child1,
  409. $child2,
  410. $child3,
  411. ];
  412. $this->defaultProvider
  413. ->expects($this->exactly(4))
  414. ->method('getChildren')
  415. ->willReturnCallback(function ($_share) use ($share, $shares) {
  416. if ($_share === $share) {
  417. return $shares;
  418. }
  419. return [];
  420. });
  421. $this->defaultProvider
  422. ->expects($this->exactly(3))
  423. ->method('delete')
  424. ->withConsecutive([$child1], [$child2], [$child3]);
  425. $result = self::invokePrivate($manager, 'deleteChildren', [$share]);
  426. $this->assertSame($shares, $result);
  427. }
  428. public function testPromoteReshareFile(): void {
  429. $manager = $this->createManagerMock()
  430. ->setMethods(['updateShare', 'getSharesInFolder', 'generalCreateChecks'])
  431. ->getMock();
  432. $file = $this->createMock(File::class);
  433. $share = $this->createMock(IShare::class);
  434. $share->method('getShareType')->willReturn(IShare::TYPE_USER);
  435. $share->method('getNodeType')->willReturn('folder');
  436. $share->method('getSharedWith')->willReturn('userB');
  437. $share->method('getNode')->willReturn($file);
  438. $reShare = $this->createMock(IShare::class);
  439. $reShare->method('getShareType')->willReturn(IShare::TYPE_USER);
  440. $reShare->method('getSharedBy')->willReturn('userB');
  441. $reShare->method('getSharedWith')->willReturn('userC');
  442. $reShare->method('getNode')->willReturn($file);
  443. $this->defaultProvider->method('getSharesBy')
  444. ->willReturnCallback(function ($userId, $shareType, $node, $reshares, $limit, $offset) use ($reShare, $file) {
  445. $this->assertEquals($file, $node);
  446. if ($shareType === IShare::TYPE_USER) {
  447. return match($userId) {
  448. 'userB' => [$reShare],
  449. };
  450. } else {
  451. return [];
  452. }
  453. });
  454. $manager->method('generalCreateChecks')->willThrowException(new GenericShareException());
  455. $manager->expects($this->exactly(1))->method('updateShare')->with($reShare);
  456. self::invokePrivate($manager, 'promoteReshares', [$share]);
  457. }
  458. public function testPromoteReshare(): void {
  459. $manager = $this->createManagerMock()
  460. ->setMethods(['updateShare', 'getSharesInFolder', 'generalCreateChecks'])
  461. ->getMock();
  462. $folder = $this->createFolderMock('/path/to/folder');
  463. $subFolder = $this->createFolderMock('/path/to/folder/sub');
  464. $otherFolder = $this->createFolderMock('/path/to/otherfolder/');
  465. $share = $this->createMock(IShare::class);
  466. $share->method('getShareType')->willReturn(IShare::TYPE_USER);
  467. $share->method('getNodeType')->willReturn('folder');
  468. $share->method('getSharedWith')->willReturn('userB');
  469. $share->method('getNode')->willReturn($folder);
  470. $reShare = $this->createMock(IShare::class);
  471. $reShare->method('getShareType')->willReturn(IShare::TYPE_USER);
  472. $reShare->method('getSharedBy')->willReturn('userB');
  473. $reShare->method('getSharedWith')->willReturn('userC');
  474. $reShare->method('getNode')->willReturn($folder);
  475. $reShareInSubFolder = $this->createMock(IShare::class);
  476. $reShareInSubFolder->method('getShareType')->willReturn(IShare::TYPE_USER);
  477. $reShareInSubFolder->method('getSharedBy')->willReturn('userB');
  478. $reShareInSubFolder->method('getNode')->willReturn($subFolder);
  479. $reShareInOtherFolder = $this->createMock(IShare::class);
  480. $reShareInOtherFolder->method('getShareType')->willReturn(IShare::TYPE_USER);
  481. $reShareInOtherFolder->method('getSharedBy')->willReturn('userB');
  482. $reShareInOtherFolder->method('getNode')->willReturn($otherFolder);
  483. $this->defaultProvider->method('getSharesBy')
  484. ->willReturnCallback(function ($userId, $shareType, $node, $reshares, $limit, $offset) use ($reShare, $reShareInSubFolder, $reShareInOtherFolder) {
  485. if ($shareType === IShare::TYPE_USER) {
  486. return match($userId) {
  487. 'userB' => [$reShare,$reShareInSubFolder,$reShareInOtherFolder],
  488. };
  489. } else {
  490. return [];
  491. }
  492. });
  493. $manager->method('generalCreateChecks')->willThrowException(new GenericShareException());
  494. $manager->expects($this->exactly(2))->method('updateShare')->withConsecutive([$reShare], [$reShareInSubFolder]);
  495. self::invokePrivate($manager, 'promoteReshares', [$share]);
  496. }
  497. public function testPromoteReshareWhenUserHasAnotherShare(): void {
  498. $manager = $this->createManagerMock()
  499. ->setMethods(['updateShare', 'getSharesInFolder', 'getSharedWith', 'generalCreateChecks'])
  500. ->getMock();
  501. $folder = $this->createFolderMock('/path/to/folder');
  502. $share = $this->createMock(IShare::class);
  503. $share->method('getShareType')->willReturn(IShare::TYPE_USER);
  504. $share->method('getNodeType')->willReturn('folder');
  505. $share->method('getSharedWith')->willReturn('userB');
  506. $share->method('getNode')->willReturn($folder);
  507. $reShare = $this->createMock(IShare::class);
  508. $reShare->method('getShareType')->willReturn(IShare::TYPE_USER);
  509. $reShare->method('getNodeType')->willReturn('folder');
  510. $reShare->method('getSharedBy')->willReturn('userB');
  511. $reShare->method('getNode')->willReturn($folder);
  512. $this->defaultProvider->method('getSharesBy')->willReturn([$reShare]);
  513. $manager->method('generalCreateChecks')->willReturn(true);
  514. /* No share is promoted because generalCreateChecks does not throw */
  515. $manager->expects($this->never())->method('updateShare');
  516. self::invokePrivate($manager, 'promoteReshares', [$share]);
  517. }
  518. public function testPromoteReshareOfUsersInGroupShare(): void {
  519. $manager = $this->createManagerMock()
  520. ->setMethods(['updateShare', 'getSharesInFolder', 'getSharedWith', 'generalCreateChecks'])
  521. ->getMock();
  522. $folder = $this->createFolderMock('/path/to/folder');
  523. $userA = $this->createMock(IUser::class);
  524. $userA->method('getUID')->willReturn('userA');
  525. $share = $this->createMock(IShare::class);
  526. $share->method('getShareType')->willReturn(IShare::TYPE_GROUP);
  527. $share->method('getNodeType')->willReturn('folder');
  528. $share->method('getSharedWith')->willReturn('Group');
  529. $share->method('getNode')->willReturn($folder);
  530. $share->method('getShareOwner')->willReturn($userA);
  531. $reShare1 = $this->createMock(IShare::class);
  532. $reShare1->method('getShareType')->willReturn(IShare::TYPE_USER);
  533. $reShare1->method('getNodeType')->willReturn('folder');
  534. $reShare1->method('getSharedBy')->willReturn('userB');
  535. $reShare1->method('getNode')->willReturn($folder);
  536. $reShare2 = $this->createMock(IShare::class);
  537. $reShare2->method('getShareType')->willReturn(IShare::TYPE_USER);
  538. $reShare2->method('getNodeType')->willReturn('folder');
  539. $reShare2->method('getSharedBy')->willReturn('userC');
  540. $reShare2->method('getNode')->willReturn($folder);
  541. $userB = $this->createMock(IUser::class);
  542. $userB->method('getUID')->willReturn('userB');
  543. $userC = $this->createMock(IUser::class);
  544. $userC->method('getUID')->willReturn('userC');
  545. $group = $this->createMock(IGroup::class);
  546. $group->method('getUsers')->willReturn([$userB, $userC]);
  547. $this->groupManager->method('get')->with('Group')->willReturn($group);
  548. $this->defaultProvider->method('getSharesBy')
  549. ->willReturnCallback(function ($userId, $shareType, $node, $reshares, $limit, $offset) use ($reShare1, $reShare2) {
  550. if ($shareType === IShare::TYPE_USER) {
  551. return match($userId) {
  552. 'userB' => [$reShare1],
  553. 'userC' => [$reShare2],
  554. };
  555. } else {
  556. return [];
  557. }
  558. });
  559. $manager->method('generalCreateChecks')->willThrowException(new GenericShareException());
  560. $manager->method('getSharedWith')->willReturn([]);
  561. $manager->expects($this->exactly(2))->method('updateShare')->withConsecutive([$reShare1], [$reShare2]);
  562. self::invokePrivate($manager, 'promoteReshares', [$share]);
  563. }
  564. public function testGetShareById(): void {
  565. $share = $this->createMock(IShare::class);
  566. $this->defaultProvider
  567. ->expects($this->once())
  568. ->method('getShareById')
  569. ->with(42)
  570. ->willReturn($share);
  571. $this->assertEquals($share, $this->manager->getShareById('default:42'));
  572. }
  573. public function testGetExpiredShareById(): void {
  574. $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class);
  575. $manager = $this->createManagerMock()
  576. ->setMethods(['deleteShare'])
  577. ->getMock();
  578. $date = new \DateTime();
  579. $date->setTime(0, 0, 0);
  580. $share = $this->manager->newShare();
  581. $share->setExpirationDate($date)
  582. ->setShareType(IShare::TYPE_LINK);
  583. $this->defaultProvider->expects($this->once())
  584. ->method('getShareById')
  585. ->with('42')
  586. ->willReturn($share);
  587. $manager->expects($this->once())
  588. ->method('deleteShare')
  589. ->with($share);
  590. $manager->getShareById('default:42');
  591. }
  592. public function testVerifyPasswordNullButEnforced(): void {
  593. $this->expectException(\InvalidArgumentException::class);
  594. $this->expectExceptionMessage('Passwords are enforced for link and mail shares');
  595. $this->config->method('getAppValue')->willReturnMap([
  596. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  597. ['core', 'shareapi_enforce_links_password', 'no', 'yes'],
  598. ]);
  599. self::invokePrivate($this->manager, 'verifyPassword', [null]);
  600. }
  601. public function testVerifyPasswordNotEnforcedGroup(): void {
  602. $this->config->method('getAppValue')->willReturnMap([
  603. ['core', 'shareapi_enforce_links_password_excluded_groups', '', '["admin"]'],
  604. ['core', 'shareapi_enforce_links_password', 'no', 'yes'],
  605. ]);
  606. // Create admin user
  607. $user = $this->createMock(IUser::class);
  608. $this->userSession->method('getUser')->willReturn($user);
  609. $this->groupManager->method('getUserGroupIds')->with($user)->willReturn(['admin']);
  610. $result = self::invokePrivate($this->manager, 'verifyPassword', [null]);
  611. $this->assertNull($result);
  612. }
  613. public function testVerifyPasswordNotEnforcedMultipleGroups(): void {
  614. $this->config->method('getAppValue')->willReturnMap([
  615. ['core', 'shareapi_enforce_links_password_excluded_groups', '', '["admin", "special"]'],
  616. ['core', 'shareapi_enforce_links_password', 'no', 'yes'],
  617. ]);
  618. // Create admin user
  619. $user = $this->createMock(IUser::class);
  620. $this->userSession->method('getUser')->willReturn($user);
  621. $this->groupManager->method('getUserGroupIds')->with($user)->willReturn(['special']);
  622. $result = self::invokePrivate($this->manager, 'verifyPassword', [null]);
  623. $this->assertNull($result);
  624. }
  625. public function testVerifyPasswordNull(): void {
  626. $this->config->method('getAppValue')->willReturnMap([
  627. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  628. ['core', 'shareapi_enforce_links_password', 'no', 'no'],
  629. ]);
  630. $result = self::invokePrivate($this->manager, 'verifyPassword', [null]);
  631. $this->assertNull($result);
  632. }
  633. public function testVerifyPasswordHook(): void {
  634. $this->config->method('getAppValue')->willReturnMap([
  635. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  636. ['core', 'shareapi_enforce_links_password', 'no', 'no'],
  637. ]);
  638. $this->dispatcher->expects($this->once())->method('dispatchTyped')
  639. ->willReturnCallback(function (Event $event) {
  640. $this->assertInstanceOf(ValidatePasswordPolicyEvent::class, $event);
  641. /** @var ValidatePasswordPolicyEvent $event */
  642. $this->assertSame('password', $event->getPassword());
  643. }
  644. );
  645. $result = self::invokePrivate($this->manager, 'verifyPassword', ['password']);
  646. $this->assertNull($result);
  647. }
  648. public function testVerifyPasswordHookFails(): void {
  649. $this->expectException(\Exception::class);
  650. $this->expectExceptionMessage('password not accepted');
  651. $this->config->method('getAppValue')->willReturnMap([
  652. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  653. ['core', 'shareapi_enforce_links_password', 'no', 'no'],
  654. ]);
  655. $this->dispatcher->expects($this->once())->method('dispatchTyped')
  656. ->willReturnCallback(function (Event $event) {
  657. $this->assertInstanceOf(ValidatePasswordPolicyEvent::class, $event);
  658. /** @var ValidatePasswordPolicyEvent $event */
  659. $this->assertSame('password', $event->getPassword());
  660. throw new HintException('message', 'password not accepted');
  661. }
  662. );
  663. self::invokePrivate($this->manager, 'verifyPassword', ['password']);
  664. }
  665. public function createShare($id, $type, $node, $sharedWith, $sharedBy, $shareOwner,
  666. $permissions, $expireDate = null, $password = null, $attributes = null) {
  667. $share = $this->createMock(IShare::class);
  668. $share->method('getShareType')->willReturn($type);
  669. $share->method('getSharedWith')->willReturn($sharedWith);
  670. $share->method('getSharedBy')->willReturn($sharedBy);
  671. $share->method('getShareOwner')->willReturn($shareOwner);
  672. $share->method('getNode')->willReturn($node);
  673. if ($node && $node->getId()) {
  674. $share->method('getNodeId')->willReturn($node->getId());
  675. }
  676. $share->method('getPermissions')->willReturn($permissions);
  677. $share->method('getAttributes')->willReturn($attributes);
  678. $share->method('getExpirationDate')->willReturn($expireDate);
  679. $share->method('getPassword')->willReturn($password);
  680. return $share;
  681. }
  682. public function dataGeneralChecks() {
  683. $user0 = 'user0';
  684. $user2 = 'user1';
  685. $group0 = 'group0';
  686. $owner = $this->createMock(IUser::class);
  687. $owner->method('getUID')
  688. ->willReturn($user0);
  689. $file = $this->createMock(File::class);
  690. $node = $this->createMock(Node::class);
  691. $storage = $this->createMock(IStorage::class);
  692. $storage->method('instanceOfStorage')
  693. ->with('\OCA\Files_Sharing\External\Storage')
  694. ->willReturn(false);
  695. $file->method('getStorage')
  696. ->willReturn($storage);
  697. $file->method('getId')->willReturn(108);
  698. $node->method('getStorage')
  699. ->willReturn($storage);
  700. $node->method('getId')->willReturn(108);
  701. $data = [
  702. [$this->createShare(null, IShare::TYPE_USER, $file, null, $user0, $user0, 31, null, null), 'Share recipient is not a valid user', true],
  703. [$this->createShare(null, IShare::TYPE_USER, $file, $group0, $user0, $user0, 31, null, null), 'Share recipient is not a valid user', true],
  704. [$this->createShare(null, IShare::TYPE_USER, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'Share recipient is not a valid user', true],
  705. [$this->createShare(null, IShare::TYPE_GROUP, $file, null, $user0, $user0, 31, null, null), 'Share recipient is not a valid group', true],
  706. [$this->createShare(null, IShare::TYPE_GROUP, $file, $user2, $user0, $user0, 31, null, null), 'Share recipient is not a valid group', true],
  707. [$this->createShare(null, IShare::TYPE_GROUP, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'Share recipient is not a valid group', true],
  708. [$this->createShare(null, IShare::TYPE_LINK, $file, $user2, $user0, $user0, 31, null, null), 'Share recipient should be empty', true],
  709. [$this->createShare(null, IShare::TYPE_LINK, $file, $group0, $user0, $user0, 31, null, null), 'Share recipient should be empty', true],
  710. [$this->createShare(null, IShare::TYPE_LINK, $file, 'foo@bar.com', $user0, $user0, 31, null, null), 'Share recipient should be empty', true],
  711. [$this->createShare(null, -1, $file, null, $user0, $user0, 31, null, null), 'Unknown share type', true],
  712. [$this->createShare(null, IShare::TYPE_USER, $file, $user2, null, $user0, 31, null, null), 'Share initiator must be set', true],
  713. [$this->createShare(null, IShare::TYPE_GROUP, $file, $group0, null, $user0, 31, null, null), 'Share initiator must be set', true],
  714. [$this->createShare(null, IShare::TYPE_LINK, $file, null, null, $user0, 31, null, null), 'Share initiator must be set', true],
  715. [$this->createShare(null, IShare::TYPE_USER, $file, $user0, $user0, $user0, 31, null, null), 'Cannot share with yourself', true],
  716. [$this->createShare(null, IShare::TYPE_USER, null, $user2, $user0, $user0, 31, null, null), 'Shared path must be set', true],
  717. [$this->createShare(null, IShare::TYPE_GROUP, null, $group0, $user0, $user0, 31, null, null), 'Shared path must be set', true],
  718. [$this->createShare(null, IShare::TYPE_LINK, null, null, $user0, $user0, 31, null, null), 'Shared path must be set', true],
  719. [$this->createShare(null, IShare::TYPE_USER, $node, $user2, $user0, $user0, 31, null, null), 'Shared path must be either a file or a folder', true],
  720. [$this->createShare(null, IShare::TYPE_GROUP, $node, $group0, $user0, $user0, 31, null, null), 'Shared path must be either a file or a folder', true],
  721. [$this->createShare(null, IShare::TYPE_LINK, $node, null, $user0, $user0, 31, null, null), 'Shared path must be either a file or a folder', true],
  722. ];
  723. $nonShareAble = $this->createMock(Folder::class);
  724. $nonShareAble->method('getId')->willReturn(108);
  725. $nonShareAble->method('isShareable')->willReturn(false);
  726. $nonShareAble->method('getPath')->willReturn('path');
  727. $nonShareAble->method('getName')->willReturn('name');
  728. $nonShareAble->method('getOwner')
  729. ->willReturn($owner);
  730. $nonShareAble->method('getStorage')
  731. ->willReturn($storage);
  732. $data[] = [$this->createShare(null, IShare::TYPE_USER, $nonShareAble, $user2, $user0, $user0, 31, null, null), 'You are not allowed to share name', true];
  733. $data[] = [$this->createShare(null, IShare::TYPE_GROUP, $nonShareAble, $group0, $user0, $user0, 31, null, null), 'You are not allowed to share name', true];
  734. $data[] = [$this->createShare(null, IShare::TYPE_LINK, $nonShareAble, null, $user0, $user0, 31, null, null), 'You are not allowed to share name', true];
  735. $limitedPermssions = $this->createMock(File::class);
  736. $limitedPermssions->method('isShareable')->willReturn(true);
  737. $limitedPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_READ);
  738. $limitedPermssions->method('getId')->willReturn(108);
  739. $limitedPermssions->method('getPath')->willReturn('path');
  740. $limitedPermssions->method('getName')->willReturn('name');
  741. $limitedPermssions->method('getOwner')
  742. ->willReturn($owner);
  743. $limitedPermssions->method('getStorage')
  744. ->willReturn($storage);
  745. $data[] = [$this->createShare(null, IShare::TYPE_USER, $limitedPermssions, $user2, $user0, $user0, null, null, null), 'Valid permissions are required for sharing', true];
  746. $data[] = [$this->createShare(null, IShare::TYPE_GROUP, $limitedPermssions, $group0, $user0, $user0, null, null, null), 'Valid permissions are required for sharing', true];
  747. $data[] = [$this->createShare(null, IShare::TYPE_LINK, $limitedPermssions, null, $user0, $user0, null, null, null), 'Valid permissions are required for sharing', true];
  748. $mount = $this->createMock(MoveableMount::class);
  749. $limitedPermssions->method('getMountPoint')->willReturn($mount);
  750. $data[] = [$this->createShare(null, IShare::TYPE_USER, $limitedPermssions, $user2, $user0, $user0, 31, null, null), 'Cannot increase permissions of path', true];
  751. $data[] = [$this->createShare(null, IShare::TYPE_GROUP, $limitedPermssions, $group0, $user0, $user0, 17, null, null), 'Cannot increase permissions of path', true];
  752. $data[] = [$this->createShare(null, IShare::TYPE_LINK, $limitedPermssions, null, $user0, $user0, 3, null, null), 'Cannot increase permissions of path', true];
  753. $nonMovableStorage = $this->createMock(IStorage::class);
  754. $nonMovableStorage->method('instanceOfStorage')
  755. ->with('\OCA\Files_Sharing\External\Storage')
  756. ->willReturn(false);
  757. $nonMovableStorage->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_ALL);
  758. $nonMoveableMountPermssions = $this->createMock(Folder::class);
  759. $nonMoveableMountPermssions->method('isShareable')->willReturn(true);
  760. $nonMoveableMountPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_READ);
  761. $nonMoveableMountPermssions->method('getId')->willReturn(108);
  762. $nonMoveableMountPermssions->method('getPath')->willReturn('path');
  763. $nonMoveableMountPermssions->method('getName')->willReturn('name');
  764. $nonMoveableMountPermssions->method('getInternalPath')->willReturn('');
  765. $nonMoveableMountPermssions->method('getOwner')
  766. ->willReturn($owner);
  767. $nonMoveableMountPermssions->method('getStorage')
  768. ->willReturn($nonMovableStorage);
  769. $data[] = [$this->createShare(null, IShare::TYPE_USER, $nonMoveableMountPermssions, $user2, $user0, $user0, 11, null, null), 'Cannot increase permissions of path', false];
  770. $data[] = [$this->createShare(null, IShare::TYPE_GROUP, $nonMoveableMountPermssions, $group0, $user0, $user0, 11, null, null), 'Cannot increase permissions of path', false];
  771. $rootFolder = $this->createMock(Folder::class);
  772. $rootFolder->method('isShareable')->willReturn(true);
  773. $rootFolder->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_ALL);
  774. $rootFolder->method('getId')->willReturn(42);
  775. $data[] = [$this->createShare(null, IShare::TYPE_USER, $rootFolder, $user2, $user0, $user0, 30, null, null), 'You cannot share your root folder', true];
  776. $data[] = [$this->createShare(null, IShare::TYPE_GROUP, $rootFolder, $group0, $user0, $user0, 2, null, null), 'You cannot share your root folder', true];
  777. $data[] = [$this->createShare(null, IShare::TYPE_LINK, $rootFolder, null, $user0, $user0, 16, null, null), 'You cannot share your root folder', true];
  778. $allPermssions = $this->createMock(Folder::class);
  779. $allPermssions->method('isShareable')->willReturn(true);
  780. $allPermssions->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_ALL);
  781. $allPermssions->method('getId')->willReturn(108);
  782. $allPermssions->method('getOwner')
  783. ->willReturn($owner);
  784. $allPermssions->method('getStorage')
  785. ->willReturn($storage);
  786. $data[] = [$this->createShare(null, IShare::TYPE_USER, $allPermssions, $user2, $user0, $user0, 30, null, null), 'Shares need at least read permissions', true];
  787. $data[] = [$this->createShare(null, IShare::TYPE_GROUP, $allPermssions, $group0, $user0, $user0, 2, null, null), 'Shares need at least read permissions', true];
  788. $data[] = [$this->createShare(null, IShare::TYPE_USER, $allPermssions, $user2, $user0, $user0, 31, null, null), null, false];
  789. $data[] = [$this->createShare(null, IShare::TYPE_GROUP, $allPermssions, $group0, $user0, $user0, 3, null, null), null, false];
  790. $data[] = [$this->createShare(null, IShare::TYPE_LINK, $allPermssions, null, $user0, $user0, 17, null, null), null, false];
  791. $remoteStorage = $this->createMock(IStorage::class);
  792. $remoteStorage->method('instanceOfStorage')
  793. ->with('\OCA\Files_Sharing\External\Storage')
  794. ->willReturn(true);
  795. $remoteFile = $this->createMock(Folder::class);
  796. $remoteFile->method('isShareable')->willReturn(true);
  797. $remoteFile->method('getPermissions')->willReturn(\OCP\Constants::PERMISSION_READ ^ \OCP\Constants::PERMISSION_UPDATE);
  798. $remoteFile->method('getId')->willReturn(108);
  799. $remoteFile->method('getOwner')
  800. ->willReturn($owner);
  801. $remoteFile->method('getStorage')
  802. ->willReturn($storage);
  803. $data[] = [$this->createShare(null, IShare::TYPE_REMOTE, $remoteFile, $user2, $user0, $user0, 1, null, null), null, false];
  804. $data[] = [$this->createShare(null, IShare::TYPE_REMOTE, $remoteFile, $user2, $user0, $user0, 3, null, null), null, false];
  805. $data[] = [$this->createShare(null, IShare::TYPE_REMOTE, $remoteFile, $user2, $user0, $user0, 31, null, null), 'Cannot increase permissions of ', true];
  806. return $data;
  807. }
  808. /**
  809. * @dataProvider dataGeneralChecks
  810. *
  811. * @param $share
  812. * @param $exceptionMessage
  813. * @param $exception
  814. */
  815. public function testGeneralChecks($share, $exceptionMessage, $exception): void {
  816. $thrown = null;
  817. $this->userManager->method('userExists')->willReturnMap([
  818. ['user0', true],
  819. ['user1', true],
  820. ]);
  821. $this->groupManager->method('groupExists')->willReturnMap([
  822. ['group0', true],
  823. ]);
  824. $userFolder = $this->createMock(Folder::class);
  825. $userFolder->expects($this->any())
  826. ->method('getId')
  827. ->willReturn(42);
  828. // Id 108 is used in the data to refer to the node of the share.
  829. $userFolder->method('getById')
  830. ->with(108)
  831. ->willReturn([$share->getNode()]);
  832. $userFolder->expects($this->any())
  833. ->method('getRelativePath')
  834. ->willReturnArgument(0);
  835. $this->rootFolder->method('getUserFolder')->willReturn($userFolder);
  836. try {
  837. self::invokePrivate($this->manager, 'generalCreateChecks', [$share]);
  838. $thrown = false;
  839. } catch (\OCP\Share\Exceptions\GenericShareException $e) {
  840. $this->assertEquals($exceptionMessage, $e->getHint());
  841. $thrown = true;
  842. } catch (\InvalidArgumentException $e) {
  843. $this->assertEquals($exceptionMessage, $e->getMessage());
  844. $thrown = true;
  845. }
  846. $this->assertSame($exception, $thrown);
  847. }
  848. public function testGeneralCheckShareRoot(): void {
  849. $this->expectException(\InvalidArgumentException::class);
  850. $this->expectExceptionMessage('You cannot share your root folder');
  851. $thrown = null;
  852. $this->userManager->method('userExists')->willReturnMap([
  853. ['user0', true],
  854. ['user1', true],
  855. ]);
  856. $userFolder = $this->createMock(Folder::class);
  857. $userFolder->method('isSubNode')->with($userFolder)->willReturn(false);
  858. $this->rootFolder->method('getUserFolder')->willReturn($userFolder);
  859. $share = $this->manager->newShare();
  860. $share->setShareType(IShare::TYPE_USER)
  861. ->setSharedWith('user0')
  862. ->setSharedBy('user1')
  863. ->setNode($userFolder);
  864. self::invokePrivate($this->manager, 'generalCreateChecks', [$share]);
  865. }
  866. public function validateExpirationDateInternalProvider() {
  867. return [[IShare::TYPE_USER], [IShare::TYPE_REMOTE], [IShare::TYPE_REMOTE_GROUP]];
  868. }
  869. /**
  870. * @dataProvider validateExpirationDateInternalProvider
  871. */
  872. public function testValidateExpirationDateInternalInPast($shareType): void {
  873. $this->expectException(\OCP\Share\Exceptions\GenericShareException::class);
  874. $this->expectExceptionMessage('Expiration date is in the past');
  875. // Expire date in the past
  876. $past = new \DateTime();
  877. $past->sub(new \DateInterval('P1D'));
  878. $share = $this->manager->newShare();
  879. $share->setShareType($shareType);
  880. $share->setExpirationDate($past);
  881. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  882. }
  883. /**
  884. * @dataProvider validateExpirationDateInternalProvider
  885. */
  886. public function testValidateExpirationDateInternalEnforceButNotSet($shareType): void {
  887. $this->expectException(\InvalidArgumentException::class);
  888. $this->expectExceptionMessage('Expiration date is enforced');
  889. $share = $this->manager->newShare();
  890. $share->setProviderId('foo')->setId('bar');
  891. $share->setShareType($shareType);
  892. if ($shareType === IShare::TYPE_USER) {
  893. $this->config->method('getAppValue')
  894. ->willReturnMap([
  895. ['core', 'shareapi_default_internal_expire_date', 'no', 'yes'],
  896. ['core', 'shareapi_enforce_internal_expire_date', 'no', 'yes'],
  897. ]);
  898. } else {
  899. $this->config->method('getAppValue')
  900. ->willReturnMap([
  901. ['core', 'shareapi_default_remote_expire_date', 'no', 'yes'],
  902. ['core', 'shareapi_enforce_remote_expire_date', 'no', 'yes'],
  903. ]);
  904. }
  905. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  906. }
  907. /**
  908. * @dataProvider validateExpirationDateInternalProvider
  909. */
  910. public function testValidateExpirationDateInternalEnforceButNotEnabledAndNotSet($shareType): void {
  911. $share = $this->manager->newShare();
  912. $share->setProviderId('foo')->setId('bar');
  913. $share->setShareType($shareType);
  914. if ($shareType === IShare::TYPE_USER) {
  915. $this->config->method('getAppValue')
  916. ->willReturnMap([
  917. ['core', 'shareapi_enforce_internal_expire_date', 'no', 'yes'],
  918. ]);
  919. } else {
  920. $this->config->method('getAppValue')
  921. ->willReturnMap([
  922. ['core', 'shareapi_enforce_remote_expire_date', 'no', 'yes'],
  923. ]);
  924. }
  925. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  926. $this->assertNull($share->getExpirationDate());
  927. }
  928. /**
  929. * @dataProvider validateExpirationDateInternalProvider
  930. */
  931. public function testValidateExpirationDateInternalEnforceButNotSetNewShare($shareType): void {
  932. $share = $this->manager->newShare();
  933. $share->setShareType($shareType);
  934. if ($shareType === IShare::TYPE_USER) {
  935. $this->config->method('getAppValue')
  936. ->willReturnMap([
  937. ['core', 'shareapi_enforce_internal_expire_date', 'no', 'yes'],
  938. ['core', 'shareapi_internal_expire_after_n_days', '7', '3'],
  939. ['core', 'shareapi_default_internal_expire_date', 'no', 'yes'],
  940. ['core', 'internal_defaultExpDays', '3', '3'],
  941. ]);
  942. } else {
  943. $this->config->method('getAppValue')
  944. ->willReturnMap([
  945. ['core', 'shareapi_enforce_remote_expire_date', 'no', 'yes'],
  946. ['core', 'shareapi_remote_expire_after_n_days', '7', '3'],
  947. ['core', 'shareapi_default_remote_expire_date', 'no', 'yes'],
  948. ['core', 'remote_defaultExpDays', '3', '3'],
  949. ]);
  950. }
  951. $expected = new \DateTime('now', $this->timezone);
  952. $expected->setTime(0, 0, 0);
  953. $expected->add(new \DateInterval('P3D'));
  954. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  955. $this->assertNotNull($share->getExpirationDate());
  956. $this->assertEquals($expected, $share->getExpirationDate());
  957. }
  958. /**
  959. * @dataProvider validateExpirationDateInternalProvider
  960. */
  961. public function testValidateExpirationDateInternalEnforceRelaxedDefaultButNotSetNewShare($shareType): void {
  962. $share = $this->manager->newShare();
  963. $share->setShareType($shareType);
  964. if ($shareType === IShare::TYPE_USER) {
  965. $this->config->method('getAppValue')
  966. ->willReturnMap([
  967. ['core', 'shareapi_enforce_internal_expire_date', 'no', 'yes'],
  968. ['core', 'shareapi_internal_expire_after_n_days', '7', '3'],
  969. ['core', 'shareapi_default_internal_expire_date', 'no', 'yes'],
  970. ['core', 'internal_defaultExpDays', '3', '1'],
  971. ]);
  972. } else {
  973. $this->config->method('getAppValue')
  974. ->willReturnMap([
  975. ['core', 'shareapi_enforce_remote_expire_date', 'no', 'yes'],
  976. ['core', 'shareapi_remote_expire_after_n_days', '7', '3'],
  977. ['core', 'shareapi_default_remote_expire_date', 'no', 'yes'],
  978. ['core', 'remote_defaultExpDays', '3', '1'],
  979. ]);
  980. }
  981. $expected = new \DateTime('now', $this->timezone);
  982. $expected->setTime(0, 0, 0);
  983. $expected->add(new \DateInterval('P1D'));
  984. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  985. $this->assertNotNull($share->getExpirationDate());
  986. $this->assertEquals($expected, $share->getExpirationDate());
  987. }
  988. /**
  989. * @dataProvider validateExpirationDateInternalProvider
  990. */
  991. public function testValidateExpirationDateInternalEnforceTooFarIntoFuture($shareType): void {
  992. $this->expectException(\OCP\Share\Exceptions\GenericShareException::class);
  993. $this->expectExceptionMessage('Cannot set expiration date more than 3 days in the future');
  994. $future = new \DateTime();
  995. $future->add(new \DateInterval('P7D'));
  996. $share = $this->manager->newShare();
  997. $share->setShareType($shareType);
  998. $share->setExpirationDate($future);
  999. if ($shareType === IShare::TYPE_USER) {
  1000. $this->config->method('getAppValue')
  1001. ->willReturnMap([
  1002. ['core', 'shareapi_enforce_internal_expire_date', 'no', 'yes'],
  1003. ['core', 'shareapi_internal_expire_after_n_days', '7', '3'],
  1004. ['core', 'shareapi_default_internal_expire_date', 'no', 'yes'],
  1005. ]);
  1006. } else {
  1007. $this->config->method('getAppValue')
  1008. ->willReturnMap([
  1009. ['core', 'shareapi_enforce_remote_expire_date', 'no', 'yes'],
  1010. ['core', 'shareapi_remote_expire_after_n_days', '7', '3'],
  1011. ['core', 'shareapi_default_remote_expire_date', 'no', 'yes'],
  1012. ]);
  1013. }
  1014. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  1015. }
  1016. /**
  1017. * @dataProvider validateExpirationDateInternalProvider
  1018. */
  1019. public function testValidateExpirationDateInternalEnforceValid($shareType): void {
  1020. $future = new \DateTime('now', $this->dateTimeZone->getTimeZone());
  1021. $future->add(new \DateInterval('P2D'));
  1022. $future->setTime(1, 2, 3);
  1023. $expected = clone $future;
  1024. $expected->setTime(0, 0, 0);
  1025. $share = $this->manager->newShare();
  1026. $share->setShareType($shareType);
  1027. $share->setExpirationDate($future);
  1028. if ($shareType === IShare::TYPE_USER) {
  1029. $this->config->method('getAppValue')
  1030. ->willReturnMap([
  1031. ['core', 'shareapi_enforce_internal_expire_date', 'no', 'yes'],
  1032. ['core', 'shareapi_internal_expire_after_n_days', '7', '3'],
  1033. ['core', 'shareapi_default_internal_expire_date', 'no', 'yes'],
  1034. ]);
  1035. } else {
  1036. $this->config->method('getAppValue')
  1037. ->willReturnMap([
  1038. ['core', 'shareapi_enforce_remote_expire_date', 'no', 'yes'],
  1039. ['core', 'shareapi_remote_expire_after_n_days', '7', '3'],
  1040. ['core', 'shareapi_default_remote_expire_date', 'no', 'yes'],
  1041. ]);
  1042. }
  1043. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  1044. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  1045. $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($future) {
  1046. return $data['expirationDate'] == $future;
  1047. }));
  1048. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  1049. $this->assertEquals($expected, $share->getExpirationDate());
  1050. }
  1051. /**
  1052. * @dataProvider validateExpirationDateInternalProvider
  1053. */
  1054. public function testValidateExpirationDateInternalNoDefault($shareType): void {
  1055. $date = new \DateTime('now', $this->dateTimeZone->getTimeZone());
  1056. $date->add(new \DateInterval('P5D'));
  1057. $date->setTime(1, 2, 3);
  1058. $expected = clone $date;
  1059. $expected->setTime(0, 0, 0);
  1060. $share = $this->manager->newShare();
  1061. $share->setShareType($shareType);
  1062. $share->setExpirationDate($date);
  1063. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  1064. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  1065. $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
  1066. return $data['expirationDate'] == $expected && $data['passwordSet'] === false;
  1067. }));
  1068. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  1069. $this->assertEquals($expected, $share->getExpirationDate());
  1070. }
  1071. /**
  1072. * @dataProvider validateExpirationDateInternalProvider
  1073. */
  1074. public function testValidateExpirationDateInternalNoDateNoDefault($shareType): void {
  1075. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  1076. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  1077. $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) {
  1078. return $data['expirationDate'] === null && $data['passwordSet'] === true;
  1079. }));
  1080. $share = $this->manager->newShare();
  1081. $share->setShareType($shareType);
  1082. $share->setPassword('password');
  1083. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  1084. $this->assertNull($share->getExpirationDate());
  1085. }
  1086. /**
  1087. * @dataProvider validateExpirationDateInternalProvider
  1088. */
  1089. public function testValidateExpirationDateInternalNoDateDefault($shareType): void {
  1090. $share = $this->manager->newShare();
  1091. $share->setShareType($shareType);
  1092. $expected = new \DateTime('now', $this->timezone);
  1093. $expected->setTime(0, 0);
  1094. $expected->add(new \DateInterval('P3D'));
  1095. $expected->setTimezone(new \DateTimeZone(date_default_timezone_get()));
  1096. if ($shareType === IShare::TYPE_USER) {
  1097. $this->config->method('getAppValue')
  1098. ->willReturnMap([
  1099. ['core', 'shareapi_default_internal_expire_date', 'no', 'yes'],
  1100. ['core', 'shareapi_internal_expire_after_n_days', '7', '3'],
  1101. ['core', 'internal_defaultExpDays', '3', '3'],
  1102. ]);
  1103. } else {
  1104. $this->config->method('getAppValue')
  1105. ->willReturnMap([
  1106. ['core', 'shareapi_default_remote_expire_date', 'no', 'yes'],
  1107. ['core', 'shareapi_remote_expire_after_n_days', '7', '3'],
  1108. ['core', 'remote_defaultExpDays', '3', '3'],
  1109. ]);
  1110. }
  1111. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  1112. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  1113. $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
  1114. return $data['expirationDate'] == $expected;
  1115. }));
  1116. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  1117. $this->assertEquals($expected, $share->getExpirationDate());
  1118. }
  1119. /**
  1120. * @dataProvider validateExpirationDateInternalProvider
  1121. */
  1122. public function testValidateExpirationDateInternalDefault($shareType): void {
  1123. $future = new \DateTime('now', $this->timezone);
  1124. $future->add(new \DateInterval('P5D'));
  1125. $future->setTime(1, 2, 3);
  1126. $expected = clone $future;
  1127. $expected->setTime(0, 0);
  1128. $share = $this->manager->newShare();
  1129. $share->setShareType($shareType);
  1130. $share->setExpirationDate($future);
  1131. if ($shareType === IShare::TYPE_USER) {
  1132. $this->config->method('getAppValue')
  1133. ->willReturnMap([
  1134. ['core', 'shareapi_default_internal_expire_date', 'no', 'yes'],
  1135. ['core', 'shareapi_internal_expire_after_n_days', '7', '3'],
  1136. ['core', 'internal_defaultExpDays', '3', '1'],
  1137. ]);
  1138. } else {
  1139. $this->config->method('getAppValue')
  1140. ->willReturnMap([
  1141. ['core', 'shareapi_default_remote_expire_date', 'no', 'yes'],
  1142. ['core', 'shareapi_remote_expire_after_n_days', '7', '3'],
  1143. ['core', 'remote_defaultExpDays', '3', '1'],
  1144. ]);
  1145. }
  1146. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  1147. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  1148. $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
  1149. return $data['expirationDate'] == $expected;
  1150. }));
  1151. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  1152. $this->assertEquals($expected, $share->getExpirationDate());
  1153. }
  1154. /**
  1155. * @dataProvider validateExpirationDateInternalProvider
  1156. */
  1157. public function testValidateExpirationDateInternalHookModification($shareType): void {
  1158. $nextWeek = new \DateTime('now', $this->timezone);
  1159. $nextWeek->add(new \DateInterval('P7D'));
  1160. $nextWeek->setTime(0, 0, 0);
  1161. $save = clone $nextWeek;
  1162. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  1163. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  1164. $hookListener->expects($this->once())->method('listener')->willReturnCallback(function ($data) {
  1165. $data['expirationDate']->sub(new \DateInterval('P2D'));
  1166. });
  1167. $share = $this->manager->newShare();
  1168. $share->setShareType($shareType);
  1169. $share->setExpirationDate($nextWeek);
  1170. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  1171. $save->sub(new \DateInterval('P2D'));
  1172. $this->assertEquals($save, $share->getExpirationDate());
  1173. }
  1174. /**
  1175. * @dataProvider validateExpirationDateInternalProvider
  1176. */
  1177. public function testValidateExpirationDateInternalHookException($shareType): void {
  1178. $this->expectException(\Exception::class);
  1179. $this->expectExceptionMessage('Invalid date!');
  1180. $nextWeek = new \DateTime();
  1181. $nextWeek->add(new \DateInterval('P7D'));
  1182. $nextWeek->setTime(0, 0, 0);
  1183. $share = $this->manager->newShare();
  1184. $share->setShareType($shareType);
  1185. $share->setExpirationDate($nextWeek);
  1186. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  1187. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  1188. $hookListener->expects($this->once())->method('listener')->willReturnCallback(function ($data) {
  1189. $data['accepted'] = false;
  1190. $data['message'] = 'Invalid date!';
  1191. });
  1192. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  1193. }
  1194. /**
  1195. * @dataProvider validateExpirationDateInternalProvider
  1196. */
  1197. public function testValidateExpirationDateInternalExistingShareNoDefault($shareType): void {
  1198. $share = $this->manager->newShare();
  1199. $share->setShareType($shareType);
  1200. $share->setId('42')->setProviderId('foo');
  1201. if ($shareType === IShare::TYPE_USER) {
  1202. $this->config->method('getAppValue')
  1203. ->willReturnMap([
  1204. ['core', 'shareapi_default_internal_expire_date', 'no', 'yes'],
  1205. ['core', 'shareapi_internal_expire_after_n_days', '7', '6'],
  1206. ]);
  1207. } else {
  1208. $this->config->method('getAppValue')
  1209. ->willReturnMap([
  1210. ['core', 'shareapi_default_remote_expire_date', 'no', 'yes'],
  1211. ['core', 'shareapi_remote_expire_after_n_days', '7', '6'],
  1212. ]);
  1213. }
  1214. self::invokePrivate($this->manager, 'validateExpirationDateInternal', [$share]);
  1215. $this->assertEquals(null, $share->getExpirationDate());
  1216. }
  1217. public function testValidateExpirationDateInPast(): void {
  1218. $this->expectException(\OCP\Share\Exceptions\GenericShareException::class);
  1219. $this->expectExceptionMessage('Expiration date is in the past');
  1220. // Expire date in the past
  1221. $past = new \DateTime();
  1222. $past->sub(new \DateInterval('P1D'));
  1223. $share = $this->manager->newShare();
  1224. $share->setExpirationDate($past);
  1225. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1226. }
  1227. public function testValidateExpirationDateEnforceButNotSet(): void {
  1228. $this->expectException(\InvalidArgumentException::class);
  1229. $this->expectExceptionMessage('Expiration date is enforced');
  1230. $share = $this->manager->newShare();
  1231. $share->setProviderId('foo')->setId('bar');
  1232. $this->config->method('getAppValue')
  1233. ->willReturnMap([
  1234. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  1235. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  1236. ]);
  1237. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1238. }
  1239. public function testValidateExpirationDateEnforceButNotEnabledAndNotSet(): void {
  1240. $share = $this->manager->newShare();
  1241. $share->setProviderId('foo')->setId('bar');
  1242. $this->config->method('getAppValue')
  1243. ->willReturnMap([
  1244. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  1245. ]);
  1246. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1247. $this->assertNull($share->getExpirationDate());
  1248. }
  1249. public function testValidateExpirationDateEnforceButNotSetNewShare(): void {
  1250. $share = $this->manager->newShare();
  1251. $this->config->method('getAppValue')
  1252. ->willReturnMap([
  1253. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  1254. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  1255. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  1256. ['core', 'link_defaultExpDays', '3', '3'],
  1257. ]);
  1258. $expected = new \DateTime('now', $this->timezone);
  1259. $expected->setTime(0, 0, 0);
  1260. $expected->add(new \DateInterval('P3D'));
  1261. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1262. $this->assertNotNull($share->getExpirationDate());
  1263. $this->assertEquals($expected, $share->getExpirationDate());
  1264. }
  1265. public function testValidateExpirationDateEnforceRelaxedDefaultButNotSetNewShare(): void {
  1266. $share = $this->manager->newShare();
  1267. $this->config->method('getAppValue')
  1268. ->willReturnMap([
  1269. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  1270. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  1271. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  1272. ['core', 'link_defaultExpDays', '3', '1'],
  1273. ]);
  1274. $expected = new \DateTime('now', $this->timezone);
  1275. $expected->setTime(0, 0, 0);
  1276. $expected->add(new \DateInterval('P1D'));
  1277. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1278. $this->assertNotNull($share->getExpirationDate());
  1279. $this->assertEquals($expected, $share->getExpirationDate());
  1280. }
  1281. public function testValidateExpirationDateEnforceTooFarIntoFuture(): void {
  1282. $this->expectException(\OCP\Share\Exceptions\GenericShareException::class);
  1283. $this->expectExceptionMessage('Cannot set expiration date more than 3 days in the future');
  1284. $future = new \DateTime();
  1285. $future->add(new \DateInterval('P7D'));
  1286. $share = $this->manager->newShare();
  1287. $share->setExpirationDate($future);
  1288. $this->config->method('getAppValue')
  1289. ->willReturnMap([
  1290. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  1291. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  1292. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  1293. ]);
  1294. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1295. }
  1296. public function testValidateExpirationDateEnforceValid(): void {
  1297. $future = new \DateTime('now', $this->timezone);
  1298. $future->add(new \DateInterval('P2D'));
  1299. $future->setTime(1, 2, 3);
  1300. $expected = clone $future;
  1301. $expected->setTime(0, 0, 0);
  1302. $share = $this->manager->newShare();
  1303. $share->setExpirationDate($future);
  1304. $this->config->method('getAppValue')
  1305. ->willReturnMap([
  1306. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  1307. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  1308. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  1309. ]);
  1310. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  1311. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  1312. $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($future) {
  1313. return $data['expirationDate'] == $future;
  1314. }));
  1315. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1316. $this->assertEquals($expected, $share->getExpirationDate());
  1317. }
  1318. public function testValidateExpirationDateNoDefault(): void {
  1319. $date = new \DateTime('now', $this->timezone);
  1320. $date->add(new \DateInterval('P5D'));
  1321. $date->setTime(1, 2, 3);
  1322. $expected = clone $date;
  1323. $expected->setTime(0, 0);
  1324. $expected->setTimezone(new \DateTimeZone(date_default_timezone_get()));
  1325. $share = $this->manager->newShare();
  1326. $share->setExpirationDate($date);
  1327. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  1328. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  1329. $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
  1330. return $data['expirationDate'] == $expected && $data['passwordSet'] === false;
  1331. }));
  1332. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1333. $this->assertEquals($expected, $share->getExpirationDate());
  1334. }
  1335. public function testValidateExpirationDateNoDateNoDefault(): void {
  1336. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  1337. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  1338. $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) {
  1339. return $data['expirationDate'] === null && $data['passwordSet'] === true;
  1340. }));
  1341. $share = $this->manager->newShare();
  1342. $share->setPassword('password');
  1343. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1344. $this->assertNull($share->getExpirationDate());
  1345. }
  1346. public function testValidateExpirationDateNoDateDefault(): void {
  1347. $share = $this->manager->newShare();
  1348. $expected = new \DateTime('now', $this->timezone);
  1349. $expected->add(new \DateInterval('P3D'));
  1350. $expected->setTime(0, 0);
  1351. $expected->setTimezone(new \DateTimeZone(date_default_timezone_get()));
  1352. $this->config->method('getAppValue')
  1353. ->willReturnMap([
  1354. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  1355. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  1356. ['core', 'link_defaultExpDays', '3', '3'],
  1357. ]);
  1358. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  1359. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  1360. $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
  1361. return $data['expirationDate'] == $expected;
  1362. }));
  1363. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1364. $this->assertEquals($expected, $share->getExpirationDate());
  1365. }
  1366. public function testValidateExpirationDateDefault(): void {
  1367. $future = new \DateTime('now', $this->timezone);
  1368. $future->add(new \DateInterval('P5D'));
  1369. $future->setTime(1, 2, 3);
  1370. $expected = clone $future;
  1371. $expected->setTime(0, 0);
  1372. $expected->setTimezone(new \DateTimeZone(date_default_timezone_get()));
  1373. $share = $this->manager->newShare();
  1374. $share->setExpirationDate($future);
  1375. $this->config->method('getAppValue')
  1376. ->willReturnMap([
  1377. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  1378. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  1379. ['core', 'link_defaultExpDays', '3', '1'],
  1380. ]);
  1381. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  1382. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  1383. $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
  1384. return $data['expirationDate'] == $expected;
  1385. }));
  1386. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1387. $this->assertEquals($expected, $share->getExpirationDate());
  1388. }
  1389. public function testValidateExpirationNegativeOffsetTimezone(): void {
  1390. $this->timezone = new \DateTimeZone('Pacific/Tahiti');
  1391. $future = new \DateTime();
  1392. $future->add(new \DateInterval('P5D'));
  1393. $expected = clone $future;
  1394. $expected->setTimezone($this->timezone);
  1395. $expected->setTime(0, 0);
  1396. $expected->setTimezone(new \DateTimeZone(date_default_timezone_get()));
  1397. $share = $this->manager->newShare();
  1398. $share->setExpirationDate($future);
  1399. $this->config->method('getAppValue')
  1400. ->willReturnMap([
  1401. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  1402. ['core', 'shareapi_expire_after_n_days', '7', '3'],
  1403. ['core', 'link_defaultExpDays', '3', '1'],
  1404. ]);
  1405. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  1406. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  1407. $hookListener->expects($this->once())->method('listener')->with($this->callback(function ($data) use ($expected) {
  1408. return $data['expirationDate'] == $expected;
  1409. }));
  1410. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1411. $this->assertEquals($expected, $share->getExpirationDate());
  1412. }
  1413. public function testValidateExpirationDateHookModification(): void {
  1414. $nextWeek = new \DateTime('now', $this->timezone);
  1415. $nextWeek->add(new \DateInterval('P7D'));
  1416. $save = clone $nextWeek;
  1417. $save->setTime(0, 0);
  1418. $save->sub(new \DateInterval('P2D'));
  1419. $save->setTimezone(new \DateTimeZone(date_default_timezone_get()));
  1420. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  1421. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  1422. $hookListener->expects($this->once())->method('listener')->willReturnCallback(function ($data) {
  1423. $data['expirationDate']->sub(new \DateInterval('P2D'));
  1424. });
  1425. $share = $this->manager->newShare();
  1426. $share->setExpirationDate($nextWeek);
  1427. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1428. $this->assertEquals($save, $share->getExpirationDate());
  1429. }
  1430. public function testValidateExpirationDateHookException(): void {
  1431. $this->expectException(\Exception::class);
  1432. $this->expectExceptionMessage('Invalid date!');
  1433. $nextWeek = new \DateTime();
  1434. $nextWeek->add(new \DateInterval('P7D'));
  1435. $nextWeek->setTime(0, 0, 0);
  1436. $share = $this->manager->newShare();
  1437. $share->setExpirationDate($nextWeek);
  1438. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['listener'])->getMock();
  1439. \OCP\Util::connectHook('\OC\Share', 'verifyExpirationDate', $hookListener, 'listener');
  1440. $hookListener->expects($this->once())->method('listener')->willReturnCallback(function ($data) {
  1441. $data['accepted'] = false;
  1442. $data['message'] = 'Invalid date!';
  1443. });
  1444. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1445. }
  1446. public function testValidateExpirationDateExistingShareNoDefault(): void {
  1447. $share = $this->manager->newShare();
  1448. $share->setId('42')->setProviderId('foo');
  1449. $this->config->method('getAppValue')
  1450. ->willReturnMap([
  1451. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  1452. ['core', 'shareapi_expire_after_n_days', '7', '6'],
  1453. ]);
  1454. self::invokePrivate($this->manager, 'validateExpirationDateLink', [$share]);
  1455. $this->assertEquals(null, $share->getExpirationDate());
  1456. }
  1457. public function testUserCreateChecksShareWithGroupMembersOnlyDifferentGroups(): void {
  1458. $this->expectException(\Exception::class);
  1459. $this->expectExceptionMessage('Sharing is only allowed with group members');
  1460. $share = $this->manager->newShare();
  1461. $sharedBy = $this->createMock(IUser::class);
  1462. $sharedWith = $this->createMock(IUser::class);
  1463. $share->setSharedBy('sharedBy')->setSharedWith('sharedWith');
  1464. $this->groupManager
  1465. ->method('getUserGroupIds')
  1466. ->willReturnMap(
  1467. [
  1468. [$sharedBy, ['group1']],
  1469. [$sharedWith, ['group2']],
  1470. ]
  1471. );
  1472. $this->userManager->method('get')->willReturnMap([
  1473. ['sharedBy', $sharedBy],
  1474. ['sharedWith', $sharedWith],
  1475. ]);
  1476. $this->config
  1477. ->method('getAppValue')
  1478. ->willReturnMap([
  1479. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  1480. ['core', 'shareapi_only_share_with_group_members_exclude_group_list', '', '[]'],
  1481. ]);
  1482. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  1483. }
  1484. public function testUserCreateChecksShareWithGroupMembersOnlySharedGroup(): void {
  1485. $share = $this->manager->newShare();
  1486. $sharedBy = $this->createMock(IUser::class);
  1487. $sharedWith = $this->createMock(IUser::class);
  1488. $share->setSharedBy('sharedBy')->setSharedWith('sharedWith');
  1489. $path = $this->createMock(Node::class);
  1490. $share->setNode($path);
  1491. $this->groupManager
  1492. ->method('getUserGroupIds')
  1493. ->willReturnMap(
  1494. [
  1495. [$sharedBy, ['group1', 'group3']],
  1496. [$sharedWith, ['group2', 'group3']],
  1497. ]
  1498. );
  1499. $this->userManager->method('get')->willReturnMap([
  1500. ['sharedBy', $sharedBy],
  1501. ['sharedWith', $sharedWith],
  1502. ]);
  1503. $this->config
  1504. ->method('getAppValue')
  1505. ->willReturnMap([
  1506. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  1507. ['core', 'shareapi_only_share_with_group_members_exclude_group_list', '', '[]'],
  1508. ]);
  1509. $this->defaultProvider
  1510. ->method('getSharesByPath')
  1511. ->with($path)
  1512. ->willReturn([]);
  1513. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  1514. $this->addToAssertionCount(1);
  1515. }
  1516. public function testUserCreateChecksIdenticalShareExists(): void {
  1517. $this->expectException(AlreadySharedException::class);
  1518. $this->expectExceptionMessage('Sharing name.txt failed, because this item is already shared with the account user');
  1519. $share = $this->manager->newShare();
  1520. $share->setSharedWithDisplayName('user');
  1521. $share2 = $this->manager->newShare();
  1522. $sharedWith = $this->createMock(IUser::class);
  1523. $path = $this->createMock(Node::class);
  1524. $share->setSharedWith('sharedWith')->setNode($path)
  1525. ->setProviderId('foo')->setId('bar');
  1526. $share2->setSharedWith('sharedWith')->setNode($path)
  1527. ->setProviderId('foo')->setId('baz');
  1528. $this->defaultProvider
  1529. ->method('getSharesByPath')
  1530. ->with($path)
  1531. ->willReturn([$share2]);
  1532. $path->method('getName')
  1533. ->willReturn('name.txt');
  1534. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  1535. }
  1536. public function testUserCreateChecksIdenticalPathSharedViaGroup(): void {
  1537. $this->expectException(AlreadySharedException::class);
  1538. $this->expectExceptionMessage('Sharing name2.txt failed, because this item is already shared with the account userName');
  1539. $share = $this->manager->newShare();
  1540. $sharedWith = $this->createMock(IUser::class);
  1541. $sharedWith->method('getUID')->willReturn('sharedWith');
  1542. $this->userManager->method('get')->with('sharedWith')->willReturn($sharedWith);
  1543. $path = $this->createMock(Node::class);
  1544. $share->setSharedWith('sharedWith')
  1545. ->setNode($path)
  1546. ->setShareOwner('shareOwner')
  1547. ->setSharedWithDisplayName('userName')
  1548. ->setProviderId('foo')
  1549. ->setId('bar');
  1550. $share2 = $this->manager->newShare();
  1551. $share2->setShareType(IShare::TYPE_GROUP)
  1552. ->setShareOwner('shareOwner2')
  1553. ->setProviderId('foo')
  1554. ->setId('baz')
  1555. ->setSharedWith('group');
  1556. $group = $this->createMock(IGroup::class);
  1557. $group->method('inGroup')
  1558. ->with($sharedWith)
  1559. ->willReturn(true);
  1560. $this->groupManager->method('get')->with('group')->willReturn($group);
  1561. $this->defaultProvider
  1562. ->method('getSharesByPath')
  1563. ->with($path)
  1564. ->willReturn([$share2]);
  1565. $path->method('getName')
  1566. ->willReturn('name2.txt');
  1567. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  1568. }
  1569. public function testUserCreateChecksIdenticalPathSharedViaDeletedGroup(): void {
  1570. $share = $this->manager->newShare();
  1571. $sharedWith = $this->createMock(IUser::class);
  1572. $sharedWith->method('getUID')->willReturn('sharedWith');
  1573. $this->userManager->method('get')->with('sharedWith')->willReturn($sharedWith);
  1574. $path = $this->createMock(Node::class);
  1575. $share->setSharedWith('sharedWith')
  1576. ->setNode($path)
  1577. ->setShareOwner('shareOwner')
  1578. ->setProviderId('foo')
  1579. ->setId('bar');
  1580. $share2 = $this->manager->newShare();
  1581. $share2->setShareType(IShare::TYPE_GROUP)
  1582. ->setShareOwner('shareOwner2')
  1583. ->setProviderId('foo')
  1584. ->setId('baz')
  1585. ->setSharedWith('group');
  1586. $this->groupManager->method('get')->with('group')->willReturn(null);
  1587. $this->defaultProvider
  1588. ->method('getSharesByPath')
  1589. ->with($path)
  1590. ->willReturn([$share2]);
  1591. $this->assertNull($this->invokePrivate($this->manager, 'userCreateChecks', [$share]));
  1592. }
  1593. public function testUserCreateChecksIdenticalPathNotSharedWithUser(): void {
  1594. $share = $this->manager->newShare();
  1595. $sharedWith = $this->createMock(IUser::class);
  1596. $path = $this->createMock(Node::class);
  1597. $share->setSharedWith('sharedWith')
  1598. ->setNode($path)
  1599. ->setShareOwner('shareOwner')
  1600. ->setProviderId('foo')
  1601. ->setId('bar');
  1602. $this->userManager->method('get')->with('sharedWith')->willReturn($sharedWith);
  1603. $share2 = $this->manager->newShare();
  1604. $share2->setShareType(IShare::TYPE_GROUP)
  1605. ->setShareOwner('shareOwner2')
  1606. ->setProviderId('foo')
  1607. ->setId('baz');
  1608. $group = $this->createMock(IGroup::class);
  1609. $group->method('inGroup')
  1610. ->with($sharedWith)
  1611. ->willReturn(false);
  1612. $this->groupManager->method('get')->with('group')->willReturn($group);
  1613. $share2->setSharedWith('group');
  1614. $this->defaultProvider
  1615. ->method('getSharesByPath')
  1616. ->with($path)
  1617. ->willReturn([$share2]);
  1618. self::invokePrivate($this->manager, 'userCreateChecks', [$share]);
  1619. $this->addToAssertionCount(1);
  1620. }
  1621. public function testGroupCreateChecksShareWithGroupMembersGroupSharingNotAllowed(): void {
  1622. $this->expectException(\Exception::class);
  1623. $this->expectExceptionMessage('Group sharing is now allowed');
  1624. $share = $this->manager->newShare();
  1625. $this->config
  1626. ->method('getAppValue')
  1627. ->willReturnMap([
  1628. ['core', 'shareapi_allow_group_sharing', 'yes', 'no'],
  1629. ]);
  1630. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  1631. }
  1632. public function testGroupCreateChecksShareWithGroupMembersOnlyNotInGroup(): void {
  1633. $this->expectException(\Exception::class);
  1634. $this->expectExceptionMessage('Sharing is only allowed within your own groups');
  1635. $share = $this->manager->newShare();
  1636. $user = $this->createMock(IUser::class);
  1637. $group = $this->createMock(IGroup::class);
  1638. $share->setSharedBy('user')->setSharedWith('group');
  1639. $group->method('inGroup')->with($user)->willReturn(false);
  1640. $this->groupManager->method('get')->with('group')->willReturn($group);
  1641. $this->userManager->method('get')->with('user')->willReturn($user);
  1642. $this->config
  1643. ->method('getAppValue')
  1644. ->willReturnMap([
  1645. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  1646. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  1647. ['core', 'shareapi_only_share_with_group_members_exclude_group_list', '', '[]'],
  1648. ]);
  1649. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  1650. }
  1651. public function testGroupCreateChecksShareWithGroupMembersOnlyNullGroup(): void {
  1652. $this->expectException(\Exception::class);
  1653. $this->expectExceptionMessage('Sharing is only allowed within your own groups');
  1654. $share = $this->manager->newShare();
  1655. $user = $this->createMock(IUser::class);
  1656. $share->setSharedBy('user')->setSharedWith('group');
  1657. $this->groupManager->method('get')->with('group')->willReturn(null);
  1658. $this->userManager->method('get')->with('user')->willReturn($user);
  1659. $this->config
  1660. ->method('getAppValue')
  1661. ->willReturnMap([
  1662. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  1663. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  1664. ['core', 'shareapi_only_share_with_group_members_exclude_group_list', '', '[]'],
  1665. ]);
  1666. $this->assertNull($this->invokePrivate($this->manager, 'groupCreateChecks', [$share]));
  1667. }
  1668. public function testGroupCreateChecksShareWithGroupMembersOnlyInGroup(): void {
  1669. $share = $this->manager->newShare();
  1670. $user = $this->createMock(IUser::class);
  1671. $group = $this->createMock(IGroup::class);
  1672. $share->setSharedBy('user')->setSharedWith('group');
  1673. $this->userManager->method('get')->with('user')->willReturn($user);
  1674. $this->groupManager->method('get')->with('group')->willReturn($group);
  1675. $group->method('inGroup')->with($user)->willReturn(true);
  1676. $path = $this->createMock(Node::class);
  1677. $share->setNode($path);
  1678. $this->defaultProvider->method('getSharesByPath')
  1679. ->with($path)
  1680. ->willReturn([]);
  1681. $this->config
  1682. ->method('getAppValue')
  1683. ->willReturnMap([
  1684. ['core', 'shareapi_only_share_with_group_members', 'no', 'yes'],
  1685. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  1686. ['core', 'shareapi_only_share_with_group_members_exclude_group_list', '', '[]'],
  1687. ]);
  1688. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  1689. $this->addToAssertionCount(1);
  1690. }
  1691. public function testGroupCreateChecksPathAlreadySharedWithSameGroup(): void {
  1692. $this->expectException(\Exception::class);
  1693. $this->expectExceptionMessage('Path is already shared with this group');
  1694. $share = $this->manager->newShare();
  1695. $path = $this->createMock(Node::class);
  1696. $share->setSharedWith('sharedWith')
  1697. ->setNode($path)
  1698. ->setProviderId('foo')
  1699. ->setId('bar');
  1700. $share2 = $this->manager->newShare();
  1701. $share2->setSharedWith('sharedWith')
  1702. ->setProviderId('foo')
  1703. ->setId('baz');
  1704. $this->defaultProvider->method('getSharesByPath')
  1705. ->with($path)
  1706. ->willReturn([$share2]);
  1707. $this->config
  1708. ->method('getAppValue')
  1709. ->willReturnMap([
  1710. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  1711. ]);
  1712. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  1713. }
  1714. public function testGroupCreateChecksPathAlreadySharedWithDifferentGroup(): void {
  1715. $share = $this->manager->newShare();
  1716. $share->setSharedWith('sharedWith');
  1717. $path = $this->createMock(Node::class);
  1718. $share->setNode($path);
  1719. $share2 = $this->manager->newShare();
  1720. $share2->setSharedWith('sharedWith2');
  1721. $this->defaultProvider->method('getSharesByPath')
  1722. ->with($path)
  1723. ->willReturn([$share2]);
  1724. $this->config
  1725. ->method('getAppValue')
  1726. ->willReturnMap([
  1727. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  1728. ]);
  1729. self::invokePrivate($this->manager, 'groupCreateChecks', [$share]);
  1730. $this->addToAssertionCount(1);
  1731. }
  1732. public function testLinkCreateChecksNoLinkSharesAllowed(): void {
  1733. $this->expectException(\Exception::class);
  1734. $this->expectExceptionMessage('Link sharing is not allowed');
  1735. $share = $this->manager->newShare();
  1736. $this->config
  1737. ->method('getAppValue')
  1738. ->willReturnMap([
  1739. ['core', 'shareapi_allow_links', 'yes', 'no'],
  1740. ]);
  1741. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1742. }
  1743. public function testFileLinkCreateChecksNoPublicUpload(): void {
  1744. $share = $this->manager->newShare();
  1745. $share->setPermissions(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE);
  1746. $share->setNodeType('file');
  1747. $this->config
  1748. ->method('getAppValue')
  1749. ->willReturnMap([
  1750. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1751. ['core', 'shareapi_allow_public_upload', 'yes', 'no']
  1752. ]);
  1753. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1754. $this->addToAssertionCount(1);
  1755. }
  1756. public function testFolderLinkCreateChecksNoPublicUpload(): void {
  1757. $this->expectException(\Exception::class);
  1758. $this->expectExceptionMessage('Public upload is not allowed');
  1759. $share = $this->manager->newShare();
  1760. $share->setPermissions(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE);
  1761. $share->setNodeType('folder');
  1762. $this->config
  1763. ->method('getAppValue')
  1764. ->willReturnMap([
  1765. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1766. ['core', 'shareapi_allow_public_upload', 'yes', 'no']
  1767. ]);
  1768. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1769. }
  1770. public function testLinkCreateChecksPublicUpload(): void {
  1771. $share = $this->manager->newShare();
  1772. $share->setPermissions(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE);
  1773. $share->setSharedWith('sharedWith');
  1774. $folder = $this->createMock(\OC\Files\Node\Folder::class);
  1775. $share->setNode($folder);
  1776. $this->config
  1777. ->method('getAppValue')
  1778. ->willReturnMap([
  1779. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1780. ['core', 'shareapi_allow_public_upload', 'yes', 'yes']
  1781. ]);
  1782. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1783. $this->addToAssertionCount(1);
  1784. }
  1785. public function testLinkCreateChecksReadOnly(): void {
  1786. $share = $this->manager->newShare();
  1787. $share->setPermissions(\OCP\Constants::PERMISSION_READ);
  1788. $share->setSharedWith('sharedWith');
  1789. $folder = $this->createMock(\OC\Files\Node\Folder::class);
  1790. $share->setNode($folder);
  1791. $this->config
  1792. ->method('getAppValue')
  1793. ->willReturnMap([
  1794. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  1795. ['core', 'shareapi_allow_public_upload', 'yes', 'no']
  1796. ]);
  1797. self::invokePrivate($this->manager, 'linkCreateChecks', [$share]);
  1798. $this->addToAssertionCount(1);
  1799. }
  1800. public function testPathCreateChecksContainsSharedMount(): void {
  1801. $this->expectException(\InvalidArgumentException::class);
  1802. $this->expectExceptionMessage('Path contains files shared with you');
  1803. $path = $this->createMock(Folder::class);
  1804. $path->method('getPath')->willReturn('path');
  1805. $mount = $this->createMock(IMountPoint::class);
  1806. $storage = $this->createMock(IStorage::class);
  1807. $mount->method('getStorage')->willReturn($storage);
  1808. $storage->method('instanceOfStorage')->with('\OCA\Files_Sharing\ISharedStorage')->willReturn(true);
  1809. $this->mountManager->method('findIn')->with('path')->willReturn([$mount]);
  1810. self::invokePrivate($this->manager, 'pathCreateChecks', [$path]);
  1811. }
  1812. public function testPathCreateChecksContainsNoSharedMount(): void {
  1813. $path = $this->createMock(Folder::class);
  1814. $path->method('getPath')->willReturn('path');
  1815. $mount = $this->createMock(IMountPoint::class);
  1816. $storage = $this->createMock(IStorage::class);
  1817. $mount->method('getStorage')->willReturn($storage);
  1818. $storage->method('instanceOfStorage')->with('\OCA\Files_Sharing\ISharedStorage')->willReturn(false);
  1819. $this->mountManager->method('findIn')->with('path')->willReturn([$mount]);
  1820. self::invokePrivate($this->manager, 'pathCreateChecks', [$path]);
  1821. $this->addToAssertionCount(1);
  1822. }
  1823. public function testPathCreateChecksContainsNoFolder(): void {
  1824. $path = $this->createMock(File::class);
  1825. self::invokePrivate($this->manager, 'pathCreateChecks', [$path]);
  1826. $this->addToAssertionCount(1);
  1827. }
  1828. public function dataIsSharingDisabledForUser() {
  1829. $data = [];
  1830. // No exclude groups
  1831. $data[] = ['no', null, null, [], false];
  1832. // empty exclude / allow list, user no groups
  1833. $data[] = ['yes', '', json_encode(['']), [], false];
  1834. $data[] = ['allow', '', json_encode(['']), [], true];
  1835. // empty exclude / allow list, user groups
  1836. $data[] = ['yes', '', json_encode(['']), ['group1', 'group2'], false];
  1837. $data[] = ['allow', '', json_encode(['']), ['group1', 'group2'], true];
  1838. // Convert old list to json
  1839. $data[] = ['yes', 'group1,group2', json_encode(['group1', 'group2']), [], false];
  1840. $data[] = ['allow', 'group1,group2', json_encode(['group1', 'group2']), [], true];
  1841. // Old list partly groups in common
  1842. $data[] = ['yes', 'group1,group2', json_encode(['group1', 'group2']), ['group1', 'group3'], false];
  1843. $data[] = ['allow', 'group1,group2', json_encode(['group1', 'group2']), ['group1', 'group3'], false];
  1844. // Old list only groups in common
  1845. $data[] = ['yes', 'group1,group2', json_encode(['group1', 'group2']), ['group1'], true];
  1846. $data[] = ['allow', 'group1,group2', json_encode(['group1', 'group2']), ['group1'], false];
  1847. // New list partly in common
  1848. $data[] = ['yes', json_encode(['group1', 'group2']), null, ['group1', 'group3'], false];
  1849. $data[] = ['allow', json_encode(['group1', 'group2']), null, ['group1', 'group3'], false];
  1850. // New list only groups in common
  1851. $data[] = ['yes', json_encode(['group1', 'group2']), null, ['group2'], true];
  1852. $data[] = ['allow', json_encode(['group1', 'group2']), null, ['group2'], false];
  1853. return $data;
  1854. }
  1855. /**
  1856. * @dataProvider dataIsSharingDisabledForUser
  1857. *
  1858. * @param string $excludeGroups
  1859. * @param string $groupList
  1860. * @param string $setList
  1861. * @param string[] $groupIds
  1862. * @param bool $expected
  1863. */
  1864. public function testIsSharingDisabledForUser($excludeGroups, $groupList, $setList, $groupIds, $expected): void {
  1865. $user = $this->createMock(IUser::class);
  1866. $this->config->method('getAppValue')
  1867. ->willReturnMap([
  1868. ['core', 'shareapi_exclude_groups', 'no', $excludeGroups],
  1869. ['core', 'shareapi_exclude_groups_list', '', $groupList],
  1870. ]);
  1871. if ($setList !== null) {
  1872. $this->config->expects($this->once())
  1873. ->method('setAppValue')
  1874. ->with('core', 'shareapi_exclude_groups_list', $setList);
  1875. } else {
  1876. $this->config->expects($this->never())
  1877. ->method('setAppValue');
  1878. }
  1879. $this->groupManager->method('getUserGroupIds')
  1880. ->with($user)
  1881. ->willReturn($groupIds);
  1882. $this->userManager->method('get')->with('user')->willReturn($user);
  1883. $res = $this->manager->sharingDisabledForUser('user');
  1884. $this->assertEquals($expected, $res);
  1885. }
  1886. public function dataCanShare() {
  1887. $data = [];
  1888. /*
  1889. * [expected, sharing enabled, disabled for user]
  1890. */
  1891. $data[] = [false, 'no', false];
  1892. $data[] = [false, 'no', true];
  1893. $data[] = [true, 'yes', false];
  1894. $data[] = [false, 'yes', true];
  1895. return $data;
  1896. }
  1897. /**
  1898. * @dataProvider dataCanShare
  1899. *
  1900. * @param bool $expected
  1901. * @param string $sharingEnabled
  1902. * @param bool $disabledForUser
  1903. */
  1904. public function testCanShare($expected, $sharingEnabled, $disabledForUser): void {
  1905. $this->config->method('getAppValue')
  1906. ->willReturnMap([
  1907. ['core', 'shareapi_enabled', 'yes', $sharingEnabled],
  1908. ]);
  1909. $manager = $this->createManagerMock()
  1910. ->setMethods(['sharingDisabledForUser'])
  1911. ->getMock();
  1912. $manager->method('sharingDisabledForUser')
  1913. ->with('user')
  1914. ->willReturn($disabledForUser);
  1915. $share = $this->manager->newShare();
  1916. $share->setSharedBy('user');
  1917. $exception = false;
  1918. try {
  1919. $res = self::invokePrivate($manager, 'canShare', [$share]);
  1920. } catch (\Exception $e) {
  1921. $exception = true;
  1922. }
  1923. $this->assertEquals($expected, !$exception);
  1924. }
  1925. public function testCreateShareUser(): void {
  1926. $manager = $this->createManagerMock()
  1927. ->setMethods(['canShare', 'generalCreateChecks', 'userCreateChecks', 'pathCreateChecks'])
  1928. ->getMock();
  1929. $shareOwner = $this->createMock(IUser::class);
  1930. $shareOwner->method('getUID')->willReturn('shareOwner');
  1931. $storage = $this->createMock(IStorage::class);
  1932. $path = $this->createMock(File::class);
  1933. $path->method('getOwner')->willReturn($shareOwner);
  1934. $path->method('getName')->willReturn('target');
  1935. $path->method('getStorage')->willReturn($storage);
  1936. $share = $this->createShare(
  1937. null,
  1938. IShare::TYPE_USER,
  1939. $path,
  1940. 'sharedWith',
  1941. 'sharedBy',
  1942. null,
  1943. \OCP\Constants::PERMISSION_ALL);
  1944. $manager->expects($this->once())
  1945. ->method('canShare')
  1946. ->with($share)
  1947. ->willReturn(true);
  1948. $manager->expects($this->once())
  1949. ->method('generalCreateChecks')
  1950. ->with($share);
  1951. ;
  1952. $manager->expects($this->once())
  1953. ->method('userCreateChecks')
  1954. ->with($share);
  1955. ;
  1956. $manager->expects($this->once())
  1957. ->method('pathCreateChecks')
  1958. ->with($path);
  1959. $this->defaultProvider
  1960. ->expects($this->once())
  1961. ->method('create')
  1962. ->with($share)
  1963. ->willReturnArgument(0);
  1964. $share->expects($this->once())
  1965. ->method('setShareOwner')
  1966. ->with('shareOwner');
  1967. $share->expects($this->once())
  1968. ->method('setTarget')
  1969. ->with('/target');
  1970. $manager->createShare($share);
  1971. }
  1972. public function testCreateShareGroup(): void {
  1973. $manager = $this->createManagerMock()
  1974. ->setMethods(['canShare', 'generalCreateChecks', 'groupCreateChecks', 'pathCreateChecks'])
  1975. ->getMock();
  1976. $shareOwner = $this->createMock(IUser::class);
  1977. $shareOwner->method('getUID')->willReturn('shareOwner');
  1978. $storage = $this->createMock(IStorage::class);
  1979. $path = $this->createMock(File::class);
  1980. $path->method('getOwner')->willReturn($shareOwner);
  1981. $path->method('getName')->willReturn('target');
  1982. $path->method('getStorage')->willReturn($storage);
  1983. $share = $this->createShare(
  1984. null,
  1985. IShare::TYPE_GROUP,
  1986. $path,
  1987. 'sharedWith',
  1988. 'sharedBy',
  1989. null,
  1990. \OCP\Constants::PERMISSION_ALL);
  1991. $manager->expects($this->once())
  1992. ->method('canShare')
  1993. ->with($share)
  1994. ->willReturn(true);
  1995. $manager->expects($this->once())
  1996. ->method('generalCreateChecks')
  1997. ->with($share);
  1998. ;
  1999. $manager->expects($this->once())
  2000. ->method('groupCreateChecks')
  2001. ->with($share);
  2002. ;
  2003. $manager->expects($this->once())
  2004. ->method('pathCreateChecks')
  2005. ->with($path);
  2006. $this->defaultProvider
  2007. ->expects($this->once())
  2008. ->method('create')
  2009. ->with($share)
  2010. ->willReturnArgument(0);
  2011. $share->expects($this->once())
  2012. ->method('setShareOwner')
  2013. ->with('shareOwner');
  2014. $share->expects($this->once())
  2015. ->method('setTarget')
  2016. ->with('/target');
  2017. $manager->createShare($share);
  2018. }
  2019. public function testCreateShareLink(): void {
  2020. $manager = $this->createManagerMock()
  2021. ->setMethods([
  2022. 'canShare',
  2023. 'generalCreateChecks',
  2024. 'linkCreateChecks',
  2025. 'pathCreateChecks',
  2026. 'validateExpirationDateLink',
  2027. 'verifyPassword',
  2028. 'setLinkParent',
  2029. ])
  2030. ->getMock();
  2031. $shareOwner = $this->createMock(IUser::class);
  2032. $shareOwner->method('getUID')->willReturn('shareOwner');
  2033. $storage = $this->createMock(IStorage::class);
  2034. $path = $this->createMock(File::class);
  2035. $path->method('getOwner')->willReturn($shareOwner);
  2036. $path->method('getName')->willReturn('target');
  2037. $path->method('getId')->willReturn(1);
  2038. $path->method('getStorage')->willReturn($storage);
  2039. $date = new \DateTime();
  2040. $share = $this->manager->newShare();
  2041. $share->setShareType(IShare::TYPE_LINK)
  2042. ->setNode($path)
  2043. ->setSharedBy('sharedBy')
  2044. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  2045. ->setExpirationDate($date)
  2046. ->setPassword('password');
  2047. $manager->expects($this->once())
  2048. ->method('canShare')
  2049. ->with($share)
  2050. ->willReturn(true);
  2051. $manager->expects($this->once())
  2052. ->method('generalCreateChecks')
  2053. ->with($share);
  2054. ;
  2055. $manager->expects($this->once())
  2056. ->method('linkCreateChecks')
  2057. ->with($share);
  2058. ;
  2059. $manager->expects($this->once())
  2060. ->method('pathCreateChecks')
  2061. ->with($path);
  2062. $manager->expects($this->once())
  2063. ->method('validateExpirationDateLink')
  2064. ->with($share)
  2065. ->willReturn($share);
  2066. $manager->expects($this->once())
  2067. ->method('verifyPassword')
  2068. ->with('password');
  2069. $manager->expects($this->once())
  2070. ->method('setLinkParent')
  2071. ->with($share);
  2072. $this->hasher->expects($this->once())
  2073. ->method('hash')
  2074. ->with('password')
  2075. ->willReturn('hashed');
  2076. $this->secureRandom->method('generate')
  2077. ->willReturn('token');
  2078. $this->defaultProvider
  2079. ->expects($this->once())
  2080. ->method('create')
  2081. ->with($share)
  2082. ->willReturnCallback(function (Share $share) {
  2083. return $share->setId(42);
  2084. });
  2085. $this->dispatcher->expects($this->exactly(2))
  2086. ->method('dispatchTyped')
  2087. ->withConsecutive(
  2088. // Pre share
  2089. [
  2090. $this->callback(function (BeforeShareCreatedEvent $e) use ($path, $date) {
  2091. $share = $e->getShare();
  2092. return $share->getShareType() === IShare::TYPE_LINK &&
  2093. $share->getNode() === $path &&
  2094. $share->getSharedBy() === 'sharedBy' &&
  2095. $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  2096. $share->getExpirationDate() === $date &&
  2097. $share->getPassword() === 'hashed' &&
  2098. $share->getToken() === 'token';
  2099. })
  2100. ],
  2101. // Post share
  2102. [
  2103. $this->callback(function (ShareCreatedEvent $e) use ($path, $date) {
  2104. $share = $e->getShare();
  2105. return $share->getShareType() === IShare::TYPE_LINK &&
  2106. $share->getNode() === $path &&
  2107. $share->getSharedBy() === 'sharedBy' &&
  2108. $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  2109. $share->getExpirationDate() === $date &&
  2110. $share->getPassword() === 'hashed' &&
  2111. $share->getToken() === 'token' &&
  2112. $share->getId() === '42' &&
  2113. $share->getTarget() === '/target';
  2114. })
  2115. ]
  2116. );
  2117. /** @var IShare $share */
  2118. $share = $manager->createShare($share);
  2119. $this->assertSame('shareOwner', $share->getShareOwner());
  2120. $this->assertEquals('/target', $share->getTarget());
  2121. $this->assertSame($date, $share->getExpirationDate());
  2122. $this->assertEquals('token', $share->getToken());
  2123. $this->assertEquals('hashed', $share->getPassword());
  2124. }
  2125. public function testCreateShareMail(): void {
  2126. $manager = $this->createManagerMock()
  2127. ->setMethods([
  2128. 'canShare',
  2129. 'generalCreateChecks',
  2130. 'linkCreateChecks',
  2131. 'pathCreateChecks',
  2132. 'validateExpirationDateLink',
  2133. 'verifyPassword',
  2134. 'setLinkParent',
  2135. ])
  2136. ->getMock();
  2137. $shareOwner = $this->createMock(IUser::class);
  2138. $shareOwner->method('getUID')->willReturn('shareOwner');
  2139. $storage = $this->createMock(IStorage::class);
  2140. $path = $this->createMock(File::class);
  2141. $path->method('getOwner')->willReturn($shareOwner);
  2142. $path->method('getName')->willReturn('target');
  2143. $path->method('getId')->willReturn(1);
  2144. $path->method('getStorage')->willReturn($storage);
  2145. $share = $this->manager->newShare();
  2146. $share->setShareType(IShare::TYPE_EMAIL)
  2147. ->setNode($path)
  2148. ->setSharedBy('sharedBy')
  2149. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2150. $manager->expects($this->once())
  2151. ->method('canShare')
  2152. ->with($share)
  2153. ->willReturn(true);
  2154. $manager->expects($this->once())
  2155. ->method('generalCreateChecks')
  2156. ->with($share);
  2157. $manager->expects($this->once())
  2158. ->method('linkCreateChecks');
  2159. $manager->expects($this->once())
  2160. ->method('pathCreateChecks')
  2161. ->with($path);
  2162. $manager->expects($this->once())
  2163. ->method('validateExpirationDateLink')
  2164. ->with($share)
  2165. ->willReturn($share);
  2166. $manager->expects($this->once())
  2167. ->method('verifyPassword');
  2168. $manager->expects($this->once())
  2169. ->method('setLinkParent');
  2170. $this->secureRandom->method('generate')
  2171. ->willReturn('token');
  2172. $this->defaultProvider
  2173. ->expects($this->once())
  2174. ->method('create')
  2175. ->with($share)
  2176. ->willReturnCallback(function (Share $share) {
  2177. return $share->setId(42);
  2178. });
  2179. $this->dispatcher->expects($this->exactly(2))
  2180. ->method('dispatchTyped')
  2181. ->withConsecutive(
  2182. [
  2183. $this->callback(function (BeforeShareCreatedEvent $e) use ($path) {
  2184. $share = $e->getShare();
  2185. return $share->getShareType() === IShare::TYPE_EMAIL &&
  2186. $share->getNode() === $path &&
  2187. $share->getSharedBy() === 'sharedBy' &&
  2188. $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  2189. $share->getExpirationDate() === null &&
  2190. $share->getPassword() === null &&
  2191. $share->getToken() === 'token';
  2192. })
  2193. ],
  2194. [
  2195. $this->callback(function (ShareCreatedEvent $e) use ($path) {
  2196. $share = $e->getShare();
  2197. return $share->getShareType() === IShare::TYPE_EMAIL &&
  2198. $share->getNode() === $path &&
  2199. $share->getSharedBy() === 'sharedBy' &&
  2200. $share->getPermissions() === \OCP\Constants::PERMISSION_ALL &&
  2201. $share->getExpirationDate() === null &&
  2202. $share->getPassword() === null &&
  2203. $share->getToken() === 'token' &&
  2204. $share->getId() === '42' &&
  2205. $share->getTarget() === '/target';
  2206. })
  2207. ],
  2208. );
  2209. /** @var IShare $share */
  2210. $share = $manager->createShare($share);
  2211. $this->assertSame('shareOwner', $share->getShareOwner());
  2212. $this->assertEquals('/target', $share->getTarget());
  2213. $this->assertEquals('token', $share->getToken());
  2214. }
  2215. public function testCreateShareHookError(): void {
  2216. $this->expectException(\Exception::class);
  2217. $this->expectExceptionMessage('I won\'t let you share');
  2218. $manager = $this->createManagerMock()
  2219. ->setMethods([
  2220. 'canShare',
  2221. 'generalCreateChecks',
  2222. 'userCreateChecks',
  2223. 'pathCreateChecks',
  2224. ])
  2225. ->getMock();
  2226. $shareOwner = $this->createMock(IUser::class);
  2227. $shareOwner->method('getUID')->willReturn('shareOwner');
  2228. $storage = $this->createMock(IStorage::class);
  2229. $path = $this->createMock(File::class);
  2230. $path->method('getOwner')->willReturn($shareOwner);
  2231. $path->method('getName')->willReturn('target');
  2232. $path->method('getStorage')->willReturn($storage);
  2233. $share = $this->createShare(
  2234. null,
  2235. IShare::TYPE_USER,
  2236. $path,
  2237. 'sharedWith',
  2238. 'sharedBy',
  2239. null,
  2240. \OCP\Constants::PERMISSION_ALL);
  2241. $manager->expects($this->once())
  2242. ->method('canShare')
  2243. ->with($share)
  2244. ->willReturn(true);
  2245. $manager->expects($this->once())
  2246. ->method('generalCreateChecks')
  2247. ->with($share);
  2248. ;
  2249. $manager->expects($this->once())
  2250. ->method('userCreateChecks')
  2251. ->with($share);
  2252. ;
  2253. $manager->expects($this->once())
  2254. ->method('pathCreateChecks')
  2255. ->with($path);
  2256. $share->expects($this->once())
  2257. ->method('setShareOwner')
  2258. ->with('shareOwner');
  2259. $share->expects($this->once())
  2260. ->method('setTarget')
  2261. ->with('/target');
  2262. // Pre share
  2263. $this->dispatcher->expects($this->once())
  2264. ->method('dispatchTyped')
  2265. ->with(
  2266. $this->isInstanceOf(BeforeShareCreatedEvent::class)
  2267. )->willReturnCallback(function (BeforeShareCreatedEvent $e) {
  2268. $e->setError('I won\'t let you share!');
  2269. $e->stopPropagation();
  2270. }
  2271. );
  2272. $manager->createShare($share);
  2273. }
  2274. public function testCreateShareOfIncomingFederatedShare(): void {
  2275. $manager = $this->createManagerMock()
  2276. ->setMethods(['canShare', 'generalCreateChecks', 'userCreateChecks', 'pathCreateChecks'])
  2277. ->getMock();
  2278. $shareOwner = $this->createMock(IUser::class);
  2279. $shareOwner->method('getUID')->willReturn('shareOwner');
  2280. $storage = $this->createMock(IStorage::class);
  2281. $storage->method('instanceOfStorage')
  2282. ->with('OCA\Files_Sharing\External\Storage')
  2283. ->willReturn(true);
  2284. $storage2 = $this->createMock(IStorage::class);
  2285. $storage2->method('instanceOfStorage')
  2286. ->with('OCA\Files_Sharing\External\Storage')
  2287. ->willReturn(false);
  2288. $path = $this->createMock(File::class);
  2289. $path->expects($this->never())->method('getOwner');
  2290. $path->method('getName')->willReturn('target');
  2291. $path->method('getStorage')->willReturn($storage);
  2292. $parent = $this->createMock(Folder::class);
  2293. $parent->method('getStorage')->willReturn($storage);
  2294. $parentParent = $this->createMock(Folder::class);
  2295. $parentParent->method('getStorage')->willReturn($storage2);
  2296. $parentParent->method('getOwner')->willReturn($shareOwner);
  2297. $path->method('getParent')->willReturn($parent);
  2298. $parent->method('getParent')->willReturn($parentParent);
  2299. $share = $this->createShare(
  2300. null,
  2301. IShare::TYPE_USER,
  2302. $path,
  2303. 'sharedWith',
  2304. 'sharedBy',
  2305. null,
  2306. \OCP\Constants::PERMISSION_ALL);
  2307. $manager->expects($this->once())
  2308. ->method('canShare')
  2309. ->with($share)
  2310. ->willReturn(true);
  2311. $manager->expects($this->once())
  2312. ->method('generalCreateChecks')
  2313. ->with($share);
  2314. ;
  2315. $manager->expects($this->once())
  2316. ->method('userCreateChecks')
  2317. ->with($share);
  2318. ;
  2319. $manager->expects($this->once())
  2320. ->method('pathCreateChecks')
  2321. ->with($path);
  2322. $this->defaultProvider
  2323. ->expects($this->once())
  2324. ->method('create')
  2325. ->with($share)
  2326. ->willReturnArgument(0);
  2327. $share->expects($this->once())
  2328. ->method('setShareOwner')
  2329. ->with('shareOwner');
  2330. $share->expects($this->once())
  2331. ->method('setTarget')
  2332. ->with('/target');
  2333. $manager->createShare($share);
  2334. }
  2335. public function testGetSharesBy(): void {
  2336. $share = $this->manager->newShare();
  2337. $node = $this->createMock(Folder::class);
  2338. $this->defaultProvider->expects($this->once())
  2339. ->method('getSharesBy')
  2340. ->with(
  2341. $this->equalTo('user'),
  2342. $this->equalTo(IShare::TYPE_USER),
  2343. $this->equalTo($node),
  2344. $this->equalTo(true),
  2345. $this->equalTo(1),
  2346. $this->equalTo(1)
  2347. )->willReturn([$share]);
  2348. $shares = $this->manager->getSharesBy('user', IShare::TYPE_USER, $node, true, 1, 1);
  2349. $this->assertCount(1, $shares);
  2350. $this->assertSame($share, $shares[0]);
  2351. }
  2352. /**
  2353. * Test to ensure we correctly remove expired link shares
  2354. *
  2355. * We have 8 Shares and we want the 3 first valid shares.
  2356. * share 3-6 and 8 are expired. Thus at the end of this test we should
  2357. * have received share 1,2 and 7. And from the manager. Share 3-6 should be
  2358. * deleted (as they are evaluated). but share 8 should still be there.
  2359. */
  2360. public function testGetSharesByExpiredLinkShares(): void {
  2361. $manager = $this->createManagerMock()
  2362. ->setMethods(['deleteShare'])
  2363. ->getMock();
  2364. /** @var \OCP\Share\IShare[] $shares */
  2365. $shares = [];
  2366. /*
  2367. * This results in an array of 8 IShare elements
  2368. */
  2369. for ($i = 0; $i < 8; $i++) {
  2370. $share = $this->manager->newShare();
  2371. $share->setId($i);
  2372. $shares[] = $share;
  2373. }
  2374. $today = new \DateTime();
  2375. $today->setTime(0, 0, 0);
  2376. /*
  2377. * Set the expiration date to today for some shares
  2378. */
  2379. $shares[2]->setExpirationDate($today);
  2380. $shares[3]->setExpirationDate($today);
  2381. $shares[4]->setExpirationDate($today);
  2382. $shares[5]->setExpirationDate($today);
  2383. /** @var \OCP\Share\IShare[] $i */
  2384. $shares2 = [];
  2385. for ($i = 0; $i < 8; $i++) {
  2386. $shares2[] = clone $shares[$i];
  2387. }
  2388. $node = $this->createMock(File::class);
  2389. /*
  2390. * Simulate the getSharesBy call.
  2391. */
  2392. $this->defaultProvider
  2393. ->method('getSharesBy')
  2394. ->willReturnCallback(function ($uid, $type, $node, $reshares, $limit, $offset) use (&$shares2) {
  2395. return array_slice($shares2, $offset, $limit);
  2396. });
  2397. /*
  2398. * Simulate the deleteShare call.
  2399. */
  2400. $manager->method('deleteShare')
  2401. ->willReturnCallback(function ($share) use (&$shares2) {
  2402. for ($i = 0; $i < count($shares2); $i++) {
  2403. if ($shares2[$i]->getId() === $share->getId()) {
  2404. array_splice($shares2, $i, 1);
  2405. break;
  2406. }
  2407. }
  2408. });
  2409. $res = $manager->getSharesBy('user', IShare::TYPE_LINK, $node, true, 3, 0);
  2410. $this->assertCount(3, $res);
  2411. $this->assertEquals($shares[0]->getId(), $res[0]->getId());
  2412. $this->assertEquals($shares[1]->getId(), $res[1]->getId());
  2413. $this->assertEquals($shares[6]->getId(), $res[2]->getId());
  2414. $this->assertCount(4, $shares2);
  2415. $this->assertEquals(0, $shares2[0]->getId());
  2416. $this->assertEquals(1, $shares2[1]->getId());
  2417. $this->assertEquals(6, $shares2[2]->getId());
  2418. $this->assertEquals(7, $shares2[3]->getId());
  2419. $this->assertSame($today, $shares[3]->getExpirationDate());
  2420. }
  2421. public function testGetShareByToken(): void {
  2422. $this->config
  2423. ->expects($this->exactly(2))
  2424. ->method('getAppValue')
  2425. ->willReturnMap([
  2426. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  2427. ['files_sharing', 'hide_disabled_user_shares', 'no', 'no'],
  2428. ]);
  2429. $factory = $this->createMock(IProviderFactory::class);
  2430. $manager = $this->createManager($factory);
  2431. $share = $this->createMock(IShare::class);
  2432. $factory->expects($this->once())
  2433. ->method('getProviderForType')
  2434. ->with(IShare::TYPE_LINK)
  2435. ->willReturn($this->defaultProvider);
  2436. $this->defaultProvider->expects($this->once())
  2437. ->method('getShareByToken')
  2438. ->with('token')
  2439. ->willReturn($share);
  2440. $ret = $manager->getShareByToken('token');
  2441. $this->assertSame($share, $ret);
  2442. }
  2443. public function testGetShareByTokenRoom(): void {
  2444. $this->config
  2445. ->expects($this->exactly(2))
  2446. ->method('getAppValue')
  2447. ->willReturnMap([
  2448. ['core', 'shareapi_allow_links', 'yes', 'no'],
  2449. ['files_sharing', 'hide_disabled_user_shares', 'no', 'no'],
  2450. ]);
  2451. $factory = $this->createMock(IProviderFactory::class);
  2452. $manager = $this->createManager($factory);
  2453. $share = $this->createMock(IShare::class);
  2454. $roomShareProvider = $this->createMock(IShareProvider::class);
  2455. $factory->expects($this->any())
  2456. ->method('getProviderForType')
  2457. ->willReturnCallback(function ($shareType) use ($roomShareProvider) {
  2458. if ($shareType !== IShare::TYPE_ROOM) {
  2459. throw new Exception\ProviderException();
  2460. }
  2461. return $roomShareProvider;
  2462. });
  2463. $roomShareProvider->expects($this->once())
  2464. ->method('getShareByToken')
  2465. ->with('token')
  2466. ->willReturn($share);
  2467. $ret = $manager->getShareByToken('token');
  2468. $this->assertSame($share, $ret);
  2469. }
  2470. public function testGetShareByTokenWithException(): void {
  2471. $this->config
  2472. ->expects($this->exactly(2))
  2473. ->method('getAppValue')
  2474. ->willReturnMap([
  2475. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  2476. ['files_sharing', 'hide_disabled_user_shares', 'no', 'no'],
  2477. ]);
  2478. $factory = $this->createMock(IProviderFactory::class);
  2479. $manager = $this->createManager($factory);
  2480. $share = $this->createMock(IShare::class);
  2481. $factory->expects($this->exactly(2))
  2482. ->method('getProviderForType')
  2483. ->withConsecutive(
  2484. [IShare::TYPE_LINK],
  2485. [IShare::TYPE_REMOTE]
  2486. )
  2487. ->willReturn($this->defaultProvider);
  2488. $this->defaultProvider->expects($this->exactly(2))
  2489. ->method('getShareByToken')
  2490. ->with('token')
  2491. ->willReturnOnConsecutiveCalls(
  2492. $this->throwException(new ShareNotFound()),
  2493. $share
  2494. );
  2495. $ret = $manager->getShareByToken('token');
  2496. $this->assertSame($share, $ret);
  2497. }
  2498. public function testGetShareByTokenHideDisabledUser(): void {
  2499. $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class);
  2500. $this->expectExceptionMessage('The requested share comes from a disabled user');
  2501. $this->config
  2502. ->expects($this->exactly(2))
  2503. ->method('getAppValue')
  2504. ->willReturnMap([
  2505. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  2506. ['files_sharing', 'hide_disabled_user_shares', 'no', 'yes'],
  2507. ]);
  2508. $this->l->expects($this->once())
  2509. ->method('t')
  2510. ->willReturnArgument(0);
  2511. $manager = $this->createManagerMock()
  2512. ->setMethods(['deleteShare'])
  2513. ->getMock();
  2514. $date = new \DateTime();
  2515. $date->setTime(0, 0, 0);
  2516. $date->add(new \DateInterval('P2D'));
  2517. $share = $this->manager->newShare();
  2518. $share->setExpirationDate($date);
  2519. $share->setShareOwner('owner');
  2520. $share->setSharedBy('sharedBy');
  2521. $sharedBy = $this->createMock(IUser::class);
  2522. $owner = $this->createMock(IUser::class);
  2523. $this->userManager->method('get')->willReturnMap([
  2524. ['sharedBy', $sharedBy],
  2525. ['owner', $owner],
  2526. ]);
  2527. $owner->expects($this->once())
  2528. ->method('isEnabled')
  2529. ->willReturn(true);
  2530. $sharedBy->expects($this->once())
  2531. ->method('isEnabled')
  2532. ->willReturn(false);
  2533. $this->defaultProvider->expects($this->once())
  2534. ->method('getShareByToken')
  2535. ->with('expiredToken')
  2536. ->willReturn($share);
  2537. $manager->expects($this->never())
  2538. ->method('deleteShare');
  2539. $manager->getShareByToken('expiredToken');
  2540. }
  2541. public function testGetShareByTokenExpired(): void {
  2542. $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class);
  2543. $this->expectExceptionMessage('The requested share does not exist anymore');
  2544. $this->config
  2545. ->expects($this->once())
  2546. ->method('getAppValue')
  2547. ->with('core', 'shareapi_allow_links', 'yes')
  2548. ->willReturn('yes');
  2549. $this->l->expects($this->once())
  2550. ->method('t')
  2551. ->willReturnArgument(0);
  2552. $manager = $this->createManagerMock()
  2553. ->setMethods(['deleteShare'])
  2554. ->getMock();
  2555. $date = new \DateTime();
  2556. $date->setTime(0, 0, 0);
  2557. $share = $this->manager->newShare();
  2558. $share->setExpirationDate($date);
  2559. $this->defaultProvider->expects($this->once())
  2560. ->method('getShareByToken')
  2561. ->with('expiredToken')
  2562. ->willReturn($share);
  2563. $manager->expects($this->once())
  2564. ->method('deleteShare')
  2565. ->with($this->equalTo($share));
  2566. $manager->getShareByToken('expiredToken');
  2567. }
  2568. public function testGetShareByTokenNotExpired(): void {
  2569. $this->config
  2570. ->expects($this->exactly(2))
  2571. ->method('getAppValue')
  2572. ->willReturnMap([
  2573. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  2574. ['files_sharing', 'hide_disabled_user_shares', 'no', 'no'],
  2575. ]);
  2576. $date = new \DateTime();
  2577. $date->setTime(0, 0, 0);
  2578. $date->add(new \DateInterval('P2D'));
  2579. $share = $this->manager->newShare();
  2580. $share->setExpirationDate($date);
  2581. $this->defaultProvider->expects($this->once())
  2582. ->method('getShareByToken')
  2583. ->with('expiredToken')
  2584. ->willReturn($share);
  2585. $res = $this->manager->getShareByToken('expiredToken');
  2586. $this->assertSame($share, $res);
  2587. }
  2588. public function testGetShareByTokenWithPublicLinksDisabled(): void {
  2589. $this->expectException(\OCP\Share\Exceptions\ShareNotFound::class);
  2590. $this->config
  2591. ->expects($this->once())
  2592. ->method('getAppValue')
  2593. ->with('core', 'shareapi_allow_links', 'yes')
  2594. ->willReturn('no');
  2595. $this->manager->getShareByToken('validToken');
  2596. }
  2597. public function testGetShareByTokenPublicUploadDisabled(): void {
  2598. $this->config
  2599. ->expects($this->exactly(3))
  2600. ->method('getAppValue')
  2601. ->willReturnMap([
  2602. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  2603. ['core', 'shareapi_allow_public_upload', 'yes', 'no'],
  2604. ['files_sharing', 'hide_disabled_user_shares', 'no', 'no'],
  2605. ]);
  2606. $share = $this->manager->newShare();
  2607. $share->setShareType(IShare::TYPE_LINK)
  2608. ->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE);
  2609. $share->setSharedWith('sharedWith');
  2610. $folder = $this->createMock(\OC\Files\Node\Folder::class);
  2611. $share->setNode($folder);
  2612. $this->defaultProvider->expects($this->once())
  2613. ->method('getShareByToken')
  2614. ->willReturn('validToken')
  2615. ->willReturn($share);
  2616. $res = $this->manager->getShareByToken('validToken');
  2617. $this->assertSame(\OCP\Constants::PERMISSION_READ, $res->getPermissions());
  2618. }
  2619. public function testCheckPasswordNoLinkShare(): void {
  2620. $share = $this->createMock(IShare::class);
  2621. $share->method('getShareType')->willReturn(IShare::TYPE_USER);
  2622. $this->assertFalse($this->manager->checkPassword($share, 'password'));
  2623. }
  2624. public function testCheckPasswordNoPassword(): void {
  2625. $share = $this->createMock(IShare::class);
  2626. $share->method('getShareType')->willReturn(IShare::TYPE_LINK);
  2627. $this->assertFalse($this->manager->checkPassword($share, 'password'));
  2628. $share->method('getPassword')->willReturn('password');
  2629. $this->assertFalse($this->manager->checkPassword($share, null));
  2630. }
  2631. public function testCheckPasswordInvalidPassword(): void {
  2632. $share = $this->createMock(IShare::class);
  2633. $share->method('getShareType')->willReturn(IShare::TYPE_LINK);
  2634. $share->method('getPassword')->willReturn('password');
  2635. $this->hasher->method('verify')->with('invalidpassword', 'password', '')->willReturn(false);
  2636. $this->assertFalse($this->manager->checkPassword($share, 'invalidpassword'));
  2637. }
  2638. public function testCheckPasswordValidPassword(): void {
  2639. $share = $this->createMock(IShare::class);
  2640. $share->method('getShareType')->willReturn(IShare::TYPE_LINK);
  2641. $share->method('getPassword')->willReturn('passwordHash');
  2642. $this->hasher->method('verify')->with('password', 'passwordHash', '')->willReturn(true);
  2643. $this->assertTrue($this->manager->checkPassword($share, 'password'));
  2644. }
  2645. public function testCheckPasswordUpdateShare(): void {
  2646. $share = $this->manager->newShare();
  2647. $share->setShareType(IShare::TYPE_LINK)
  2648. ->setPassword('passwordHash');
  2649. $this->hasher->method('verify')->with('password', 'passwordHash', '')
  2650. ->willReturnCallback(function ($pass, $hash, &$newHash) {
  2651. $newHash = 'newHash';
  2652. return true;
  2653. });
  2654. $this->defaultProvider->expects($this->once())
  2655. ->method('update')
  2656. ->with($this->callback(function (\OCP\Share\IShare $share) {
  2657. return $share->getPassword() === 'newHash';
  2658. }));
  2659. $this->assertTrue($this->manager->checkPassword($share, 'password'));
  2660. }
  2661. public function testUpdateShareCantChangeShareType(): void {
  2662. $this->expectException(\Exception::class);
  2663. $this->expectExceptionMessage('Cannot change share type');
  2664. $manager = $this->createManagerMock()
  2665. ->setMethods([
  2666. 'canShare',
  2667. 'getShareById'
  2668. ])
  2669. ->getMock();
  2670. $originalShare = $this->manager->newShare();
  2671. $originalShare->setShareType(IShare::TYPE_GROUP);
  2672. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2673. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2674. $share = $this->manager->newShare();
  2675. $attrs = $this->manager->newShare()->newAttributes();
  2676. $attrs->setAttribute('app1', 'perm1', true);
  2677. $share->setProviderId('foo')
  2678. ->setId('42')
  2679. ->setShareType(IShare::TYPE_USER);
  2680. $manager->updateShare($share);
  2681. }
  2682. public function testUpdateShareCantChangeRecipientForGroupShare(): void {
  2683. $this->expectException(\Exception::class);
  2684. $this->expectExceptionMessage('Can only update recipient on user shares');
  2685. $manager = $this->createManagerMock()
  2686. ->setMethods([
  2687. 'canShare',
  2688. 'getShareById'
  2689. ])
  2690. ->getMock();
  2691. $originalShare = $this->manager->newShare();
  2692. $originalShare->setShareType(IShare::TYPE_GROUP)
  2693. ->setSharedWith('origGroup');
  2694. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2695. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2696. $share = $this->manager->newShare();
  2697. $share->setProviderId('foo')
  2698. ->setId('42')
  2699. ->setShareType(IShare::TYPE_GROUP)
  2700. ->setSharedWith('newGroup');
  2701. $manager->updateShare($share);
  2702. }
  2703. public function testUpdateShareCantShareWithOwner(): void {
  2704. $this->expectException(\Exception::class);
  2705. $this->expectExceptionMessage('Cannot share with the share owner');
  2706. $manager = $this->createManagerMock()
  2707. ->setMethods([
  2708. 'canShare',
  2709. 'getShareById'
  2710. ])
  2711. ->getMock();
  2712. $originalShare = $this->manager->newShare();
  2713. $originalShare->setShareType(IShare::TYPE_USER)
  2714. ->setSharedWith('sharedWith');
  2715. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2716. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2717. $share = $this->manager->newShare();
  2718. $share->setProviderId('foo')
  2719. ->setId('42')
  2720. ->setShareType(IShare::TYPE_USER)
  2721. ->setSharedWith('newUser')
  2722. ->setShareOwner('newUser');
  2723. $manager->updateShare($share);
  2724. }
  2725. public function testUpdateShareUser(): void {
  2726. $this->userManager->expects($this->any())->method('userExists')->willReturn(true);
  2727. $manager = $this->createManagerMock()
  2728. ->setMethods([
  2729. 'canShare',
  2730. 'getShareById',
  2731. 'generalCreateChecks',
  2732. 'userCreateChecks',
  2733. 'pathCreateChecks',
  2734. ])
  2735. ->getMock();
  2736. $originalShare = $this->manager->newShare();
  2737. $originalShare->setShareType(IShare::TYPE_USER)
  2738. ->setSharedWith('origUser')
  2739. ->setPermissions(1);
  2740. $node = $this->createMock(File::class);
  2741. $node->method('getId')->willReturn(100);
  2742. $node->method('getPath')->willReturn('/newUser/files/myPath');
  2743. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2744. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2745. $share = $this->manager->newShare();
  2746. $attrs = $this->manager->newShare()->newAttributes();
  2747. $attrs->setAttribute('app1', 'perm1', true);
  2748. $share->setProviderId('foo')
  2749. ->setId('42')
  2750. ->setShareType(IShare::TYPE_USER)
  2751. ->setSharedWith('origUser')
  2752. ->setShareOwner('newUser')
  2753. ->setSharedBy('sharer')
  2754. ->setPermissions(31)
  2755. ->setAttributes($attrs)
  2756. ->setNode($node);
  2757. $this->defaultProvider->expects($this->once())
  2758. ->method('update')
  2759. ->with($share)
  2760. ->willReturn($share);
  2761. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2762. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
  2763. $hookListener->expects($this->never())->method('post');
  2764. $this->rootFolder->method('getUserFolder')->with('newUser')->willReturnSelf();
  2765. $this->rootFolder->method('getRelativePath')->with('/newUser/files/myPath')->willReturn('/myPath');
  2766. $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2767. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener2, 'post');
  2768. $hookListener2->expects($this->once())->method('post')->with([
  2769. 'itemType' => 'file',
  2770. 'itemSource' => 100,
  2771. 'shareType' => IShare::TYPE_USER,
  2772. 'shareWith' => 'origUser',
  2773. 'uidOwner' => 'sharer',
  2774. 'permissions' => 31,
  2775. 'path' => '/myPath',
  2776. 'attributes' => $attrs->toArray(),
  2777. ]);
  2778. $manager->updateShare($share);
  2779. }
  2780. public function testUpdateShareGroup(): void {
  2781. $manager = $this->createManagerMock()
  2782. ->setMethods([
  2783. 'canShare',
  2784. 'getShareById',
  2785. 'generalCreateChecks',
  2786. 'groupCreateChecks',
  2787. 'pathCreateChecks',
  2788. ])
  2789. ->getMock();
  2790. $originalShare = $this->manager->newShare();
  2791. $originalShare->setShareType(IShare::TYPE_GROUP)
  2792. ->setSharedWith('origUser')
  2793. ->setPermissions(31);
  2794. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2795. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2796. $node = $this->createMock(File::class);
  2797. $share = $this->manager->newShare();
  2798. $share->setProviderId('foo')
  2799. ->setId('42')
  2800. ->setShareType(IShare::TYPE_GROUP)
  2801. ->setSharedWith('origUser')
  2802. ->setShareOwner('owner')
  2803. ->setNode($node)
  2804. ->setPermissions(31);
  2805. $this->defaultProvider->expects($this->once())
  2806. ->method('update')
  2807. ->with($share)
  2808. ->willReturn($share);
  2809. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2810. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
  2811. $hookListener->expects($this->never())->method('post');
  2812. $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2813. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener2, 'post');
  2814. $hookListener2->expects($this->never())->method('post');
  2815. $manager->updateShare($share);
  2816. }
  2817. public function testUpdateShareLink(): void {
  2818. $manager = $this->createManagerMock()
  2819. ->setMethods([
  2820. 'canShare',
  2821. 'getShareById',
  2822. 'generalCreateChecks',
  2823. 'linkCreateChecks',
  2824. 'pathCreateChecks',
  2825. 'verifyPassword',
  2826. 'validateExpirationDateLink',
  2827. ])
  2828. ->getMock();
  2829. $originalShare = $this->manager->newShare();
  2830. $originalShare->setShareType(IShare::TYPE_LINK)
  2831. ->setPermissions(15);
  2832. $tomorrow = new \DateTime();
  2833. $tomorrow->setTime(0, 0, 0);
  2834. $tomorrow->add(new \DateInterval('P1D'));
  2835. $file = $this->createMock(File::class);
  2836. $file->method('getId')->willReturn(100);
  2837. $share = $this->manager->newShare();
  2838. $share->setProviderId('foo')
  2839. ->setId('42')
  2840. ->setShareType(IShare::TYPE_LINK)
  2841. ->setToken('token')
  2842. ->setSharedBy('owner')
  2843. ->setShareOwner('owner')
  2844. ->setPassword('password')
  2845. ->setExpirationDate($tomorrow)
  2846. ->setNode($file)
  2847. ->setPermissions(15);
  2848. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2849. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2850. $manager->expects($this->once())->method('validateExpirationDateLink')->with($share);
  2851. $manager->expects($this->once())->method('verifyPassword')->with('password');
  2852. $this->hasher->expects($this->once())
  2853. ->method('hash')
  2854. ->with('password')
  2855. ->willReturn('hashed');
  2856. $this->defaultProvider->expects($this->once())
  2857. ->method('update')
  2858. ->with($share)
  2859. ->willReturn($share);
  2860. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2861. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
  2862. $hookListener->expects($this->once())->method('post')->with([
  2863. 'itemType' => 'file',
  2864. 'itemSource' => 100,
  2865. 'date' => $tomorrow,
  2866. 'uidOwner' => 'owner',
  2867. ]);
  2868. $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2869. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post');
  2870. $hookListener2->expects($this->once())->method('post')->with([
  2871. 'itemType' => 'file',
  2872. 'itemSource' => 100,
  2873. 'uidOwner' => 'owner',
  2874. 'token' => 'token',
  2875. 'disabled' => false,
  2876. ]);
  2877. $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2878. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post');
  2879. $hookListener3->expects($this->never())->method('post');
  2880. $manager->updateShare($share);
  2881. }
  2882. public function testUpdateShareLinkEnableSendPasswordByTalkWithNoPassword(): void {
  2883. $this->expectException(\InvalidArgumentException::class);
  2884. $this->expectExceptionMessage('Cannot enable sending the password by Talk with an empty password');
  2885. $manager = $this->createManagerMock()
  2886. ->setMethods([
  2887. 'canShare',
  2888. 'getShareById',
  2889. 'generalCreateChecks',
  2890. 'linkCreateChecks',
  2891. 'pathCreateChecks',
  2892. 'verifyPassword',
  2893. 'validateExpirationDateLink',
  2894. ])
  2895. ->getMock();
  2896. $originalShare = $this->manager->newShare();
  2897. $originalShare->setShareType(IShare::TYPE_LINK)
  2898. ->setPermissions(15);
  2899. $tomorrow = new \DateTime();
  2900. $tomorrow->setTime(0, 0, 0);
  2901. $tomorrow->add(new \DateInterval('P1D'));
  2902. $file = $this->createMock(File::class);
  2903. $file->method('getId')->willReturn(100);
  2904. $share = $this->manager->newShare();
  2905. $share->setProviderId('foo')
  2906. ->setId('42')
  2907. ->setShareType(IShare::TYPE_LINK)
  2908. ->setToken('token')
  2909. ->setSharedBy('owner')
  2910. ->setShareOwner('owner')
  2911. ->setPassword(null)
  2912. ->setSendPasswordByTalk(true)
  2913. ->setExpirationDate($tomorrow)
  2914. ->setNode($file)
  2915. ->setPermissions(15);
  2916. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2917. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2918. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  2919. $manager->expects($this->once())->method('linkCreateChecks')->with($share);
  2920. $manager->expects($this->never())->method('verifyPassword');
  2921. $manager->expects($this->never())->method('pathCreateChecks');
  2922. $manager->expects($this->never())->method('validateExpirationDateLink');
  2923. $this->hasher->expects($this->never())
  2924. ->method('hash');
  2925. $this->defaultProvider->expects($this->never())
  2926. ->method('update');
  2927. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2928. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
  2929. $hookListener->expects($this->never())->method('post');
  2930. $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2931. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post');
  2932. $hookListener2->expects($this->never())->method('post');
  2933. $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2934. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post');
  2935. $hookListener3->expects($this->never())->method('post');
  2936. $manager->updateShare($share);
  2937. }
  2938. public function testUpdateShareMail(): void {
  2939. $manager = $this->createManagerMock()
  2940. ->setMethods([
  2941. 'canShare',
  2942. 'getShareById',
  2943. 'generalCreateChecks',
  2944. 'verifyPassword',
  2945. 'pathCreateChecks',
  2946. 'linkCreateChecks',
  2947. 'validateExpirationDateLink',
  2948. ])
  2949. ->getMock();
  2950. $originalShare = $this->manager->newShare();
  2951. $originalShare->setShareType(IShare::TYPE_EMAIL)
  2952. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2953. $tomorrow = new \DateTime();
  2954. $tomorrow->setTime(0, 0, 0);
  2955. $tomorrow->add(new \DateInterval('P1D'));
  2956. $file = $this->createMock(File::class);
  2957. $file->method('getId')->willReturn(100);
  2958. $share = $this->manager->newShare();
  2959. $share->setProviderId('foo')
  2960. ->setId('42')
  2961. ->setShareType(IShare::TYPE_EMAIL)
  2962. ->setToken('token')
  2963. ->setSharedBy('owner')
  2964. ->setShareOwner('owner')
  2965. ->setPassword('password')
  2966. ->setExpirationDate($tomorrow)
  2967. ->setNode($file)
  2968. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  2969. $manager->expects($this->once())->method('canShare')->willReturn(true);
  2970. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  2971. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  2972. $manager->expects($this->once())->method('verifyPassword')->with('password');
  2973. $manager->expects($this->once())->method('pathCreateChecks')->with($file);
  2974. $manager->expects($this->once())->method('linkCreateChecks');
  2975. $manager->expects($this->once())->method('validateExpirationDateLink');
  2976. $this->hasher->expects($this->once())
  2977. ->method('hash')
  2978. ->with('password')
  2979. ->willReturn('hashed');
  2980. $this->defaultProvider->expects($this->once())
  2981. ->method('update')
  2982. ->with($share, 'password')
  2983. ->willReturn($share);
  2984. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2985. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
  2986. $hookListener->expects($this->once())->method('post')->with([
  2987. 'itemType' => 'file',
  2988. 'itemSource' => 100,
  2989. 'date' => $tomorrow,
  2990. 'uidOwner' => 'owner',
  2991. ]);
  2992. $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  2993. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post');
  2994. $hookListener2->expects($this->once())->method('post')->with([
  2995. 'itemType' => 'file',
  2996. 'itemSource' => 100,
  2997. 'uidOwner' => 'owner',
  2998. 'token' => 'token',
  2999. 'disabled' => false,
  3000. ]);
  3001. $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3002. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post');
  3003. $hookListener3->expects($this->never())->method('post');
  3004. $manager->updateShare($share);
  3005. }
  3006. public function testUpdateShareMailEnableSendPasswordByTalk(): void {
  3007. $manager = $this->createManagerMock()
  3008. ->setMethods([
  3009. 'canShare',
  3010. 'getShareById',
  3011. 'generalCreateChecks',
  3012. 'verifyPassword',
  3013. 'pathCreateChecks',
  3014. 'linkCreateChecks',
  3015. 'validateExpirationDateLink',
  3016. ])
  3017. ->getMock();
  3018. $originalShare = $this->manager->newShare();
  3019. $originalShare->setShareType(IShare::TYPE_EMAIL)
  3020. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  3021. ->setPassword(null)
  3022. ->setSendPasswordByTalk(false);
  3023. $tomorrow = new \DateTime();
  3024. $tomorrow->setTime(0, 0, 0);
  3025. $tomorrow->add(new \DateInterval('P1D'));
  3026. $file = $this->createMock(File::class);
  3027. $file->method('getId')->willReturn(100);
  3028. $share = $this->manager->newShare();
  3029. $share->setProviderId('foo')
  3030. ->setId('42')
  3031. ->setShareType(IShare::TYPE_EMAIL)
  3032. ->setToken('token')
  3033. ->setSharedBy('owner')
  3034. ->setShareOwner('owner')
  3035. ->setPassword('password')
  3036. ->setSendPasswordByTalk(true)
  3037. ->setExpirationDate($tomorrow)
  3038. ->setNode($file)
  3039. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  3040. $manager->expects($this->once())->method('canShare')->willReturn(true);
  3041. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  3042. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  3043. $manager->expects($this->once())->method('verifyPassword')->with('password');
  3044. $manager->expects($this->once())->method('pathCreateChecks')->with($file);
  3045. $manager->expects($this->once())->method('linkCreateChecks');
  3046. $manager->expects($this->once())->method('validateExpirationDateLink');
  3047. $this->hasher->expects($this->once())
  3048. ->method('hash')
  3049. ->with('password')
  3050. ->willReturn('hashed');
  3051. $this->defaultProvider->expects($this->once())
  3052. ->method('update')
  3053. ->with($share, 'password')
  3054. ->willReturn($share);
  3055. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3056. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
  3057. $hookListener->expects($this->once())->method('post')->with([
  3058. 'itemType' => 'file',
  3059. 'itemSource' => 100,
  3060. 'date' => $tomorrow,
  3061. 'uidOwner' => 'owner',
  3062. ]);
  3063. $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3064. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post');
  3065. $hookListener2->expects($this->once())->method('post')->with([
  3066. 'itemType' => 'file',
  3067. 'itemSource' => 100,
  3068. 'uidOwner' => 'owner',
  3069. 'token' => 'token',
  3070. 'disabled' => false,
  3071. ]);
  3072. $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3073. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post');
  3074. $hookListener3->expects($this->never())->method('post');
  3075. $manager->updateShare($share);
  3076. }
  3077. public function testUpdateShareMailEnableSendPasswordByTalkWithDifferentPassword(): void {
  3078. $manager = $this->createManagerMock()
  3079. ->setMethods([
  3080. 'canShare',
  3081. 'getShareById',
  3082. 'generalCreateChecks',
  3083. 'verifyPassword',
  3084. 'pathCreateChecks',
  3085. 'linkCreateChecks',
  3086. 'validateExpirationDateLink',
  3087. ])
  3088. ->getMock();
  3089. $originalShare = $this->manager->newShare();
  3090. $originalShare->setShareType(IShare::TYPE_EMAIL)
  3091. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  3092. ->setPassword('anotherPasswordHash')
  3093. ->setSendPasswordByTalk(false);
  3094. $tomorrow = new \DateTime();
  3095. $tomorrow->setTime(0, 0, 0);
  3096. $tomorrow->add(new \DateInterval('P1D'));
  3097. $file = $this->createMock(File::class);
  3098. $file->method('getId')->willReturn(100);
  3099. $share = $this->manager->newShare();
  3100. $share->setProviderId('foo')
  3101. ->setId('42')
  3102. ->setShareType(IShare::TYPE_EMAIL)
  3103. ->setToken('token')
  3104. ->setSharedBy('owner')
  3105. ->setShareOwner('owner')
  3106. ->setPassword('password')
  3107. ->setSendPasswordByTalk(true)
  3108. ->setExpirationDate($tomorrow)
  3109. ->setNode($file)
  3110. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  3111. $manager->expects($this->once())->method('canShare')->willReturn(true);
  3112. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  3113. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  3114. $manager->expects($this->once())->method('verifyPassword')->with('password');
  3115. $manager->expects($this->once())->method('pathCreateChecks')->with($file);
  3116. $manager->expects($this->once())->method('linkCreateChecks');
  3117. $manager->expects($this->once())->method('validateExpirationDateLink');
  3118. $this->hasher->expects($this->once())
  3119. ->method('verify')
  3120. ->with('password', 'anotherPasswordHash')
  3121. ->willReturn(false);
  3122. $this->hasher->expects($this->once())
  3123. ->method('hash')
  3124. ->with('password')
  3125. ->willReturn('hashed');
  3126. $this->defaultProvider->expects($this->once())
  3127. ->method('update')
  3128. ->with($share, 'password')
  3129. ->willReturn($share);
  3130. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3131. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
  3132. $hookListener->expects($this->once())->method('post')->with([
  3133. 'itemType' => 'file',
  3134. 'itemSource' => 100,
  3135. 'date' => $tomorrow,
  3136. 'uidOwner' => 'owner',
  3137. ]);
  3138. $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3139. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post');
  3140. $hookListener2->expects($this->once())->method('post')->with([
  3141. 'itemType' => 'file',
  3142. 'itemSource' => 100,
  3143. 'uidOwner' => 'owner',
  3144. 'token' => 'token',
  3145. 'disabled' => false,
  3146. ]);
  3147. $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3148. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post');
  3149. $hookListener3->expects($this->never())->method('post');
  3150. $manager->updateShare($share);
  3151. }
  3152. public function testUpdateShareMailEnableSendPasswordByTalkWithNoPassword(): void {
  3153. $this->expectException(\InvalidArgumentException::class);
  3154. $this->expectExceptionMessage('Cannot enable sending the password by Talk with an empty password');
  3155. $manager = $this->createManagerMock()
  3156. ->setMethods([
  3157. 'canShare',
  3158. 'getShareById',
  3159. 'generalCreateChecks',
  3160. 'verifyPassword',
  3161. 'pathCreateChecks',
  3162. 'linkCreateChecks',
  3163. 'validateExpirationDateLink',
  3164. ])
  3165. ->getMock();
  3166. $originalShare = $this->manager->newShare();
  3167. $originalShare->setShareType(IShare::TYPE_EMAIL)
  3168. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  3169. ->setPassword(null)
  3170. ->setSendPasswordByTalk(false);
  3171. $tomorrow = new \DateTime();
  3172. $tomorrow->setTime(0, 0, 0);
  3173. $tomorrow->add(new \DateInterval('P1D'));
  3174. $file = $this->createMock(File::class);
  3175. $file->method('getId')->willReturn(100);
  3176. $share = $this->manager->newShare();
  3177. $share->setProviderId('foo')
  3178. ->setId('42')
  3179. ->setShareType(IShare::TYPE_EMAIL)
  3180. ->setToken('token')
  3181. ->setSharedBy('owner')
  3182. ->setShareOwner('owner')
  3183. ->setPassword(null)
  3184. ->setSendPasswordByTalk(true)
  3185. ->setExpirationDate($tomorrow)
  3186. ->setNode($file)
  3187. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  3188. $manager->expects($this->once())->method('canShare')->willReturn(true);
  3189. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  3190. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  3191. $manager->expects($this->never())->method('verifyPassword');
  3192. $manager->expects($this->never())->method('pathCreateChecks');
  3193. $manager->expects($this->once())->method('linkCreateChecks');
  3194. $manager->expects($this->never())->method('validateExpirationDateLink');
  3195. // If the password is empty, we have nothing to hash
  3196. $this->hasher->expects($this->never())
  3197. ->method('hash');
  3198. $this->defaultProvider->expects($this->never())
  3199. ->method('update');
  3200. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3201. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
  3202. $hookListener->expects($this->never())->method('post');
  3203. $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3204. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post');
  3205. $hookListener2->expects($this->never())->method('post');
  3206. $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3207. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post');
  3208. $hookListener3->expects($this->never())->method('post');
  3209. $manager->updateShare($share);
  3210. }
  3211. public function testUpdateShareMailEnableSendPasswordByTalkRemovingPassword(): void {
  3212. $this->expectException(\InvalidArgumentException::class);
  3213. $this->expectExceptionMessage('Cannot enable sending the password by Talk with an empty password');
  3214. $manager = $this->createManagerMock()
  3215. ->setMethods([
  3216. 'canShare',
  3217. 'getShareById',
  3218. 'generalCreateChecks',
  3219. 'verifyPassword',
  3220. 'pathCreateChecks',
  3221. 'linkCreateChecks',
  3222. 'validateExpirationDateLink',
  3223. ])
  3224. ->getMock();
  3225. $originalShare = $this->manager->newShare();
  3226. $originalShare->setShareType(IShare::TYPE_EMAIL)
  3227. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  3228. ->setPassword('passwordHash')
  3229. ->setSendPasswordByTalk(false);
  3230. $tomorrow = new \DateTime();
  3231. $tomorrow->setTime(0, 0, 0);
  3232. $tomorrow->add(new \DateInterval('P1D'));
  3233. $file = $this->createMock(File::class);
  3234. $file->method('getId')->willReturn(100);
  3235. $share = $this->manager->newShare();
  3236. $share->setProviderId('foo')
  3237. ->setId('42')
  3238. ->setShareType(IShare::TYPE_EMAIL)
  3239. ->setToken('token')
  3240. ->setSharedBy('owner')
  3241. ->setShareOwner('owner')
  3242. ->setPassword(null)
  3243. ->setSendPasswordByTalk(true)
  3244. ->setExpirationDate($tomorrow)
  3245. ->setNode($file)
  3246. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  3247. $manager->expects($this->once())->method('canShare')->willReturn(true);
  3248. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  3249. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  3250. $manager->expects($this->once())->method('verifyPassword');
  3251. $manager->expects($this->never())->method('pathCreateChecks');
  3252. $manager->expects($this->once())->method('linkCreateChecks');
  3253. $manager->expects($this->never())->method('validateExpirationDateLink');
  3254. // If the password is empty, we have nothing to hash
  3255. $this->hasher->expects($this->never())
  3256. ->method('hash');
  3257. $this->defaultProvider->expects($this->never())
  3258. ->method('update');
  3259. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3260. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
  3261. $hookListener->expects($this->never())->method('post');
  3262. $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3263. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post');
  3264. $hookListener2->expects($this->never())->method('post');
  3265. $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3266. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post');
  3267. $hookListener3->expects($this->never())->method('post');
  3268. $manager->updateShare($share);
  3269. }
  3270. public function testUpdateShareMailEnableSendPasswordByTalkRemovingPasswordWithEmptyString(): void {
  3271. $this->expectException(\InvalidArgumentException::class);
  3272. $this->expectExceptionMessage('Cannot enable sending the password by Talk with an empty password');
  3273. $manager = $this->createManagerMock()
  3274. ->setMethods([
  3275. 'canShare',
  3276. 'getShareById',
  3277. 'generalCreateChecks',
  3278. 'verifyPassword',
  3279. 'pathCreateChecks',
  3280. 'linkCreateChecks',
  3281. 'validateExpirationDateLink',
  3282. ])
  3283. ->getMock();
  3284. $originalShare = $this->manager->newShare();
  3285. $originalShare->setShareType(IShare::TYPE_EMAIL)
  3286. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  3287. ->setPassword('passwordHash')
  3288. ->setSendPasswordByTalk(false);
  3289. $tomorrow = new \DateTime();
  3290. $tomorrow->setTime(0, 0, 0);
  3291. $tomorrow->add(new \DateInterval('P1D'));
  3292. $file = $this->createMock(File::class);
  3293. $file->method('getId')->willReturn(100);
  3294. $share = $this->manager->newShare();
  3295. $share->setProviderId('foo')
  3296. ->setId('42')
  3297. ->setShareType(IShare::TYPE_EMAIL)
  3298. ->setToken('token')
  3299. ->setSharedBy('owner')
  3300. ->setShareOwner('owner')
  3301. ->setPassword('')
  3302. ->setSendPasswordByTalk(true)
  3303. ->setExpirationDate($tomorrow)
  3304. ->setNode($file)
  3305. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  3306. $manager->expects($this->once())->method('canShare')->willReturn(true);
  3307. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  3308. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  3309. $manager->expects($this->once())->method('verifyPassword');
  3310. $manager->expects($this->never())->method('pathCreateChecks');
  3311. $manager->expects($this->once())->method('linkCreateChecks');
  3312. $manager->expects($this->never())->method('validateExpirationDateLink');
  3313. // If the password is empty, we have nothing to hash
  3314. $this->hasher->expects($this->never())
  3315. ->method('hash');
  3316. $this->defaultProvider->expects($this->never())
  3317. ->method('update');
  3318. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3319. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
  3320. $hookListener->expects($this->never())->method('post');
  3321. $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3322. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post');
  3323. $hookListener2->expects($this->never())->method('post');
  3324. $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3325. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post');
  3326. $hookListener3->expects($this->never())->method('post');
  3327. $manager->updateShare($share);
  3328. }
  3329. public function testUpdateShareMailEnableSendPasswordByTalkWithPreviousPassword(): void {
  3330. $this->expectException(\InvalidArgumentException::class);
  3331. $this->expectExceptionMessage('Cannot enable sending the password by Talk without setting a new password');
  3332. $manager = $this->createManagerMock()
  3333. ->setMethods([
  3334. 'canShare',
  3335. 'getShareById',
  3336. 'generalCreateChecks',
  3337. 'verifyPassword',
  3338. 'pathCreateChecks',
  3339. 'linkCreateChecks',
  3340. 'validateExpirationDateLink',
  3341. ])
  3342. ->getMock();
  3343. $originalShare = $this->manager->newShare();
  3344. $originalShare->setShareType(IShare::TYPE_EMAIL)
  3345. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  3346. ->setPassword('password')
  3347. ->setSendPasswordByTalk(false);
  3348. $tomorrow = new \DateTime();
  3349. $tomorrow->setTime(0, 0, 0);
  3350. $tomorrow->add(new \DateInterval('P1D'));
  3351. $file = $this->createMock(File::class);
  3352. $file->method('getId')->willReturn(100);
  3353. $share = $this->manager->newShare();
  3354. $share->setProviderId('foo')
  3355. ->setId('42')
  3356. ->setShareType(IShare::TYPE_EMAIL)
  3357. ->setToken('token')
  3358. ->setSharedBy('owner')
  3359. ->setShareOwner('owner')
  3360. ->setPassword('password')
  3361. ->setSendPasswordByTalk(true)
  3362. ->setExpirationDate($tomorrow)
  3363. ->setNode($file)
  3364. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  3365. $manager->expects($this->once())->method('canShare')->willReturn(true);
  3366. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  3367. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  3368. $manager->expects($this->never())->method('verifyPassword');
  3369. $manager->expects($this->never())->method('pathCreateChecks');
  3370. $manager->expects($this->once())->method('linkCreateChecks');
  3371. $manager->expects($this->never())->method('validateExpirationDateLink');
  3372. // If the old & new passwords are the same, we don't do anything
  3373. $this->hasher->expects($this->never())
  3374. ->method('verify');
  3375. $this->hasher->expects($this->never())
  3376. ->method('hash');
  3377. $this->defaultProvider->expects($this->never())
  3378. ->method('update');
  3379. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3380. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
  3381. $hookListener->expects($this->never())->method('post');
  3382. $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3383. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post');
  3384. $hookListener2->expects($this->never())->method('post');
  3385. $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3386. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post');
  3387. $hookListener3->expects($this->never())->method('post');
  3388. $manager->updateShare($share);
  3389. }
  3390. public function testUpdateShareMailDisableSendPasswordByTalkWithPreviousPassword(): void {
  3391. $this->expectException(\InvalidArgumentException::class);
  3392. $this->expectExceptionMessage('Cannot disable sending the password by Talk without setting a new password');
  3393. $manager = $this->createManagerMock()
  3394. ->setMethods([
  3395. 'canShare',
  3396. 'getShareById',
  3397. 'generalCreateChecks',
  3398. 'verifyPassword',
  3399. 'pathCreateChecks',
  3400. 'linkCreateChecks',
  3401. 'validateExpirationDateLink',
  3402. ])
  3403. ->getMock();
  3404. $originalShare = $this->manager->newShare();
  3405. $originalShare->setShareType(IShare::TYPE_EMAIL)
  3406. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  3407. ->setPassword('passwordHash')
  3408. ->setSendPasswordByTalk(true);
  3409. $tomorrow = new \DateTime();
  3410. $tomorrow->setTime(0, 0, 0);
  3411. $tomorrow->add(new \DateInterval('P1D'));
  3412. $file = $this->createMock(File::class);
  3413. $file->method('getId')->willReturn(100);
  3414. $share = $this->manager->newShare();
  3415. $share->setProviderId('foo')
  3416. ->setId('42')
  3417. ->setShareType(IShare::TYPE_EMAIL)
  3418. ->setToken('token')
  3419. ->setSharedBy('owner')
  3420. ->setShareOwner('owner')
  3421. ->setPassword('passwordHash')
  3422. ->setSendPasswordByTalk(false)
  3423. ->setExpirationDate($tomorrow)
  3424. ->setNode($file)
  3425. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  3426. $manager->expects($this->once())->method('canShare')->willReturn(true);
  3427. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  3428. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  3429. $manager->expects($this->never())->method('verifyPassword');
  3430. $manager->expects($this->never())->method('pathCreateChecks');
  3431. $manager->expects($this->once())->method('linkCreateChecks');
  3432. $manager->expects($this->never())->method('validateExpirationDateLink');
  3433. // If the old & new passwords are the same, we don't do anything
  3434. $this->hasher->expects($this->never())
  3435. ->method('verify');
  3436. $this->hasher->expects($this->never())
  3437. ->method('hash');
  3438. $this->defaultProvider->expects($this->never())
  3439. ->method('update');
  3440. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3441. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
  3442. $hookListener->expects($this->never())->method('post');
  3443. $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3444. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post');
  3445. $hookListener2->expects($this->never())->method('post');
  3446. $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3447. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post');
  3448. $hookListener3->expects($this->never())->method('post');
  3449. $manager->updateShare($share);
  3450. }
  3451. public function testUpdateShareMailDisableSendPasswordByTalkWithoutChangingPassword(): void {
  3452. $this->expectException(\InvalidArgumentException::class);
  3453. $this->expectExceptionMessage('Cannot disable sending the password by Talk without setting a new password');
  3454. $manager = $this->createManagerMock()
  3455. ->setMethods([
  3456. 'canShare',
  3457. 'getShareById',
  3458. 'generalCreateChecks',
  3459. 'verifyPassword',
  3460. 'pathCreateChecks',
  3461. 'linkCreateChecks',
  3462. 'validateExpirationDateLink',
  3463. ])
  3464. ->getMock();
  3465. $originalShare = $this->manager->newShare();
  3466. $originalShare->setShareType(IShare::TYPE_EMAIL)
  3467. ->setPermissions(\OCP\Constants::PERMISSION_ALL)
  3468. ->setPassword('passwordHash')
  3469. ->setSendPasswordByTalk(true);
  3470. $tomorrow = new \DateTime();
  3471. $tomorrow->setTime(0, 0, 0);
  3472. $tomorrow->add(new \DateInterval('P1D'));
  3473. $file = $this->createMock(File::class);
  3474. $file->method('getId')->willReturn(100);
  3475. $share = $this->manager->newShare();
  3476. $share->setProviderId('foo')
  3477. ->setId('42')
  3478. ->setShareType(IShare::TYPE_EMAIL)
  3479. ->setToken('token')
  3480. ->setSharedBy('owner')
  3481. ->setShareOwner('owner')
  3482. ->setPassword('passwordHash')
  3483. ->setSendPasswordByTalk(false)
  3484. ->setExpirationDate($tomorrow)
  3485. ->setNode($file)
  3486. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  3487. $manager->expects($this->once())->method('canShare')->willReturn(true);
  3488. $manager->expects($this->once())->method('getShareById')->with('foo:42')->willReturn($originalShare);
  3489. $manager->expects($this->once())->method('generalCreateChecks')->with($share);
  3490. $manager->expects($this->never())->method('verifyPassword');
  3491. $manager->expects($this->never())->method('pathCreateChecks');
  3492. $manager->expects($this->once())->method('linkCreateChecks');
  3493. $manager->expects($this->never())->method('validateExpirationDateLink');
  3494. // If the old & new passwords are the same, we don't do anything
  3495. $this->hasher->expects($this->never())
  3496. ->method('verify');
  3497. $this->hasher->expects($this->never())
  3498. ->method('hash');
  3499. $this->defaultProvider->expects($this->never())
  3500. ->method('update');
  3501. $hookListener = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3502. \OCP\Util::connectHook('OCP\Share', 'post_set_expiration_date', $hookListener, 'post');
  3503. $hookListener->expects($this->never())->method('post');
  3504. $hookListener2 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3505. \OCP\Util::connectHook('OCP\Share', 'post_update_password', $hookListener2, 'post');
  3506. $hookListener2->expects($this->never())->method('post');
  3507. $hookListener3 = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
  3508. \OCP\Util::connectHook('OCP\Share', 'post_update_permissions', $hookListener3, 'post');
  3509. $hookListener3->expects($this->never())->method('post');
  3510. $manager->updateShare($share);
  3511. }
  3512. public function testMoveShareLink(): void {
  3513. $this->expectException(\InvalidArgumentException::class);
  3514. $this->expectExceptionMessage('Cannot change target of link share');
  3515. $share = $this->manager->newShare();
  3516. $share->setShareType(IShare::TYPE_LINK);
  3517. $recipient = $this->createMock(IUser::class);
  3518. $this->manager->moveShare($share, $recipient);
  3519. }
  3520. public function testMoveShareUserNotRecipient(): void {
  3521. $this->expectException(\InvalidArgumentException::class);
  3522. $this->expectExceptionMessage('Invalid share recipient');
  3523. $share = $this->manager->newShare();
  3524. $share->setShareType(IShare::TYPE_USER);
  3525. $share->setSharedWith('sharedWith');
  3526. $this->manager->moveShare($share, 'recipient');
  3527. }
  3528. public function testMoveShareUser(): void {
  3529. $share = $this->manager->newShare();
  3530. $share->setShareType(IShare::TYPE_USER)
  3531. ->setId('42')
  3532. ->setProviderId('foo');
  3533. $share->setSharedWith('recipient');
  3534. $this->defaultProvider->method('move')->with($share, 'recipient')->willReturnArgument(0);
  3535. $this->manager->moveShare($share, 'recipient');
  3536. $this->addToAssertionCount(1);
  3537. }
  3538. public function testMoveShareGroupNotRecipient(): void {
  3539. $this->expectException(\InvalidArgumentException::class);
  3540. $this->expectExceptionMessage('Invalid share recipient');
  3541. $share = $this->manager->newShare();
  3542. $share->setShareType(IShare::TYPE_GROUP);
  3543. $sharedWith = $this->createMock(IGroup::class);
  3544. $share->setSharedWith('shareWith');
  3545. $recipient = $this->createMock(IUser::class);
  3546. $sharedWith->method('inGroup')->with($recipient)->willReturn(false);
  3547. $this->groupManager->method('get')->with('shareWith')->willReturn($sharedWith);
  3548. $this->userManager->method('get')->with('recipient')->willReturn($recipient);
  3549. $this->manager->moveShare($share, 'recipient');
  3550. }
  3551. public function testMoveShareGroupNull(): void {
  3552. $this->expectException(\InvalidArgumentException::class);
  3553. $this->expectExceptionMessage('Group "shareWith" does not exist');
  3554. $share = $this->manager->newShare();
  3555. $share->setShareType(IShare::TYPE_GROUP);
  3556. $share->setSharedWith('shareWith');
  3557. $recipient = $this->createMock(IUser::class);
  3558. $this->groupManager->method('get')->with('shareWith')->willReturn(null);
  3559. $this->userManager->method('get')->with('recipient')->willReturn($recipient);
  3560. $this->manager->moveShare($share, 'recipient');
  3561. }
  3562. public function testMoveShareGroup(): void {
  3563. $share = $this->manager->newShare();
  3564. $share->setShareType(IShare::TYPE_GROUP)
  3565. ->setId('42')
  3566. ->setProviderId('foo');
  3567. $group = $this->createMock(IGroup::class);
  3568. $share->setSharedWith('group');
  3569. $recipient = $this->createMock(IUser::class);
  3570. $group->method('inGroup')->with($recipient)->willReturn(true);
  3571. $this->groupManager->method('get')->with('group')->willReturn($group);
  3572. $this->userManager->method('get')->with('recipient')->willReturn($recipient);
  3573. $this->defaultProvider->method('move')->with($share, 'recipient')->willReturnArgument(0);
  3574. $this->manager->moveShare($share, 'recipient');
  3575. $this->addToAssertionCount(1);
  3576. }
  3577. /**
  3578. * @dataProvider dataTestShareProviderExists
  3579. */
  3580. public function testShareProviderExists($shareType, $expected): void {
  3581. $factory = $this->getMockBuilder('OCP\Share\IProviderFactory')->getMock();
  3582. $factory->expects($this->any())->method('getProviderForType')
  3583. ->willReturnCallback(function ($id) {
  3584. if ($id === IShare::TYPE_USER) {
  3585. return true;
  3586. }
  3587. throw new Exception\ProviderException();
  3588. });
  3589. $manager = $this->createManager($factory);
  3590. $this->assertSame($expected,
  3591. $manager->shareProviderExists($shareType)
  3592. );
  3593. }
  3594. public function dataTestShareProviderExists() {
  3595. return [
  3596. [IShare::TYPE_USER, true],
  3597. [42, false],
  3598. ];
  3599. }
  3600. public function testGetSharesInFolder(): void {
  3601. $factory = new DummyFactory2($this->createMock(IServerContainer::class));
  3602. $manager = $this->createManager($factory);
  3603. $factory->setProvider($this->defaultProvider);
  3604. $extraProvider = $this->createMock(IShareProvider::class);
  3605. $factory->setSecondProvider($extraProvider);
  3606. $share1 = $this->createMock(IShare::class);
  3607. $share2 = $this->createMock(IShare::class);
  3608. $share3 = $this->createMock(IShare::class);
  3609. $share4 = $this->createMock(IShare::class);
  3610. $folder = $this->createMock(Folder::class);
  3611. $this->defaultProvider->method('getSharesInFolder')
  3612. ->with(
  3613. $this->equalTo('user'),
  3614. $this->equalTo($folder),
  3615. $this->equalTo(false)
  3616. )->willReturn([
  3617. 1 => [$share1],
  3618. 2 => [$share2],
  3619. ]);
  3620. $extraProvider->method('getSharesInFolder')
  3621. ->with(
  3622. $this->equalTo('user'),
  3623. $this->equalTo($folder),
  3624. $this->equalTo(false)
  3625. )->willReturn([
  3626. 2 => [$share3],
  3627. 3 => [$share4],
  3628. ]);
  3629. $result = $manager->getSharesInFolder('user', $folder, false);
  3630. $expects = [
  3631. 1 => [$share1],
  3632. 2 => [$share2, $share3],
  3633. 3 => [$share4],
  3634. ];
  3635. $this->assertSame($expects, $result);
  3636. }
  3637. public function testGetAccessList(): void {
  3638. $factory = new DummyFactory2($this->createMock(IServerContainer::class));
  3639. $manager = $this->createManager($factory);
  3640. $factory->setProvider($this->defaultProvider);
  3641. $extraProvider = $this->createMock(IShareProvider::class);
  3642. $factory->setSecondProvider($extraProvider);
  3643. $nodeOwner = $this->createMock(IUser::class);
  3644. $nodeOwner->expects($this->once())
  3645. ->method('getUID')
  3646. ->willReturn('user1');
  3647. $node = $this->createMock(Node::class);
  3648. $node->expects($this->once())
  3649. ->method('getOwner')
  3650. ->willReturn($nodeOwner);
  3651. $node->method('getId')
  3652. ->willReturn(42);
  3653. $userFolder = $this->createMock(Folder::class);
  3654. $file = $this->createMock(File::class);
  3655. $folder = $this->createMock(Folder::class);
  3656. $owner = $this->createMock(IUser::class);
  3657. $owner->expects($this->once())
  3658. ->method('getUID')
  3659. ->willReturn('owner');
  3660. $file->method('getParent')
  3661. ->willReturn($folder);
  3662. $file->method('getPath')
  3663. ->willReturn('/owner/files/folder/file');
  3664. $file->method('getOwner')
  3665. ->willReturn($owner);
  3666. $file->method('getId')
  3667. ->willReturn(23);
  3668. $folder->method('getParent')
  3669. ->willReturn($userFolder);
  3670. $folder->method('getPath')
  3671. ->willReturn('/owner/files/folder');
  3672. $userFolder->method('getFirstNodeById')
  3673. ->with($this->equalTo(42))
  3674. ->willReturn($file);
  3675. $userFolder->method('getPath')
  3676. ->willReturn('/user1/files');
  3677. $this->userManager->method('userExists')
  3678. ->with($this->equalTo('user1'))
  3679. ->willReturn(true);
  3680. $this->defaultProvider->method('getAccessList')
  3681. ->with(
  3682. $this->equalTo([$file, $folder]),
  3683. false
  3684. )
  3685. ->willReturn([
  3686. 'users' => [
  3687. 'user1',
  3688. 'user2',
  3689. 'user3',
  3690. '123456',
  3691. ],
  3692. 'public' => true,
  3693. ]);
  3694. $extraProvider->method('getAccessList')
  3695. ->with(
  3696. $this->equalTo([$file, $folder]),
  3697. false
  3698. )
  3699. ->willReturn([
  3700. 'users' => [
  3701. 'user3',
  3702. 'user4',
  3703. 'user5',
  3704. '234567',
  3705. ],
  3706. 'remote' => true,
  3707. ]);
  3708. $this->rootFolder->method('getUserFolder')
  3709. ->with($this->equalTo('user1'))
  3710. ->willReturn($userFolder);
  3711. $expected = [
  3712. 'users' => ['owner', 'user1', 'user2', 'user3', '123456','user4', 'user5', '234567'],
  3713. 'remote' => true,
  3714. 'public' => true,
  3715. ];
  3716. $result = $manager->getAccessList($node, true, false);
  3717. $this->assertSame($expected['public'], $result['public']);
  3718. $this->assertSame($expected['remote'], $result['remote']);
  3719. $this->assertSame($expected['users'], $result['users']);
  3720. }
  3721. public function testGetAccessListWithCurrentAccess(): void {
  3722. $factory = new DummyFactory2($this->createMock(IServerContainer::class));
  3723. $manager = $this->createManager($factory);
  3724. $factory->setProvider($this->defaultProvider);
  3725. $extraProvider = $this->createMock(IShareProvider::class);
  3726. $factory->setSecondProvider($extraProvider);
  3727. $nodeOwner = $this->createMock(IUser::class);
  3728. $nodeOwner->expects($this->once())
  3729. ->method('getUID')
  3730. ->willReturn('user1');
  3731. $node = $this->createMock(Node::class);
  3732. $node->expects($this->once())
  3733. ->method('getOwner')
  3734. ->willReturn($nodeOwner);
  3735. $node->method('getId')
  3736. ->willReturn(42);
  3737. $userFolder = $this->createMock(Folder::class);
  3738. $file = $this->createMock(File::class);
  3739. $owner = $this->createMock(IUser::class);
  3740. $owner->expects($this->once())
  3741. ->method('getUID')
  3742. ->willReturn('owner');
  3743. $folder = $this->createMock(Folder::class);
  3744. $file->method('getParent')
  3745. ->willReturn($folder);
  3746. $file->method('getPath')
  3747. ->willReturn('/owner/files/folder/file');
  3748. $file->method('getOwner')
  3749. ->willReturn($owner);
  3750. $file->method('getId')
  3751. ->willReturn(23);
  3752. $folder->method('getParent')
  3753. ->willReturn($userFolder);
  3754. $folder->method('getPath')
  3755. ->willReturn('/owner/files/folder');
  3756. $userFolder->method('getFirstNodeById')
  3757. ->with($this->equalTo(42))
  3758. ->willReturn($file);
  3759. $userFolder->method('getPath')
  3760. ->willReturn('/user1/files');
  3761. $this->userManager->method('userExists')
  3762. ->with($this->equalTo('user1'))
  3763. ->willReturn(true);
  3764. $this->defaultProvider->method('getAccessList')
  3765. ->with(
  3766. $this->equalTo([$file, $folder]),
  3767. true
  3768. )
  3769. ->willReturn([
  3770. 'users' => [
  3771. 'user1' => [],
  3772. 'user2' => [],
  3773. 'user3' => [],
  3774. '123456' => [],
  3775. ],
  3776. 'public' => true,
  3777. ]);
  3778. $extraProvider->method('getAccessList')
  3779. ->with(
  3780. $this->equalTo([$file, $folder]),
  3781. true
  3782. )
  3783. ->willReturn([
  3784. 'users' => [
  3785. 'user3' => [],
  3786. 'user4' => [],
  3787. 'user5' => [],
  3788. '234567' => [],
  3789. ],
  3790. 'remote' => [
  3791. 'remote1',
  3792. ],
  3793. ]);
  3794. $this->rootFolder->method('getUserFolder')
  3795. ->with($this->equalTo('user1'))
  3796. ->willReturn($userFolder);
  3797. $expected = [
  3798. 'users' => [
  3799. 'owner' => [
  3800. 'node_id' => 23,
  3801. 'node_path' => '/folder/file'
  3802. ]
  3803. , 'user1' => [], 'user2' => [], 'user3' => [], '123456' => [], 'user4' => [], 'user5' => [], '234567' => []],
  3804. 'remote' => [
  3805. 'remote1',
  3806. ],
  3807. 'public' => true,
  3808. ];
  3809. $result = $manager->getAccessList($node, true, true);
  3810. $this->assertSame($expected['public'], $result['public']);
  3811. $this->assertSame($expected['remote'], $result['remote']);
  3812. $this->assertSame($expected['users'], $result['users']);
  3813. }
  3814. public function testGetAllShares(): void {
  3815. $factory = new DummyFactory2($this->createMock(IServerContainer::class));
  3816. $manager = $this->createManager($factory);
  3817. $factory->setProvider($this->defaultProvider);
  3818. $extraProvider = $this->createMock(IShareProvider::class);
  3819. $factory->setSecondProvider($extraProvider);
  3820. $share1 = $this->createMock(IShare::class);
  3821. $share2 = $this->createMock(IShare::class);
  3822. $share3 = $this->createMock(IShare::class);
  3823. $share4 = $this->createMock(IShare::class);
  3824. $this->defaultProvider->method('getAllShares')
  3825. ->willReturnCallback(function () use ($share1, $share2) {
  3826. yield $share1;
  3827. yield $share2;
  3828. });
  3829. $extraProvider->method('getAllShares')
  3830. ->willReturnCallback(function () use ($share3, $share4) {
  3831. yield $share3;
  3832. yield $share4;
  3833. });
  3834. // "yield from", used in "getAllShares()", does not reset the keys, so
  3835. // "use_keys" has to be disabled to collect all the values while
  3836. // ignoring the keys returned by the generator.
  3837. $result = iterator_to_array($manager->getAllShares(), $use_keys = false);
  3838. $expects = [$share1, $share2, $share3, $share4];
  3839. $this->assertSame($expects, $result);
  3840. }
  3841. public function dataCurrentUserCanEnumerateTargetUser(): array {
  3842. return [
  3843. 'Full match guest' => [true, true, false, false, false, false, false, true],
  3844. 'Full match user' => [false, true, false, false, false, false, false, true],
  3845. 'Enumeration off guest' => [true, false, false, false, false, false, false, false],
  3846. 'Enumeration off user' => [false, false, false, false, false, false, false, false],
  3847. 'Enumeration guest' => [true, false, true, false, false, false, false, true],
  3848. 'Enumeration user' => [false, false, true, false, false, false, false, true],
  3849. // Restricted enumerations guests never works
  3850. 'Guest phone' => [true, false, true, true, false, false, false, false],
  3851. 'Guest group' => [true, false, true, false, true, false, false, false],
  3852. 'Guest both' => [true, false, true, true, true, false, false, false],
  3853. // Restricted enumerations users
  3854. 'User phone but not known' => [false, false, true, true, false, false, false, false],
  3855. 'User phone known' => [false, false, true, true, false, true, false, true],
  3856. 'User group but no match' => [false, false, true, false, true, false, false, false],
  3857. 'User group with match' => [false, false, true, false, true, false, true, true],
  3858. ];
  3859. }
  3860. /**
  3861. * @dataProvider dataCurrentUserCanEnumerateTargetUser
  3862. * @param bool $expected
  3863. */
  3864. public function testCurrentUserCanEnumerateTargetUser(bool $currentUserIsGuest, bool $allowEnumerationFullMatch, bool $allowEnumeration, bool $limitEnumerationToPhone, bool $limitEnumerationToGroups, bool $isKnownToUser, bool $haveCommonGroup, bool $expected): void {
  3865. /** @var IManager|MockObject $manager */
  3866. $manager = $this->createManagerMock()
  3867. ->setMethods([
  3868. 'allowEnumerationFullMatch',
  3869. 'allowEnumeration',
  3870. 'limitEnumerationToPhone',
  3871. 'limitEnumerationToGroups',
  3872. ])
  3873. ->getMock();
  3874. $manager->method('allowEnumerationFullMatch')
  3875. ->willReturn($allowEnumerationFullMatch);
  3876. $manager->method('allowEnumeration')
  3877. ->willReturn($allowEnumeration);
  3878. $manager->method('limitEnumerationToPhone')
  3879. ->willReturn($limitEnumerationToPhone);
  3880. $manager->method('limitEnumerationToGroups')
  3881. ->willReturn($limitEnumerationToGroups);
  3882. $this->knownUserService->method('isKnownToUser')
  3883. ->with('current', 'target')
  3884. ->willReturn($isKnownToUser);
  3885. $currentUser = null;
  3886. if (!$currentUserIsGuest) {
  3887. $currentUser = $this->createMock(IUser::class);
  3888. $currentUser->method('getUID')
  3889. ->willReturn('current');
  3890. }
  3891. $targetUser = $this->createMock(IUser::class);
  3892. $targetUser->method('getUID')
  3893. ->willReturn('target');
  3894. if ($haveCommonGroup) {
  3895. $this->groupManager->method('getUserGroupIds')
  3896. ->willReturnMap([
  3897. [$targetUser, ['gid1', 'gid2']],
  3898. [$currentUser, ['gid2', 'gid3']],
  3899. ]);
  3900. } else {
  3901. $this->groupManager->method('getUserGroupIds')
  3902. ->willReturnMap([
  3903. [$targetUser, ['gid1', 'gid2']],
  3904. [$currentUser, ['gid3', 'gid4']],
  3905. ]);
  3906. }
  3907. $this->assertSame($expected, $manager->currentUserCanEnumerateTargetUser($currentUser, $targetUser));
  3908. }
  3909. }
  3910. class DummyFactory implements IProviderFactory {
  3911. /** @var IShareProvider */
  3912. protected $provider;
  3913. public function __construct(\OCP\IServerContainer $serverContainer) {
  3914. }
  3915. /**
  3916. * @param IShareProvider $provider
  3917. */
  3918. public function setProvider($provider) {
  3919. $this->provider = $provider;
  3920. }
  3921. /**
  3922. * @param string $id
  3923. * @return IShareProvider
  3924. */
  3925. public function getProvider($id) {
  3926. return $this->provider;
  3927. }
  3928. /**
  3929. * @param int $shareType
  3930. * @return IShareProvider
  3931. */
  3932. public function getProviderForType($shareType) {
  3933. return $this->provider;
  3934. }
  3935. /**
  3936. * @return IShareProvider[]
  3937. */
  3938. public function getAllProviders() {
  3939. return [$this->provider];
  3940. }
  3941. public function registerProvider(string $shareProvier): void {
  3942. }
  3943. }
  3944. class DummyFactory2 extends DummyFactory {
  3945. /** @var IShareProvider */
  3946. private $provider2;
  3947. /**
  3948. * @param IShareProvider $provider
  3949. */
  3950. public function setSecondProvider($provider) {
  3951. $this->provider2 = $provider;
  3952. }
  3953. public function getAllProviders() {
  3954. return [$this->provider, $this->provider2];
  3955. }
  3956. public function registerProvider(string $shareProvier): void {
  3957. }
  3958. }