ManagerTest.php 152 KB

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