1
0

ManagerTest.php 150 KB

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