api.php 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <icewind@owncloud.com>
  7. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. * @author Vincent Petry <pvince81@owncloud.com>
  11. *
  12. * @copyright Copyright (c) 2015, ownCloud, Inc.
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. use OCA\Files\Share;
  29. use OCA\Files_sharing\Tests\TestCase;
  30. /**
  31. * Class Test_Files_Sharing_Api
  32. */
  33. class Test_Files_Sharing_Api extends TestCase {
  34. const TEST_FOLDER_NAME = '/folder_share_api_test';
  35. private static $tempStorage;
  36. protected function setUp() {
  37. parent::setUp();
  38. \OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups', 'no');
  39. \OC::$server->getAppConfig()->setValue('core', 'shareapi_expire_after_n_days', '7');
  40. $this->folder = self::TEST_FOLDER_NAME;
  41. $this->subfolder = '/subfolder_share_api_test';
  42. $this->subsubfolder = '/subsubfolder_share_api_test';
  43. $this->filename = '/share-api-test.txt';
  44. // save file with content
  45. $this->view->file_put_contents($this->filename, $this->data);
  46. $this->view->mkdir($this->folder);
  47. $this->view->mkdir($this->folder . $this->subfolder);
  48. $this->view->mkdir($this->folder . $this->subfolder . $this->subsubfolder);
  49. $this->view->file_put_contents($this->folder.$this->filename, $this->data);
  50. $this->view->file_put_contents($this->folder . $this->subfolder . $this->filename, $this->data);
  51. }
  52. protected function tearDown() {
  53. if($this->view instanceof \OC\Files\View) {
  54. $this->view->unlink($this->filename);
  55. $this->view->deleteAll($this->folder);
  56. }
  57. self::$tempStorage = null;
  58. parent::tearDown();
  59. }
  60. /**
  61. * @medium
  62. */
  63. function testCreateShareUserFile() {
  64. // simulate a post request
  65. $_POST['path'] = $this->filename;
  66. $_POST['shareWith'] = \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2;
  67. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_USER;
  68. $result = \OCA\Files_Sharing\API\Local::createShare([]);
  69. $this->assertTrue($result->succeeded());
  70. $data = $result->getData();
  71. $this->assertEquals(23, $data['permissions']);
  72. $this->assertEmpty($data['expiration']);
  73. $share = $this->getShareFromId($data['id']);
  74. $items = \OCP\Share::getItemShared('file', $share['item_source']);
  75. $this->assertTrue(!empty($items));
  76. $fileinfo = $this->view->getFileInfo($this->filename);
  77. \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  78. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  79. }
  80. function testCreateShareUserFolder() {
  81. // simulate a post request
  82. $_POST['path'] = $this->folder;
  83. $_POST['shareWith'] = \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2;
  84. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_USER;
  85. $result = \OCA\Files_Sharing\API\Local::createShare([]);
  86. $this->assertTrue($result->succeeded());
  87. $data = $result->getData();
  88. $this->assertEquals(31, $data['permissions']);
  89. $this->assertEmpty($data['expiration']);
  90. $share = $this->getShareFromId($data['id']);
  91. $items = \OCP\Share::getItemShared('file', $share['item_source']);
  92. $this->assertTrue(!empty($items));
  93. $fileinfo = $this->view->getFileInfo($this->folder);
  94. \OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  95. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  96. }
  97. function testCreateShareGroupFile() {
  98. // simulate a post request
  99. $_POST['path'] = $this->filename;
  100. $_POST['shareWith'] = \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_GROUP1;
  101. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_GROUP;
  102. $result = \OCA\Files_Sharing\API\Local::createShare([]);
  103. $this->assertTrue($result->succeeded());
  104. $data = $result->getData();
  105. $this->assertEquals(23, $data['permissions']);
  106. $this->assertEmpty($data['expiration']);
  107. $share = $this->getShareFromId($data['id']);
  108. $items = \OCP\Share::getItemShared('file', $share['item_source']);
  109. $this->assertTrue(!empty($items));
  110. $fileinfo = $this->view->getFileInfo($this->filename);
  111. \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP,
  112. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_GROUP1);
  113. }
  114. function testCreateShareGroupFolder() {
  115. // simulate a post request
  116. $_POST['path'] = $this->folder;
  117. $_POST['shareWith'] = \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_GROUP1;
  118. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_GROUP;
  119. $result = \OCA\Files_Sharing\API\Local::createShare([]);
  120. $this->assertTrue($result->succeeded());
  121. $data = $result->getData();
  122. $this->assertEquals(31, $data['permissions']);
  123. $this->assertEmpty($data['expiration']);
  124. $share = $this->getShareFromId($data['id']);
  125. $items = \OCP\Share::getItemShared('file', $share['item_source']);
  126. $this->assertTrue(!empty($items));
  127. $fileinfo = $this->view->getFileInfo($this->folder);
  128. \OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP,
  129. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_GROUP1);
  130. }
  131. public function testCreateShareLink() {
  132. // simulate a post request
  133. $_POST['path'] = $this->folder;
  134. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
  135. $result = \OCA\Files_Sharing\API\Local::createShare([]);
  136. // check if API call was successful
  137. $this->assertTrue($result->succeeded());
  138. $data = $result->getData();
  139. $this->assertEquals(1, $data['permissions']);
  140. $this->assertEmpty($data['expiration']);
  141. $this->assertTrue(is_string($data['token']));
  142. // check for correct link
  143. $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
  144. $this->assertEquals($url, $data['url']);
  145. $share = $this->getShareFromId($data['id']);
  146. $items = \OCP\Share::getItemShared('file', $share['item_source']);
  147. $this->assertTrue(!empty($items));
  148. $fileinfo = $this->view->getFileInfo($this->folder);
  149. \OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  150. }
  151. public function testCreateShareLinkPublicUpload() {
  152. // simulate a post request
  153. $_POST['path'] = $this->folder;
  154. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
  155. $_POST['publicUpload'] = 'true';
  156. $result = \OCA\Files_Sharing\API\Local::createShare(array());
  157. // check if API call was successful
  158. $this->assertTrue($result->succeeded());
  159. $data = $result->getData();
  160. $this->assertEquals(7, $data['permissions']);
  161. $this->assertEmpty($data['expiration']);
  162. $this->assertTrue(is_string($data['token']));
  163. // check for correct link
  164. $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
  165. $this->assertEquals($url, $data['url']);
  166. $share = $this->getShareFromId($data['id']);
  167. $items = \OCP\Share::getItemShared('file', $share['item_source']);
  168. $this->assertTrue(!empty($items));
  169. $fileinfo = $this->view->getFileInfo($this->folder);
  170. \OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  171. }
  172. /**
  173. * @medium
  174. */
  175. public function testCreateShareInvalidPermissions() {
  176. // simulate a post request
  177. $_POST['path'] = $this->filename;
  178. $_POST['shareWith'] = \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2;
  179. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_USER;
  180. $_POST['permissions'] = \OCP\Constants::PERMISSION_SHARE;
  181. $result = \OCA\Files_Sharing\API\Local::createShare([]);
  182. // share was successful?
  183. $this->assertFalse($result->succeeded());
  184. $this->assertEquals(400, $result->getStatusCode());
  185. $shares = \OCP\Share::getItemShared('file', null);
  186. $this->assertCount(0, $shares);
  187. $fileinfo = $this->view->getFileInfo($this->filename);
  188. \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  189. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  190. }
  191. function testEnfoceLinkPassword() {
  192. $appConfig = \OC::$server->getAppConfig();
  193. $appConfig->setValue('core', 'shareapi_enforce_links_password', 'yes');
  194. // don't allow to share link without a password
  195. $_POST['path'] = $this->folder;
  196. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
  197. $result = \OCA\Files_Sharing\API\Local::createShare(array());
  198. $this->assertFalse($result->succeeded());
  199. // don't allow to share link without a empty password
  200. $_POST['path'] = $this->folder;
  201. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
  202. $_POST['password'] = '';
  203. $result = \OCA\Files_Sharing\API\Local::createShare(array());
  204. $this->assertFalse($result->succeeded());
  205. // share with password should succeed
  206. $_POST['path'] = $this->folder;
  207. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
  208. $_POST['password'] = 'foo';
  209. $result = \OCA\Files_Sharing\API\Local::createShare(array());
  210. $this->assertTrue($result->succeeded());
  211. $data = $result->getData();
  212. // setting new password should succeed
  213. $params = array();
  214. $params['id'] = $data['id'];
  215. $params['_put'] = array();
  216. $params['_put']['password'] = 'bar';
  217. $result = \OCA\Files_Sharing\API\Local::updateShare($params);
  218. $this->assertTrue($result->succeeded());
  219. // removing password should fail
  220. $params = array();
  221. $params['id'] = $data['id'];
  222. $params['_put'] = array();
  223. $params['_put']['password'] = '';
  224. $result = \OCA\Files_Sharing\API\Local::updateShare($params);
  225. $this->assertFalse($result->succeeded());
  226. // cleanup
  227. $fileinfo = $this->view->getFileInfo($this->folder);
  228. \OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  229. $appConfig->setValue('core', 'shareapi_enforce_links_password', 'no');
  230. }
  231. /**
  232. * @medium
  233. */
  234. function testSharePermissions() {
  235. // sharing file to a user should work if shareapi_exclude_groups is set
  236. // to no
  237. \OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups', 'no');
  238. $_POST['path'] = $this->filename;
  239. $_POST['shareWith'] = \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2;
  240. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_USER;
  241. $result = \OCA\Files_Sharing\API\Local::createShare(array());
  242. $this->assertTrue($result->succeeded());
  243. $data = $result->getData();
  244. $share = $this->getShareFromId($data['id']);
  245. $items = \OCP\Share::getItemShared('file', $share['item_source']);
  246. $this->assertTrue(!empty($items));
  247. $fileinfo = $this->view->getFileInfo($this->filename);
  248. $result = \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  249. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  250. $this->assertTrue($result);
  251. // exclude groups, but not the group the user belongs to. Sharing should still work
  252. \OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups', 'yes');
  253. \OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups_list', 'admin,group1,group2');
  254. $_POST['path'] = $this->filename;
  255. $_POST['shareWith'] = \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2;
  256. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_USER;
  257. $result = \OCA\Files_Sharing\API\Local::createShare(array());
  258. $this->assertTrue($result->succeeded());
  259. $data = $result->getData();
  260. $share = $this->getShareFromId($data['id']);
  261. $items = \OCP\Share::getItemShared('file', $share['item_source']);
  262. $this->assertTrue(!empty($items));
  263. $fileinfo = $this->view->getFileInfo($this->filename);
  264. $result = \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  265. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  266. $this->assertTrue($result);
  267. // now we exclude the group the user belongs to ('group'), sharing should fail now
  268. \OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups_list', 'admin,group');
  269. $_POST['path'] = $this->filename;
  270. $_POST['shareWith'] = \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2;
  271. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_USER;
  272. $result = \OCA\Files_Sharing\API\Local::createShare(array());
  273. $this->assertFalse($result->succeeded());
  274. // cleanup
  275. \OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups', 'no');
  276. \OC::$server->getAppConfig()->setValue('core', 'shareapi_exclude_groups_list', '');
  277. }
  278. /**
  279. * @medium
  280. * @depends testCreateShareUserFile
  281. */
  282. function testGetAllShares() {
  283. $fileinfo = $this->view->getFileInfo($this->filename);
  284. \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  285. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  286. $result = \OCA\Files_Sharing\API\Local::getAllShares(array());
  287. $this->assertTrue($result->succeeded());
  288. // test should return two shares created from testCreateShare()
  289. $this->assertTrue(count($result->getData()) === 1);
  290. \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  291. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  292. }
  293. function testGetAllSharesWithMe() {
  294. $fileinfo1 = $this->view->getFileInfo($this->filename);
  295. $fileinfo2 = $this->view->getFileInfo($this->folder.$this->filename);
  296. \OCP\Share::shareItem('file', $fileinfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  297. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  298. \OCP\Share::shareItem('folder', $fileinfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  299. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  300. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  301. $_GET['shared_with_me'] = 1;
  302. $result = \OCA\Files_Sharing\API\Local::getAllShares(array());
  303. $this->assertTrue($result->succeeded());
  304. $this->assertTrue(count($result->getData()) === 2);
  305. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  306. \OCP\Share::unshare('file', $fileinfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  307. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  308. \OCP\Share::unshare('folder', $fileinfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  309. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  310. }
  311. /**
  312. * @medium
  313. * @depends testCreateShareLink
  314. */
  315. function testPublicLinkUrl() {
  316. // simulate a post request
  317. $_POST['path'] = $this->folder;
  318. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
  319. $result = \OCA\Files_Sharing\API\Local::createShare([]);
  320. $this->assertTrue($result->succeeded());
  321. $data = $result->getData();
  322. // check if we have a token
  323. $this->assertTrue(is_string($data['token']));
  324. $id = $data['id'];
  325. // check for correct link
  326. $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
  327. $this->assertEquals($url, $data['url']);
  328. // check for link in getall shares
  329. $result = \OCA\Files_Sharing\API\Local::getAllShares([]);
  330. $this->assertTrue($result->succeeded());
  331. $data = $result->getData();
  332. $this->assertEquals($url, current($data)['url']);
  333. // check for path
  334. $_GET['path'] = $this->folder;
  335. $result = \OCA\Files_Sharing\API\Local::getAllShares([]);
  336. $this->assertTrue($result->succeeded());
  337. $data = $result->getData();
  338. $this->assertEquals($url, current($data)['url']);
  339. // check in share id
  340. $result = \OCA\Files_Sharing\API\Local::getShare(['id' => $id]);
  341. $this->assertTrue($result->succeeded());
  342. $data = $result->getData();
  343. $this->assertEquals($url, current($data)['url']);
  344. //Clean up share
  345. $fileinfo = $this->view->getFileInfo($this->folder);
  346. \OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  347. }
  348. /**
  349. * @medium
  350. * @depends testCreateShareUserFile
  351. * @depends testCreateShareLink
  352. */
  353. function testGetShareFromSource() {
  354. $fileInfo = $this->view->getFileInfo($this->filename);
  355. \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  356. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  357. \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK,
  358. null, 1);
  359. $_GET['path'] = $this->filename;
  360. $result = \OCA\Files_Sharing\API\Local::getAllShares(array());
  361. $this->assertTrue($result->succeeded());
  362. // test should return one share created from testCreateShare()
  363. $this->assertTrue(count($result->getData()) === 2);
  364. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  365. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  366. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  367. }
  368. /**
  369. * @medium
  370. * @depends testCreateShareUserFile
  371. * @depends testCreateShareLink
  372. */
  373. function testGetShareFromSourceWithReshares() {
  374. $fileInfo = $this->view->getFileInfo($this->filename);
  375. // share the file as user1 to user2
  376. \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  377. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  378. // login as user2 and reshare the file to user3
  379. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  380. \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  381. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3, 31);
  382. // login as user1 again
  383. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  384. $_GET['path'] = $this->filename;
  385. $result = \OCA\Files_Sharing\API\Local::getAllShares(array());
  386. $this->assertTrue($result->succeeded());
  387. // test should return one share
  388. $this->assertTrue(count($result->getData()) === 1);
  389. // now also ask for the reshares
  390. $_GET['reshares'] = 'true';
  391. $result = \OCA\Files_Sharing\API\Local::getAllShares(array());
  392. $this->assertTrue($result->succeeded());
  393. // now we should get two shares, the initial share and the reshare
  394. $this->assertTrue(count($result->getData()) === 2);
  395. // unshare files again
  396. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  397. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  398. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3);
  399. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  400. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  401. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  402. }
  403. /**
  404. * @medium
  405. * @depends testCreateShareUserFile
  406. */
  407. function testGetShareFromId() {
  408. $fileInfo = $this->view->getFileInfo($this->filename);
  409. $result = \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  410. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  411. // share was successful?
  412. $this->assertTrue($result);
  413. // get item to determine share ID
  414. $result = \OCP\Share::getItemShared('file', $fileInfo['fileid']);
  415. $this->assertEquals(1, count($result));
  416. // get first element
  417. $share = reset($result);
  418. // call getShare() with share ID
  419. $params = array('id' => $share['id']);
  420. $result = \OCA\Files_Sharing\API\Local::getShare($params);
  421. $this->assertTrue($result->succeeded());
  422. // test should return one share created from testCreateShare()
  423. $this->assertEquals(1, count($result->getData()));
  424. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  425. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  426. }
  427. /**
  428. * @medium
  429. */
  430. function testGetShareFromFolder() {
  431. $fileInfo1 = $this->view->getFileInfo($this->filename);
  432. $fileInfo2 = $this->view->getFileInfo($this->folder.'/'.$this->filename);
  433. $result = \OCP\Share::shareItem('file', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  434. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  435. // share was successful?
  436. $this->assertTrue($result);
  437. $result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK,
  438. null, 1);
  439. // share was successful?
  440. $this->assertTrue(is_string($result));
  441. $_GET['path'] = $this->folder;
  442. $_GET['subfiles'] = 'true';
  443. $result = \OCA\Files_Sharing\API\Local::getAllShares(array());
  444. $this->assertTrue($result->succeeded());
  445. // test should return one share within $this->folder
  446. $this->assertTrue(count($result->getData()) === 1);
  447. \OCP\Share::unshare('file', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  448. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  449. \OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  450. }
  451. function testGetShareFromFolderWithFile() {
  452. $fileInfo1 = $this->view->getFileInfo($this->filename);
  453. $result = \OCP\Share::shareItem('file', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  454. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  455. // share was successful?
  456. $this->assertTrue($result);
  457. $_GET = [
  458. 'path' => $this->filename,
  459. 'subfiles' => 1
  460. ];
  461. $result = \OCA\Files_Sharing\API\Local::getAllShares([]);
  462. $this->assertFalse($result->succeeded());
  463. $this->assertEquals(400, $result->getStatusCode());
  464. $this->assertEquals('not a directory', $result->getMeta()['message']);
  465. \OCP\Share::unshare('file', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  466. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  467. }
  468. /**
  469. * share a folder, than reshare a file within the shared folder and check if we construct the correct path
  470. * @medium
  471. */
  472. function testGetShareFromFolderReshares() {
  473. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  474. $fileInfo1 = $this->view->getFileInfo($this->folder);
  475. $fileInfo2 = $this->view->getFileInfo($this->folder.'/'.$this->filename);
  476. $fileInfo3 = $this->view->getFileInfo($this->folder.'/' . $this->subfolder . '/' .$this->filename);
  477. // share root folder to user2
  478. $result = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  479. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  480. // share was successful?
  481. $this->assertTrue($result);
  482. // login as user2
  483. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  484. // share file in root folder
  485. $result = \OCP\Share::shareItem('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  486. // share was successful?
  487. $this->assertTrue(is_string($result));
  488. // share file in subfolder
  489. $result = \OCP\Share::shareItem('file', $fileInfo3['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  490. // share was successful?
  491. $this->assertTrue(is_string($result));
  492. $testValues=array(
  493. array('query' => $this->folder,
  494. 'expectedResult' => $this->folder . $this->filename),
  495. array('query' => $this->folder . $this->subfolder,
  496. 'expectedResult' => $this->folder . $this->subfolder . $this->filename),
  497. );
  498. foreach ($testValues as $value) {
  499. $_GET['path'] = $value['query'];
  500. $_GET['subfiles'] = 'true';
  501. $result = \OCA\Files_Sharing\API\Local::getAllShares(array());
  502. $this->assertTrue($result->succeeded());
  503. // test should return one share within $this->folder
  504. $data = $result->getData();
  505. $this->assertEquals($value['expectedResult'], $data[0]['path']);
  506. }
  507. // cleanup
  508. \OCP\Share::unshare('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  509. \OCP\Share::unshare('file', $fileInfo3['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  510. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  511. \OCP\Share::unshare('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  512. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  513. }
  514. /**
  515. * reshare a sub folder and check if we get the correct path
  516. * @medium
  517. */
  518. function testGetShareFromSubFolderReShares() {
  519. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  520. $fileInfo = $this->view->getFileInfo($this->folder . $this->subfolder);
  521. // share sub-folder to user2
  522. $result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  523. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  524. // share was successful?
  525. $this->assertTrue($result);
  526. // login as user2
  527. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  528. // reshare subfolder
  529. $result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  530. // share was successful?
  531. $this->assertTrue(is_string($result));
  532. $_GET['path'] = '/';
  533. $_GET['subfiles'] = 'true';
  534. $result = \OCA\Files_Sharing\API\Local::getAllShares(array());
  535. $this->assertTrue($result->succeeded());
  536. // test should return one share within $this->folder
  537. $data = $result->getData();
  538. // we should get exactly one result
  539. $this->assertEquals(1, count($data));
  540. $expectedPath = $this->subfolder;
  541. $this->assertEquals($expectedPath, $data[0]['path']);
  542. // cleanup
  543. $result = \OCP\Share::unshare('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  544. $this->assertTrue($result);
  545. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  546. $result = \OCP\Share::unshare('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  547. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  548. $this->assertTrue($result);
  549. }
  550. /**
  551. * test re-re-share of folder if the path gets constructed correctly
  552. * @medium
  553. */
  554. function testGetShareFromFolderReReShares() {
  555. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  556. $fileInfo1 = $this->view->getFileInfo($this->folder . $this->subfolder);
  557. $fileInfo2 = $this->view->getFileInfo($this->folder . $this->subfolder . $this->subsubfolder);
  558. // share sub-folder to user2
  559. $result = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  560. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  561. // share was successful?
  562. $this->assertTrue($result);
  563. // login as user2
  564. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  565. // reshare subsubfolder
  566. $result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  567. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3, 31);
  568. // share was successful?
  569. $this->assertTrue($result);
  570. // login as user3
  571. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  572. $result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  573. // share was successful?
  574. $this->assertTrue(is_string($result));
  575. $_GET['path'] = '/';
  576. $_GET['subfiles'] = 'true';
  577. $result = \OCA\Files_Sharing\API\Local::getAllShares(array());
  578. $this->assertTrue($result->succeeded());
  579. // test should return one share within $this->folder
  580. $data = $result->getData();
  581. // we should get exactly one result
  582. $this->assertEquals(1, count($data));
  583. $expectedPath = $this->subsubfolder;
  584. $this->assertEquals($expectedPath, $data[0]['path']);
  585. // cleanup
  586. $result = \OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  587. $this->assertTrue($result);
  588. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  589. $result = \OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  590. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3);
  591. $this->assertTrue($result);
  592. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  593. $result = \OCP\Share::unshare('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  594. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  595. $this->assertTrue($result);
  596. }
  597. /**
  598. * test multiple shared folder if the path gets constructed correctly
  599. * @medium
  600. */
  601. function testGetShareMultipleSharedFolder() {
  602. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  603. $fileInfo1 = $this->view->getFileInfo($this->folder);
  604. $fileInfo2 = $this->view->getFileInfo($this->folder . $this->subfolder);
  605. // share sub-folder to user2
  606. $result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  607. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  608. // share was successful?
  609. $this->assertTrue($result);
  610. // share folder to user2
  611. $result = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  612. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  613. // share was successful?
  614. $this->assertTrue($result);
  615. // login as user2
  616. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  617. $result = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  618. // share was successful?
  619. $this->assertTrue(is_string($result));
  620. // ask for subfolder
  621. $expectedPath1 = $this->subfolder;
  622. $_GET['path'] = $expectedPath1;
  623. $result1 = \OCA\Files_Sharing\API\Local::getAllShares(array());
  624. $this->assertTrue($result1->succeeded());
  625. // test should return one share within $this->folder
  626. $data1 = $result1->getData();
  627. $share1 = reset($data1);
  628. // ask for folder/subfolder
  629. $expectedPath2 = $this->folder . $this->subfolder;
  630. $_GET['path'] = $expectedPath2;
  631. $result2 = \OCA\Files_Sharing\API\Local::getAllShares(array());
  632. $this->assertTrue($result2->succeeded());
  633. // test should return one share within $this->folder
  634. $data2 = $result2->getData();
  635. $share2 = reset($data2);
  636. // validate results
  637. // we should get exactly one result each time
  638. $this->assertEquals(1, count($data1));
  639. $this->assertEquals(1, count($data2));
  640. $this->assertEquals($expectedPath1, $share1['path']);
  641. $this->assertEquals($expectedPath2, $share2['path']);
  642. // cleanup
  643. $result = \OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  644. $this->assertTrue($result);
  645. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  646. $result = \OCP\Share::unshare('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  647. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  648. $this->assertTrue($result);
  649. $result = \OCP\Share::unshare('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  650. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  651. $this->assertTrue($result);
  652. }
  653. /**
  654. * test re-re-share of folder if the path gets constructed correctly
  655. * @medium
  656. */
  657. function testGetShareFromFileReReShares() {
  658. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  659. $fileInfo1 = $this->view->getFileInfo($this->folder . $this->subfolder);
  660. $fileInfo2 = $this->view->getFileInfo($this->folder. $this->subfolder . $this->filename);
  661. // share sub-folder to user2
  662. $result = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  663. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  664. // share was successful?
  665. $this->assertTrue($result);
  666. // login as user2
  667. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  668. // reshare subsubfolder
  669. $result = \OCP\Share::shareItem('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  670. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3, 31);
  671. // share was successful?
  672. $this->assertTrue($result);
  673. // login as user3
  674. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  675. $result = \OCP\Share::shareItem('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  676. // share was successful?
  677. $this->assertTrue(is_string($result));
  678. $_GET['path'] = '/';
  679. $_GET['subfiles'] = 'true';
  680. $result = \OCA\Files_Sharing\API\Local::getAllShares(array());
  681. $this->assertTrue($result->succeeded());
  682. // test should return one share within $this->folder
  683. $data = $result->getData();
  684. // we should get exactly one result
  685. $this->assertEquals(1, count($data));
  686. $expectedPath = $this->filename;
  687. $this->assertEquals($expectedPath, $data[0]['path']);
  688. // cleanup
  689. $result = \OCP\Share::unshare('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  690. $this->assertTrue($result);
  691. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  692. $result = \OCP\Share::unshare('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  693. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3);
  694. $this->assertTrue($result);
  695. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  696. $result = \OCP\Share::unshare('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  697. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  698. $this->assertTrue($result);
  699. }
  700. /**
  701. * @medium
  702. */
  703. function testGetShareFromUnknownId() {
  704. $params = array('id' => 0);
  705. $result = \OCA\Files_Sharing\API\Local::getShare($params);
  706. $this->assertEquals(404, $result->getStatusCode());
  707. $meta = $result->getMeta();
  708. $this->assertEquals('share doesn\'t exist', $meta['message']);
  709. }
  710. /**
  711. * @medium
  712. * @depends testCreateShareUserFile
  713. * @depends testCreateShareLink
  714. */
  715. function testUpdateShare() {
  716. $fileInfo = $this->view->getFileInfo($this->filename);
  717. $result = \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  718. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, \OCP\Constants::PERMISSION_ALL);
  719. // share was successful?
  720. $this->assertTrue($result);
  721. $result = \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK,
  722. null, 1);
  723. // share was successful?
  724. $this->assertTrue(is_string($result));
  725. $items = \OCP\Share::getItemShared('file', null);
  726. // make sure that we found a link share and a user share
  727. $this->assertEquals(count($items), 2);
  728. $linkShare = null;
  729. $userShare = null;
  730. foreach ($items as $item) {
  731. if ($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) {
  732. $linkShare = $item;
  733. }
  734. if ($item['share_type'] === \OCP\Share::SHARE_TYPE_USER) {
  735. $userShare = $item;
  736. }
  737. }
  738. // make sure that we found a link share and a user share
  739. $this->assertTrue(is_array($linkShare));
  740. $this->assertTrue(is_array($userShare));
  741. // check if share have expected permissions, single shared files never have
  742. // delete permissions
  743. $this->assertEquals(\OCP\Constants::PERMISSION_ALL & ~\OCP\Constants::PERMISSION_DELETE, $userShare['permissions']);
  744. // update permissions
  745. $params = array();
  746. $params['id'] = $userShare['id'];
  747. $params['_put'] = array();
  748. $params['_put']['permissions'] = 1;
  749. $result = \OCA\Files_Sharing\API\Local::updateShare($params);
  750. $meta = $result->getMeta();
  751. $this->assertTrue($result->succeeded(), $meta['message']);
  752. $items = \OCP\Share::getItemShared('file', $userShare['file_source']);
  753. $newUserShare = null;
  754. foreach ($items as $item) {
  755. if ($item['share_with'] === \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2) {
  756. $newUserShare = $item;
  757. break;
  758. }
  759. }
  760. $this->assertTrue(is_array($newUserShare));
  761. $this->assertEquals('1', $newUserShare['permissions']);
  762. // update password for link share
  763. $this->assertTrue(empty($linkShare['share_with']));
  764. $params = array();
  765. $params['id'] = $linkShare['id'];
  766. $params['_put'] = array();
  767. $params['_put']['password'] = 'foo';
  768. $result = \OCA\Files_Sharing\API\Local::updateShare($params);
  769. $this->assertTrue($result->succeeded());
  770. $items = \OCP\Share::getItemShared('file', $linkShare['file_source']);
  771. $newLinkShare = null;
  772. foreach ($items as $item) {
  773. if ($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) {
  774. $newLinkShare = $item;
  775. break;
  776. }
  777. }
  778. $this->assertTrue(is_array($newLinkShare));
  779. $this->assertTrue(!empty($newLinkShare['share_with']));
  780. // Remove password for link share
  781. $params = array();
  782. $params['id'] = $linkShare['id'];
  783. $params['_put'] = array();
  784. $params['_put']['password'] = '';
  785. $result = \OCA\Files_Sharing\API\Local::updateShare($params);
  786. $this->assertTrue($result->succeeded());
  787. $items = \OCP\Share::getItemShared('file', $linkShare['file_source']);
  788. $newLinkShare = null;
  789. foreach ($items as $item) {
  790. if ($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) {
  791. $newLinkShare = $item;
  792. break;
  793. }
  794. }
  795. $this->assertTrue(is_array($newLinkShare));
  796. $this->assertTrue(empty($newLinkShare['share_with']));
  797. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  798. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  799. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  800. }
  801. /**
  802. * @medium
  803. * @depends testCreateShareUserFile
  804. */
  805. public function testUpdateShareInvalidPermissions() {
  806. $fileInfo = $this->view->getFileInfo($this->filename);
  807. $result = \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  808. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, \OCP\Constants::PERMISSION_ALL);
  809. // share was successful?
  810. $this->assertTrue($result);
  811. $share = \OCP\Share::getItemShared('file', null);
  812. $this->assertCount(1, $share);
  813. $share = reset($share);
  814. // check if share have expected permissions, single shared files never have
  815. // delete permissions
  816. $this->assertEquals(\OCP\Constants::PERMISSION_ALL & ~\OCP\Constants::PERMISSION_DELETE, $share['permissions']);
  817. // update permissions
  818. $params = [];
  819. $params['id'] = $share['id'];
  820. $params['_put'] = [];
  821. $params['_put']['permissions'] = \OCP\Constants::PERMISSION_SHARE;
  822. $result = \OCA\Files_Sharing\API\Local::updateShare($params);
  823. //Updating should fail with 400
  824. $this->assertFalse($result->succeeded());
  825. $this->assertEquals(400, $result->getStatusCode());
  826. $share = \OCP\Share::getItemShared('file', $share['file_source']);
  827. $share = reset($share);
  828. //Permissions should not have changed!
  829. $this->assertEquals(\OCP\Constants::PERMISSION_ALL & ~\OCP\Constants::PERMISSION_DELETE, $share['permissions']);
  830. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  831. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  832. }
  833. /**
  834. * @medium
  835. */
  836. function testUpdateShareUpload() {
  837. $fileInfo = $this->view->getFileInfo($this->folder);
  838. $result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK,
  839. null, 1);
  840. // share was successful?
  841. $this->assertTrue(is_string($result));
  842. $items = \OCP\Share::getItemShared('file', null);
  843. // make sure that we found a link share and a user share
  844. $this->assertEquals(1, count($items));
  845. $linkShare = null;
  846. foreach ($items as $item) {
  847. if ($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) {
  848. $linkShare = $item;
  849. }
  850. }
  851. // make sure that we found a link share
  852. $this->assertTrue(is_array($linkShare));
  853. // update public upload
  854. $params = array();
  855. $params['id'] = $linkShare['id'];
  856. $params['_put'] = array();
  857. $params['_put']['publicUpload'] = 'true';
  858. $result = \OCA\Files_Sharing\API\Local::updateShare($params);
  859. $this->assertTrue($result->succeeded());
  860. $items = \OCP\Share::getItemShared('file', $linkShare['file_source']);
  861. $updatedLinkShare = null;
  862. foreach ($items as $item) {
  863. if ($item['share_type'] === \OCP\Share::SHARE_TYPE_LINK) {
  864. $updatedLinkShare = $item;
  865. break;
  866. }
  867. }
  868. $this->assertTrue(is_array($updatedLinkShare));
  869. $this->assertEquals(7, $updatedLinkShare['permissions']);
  870. // cleanup
  871. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  872. }
  873. /**
  874. * @medium
  875. */
  876. function testUpdateShareExpireDate() {
  877. $fileInfo = $this->view->getFileInfo($this->folder);
  878. $config = \OC::$server->getConfig();
  879. // enforce expire date, by default 7 days after the file was shared
  880. $config->setAppValue('core', 'shareapi_default_expire_date', 'yes');
  881. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'yes');
  882. $dateWithinRange = new \DateTime();
  883. $dateWithinRange->add(new \DateInterval('P5D'));
  884. $dateOutOfRange = new \DateTime();
  885. $dateOutOfRange->add(new \DateInterval('P8D'));
  886. $result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK,
  887. null, 1);
  888. // share was successful?
  889. $this->assertTrue(is_string($result));
  890. $items = \OCP\Share::getItemShared('file', null);
  891. // make sure that we found a link share
  892. $this->assertEquals(1, count($items));
  893. $linkShare = reset($items);
  894. // update expire date to a valid value
  895. $params = array();
  896. $params['id'] = $linkShare['id'];
  897. $params['_put'] = array();
  898. $params['_put']['expireDate'] = $dateWithinRange->format('Y-m-d');
  899. $result = \OCA\Files_Sharing\API\Local::updateShare($params);
  900. $this->assertTrue($result->succeeded());
  901. $items = \OCP\Share::getItemShared('file', $linkShare['file_source']);
  902. $updatedLinkShare = reset($items);
  903. // date should be changed
  904. $this->assertTrue(is_array($updatedLinkShare));
  905. $this->assertEquals($dateWithinRange->format('Y-m-d') . ' 00:00:00', $updatedLinkShare['expiration']);
  906. // update expire date to a value out of range
  907. $params = array();
  908. $params['id'] = $linkShare['id'];
  909. $params['_put'] = array();
  910. $params['_put']['expireDate'] = $dateOutOfRange->format('Y-m-d');
  911. $result = \OCA\Files_Sharing\API\Local::updateShare($params);
  912. $this->assertFalse($result->succeeded());
  913. $items = \OCP\Share::getItemShared('file', $linkShare['file_source']);
  914. $updatedLinkShare = reset($items);
  915. // date shouldn't be changed
  916. $this->assertTrue(is_array($updatedLinkShare));
  917. $this->assertEquals($dateWithinRange->format('Y-m-d') . ' 00:00:00', $updatedLinkShare['expiration']);
  918. // Try to remove expire date
  919. $params = array();
  920. $params['id'] = $linkShare['id'];
  921. $params['_put'] = ['expireDate' => ''];
  922. $result = \OCA\Files_Sharing\API\Local::updateShare($params);
  923. $this->assertFalse($result->succeeded());
  924. $items = \OCP\Share::getItemShared('file', $linkShare['file_source']);
  925. $updatedLinkShare = reset($items);
  926. // date shouldn't be changed
  927. $this->assertTrue(is_array($updatedLinkShare));
  928. $this->assertEquals($dateWithinRange->format('Y-m-d') . ' 00:00:00', $updatedLinkShare['expiration']);
  929. // cleanup
  930. $config->setAppValue('core', 'shareapi_default_expire_date', 'no');
  931. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'no');
  932. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  933. }
  934. /**
  935. * @medium
  936. * @depends testCreateShareUserFile
  937. */
  938. function testDeleteShare() {
  939. $fileInfo = $this->view->getFileInfo($this->filename);
  940. \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  941. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  942. \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK,
  943. null, 1);
  944. $items = \OCP\Share::getItemShared('file', null);
  945. $this->assertEquals(2, count($items));
  946. foreach ($items as $item) {
  947. $result = \OCA\Files_Sharing\API\Local::deleteShare(array('id' => $item['id']));
  948. $this->assertTrue($result->succeeded());
  949. }
  950. $itemsAfterDelete = \OCP\Share::getItemShared('file', null);
  951. $this->assertTrue(empty($itemsAfterDelete));
  952. }
  953. /**
  954. * test unshare of a reshared file
  955. */
  956. function testDeleteReshare() {
  957. // user 1 shares a folder with user2
  958. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  959. $fileInfo1 = $this->view->getFileInfo($this->folder);
  960. $fileInfo2 = $this->view->getFileInfo($this->folder.'/'.$this->filename);
  961. $result1 = \OCP\Share::shareItem('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  962. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  963. $this->assertTrue($result1);
  964. // user2 shares a file from the folder as link
  965. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  966. $result2 = \OCP\Share::shareItem('file', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_LINK, null, 1);
  967. $this->assertTrue(is_string($result2));
  968. // test if we can unshare the link again
  969. $items = \OCP\Share::getItemShared('file', null);
  970. $this->assertEquals(1, count($items));
  971. $item = reset($items);
  972. $result3 = \OCA\Files_Sharing\API\Local::deleteShare(array('id' => $item['id']));
  973. $this->assertTrue($result3->succeeded());
  974. // cleanup
  975. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  976. $result = \OCP\Share::unshare('folder', $fileInfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  977. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  978. $this->assertTrue($result);
  979. }
  980. /**
  981. * share a folder which contains a share mount point, should be forbidden
  982. */
  983. public function testShareFolderWithAMountPoint() {
  984. // user 1 shares a folder with user2
  985. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  986. $fileInfo = $this->view->getFileInfo($this->folder);
  987. $result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  988. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  989. $this->assertTrue($result);
  990. // user2 shares a file from the folder as link
  991. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  992. $view = new \OC\Files\View('/' . \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2 . '/files');
  993. $view->mkdir("localDir");
  994. // move mount point to the folder "localDir"
  995. $result = $view->rename($this->folder, 'localDir/'.$this->folder);
  996. $this->assertTrue($result !== false);
  997. // try to share "localDir"
  998. $fileInfo2 = $view->getFileInfo('localDir');
  999. $this->assertTrue($fileInfo2 instanceof \OC\Files\FileInfo);
  1000. try {
  1001. $result2 = \OCP\Share::shareItem('folder', $fileInfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  1002. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER3, 31);
  1003. } catch (\Exception $e) {
  1004. $result2 = false;
  1005. }
  1006. $this->assertFalse($result2);
  1007. //cleanup
  1008. $result = $view->rename('localDir/' . $this->folder, $this->folder);
  1009. $this->assertTrue($result !== false);
  1010. $view->unlink('localDir');
  1011. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  1012. \OCP\Share::unshare('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  1013. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  1014. }
  1015. /**
  1016. * Post init mount points hook for mounting simulated ext storage
  1017. */
  1018. public static function initTestMountPointsHook($data) {
  1019. if ($data['user'] === \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1) {
  1020. \OC\Files\Filesystem::mount(self::$tempStorage, array(), '/' . \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1 . '/files' . self::TEST_FOLDER_NAME);
  1021. }
  1022. }
  1023. /**
  1024. * Tests mounting a folder that is an external storage mount point.
  1025. */
  1026. public function testShareStorageMountPoint() {
  1027. self::$tempStorage = new \OC\Files\Storage\Temporary(array());
  1028. self::$tempStorage->file_put_contents('test.txt', 'abcdef');
  1029. self::$tempStorage->getScanner()->scan('');
  1030. // needed because the sharing code sometimes switches the user internally and mounts the user's
  1031. // storages. In our case the temp storage isn't mounted automatically, so doing it in the post hook
  1032. // (similar to how ext storage works)
  1033. OCP\Util::connectHook('OC_Filesystem', 'post_initMountPoints', '\Test_Files_Sharing_Api', 'initTestMountPointsHook');
  1034. // logging in will auto-mount the temp storage for user1 as well
  1035. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  1036. $fileInfo = $this->view->getFileInfo($this->folder);
  1037. // user 1 shares the mount point folder with user2
  1038. $result = \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  1039. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  1040. $this->assertTrue($result);
  1041. // user2: check that mount point name appears correctly
  1042. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  1043. $view = new \OC\Files\View('/' . \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2 . '/files');
  1044. $this->assertTrue($view->file_exists($this->folder));
  1045. $this->assertTrue($view->file_exists($this->folder . '/test.txt'));
  1046. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  1047. \OCP\Share::unshare('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER,
  1048. \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  1049. \OC_Hook::clear('OC_Filesystem', 'post_initMountPoints', '\Test_Files_Sharing_Api', 'initTestMountPointsHook');
  1050. }
  1051. /**
  1052. * @expectedException \Exception
  1053. */
  1054. public function testShareNonExisting() {
  1055. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  1056. $id = PHP_INT_MAX - 1;
  1057. \OCP\Share::shareItem('file', $id, \OCP\Share::SHARE_TYPE_LINK, \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  1058. }
  1059. /**
  1060. * @expectedException \Exception
  1061. */
  1062. public function testShareNotOwner() {
  1063. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  1064. \OC\Files\Filesystem::file_put_contents('foo.txt', 'bar');
  1065. $info = \OC\Files\Filesystem::getFileInfo('foo.txt');
  1066. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  1067. \OCP\Share::shareItem('file', $info->getId(), \OCP\Share::SHARE_TYPE_LINK, \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  1068. }
  1069. public function testDefaultExpireDate() {
  1070. \Test_Files_Sharing_Api::loginHelper(\Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER1);
  1071. // TODO drop this once all code paths use the DI version - otherwise
  1072. // the cache inside this config object is out of date because
  1073. // OC_Appconfig is used and bypasses this cache which lead to integrity
  1074. // constraint violations
  1075. $config = \OC::$server->getConfig();
  1076. $config->deleteAppValue('core', 'shareapi_default_expire_date');
  1077. $config->deleteAppValue('core', 'shareapi_enforce_expire_date');
  1078. $config->deleteAppValue('core', 'shareapi_expire_after_n_days');
  1079. $config->setAppValue('core', 'shareapi_default_expire_date', 'yes');
  1080. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'yes');
  1081. $config->setAppValue('core', 'shareapi_expire_after_n_days', '2');
  1082. // default expire date is set to 2 days
  1083. // the time when the share was created is set to 3 days in the past
  1084. // user defined expire date is set to +2 days from now on
  1085. // -> link should be already expired by the default expire date but the user
  1086. // share should still exists.
  1087. $now = time();
  1088. $dateFormat = 'Y-m-d H:i:s';
  1089. $shareCreated = $now - 3 * 24 * 60 * 60;
  1090. $expireDate = date($dateFormat, $now + 2 * 24 * 60 * 60);
  1091. $info = OC\Files\Filesystem::getFileInfo($this->filename);
  1092. $this->assertTrue($info instanceof \OC\Files\FileInfo);
  1093. $result = \OCP\Share::shareItem('file', $info->getId(), \OCP\Share::SHARE_TYPE_LINK, null, \OCP\Constants::PERMISSION_READ);
  1094. $this->assertTrue(is_string($result));
  1095. $result = \OCP\Share::shareItem('file', $info->getId(), \OCP\Share::SHARE_TYPE_USER, \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2, 31);
  1096. $this->assertTrue($result);
  1097. $result = \OCP\Share::setExpirationDate('file', $info->getId() , $expireDate, $now);
  1098. $this->assertTrue($result);
  1099. //manipulate stime so that both shares are older then the default expire date
  1100. $statement = "UPDATE `*PREFIX*share` SET `stime` = ? WHERE `share_type` = ?";
  1101. $query = \OCP\DB::prepare($statement);
  1102. $result = $query->execute(array($shareCreated, \OCP\Share::SHARE_TYPE_LINK));
  1103. $this->assertSame(1, $result);
  1104. $query = \OCP\DB::prepare($statement);
  1105. $result = $query->execute(array($shareCreated, \OCP\Share::SHARE_TYPE_USER));
  1106. $this->assertSame(1, $result);
  1107. // now the link share should expire because of enforced default expire date
  1108. // the user share should still exist
  1109. $result = \OCP\Share::getItemShared('file', $info->getId());
  1110. $this->assertTrue(is_array($result));
  1111. $this->assertSame(1, count($result));
  1112. $share = reset($result);
  1113. $this->assertSame(\OCP\Share::SHARE_TYPE_USER, $share['share_type']);
  1114. //cleanup
  1115. $result = \OCP\Share::unshare('file', $info->getId(), \OCP\Share::SHARE_TYPE_USER, \Test_Files_Sharing_Api::TEST_FILES_SHARING_API_USER2);
  1116. $this->assertTrue($result);
  1117. $config->setAppValue('core', 'shareapi_default_expire_date', 'no');
  1118. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'no');
  1119. }
  1120. public function datesProvider() {
  1121. $date = new \DateTime();
  1122. $date->add(new \DateInterval('P5D'));
  1123. $year = (int)$date->format('Y');
  1124. return [
  1125. [$date->format('Y-m-d'), true],
  1126. [$year+1 . '-1-1', false],
  1127. [$date->format('Y-m-dTH:m'), false],
  1128. ['abc', false],
  1129. [$date->format('Y-m-d') . 'xyz', false],
  1130. ];
  1131. }
  1132. /**
  1133. * Make sure only ISO 8601 dates are accepted
  1134. *
  1135. * @dataProvider datesProvider
  1136. */
  1137. public function testPublicLinkExpireDate($date, $valid) {
  1138. $_POST['path'] = $this->folder;
  1139. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
  1140. $_POST['expireDate'] = $date;
  1141. $result = \OCA\Files_Sharing\API\Local::createShare([]);
  1142. if ($valid === false) {
  1143. $this->assertFalse($result->succeeded());
  1144. $this->assertEquals(404, $result->getStatusCode());
  1145. $this->assertEquals('Invalid Date. Format must be YYYY-MM-DD.', $result->getMeta()['message']);
  1146. return;
  1147. }
  1148. $this->assertTrue($result->succeeded());
  1149. $data = $result->getData();
  1150. $this->assertTrue(is_string($data['token']));
  1151. $this->assertEquals($date, substr($data['expiration'], 0, 10));
  1152. // check for correct link
  1153. $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
  1154. $this->assertEquals($url, $data['url']);
  1155. $share = $this->getShareFromId($data['id']);
  1156. $items = \OCP\Share::getItemShared('file', $share['item_source']);
  1157. $this->assertTrue(!empty($items));
  1158. $item = reset($items);
  1159. $this->assertTrue(is_array($item));
  1160. $this->assertEquals($date, substr($item['expiration'], 0, 10));
  1161. $fileinfo = $this->view->getFileInfo($this->folder);
  1162. \OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  1163. }
  1164. public function testCreatePublicLinkExpireDateValid() {
  1165. $config = \OC::$server->getConfig();
  1166. // enforce expire date, by default 7 days after the file was shared
  1167. $config->setAppValue('core', 'shareapi_default_expire_date', 'yes');
  1168. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'yes');
  1169. $date = new \DateTime();
  1170. $date->add(new \DateInterval('P5D'));
  1171. $_POST['path'] = $this->folder;
  1172. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
  1173. $_POST['expireDate'] = $date->format('Y-m-d');
  1174. $result = \OCA\Files_Sharing\API\Local::createShare([]);
  1175. $this->assertTrue($result->succeeded());
  1176. $data = $result->getData();
  1177. $this->assertTrue(is_string($data['token']));
  1178. $this->assertEquals($date->format('Y-m-d') . ' 00:00:00', $data['expiration']);
  1179. // check for correct link
  1180. $url = \OC::$server->getURLGenerator()->getAbsoluteURL('/index.php/s/' . $data['token']);
  1181. $this->assertEquals($url, $data['url']);
  1182. $share = $this->getShareFromId($data['id']);
  1183. $items = \OCP\Share::getItemShared('file', $share['item_source']);
  1184. $this->assertTrue(!empty($items));
  1185. $item = reset($items);
  1186. $this->assertTrue(is_array($item));
  1187. $this->assertEquals($date->format('Y-m-d'), substr($item['expiration'], 0, 10));
  1188. $fileinfo = $this->view->getFileInfo($this->folder);
  1189. \OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  1190. $config->setAppValue('core', 'shareapi_default_expire_date', 'no');
  1191. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'no');
  1192. }
  1193. public function testCreatePublicLinkExpireDateInvalidFuture() {
  1194. $config = \OC::$server->getConfig();
  1195. // enforce expire date, by default 7 days after the file was shared
  1196. $config->setAppValue('core', 'shareapi_default_expire_date', 'yes');
  1197. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'yes');
  1198. $date = new \DateTime();
  1199. $date->add(new \DateInterval('P8D'));
  1200. $_POST['path'] = $this->folder;
  1201. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
  1202. $_POST['expireDate'] = $date->format('Y-m-d');
  1203. $result = \OCA\Files_Sharing\API\Local::createShare([]);
  1204. $this->assertFalse($result->succeeded());
  1205. $this->assertEquals(404, $result->getStatusCode());
  1206. $this->assertEquals('Cannot set expiration date. Shares cannot expire later than 7 after they have been shared', $result->getMeta()['message']);
  1207. $config->setAppValue('core', 'shareapi_default_expire_date', 'no');
  1208. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'no');
  1209. }
  1210. public function testCreatePublicLinkExpireDateInvalidPast() {
  1211. $config = \OC::$server->getConfig();
  1212. $date = new \DateTime();
  1213. $date->sub(new \DateInterval('P8D'));
  1214. $_POST['path'] = $this->folder;
  1215. $_POST['shareType'] = \OCP\Share::SHARE_TYPE_LINK;
  1216. $_POST['expireDate'] = $date->format('Y-m-d');
  1217. $result = \OCA\Files_Sharing\API\Local::createShare([]);
  1218. $this->assertFalse($result->succeeded());
  1219. $this->assertEquals(404, $result->getStatusCode());
  1220. $this->assertEquals('Cannot set expiration date. Expiration date is in the past', $result->getMeta()['message']);
  1221. $config->setAppValue('core', 'shareapi_default_expire_date', 'no');
  1222. $config->setAppValue('core', 'shareapi_enforce_expire_date', 'no');
  1223. }
  1224. }