ManagerTest.php 150 KB

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