ManagerTest.php 143 KB

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