ManagerTest.php 147 KB

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