ManagerTest.php 152 KB

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