You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ManagerTest.php 145KB

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