ManagerTest.php 149 KB

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