1
0

WebDav.php 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Sergio Bertolin <sbertolin@solidgear.es>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author David Toledo <dtoledo@solidgear.es>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author John Molakvoæ <skjnldsv@protonmail.com>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Sergio Bertolin <sbertolin@solidgear.es>
  14. * @author Sergio Bertolín <sbertolin@solidgear.es>
  15. * @author Thomas Müller <thomas.mueller@tmit.eu>
  16. * @author Vincent Petry <vincent@nextcloud.com>
  17. *
  18. * @license GNU AGPL version 3 or any later version
  19. *
  20. * This program is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License as
  22. * published by the Free Software Foundation, either version 3 of the
  23. * License, or (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU Affero General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Affero General Public License
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32. *
  33. */
  34. use GuzzleHttp\Client as GClient;
  35. use GuzzleHttp\Message\ResponseInterface;
  36. use PHPUnit\Framework\Assert;
  37. use Sabre\DAV\Client as SClient;
  38. use Sabre\DAV\Xml\Property\ResourceType;
  39. require __DIR__ . '/../../vendor/autoload.php';
  40. trait WebDav {
  41. use Sharing;
  42. /** @var string */
  43. private $davPath = "remote.php/webdav";
  44. /** @var boolean */
  45. private $usingOldDavPath = true;
  46. /** @var ResponseInterface */
  47. private $response;
  48. /** @var array map with user as key and another map as value, which has path as key and etag as value */
  49. private $storedETAG = null;
  50. /** @var int */
  51. private $storedFileID = null;
  52. private string $s3MultipartDestination;
  53. private string $uploadId;
  54. /**
  55. * @Given /^using dav path "([^"]*)"$/
  56. */
  57. public function usingDavPath($davPath) {
  58. $this->davPath = $davPath;
  59. }
  60. /**
  61. * @Given /^using old dav path$/
  62. */
  63. public function usingOldDavPath() {
  64. $this->davPath = "remote.php/webdav";
  65. $this->usingOldDavPath = true;
  66. }
  67. /**
  68. * @Given /^using new dav path$/
  69. */
  70. public function usingNewDavPath() {
  71. $this->davPath = "remote.php/dav";
  72. $this->usingOldDavPath = false;
  73. }
  74. public function getDavFilesPath($user) {
  75. if ($this->usingOldDavPath === true) {
  76. return $this->davPath;
  77. } else {
  78. return $this->davPath . '/files/' . $user;
  79. }
  80. }
  81. public function makeDavRequest($user, $method, $path, $headers, $body = null, $type = "files") {
  82. if ($type === "files") {
  83. $fullUrl = substr($this->baseUrl, 0, -4) . $this->getDavFilesPath($user) . "$path";
  84. } elseif ($type === "uploads") {
  85. $fullUrl = substr($this->baseUrl, 0, -4) . $this->davPath . "$path";
  86. } else {
  87. $fullUrl = substr($this->baseUrl, 0, -4) . $this->davPath . '/' . $type . "$path";
  88. }
  89. $client = new GClient();
  90. $options = [
  91. 'headers' => $headers,
  92. 'body' => $body
  93. ];
  94. if ($user === 'admin') {
  95. $options['auth'] = $this->adminUser;
  96. } else {
  97. $options['auth'] = [$user, $this->regularUser];
  98. }
  99. return $client->request($method, $fullUrl, $options);
  100. }
  101. /**
  102. * @Given /^User "([^"]*)" moved (file|folder|entry) "([^"]*)" to "([^"]*)"$/
  103. * @param string $user
  104. * @param string $fileSource
  105. * @param string $fileDestination
  106. */
  107. public function userMovedFile($user, $entry, $fileSource, $fileDestination) {
  108. $fullUrl = substr($this->baseUrl, 0, -4) . $this->getDavFilesPath($user);
  109. $headers['Destination'] = $fullUrl . $fileDestination;
  110. $this->response = $this->makeDavRequest($user, "MOVE", $fileSource, $headers);
  111. Assert::assertEquals(201, $this->response->getStatusCode());
  112. }
  113. /**
  114. * @When /^User "([^"]*)" moves (file|folder|entry) "([^"]*)" to "([^"]*)"$/
  115. * @param string $user
  116. * @param string $fileSource
  117. * @param string $fileDestination
  118. */
  119. public function userMovesFile($user, $entry, $fileSource, $fileDestination) {
  120. $fullUrl = substr($this->baseUrl, 0, -4) . $this->getDavFilesPath($user);
  121. $headers['Destination'] = $fullUrl . $fileDestination;
  122. try {
  123. $this->response = $this->makeDavRequest($user, "MOVE", $fileSource, $headers);
  124. } catch (\GuzzleHttp\Exception\ClientException $e) {
  125. $this->response = $e->getResponse();
  126. }
  127. }
  128. /**
  129. * @When /^User "([^"]*)" copies file "([^"]*)" to "([^"]*)"$/
  130. * @param string $user
  131. * @param string $fileSource
  132. * @param string $fileDestination
  133. */
  134. public function userCopiesFileTo($user, $fileSource, $fileDestination) {
  135. $fullUrl = substr($this->baseUrl, 0, -4) . $this->getDavFilesPath($user);
  136. $headers['Destination'] = $fullUrl . $fileDestination;
  137. try {
  138. $this->response = $this->makeDavRequest($user, 'COPY', $fileSource, $headers);
  139. } catch (\GuzzleHttp\Exception\ClientException $e) {
  140. // 4xx and 5xx responses cause an exception
  141. $this->response = $e->getResponse();
  142. }
  143. }
  144. /**
  145. * @When /^Downloading file "([^"]*)" with range "([^"]*)"$/
  146. * @param string $fileSource
  147. * @param string $range
  148. */
  149. public function downloadFileWithRange($fileSource, $range) {
  150. $headers['Range'] = $range;
  151. $this->response = $this->makeDavRequest($this->currentUser, "GET", $fileSource, $headers);
  152. }
  153. /**
  154. * @When /^Downloading last public shared file with range "([^"]*)"$/
  155. * @param string $range
  156. */
  157. public function downloadPublicFileWithRange($range) {
  158. $token = $this->lastShareData->data->token;
  159. $fullUrl = substr($this->baseUrl, 0, -4) . "public.php/webdav";
  160. $client = new GClient();
  161. $options = [];
  162. $options['auth'] = [$token, ""];
  163. $options['headers'] = [
  164. 'Range' => $range
  165. ];
  166. $this->response = $client->request("GET", $fullUrl, $options);
  167. }
  168. /**
  169. * @When /^Downloading last public shared file inside a folder "([^"]*)" with range "([^"]*)"$/
  170. * @param string $range
  171. */
  172. public function downloadPublicFileInsideAFolderWithRange($path, $range) {
  173. $token = $this->lastShareData->data->token;
  174. $fullUrl = substr($this->baseUrl, 0, -4) . "public.php/webdav" . "$path";
  175. $client = new GClient();
  176. $options = [
  177. 'headers' => [
  178. 'Range' => $range
  179. ]
  180. ];
  181. $options['auth'] = [$token, ""];
  182. $this->response = $client->request("GET", $fullUrl, $options);
  183. }
  184. /**
  185. * @Then /^Downloaded content should be "([^"]*)"$/
  186. * @param string $content
  187. */
  188. public function downloadedContentShouldBe($content) {
  189. Assert::assertEquals($content, (string)$this->response->getBody());
  190. }
  191. /**
  192. * @Then /^File "([^"]*)" should have prop "([^"]*):([^"]*)" equal to "([^"]*)"$/
  193. * @param string $file
  194. * @param string $prefix
  195. * @param string $prop
  196. * @param string $value
  197. */
  198. public function checkPropForFile($file, $prefix, $prop, $value) {
  199. $elementList = $this->propfindFile($this->currentUser, $file, "<$prefix:$prop/>");
  200. $property = $elementList['/'.$this->getDavFilesPath($this->currentUser).$file][200]["{DAV:}$prop"];
  201. Assert::assertEquals($property, $value);
  202. }
  203. /**
  204. * @Then /^Image search should work$/
  205. */
  206. public function search(): void {
  207. $this->searchFile($this->currentUser);
  208. Assert::assertEquals(207, $this->response->getStatusCode());
  209. }
  210. /**
  211. * @Then /^Downloaded content when downloading file "([^"]*)" with range "([^"]*)" should be "([^"]*)"$/
  212. * @param string $fileSource
  213. * @param string $range
  214. * @param string $content
  215. */
  216. public function downloadedContentWhenDownloadindShouldBe($fileSource, $range, $content) {
  217. $this->downloadFileWithRange($fileSource, $range);
  218. $this->downloadedContentShouldBe($content);
  219. }
  220. /**
  221. * @When Downloading file :fileName
  222. * @param string $fileName
  223. */
  224. public function downloadingFile($fileName) {
  225. try {
  226. $this->response = $this->makeDavRequest($this->currentUser, 'GET', $fileName, []);
  227. } catch (\GuzzleHttp\Exception\ClientException $e) {
  228. $this->response = $e->getResponse();
  229. }
  230. }
  231. /**
  232. * @Then Downloaded content should start with :start
  233. * @param int $start
  234. * @throws \Exception
  235. */
  236. public function downloadedContentShouldStartWith($start) {
  237. if (strpos($this->response->getBody()->getContents(), $start) !== 0) {
  238. throw new \Exception(
  239. sprintf(
  240. "Expected '%s', got '%s'",
  241. $start,
  242. $this->response->getBody()->getContents()
  243. )
  244. );
  245. }
  246. }
  247. /**
  248. * @Then /^as "([^"]*)" gets properties of (file|folder|entry) "([^"]*)" with$/
  249. * @param string $user
  250. * @param string $elementType
  251. * @param string $path
  252. * @param \Behat\Gherkin\Node\TableNode|null $propertiesTable
  253. */
  254. public function asGetsPropertiesOfFolderWith($user, $elementType, $path, $propertiesTable) {
  255. $properties = null;
  256. if ($propertiesTable instanceof \Behat\Gherkin\Node\TableNode) {
  257. foreach ($propertiesTable->getRows() as $row) {
  258. $properties[] = $row[0];
  259. }
  260. }
  261. $this->response = $this->listFolder($user, $path, 0, $properties);
  262. }
  263. /**
  264. * @Then /^as "([^"]*)" the (file|folder|entry) "([^"]*)" does not exist$/
  265. * @param string $user
  266. * @param string $entry
  267. * @param string $path
  268. * @param \Behat\Gherkin\Node\TableNode|null $propertiesTable
  269. */
  270. public function asTheFileOrFolderDoesNotExist($user, $entry, $path) {
  271. $client = $this->getSabreClient($user);
  272. $response = $client->request('HEAD', $this->makeSabrePath($user, $path));
  273. if ($response['statusCode'] !== 404) {
  274. throw new \Exception($entry . ' "' . $path . '" expected to not exist (status code ' . $response['statusCode'] . ', expected 404)');
  275. }
  276. return $response;
  277. }
  278. /**
  279. * @Then /^as "([^"]*)" the (file|folder|entry) "([^"]*)" exists$/
  280. * @param string $user
  281. * @param string $entry
  282. * @param string $path
  283. */
  284. public function asTheFileOrFolderExists($user, $entry, $path) {
  285. $this->response = $this->listFolder($user, $path, 0);
  286. }
  287. /**
  288. * @Then the single response should contain a property :key with value :value
  289. * @param string $key
  290. * @param string $expectedValue
  291. * @throws \Exception
  292. */
  293. public function theSingleResponseShouldContainAPropertyWithValue($key, $expectedValue) {
  294. $keys = $this->response;
  295. if (!array_key_exists($key, $keys)) {
  296. throw new \Exception("Cannot find property \"$key\" with \"$expectedValue\"");
  297. }
  298. $value = $keys[$key];
  299. if ($value instanceof ResourceType) {
  300. $value = $value->getValue();
  301. if (empty($value)) {
  302. $value = '';
  303. } else {
  304. $value = $value[0];
  305. }
  306. }
  307. if ($value != $expectedValue) {
  308. throw new \Exception("Property \"$key\" found with value \"$value\", expected \"$expectedValue\"");
  309. }
  310. }
  311. /**
  312. * @Then the response should contain a share-types property with
  313. */
  314. public function theResponseShouldContainAShareTypesPropertyWith($table) {
  315. $keys = $this->response;
  316. if (!array_key_exists('{http://owncloud.org/ns}share-types', $keys)) {
  317. throw new \Exception("Cannot find property \"{http://owncloud.org/ns}share-types\"");
  318. }
  319. $foundTypes = [];
  320. $data = $keys['{http://owncloud.org/ns}share-types'];
  321. foreach ($data as $item) {
  322. if ($item['name'] !== '{http://owncloud.org/ns}share-type') {
  323. throw new \Exception('Invalid property found: "' . $item['name'] . '"');
  324. }
  325. $foundTypes[] = $item['value'];
  326. }
  327. foreach ($table->getRows() as $row) {
  328. $key = array_search($row[0], $foundTypes);
  329. if ($key === false) {
  330. throw new \Exception('Expected type ' . $row[0] . ' not found');
  331. }
  332. unset($foundTypes[$key]);
  333. }
  334. if ($foundTypes !== []) {
  335. throw new \Exception('Found more share types then specified: ' . $foundTypes);
  336. }
  337. }
  338. /**
  339. * @Then the response should contain an empty property :property
  340. * @param string $property
  341. * @throws \Exception
  342. */
  343. public function theResponseShouldContainAnEmptyProperty($property) {
  344. $properties = $this->response;
  345. if (!array_key_exists($property, $properties)) {
  346. throw new \Exception("Cannot find property \"$property\"");
  347. }
  348. if ($properties[$property] !== null) {
  349. throw new \Exception("Property \"$property\" is not empty");
  350. }
  351. }
  352. /*Returns the elements of a propfind, $folderDepth requires 1 to see elements without children*/
  353. public function listFolder($user, $path, $folderDepth, $properties = null) {
  354. $client = $this->getSabreClient($user);
  355. if (!$properties) {
  356. $properties = [
  357. '{DAV:}getetag'
  358. ];
  359. }
  360. $response = $client->propfind($this->makeSabrePath($user, $path), $properties, $folderDepth);
  361. return $response;
  362. }
  363. /**
  364. * Returns the elements of a profind command
  365. * @param string $properties properties which needs to be included in the report
  366. * @param string $filterRules filter-rules to choose what needs to appear in the report
  367. */
  368. public function propfindFile(string $user, string $path, string $properties = '') {
  369. $client = $this->getSabreClient($user);
  370. $body = '<?xml version="1.0" encoding="utf-8" ?>
  371. <d:propfind xmlns:d="DAV:"
  372. xmlns:oc="http://owncloud.org/ns"
  373. xmlns:nc="http://nextcloud.org/ns"
  374. xmlns:ocs="http://open-collaboration-services.org/ns">
  375. <d:prop>
  376. ' . $properties . '
  377. </d:prop>
  378. </d:propfind>';
  379. $response = $client->request('PROPFIND', $this->makeSabrePath($user, $path), $body);
  380. $parsedResponse = $client->parseMultistatus($response['body']);
  381. return $parsedResponse;
  382. }
  383. /**
  384. * Returns the elements of a searc command
  385. * @param string $properties properties which needs to be included in the report
  386. * @param string $filterRules filter-rules to choose what needs to appear in the report
  387. */
  388. public function searchFile(string $user, ?string $properties = null, ?string $scope = null, ?string $condition = null) {
  389. $client = $this->getSabreClient($user);
  390. if ($properties === null) {
  391. $properties = '<oc:fileid /> <d:getlastmodified /> <d:getetag /> <d:getcontenttype /> <d:getcontentlength /> <nc:has-preview /> <oc:favorite /> <d:resourcetype />';
  392. }
  393. if ($condition === null) {
  394. $condition = '<d:and>
  395. <d:or>
  396. <d:eq>
  397. <d:prop>
  398. <d:getcontenttype/>
  399. </d:prop>
  400. <d:literal>image/png</d:literal>
  401. </d:eq>
  402. <d:eq>
  403. <d:prop>
  404. <d:getcontenttype/>
  405. </d:prop>
  406. <d:literal>image/jpeg</d:literal>
  407. </d:eq>
  408. <d:eq>
  409. <d:prop>
  410. <d:getcontenttype/>
  411. </d:prop>
  412. <d:literal>image/heic</d:literal>
  413. </d:eq>
  414. <d:eq>
  415. <d:prop>
  416. <d:getcontenttype/>
  417. </d:prop>
  418. <d:literal>video/mp4</d:literal>
  419. </d:eq>
  420. <d:eq>
  421. <d:prop>
  422. <d:getcontenttype/>
  423. </d:prop>
  424. <d:literal>video/quicktime</d:literal>
  425. </d:eq>
  426. </d:or>
  427. <d:eq>
  428. <d:prop>
  429. <oc:owner-id/>
  430. </d:prop>
  431. <d:literal>' . $user . '</d:literal>
  432. </d:eq>
  433. </d:and>';
  434. }
  435. if ($scope === null) {
  436. $scope = '<d:href>/files/' . $user . '</d:href><d:depth>infinity</d:depth>';
  437. }
  438. $body = '<?xml version="1.0" encoding="UTF-8"?>
  439. <d:searchrequest xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns" xmlns:nc="http://nextcloud.org/ns" xmlns:ns="https://github.com/icewind1991/SearchDAV/ns" xmlns:ocs="http://open-collaboration-services.org/ns">
  440. <d:basicsearch>
  441. <d:select>
  442. <d:prop>' . $properties . '</d:prop>
  443. </d:select>
  444. <d:from><d:scope>' . $scope . '</d:scope></d:from>
  445. <d:where>' . $condition . '</d:where>
  446. <d:orderby>
  447. <d:order>
  448. <d:prop><d:getlastmodified/></d:prop>
  449. <d:descending/>
  450. </d:order>
  451. </d:orderby>
  452. <d:limit>
  453. <d:nresults>35</d:nresults>
  454. <ns:firstresult>0</ns:firstresult>
  455. </d:limit>
  456. </d:basicsearch>
  457. </d:searchrequest>';
  458. try {
  459. $this->response = $this->makeDavRequest($user, "SEARCH", '', [
  460. 'Content-Type' => 'text/xml'
  461. ], $body, '');
  462. var_dump((string)$this->response->getBody());
  463. } catch (\GuzzleHttp\Exception\ServerException $e) {
  464. // 5xx responses cause a server exception
  465. $this->response = $e->getResponse();
  466. } catch (\GuzzleHttp\Exception\ClientException $e) {
  467. // 4xx responses cause a client exception
  468. $this->response = $e->getResponse();
  469. }
  470. }
  471. /* Returns the elements of a report command
  472. * @param string $user
  473. * @param string $path
  474. * @param string $properties properties which needs to be included in the report
  475. * @param string $filterRules filter-rules to choose what needs to appear in the report
  476. */
  477. public function reportFolder($user, $path, $properties, $filterRules) {
  478. $client = $this->getSabreClient($user);
  479. $body = '<?xml version="1.0" encoding="utf-8" ?>
  480. <oc:filter-files xmlns:a="DAV:" xmlns:oc="http://owncloud.org/ns" >
  481. <a:prop>
  482. ' . $properties . '
  483. </a:prop>
  484. <oc:filter-rules>
  485. ' . $filterRules . '
  486. </oc:filter-rules>
  487. </oc:filter-files>';
  488. $response = $client->request('REPORT', $this->makeSabrePath($user, $path), $body);
  489. $parsedResponse = $client->parseMultistatus($response['body']);
  490. return $parsedResponse;
  491. }
  492. public function makeSabrePath($user, $path, $type = 'files') {
  493. if ($type === 'files') {
  494. return $this->encodePath($this->getDavFilesPath($user) . $path);
  495. } else {
  496. return $this->encodePath($this->davPath . '/' . $type . '/' . $user . '/' . $path);
  497. }
  498. }
  499. public function getSabreClient($user) {
  500. $fullUrl = substr($this->baseUrl, 0, -4);
  501. $settings = [
  502. 'baseUri' => $fullUrl,
  503. 'userName' => $user,
  504. ];
  505. if ($user === 'admin') {
  506. $settings['password'] = $this->adminUser[1];
  507. } else {
  508. $settings['password'] = $this->regularUser;
  509. }
  510. $settings['authType'] = SClient::AUTH_BASIC;
  511. return new SClient($settings);
  512. }
  513. /**
  514. * @Then /^user "([^"]*)" should see following elements$/
  515. * @param string $user
  516. * @param \Behat\Gherkin\Node\TableNode|null $expectedElements
  517. */
  518. public function checkElementList($user, $expectedElements) {
  519. $elementList = $this->listFolder($user, '/', 3);
  520. if ($expectedElements instanceof \Behat\Gherkin\Node\TableNode) {
  521. $elementRows = $expectedElements->getRows();
  522. $elementsSimplified = $this->simplifyArray($elementRows);
  523. foreach ($elementsSimplified as $expectedElement) {
  524. $webdavPath = "/" . $this->getDavFilesPath($user) . $expectedElement;
  525. if (!array_key_exists($webdavPath, $elementList)) {
  526. Assert::fail("$webdavPath" . " is not in propfind answer");
  527. }
  528. }
  529. }
  530. }
  531. /**
  532. * @When User :user uploads file :source to :destination
  533. * @param string $user
  534. * @param string $source
  535. * @param string $destination
  536. */
  537. public function userUploadsAFileTo($user, $source, $destination) {
  538. $file = \GuzzleHttp\Psr7\Utils::streamFor(fopen($source, 'r'));
  539. try {
  540. $this->response = $this->makeDavRequest($user, "PUT", $destination, [], $file);
  541. } catch (\GuzzleHttp\Exception\ServerException $e) {
  542. // 5xx responses cause a server exception
  543. $this->response = $e->getResponse();
  544. } catch (\GuzzleHttp\Exception\ClientException $e) {
  545. // 4xx responses cause a client exception
  546. $this->response = $e->getResponse();
  547. }
  548. }
  549. /**
  550. * @When User :user adds a file of :bytes bytes to :destination
  551. * @param string $user
  552. * @param string $bytes
  553. * @param string $destination
  554. */
  555. public function userAddsAFileTo($user, $bytes, $destination) {
  556. $filename = "filespecificSize.txt";
  557. $this->createFileSpecificSize($filename, $bytes);
  558. Assert::assertEquals(1, file_exists("work/$filename"));
  559. $this->userUploadsAFileTo($user, "work/$filename", $destination);
  560. $this->removeFile("work/", $filename);
  561. $expectedElements = new \Behat\Gherkin\Node\TableNode([["$destination"]]);
  562. $this->checkElementList($user, $expectedElements);
  563. }
  564. /**
  565. * @When User :user uploads file with content :content to :destination
  566. */
  567. public function userUploadsAFileWithContentTo($user, $content, $destination) {
  568. $file = \GuzzleHttp\Psr7\Utils::streamFor($content);
  569. try {
  570. $this->response = $this->makeDavRequest($user, "PUT", $destination, [], $file);
  571. } catch (\GuzzleHttp\Exception\ServerException $e) {
  572. // 5xx responses cause a server exception
  573. $this->response = $e->getResponse();
  574. } catch (\GuzzleHttp\Exception\ClientException $e) {
  575. // 4xx responses cause a client exception
  576. $this->response = $e->getResponse();
  577. }
  578. }
  579. /**
  580. * @When /^User "([^"]*)" deletes (file|folder) "([^"]*)"$/
  581. * @param string $user
  582. * @param string $type
  583. * @param string $file
  584. */
  585. public function userDeletesFile($user, $type, $file) {
  586. try {
  587. $this->response = $this->makeDavRequest($user, 'DELETE', $file, []);
  588. } catch (\GuzzleHttp\Exception\ServerException $e) {
  589. // 5xx responses cause a server exception
  590. $this->response = $e->getResponse();
  591. } catch (\GuzzleHttp\Exception\ClientException $e) {
  592. // 4xx responses cause a client exception
  593. $this->response = $e->getResponse();
  594. }
  595. }
  596. /**
  597. * @Given User :user created a folder :destination
  598. * @param string $user
  599. * @param string $destination
  600. */
  601. public function userCreatedAFolder($user, $destination) {
  602. try {
  603. $destination = '/' . ltrim($destination, '/');
  604. $this->response = $this->makeDavRequest($user, "MKCOL", $destination, []);
  605. } catch (\GuzzleHttp\Exception\ServerException $e) {
  606. // 5xx responses cause a server exception
  607. $this->response = $e->getResponse();
  608. } catch (\GuzzleHttp\Exception\ClientException $e) {
  609. // 4xx responses cause a client exception
  610. $this->response = $e->getResponse();
  611. }
  612. }
  613. /**
  614. * @Given user :user uploads chunk file :num of :total with :data to :destination
  615. * @param string $user
  616. * @param int $num
  617. * @param int $total
  618. * @param string $data
  619. * @param string $destination
  620. */
  621. public function userUploadsChunkFileOfWithToWithChecksum($user, $num, $total, $data, $destination) {
  622. $num -= 1;
  623. $data = \GuzzleHttp\Psr7\Utils::streamFor($data);
  624. $file = $destination . '-chunking-42-' . $total . '-' . $num;
  625. $this->makeDavRequest($user, 'PUT', $file, ['OC-Chunked' => '1'], $data, "uploads");
  626. }
  627. /**
  628. * @Given user :user uploads bulked files :name1 with :content1 and :name2 with :content2 and :name3 with :content3
  629. * @param string $user
  630. * @param string $name1
  631. * @param string $content1
  632. * @param string $name2
  633. * @param string $content2
  634. * @param string $name3
  635. * @param string $content3
  636. */
  637. public function userUploadsBulkedFiles($user, $name1, $content1, $name2, $content2, $name3, $content3) {
  638. $boundary = "boundary_azertyuiop";
  639. $body = "";
  640. $body .= '--'.$boundary."\r\n";
  641. $body .= "X-File-Path: ".$name1."\r\n";
  642. $body .= "X-File-MD5: f6a6263167c92de8644ac998b3c4e4d1\r\n";
  643. $body .= "X-OC-Mtime: 1111111111\r\n";
  644. $body .= "Content-Length: ".strlen($content1)."\r\n";
  645. $body .= "\r\n";
  646. $body .= $content1."\r\n";
  647. $body .= '--'.$boundary."\r\n";
  648. $body .= "X-File-Path: ".$name2."\r\n";
  649. $body .= "X-File-MD5: 87c7d4068be07d390a1fffd21bf1e944\r\n";
  650. $body .= "X-OC-Mtime: 2222222222\r\n";
  651. $body .= "Content-Length: ".strlen($content2)."\r\n";
  652. $body .= "\r\n";
  653. $body .= $content2."\r\n";
  654. $body .= '--'.$boundary."\r\n";
  655. $body .= "X-File-Path: ".$name3."\r\n";
  656. $body .= "X-File-MD5: e86a1cf0678099986a901c79086f5617\r\n";
  657. $body .= "X-File-Mtime: 3333333333\r\n";
  658. $body .= "Content-Length: ".strlen($content3)."\r\n";
  659. $body .= "\r\n";
  660. $body .= $content3."\r\n";
  661. $body .= '--'.$boundary."--\r\n";
  662. $stream = fopen('php://temp', 'r+');
  663. fwrite($stream, $body);
  664. rewind($stream);
  665. $client = new GClient();
  666. $options = [
  667. 'auth' => [$user, $this->regularUser],
  668. 'headers' => [
  669. 'Content-Type' => 'multipart/related; boundary='.$boundary,
  670. 'Content-Length' => (string)strlen($body),
  671. ],
  672. 'body' => $body
  673. ];
  674. return $client->request("POST", substr($this->baseUrl, 0, -4) . "remote.php/dav/bulk", $options);
  675. }
  676. /**
  677. * @Given user :user creates a new chunking upload with id :id
  678. */
  679. public function userCreatesANewChunkingUploadWithId($user, $id) {
  680. $this->parts = [];
  681. $destination = '/uploads/' . $user . '/' . $id;
  682. $this->makeDavRequest($user, 'MKCOL', $destination, [], null, "uploads");
  683. }
  684. /**
  685. * @Given user :user uploads new chunk file :num with :data to id :id
  686. */
  687. public function userUploadsNewChunkFileOfWithToId($user, $num, $data, $id) {
  688. $data = \GuzzleHttp\Psr7\Utils::streamFor($data);
  689. $destination = '/uploads/' . $user . '/' . $id . '/' . $num;
  690. $this->makeDavRequest($user, 'PUT', $destination, [], $data, "uploads");
  691. }
  692. /**
  693. * @Given user :user moves new chunk file with id :id to :dest
  694. */
  695. public function userMovesNewChunkFileWithIdToMychunkedfile($user, $id, $dest) {
  696. $source = '/uploads/' . $user . '/' . $id . '/.file';
  697. $destination = substr($this->baseUrl, 0, -4) . $this->getDavFilesPath($user) . $dest;
  698. $this->makeDavRequest($user, 'MOVE', $source, [
  699. 'Destination' => $destination
  700. ], null, "uploads");
  701. }
  702. /**
  703. * @Then user :user moves new chunk file with id :id to :dest with size :size
  704. */
  705. public function userMovesNewChunkFileWithIdToMychunkedfileWithSize($user, $id, $dest, $size) {
  706. $source = '/uploads/' . $user . '/' . $id . '/.file';
  707. $destination = substr($this->baseUrl, 0, -4) . $this->getDavFilesPath($user) . $dest;
  708. try {
  709. $this->response = $this->makeDavRequest($user, 'MOVE', $source, [
  710. 'Destination' => $destination,
  711. 'OC-Total-Length' => $size
  712. ], null, "uploads");
  713. } catch (\GuzzleHttp\Exception\BadResponseException $ex) {
  714. $this->response = $ex->getResponse();
  715. }
  716. }
  717. /**
  718. * @Given user :user creates a new chunking v2 upload with id :id and destination :targetDestination
  719. */
  720. public function userCreatesANewChunkingv2UploadWithIdAndDestination($user, $id, $targetDestination) {
  721. $this->s3MultipartDestination = $this->getTargetDestination($user, $targetDestination);
  722. $this->newUploadId();
  723. $destination = '/uploads/' . $user . '/' . $this->getUploadId($id);
  724. $this->response = $this->makeDavRequest($user, 'MKCOL', $destination, [
  725. 'Destination' => $this->s3MultipartDestination,
  726. ], null, "uploads");
  727. }
  728. /**
  729. * @Given user :user uploads new chunk v2 file :num to id :id
  730. */
  731. public function userUploadsNewChunkv2FileToIdAndDestination($user, $num, $id) {
  732. $data = \GuzzleHttp\Psr7\Utils::streamFor(fopen('/tmp/part-upload-' . $num, 'r'));
  733. $destination = '/uploads/' . $user . '/' . $this->getUploadId($id) . '/' . $num;
  734. $this->response = $this->makeDavRequest($user, 'PUT', $destination, [
  735. 'Destination' => $this->s3MultipartDestination
  736. ], $data, "uploads");
  737. }
  738. /**
  739. * @Given user :user moves new chunk v2 file with id :id
  740. */
  741. public function userMovesNewChunkv2FileWithIdToMychunkedfileAndDestination($user, $id) {
  742. $source = '/uploads/' . $user . '/' . $this->getUploadId($id) . '/.file';
  743. try {
  744. $this->response = $this->makeDavRequest($user, 'MOVE', $source, [
  745. 'Destination' => $this->s3MultipartDestination,
  746. ], null, "uploads");
  747. } catch (\GuzzleHttp\Exception\ServerException $e) {
  748. // 5xx responses cause a server exception
  749. $this->response = $e->getResponse();
  750. } catch (\GuzzleHttp\Exception\ClientException $e) {
  751. // 4xx responses cause a client exception
  752. $this->response = $e->getResponse();
  753. }
  754. }
  755. private function getTargetDestination(string $user, string $destination): string {
  756. return substr($this->baseUrl, 0, -4) . $this->getDavFilesPath($user) . $destination;
  757. }
  758. private function getUploadId(string $id): string {
  759. return $id . '-' . $this->uploadId;
  760. }
  761. private function newUploadId() {
  762. $this->uploadId = (string)time();
  763. }
  764. /**
  765. * @Given /^Downloading file "([^"]*)" as "([^"]*)"$/
  766. */
  767. public function downloadingFileAs($fileName, $user) {
  768. try {
  769. $this->response = $this->makeDavRequest($user, 'GET', $fileName, []);
  770. } catch (\GuzzleHttp\Exception\ServerException $e) {
  771. // 5xx responses cause a server exception
  772. $this->response = $e->getResponse();
  773. } catch (\GuzzleHttp\Exception\ClientException $e) {
  774. // 4xx responses cause a client exception
  775. $this->response = $e->getResponse();
  776. }
  777. }
  778. /**
  779. * URL encodes the given path but keeps the slashes
  780. *
  781. * @param string $path to encode
  782. * @return string encoded path
  783. */
  784. private function encodePath($path) {
  785. // slashes need to stay
  786. return str_replace('%2F', '/', rawurlencode($path));
  787. }
  788. /**
  789. * @When user :user favorites element :path
  790. */
  791. public function userFavoritesElement($user, $path) {
  792. $this->response = $this->changeFavStateOfAnElement($user, $path, 1, 0, null);
  793. }
  794. /**
  795. * @When user :user unfavorites element :path
  796. */
  797. public function userUnfavoritesElement($user, $path) {
  798. $this->response = $this->changeFavStateOfAnElement($user, $path, 0, 0, null);
  799. }
  800. /*Set the elements of a proppatch, $folderDepth requires 1 to see elements without children*/
  801. public function changeFavStateOfAnElement($user, $path, $favOrUnfav, $folderDepth, $properties = null) {
  802. $fullUrl = substr($this->baseUrl, 0, -4);
  803. $settings = [
  804. 'baseUri' => $fullUrl,
  805. 'userName' => $user,
  806. ];
  807. if ($user === 'admin') {
  808. $settings['password'] = $this->adminUser[1];
  809. } else {
  810. $settings['password'] = $this->regularUser;
  811. }
  812. $settings['authType'] = SClient::AUTH_BASIC;
  813. $client = new SClient($settings);
  814. if (!$properties) {
  815. $properties = [
  816. '{http://owncloud.org/ns}favorite' => $favOrUnfav
  817. ];
  818. }
  819. $response = $client->proppatch($this->getDavFilesPath($user) . $path, $properties, $folderDepth);
  820. return $response;
  821. }
  822. /**
  823. * @Given user :user stores etag of element :path
  824. */
  825. public function userStoresEtagOfElement($user, $path) {
  826. $propertiesTable = new \Behat\Gherkin\Node\TableNode([['{DAV:}getetag']]);
  827. $this->asGetsPropertiesOfFolderWith($user, 'entry', $path, $propertiesTable);
  828. $pathETAG[$path] = $this->response['{DAV:}getetag'];
  829. $this->storedETAG[$user] = $pathETAG;
  830. }
  831. /**
  832. * @Then etag of element :path of user :user has not changed
  833. */
  834. public function checkIfETAGHasNotChanged($path, $user) {
  835. $propertiesTable = new \Behat\Gherkin\Node\TableNode([['{DAV:}getetag']]);
  836. $this->asGetsPropertiesOfFolderWith($user, 'entry', $path, $propertiesTable);
  837. Assert::assertEquals($this->response['{DAV:}getetag'], $this->storedETAG[$user][$path]);
  838. }
  839. /**
  840. * @Then etag of element :path of user :user has changed
  841. */
  842. public function checkIfETAGHasChanged($path, $user) {
  843. $propertiesTable = new \Behat\Gherkin\Node\TableNode([['{DAV:}getetag']]);
  844. $this->asGetsPropertiesOfFolderWith($user, 'entry', $path, $propertiesTable);
  845. Assert::assertNotEquals($this->response['{DAV:}getetag'], $this->storedETAG[$user][$path]);
  846. }
  847. /**
  848. * @When Connecting to dav endpoint
  849. */
  850. public function connectingToDavEndpoint() {
  851. try {
  852. $this->response = $this->makeDavRequest(null, 'PROPFIND', '', []);
  853. } catch (\GuzzleHttp\Exception\ClientException $e) {
  854. $this->response = $e->getResponse();
  855. }
  856. }
  857. /**
  858. * @Then there are no duplicate headers
  859. */
  860. public function thereAreNoDuplicateHeaders() {
  861. $headers = $this->response->getHeaders();
  862. foreach ($headers as $headerName => $headerValues) {
  863. // if a header has multiple values, they must be different
  864. if (count($headerValues) > 1 && count(array_unique($headerValues)) < count($headerValues)) {
  865. throw new \Exception('Duplicate header found: ' . $headerName);
  866. }
  867. }
  868. }
  869. /**
  870. * @Then /^user "([^"]*)" in folder "([^"]*)" should have favorited the following elements$/
  871. * @param string $user
  872. * @param string $folder
  873. * @param \Behat\Gherkin\Node\TableNode|null $expectedElements
  874. */
  875. public function checkFavoritedElements($user, $folder, $expectedElements) {
  876. $elementList = $this->reportFolder($user,
  877. $folder,
  878. '<oc:favorite/>',
  879. '<oc:favorite>1</oc:favorite>');
  880. if ($expectedElements instanceof \Behat\Gherkin\Node\TableNode) {
  881. $elementRows = $expectedElements->getRows();
  882. $elementsSimplified = $this->simplifyArray($elementRows);
  883. foreach ($elementsSimplified as $expectedElement) {
  884. $webdavPath = "/" . $this->getDavFilesPath($user) . $expectedElement;
  885. if (!array_key_exists($webdavPath, $elementList)) {
  886. Assert::fail("$webdavPath" . " is not in report answer");
  887. }
  888. }
  889. }
  890. }
  891. /**
  892. * @When /^User "([^"]*)" deletes everything from folder "([^"]*)"$/
  893. * @param string $user
  894. * @param string $folder
  895. */
  896. public function userDeletesEverythingInFolder($user, $folder) {
  897. $elementList = $this->listFolder($user, $folder, 1);
  898. $elementListKeys = array_keys($elementList);
  899. array_shift($elementListKeys);
  900. $davPrefix = "/" . $this->getDavFilesPath($user);
  901. foreach ($elementListKeys as $element) {
  902. if (substr($element, 0, strlen($davPrefix)) == $davPrefix) {
  903. $element = substr($element, strlen($davPrefix));
  904. }
  905. $this->userDeletesFile($user, "element", $element);
  906. }
  907. }
  908. /**
  909. * @param string $user
  910. * @param string $path
  911. * @return int
  912. */
  913. private function getFileIdForPath($user, $path) {
  914. $propertiesTable = new \Behat\Gherkin\Node\TableNode([["{http://owncloud.org/ns}fileid"]]);
  915. $this->asGetsPropertiesOfFolderWith($user, 'file', $path, $propertiesTable);
  916. return (int)$this->response['{http://owncloud.org/ns}fileid'];
  917. }
  918. /**
  919. * @Given /^User "([^"]*)" stores id of file "([^"]*)"$/
  920. * @param string $user
  921. * @param string $path
  922. */
  923. public function userStoresFileIdForPath($user, $path) {
  924. $this->storedFileID = $this->getFileIdForPath($user, $path);
  925. }
  926. /**
  927. * @Given /^User "([^"]*)" checks id of file "([^"]*)"$/
  928. * @param string $user
  929. * @param string $path
  930. */
  931. public function userChecksFileIdForPath($user, $path) {
  932. $currentFileID = $this->getFileIdForPath($user, $path);
  933. Assert::assertEquals($currentFileID, $this->storedFileID);
  934. }
  935. /**
  936. * @Given /^user "([^"]*)" creates a file locally with "([^"]*)" x 5 MB chunks$/
  937. */
  938. public function userCreatesAFileLocallyWithChunks($arg1, $chunks) {
  939. $this->parts = [];
  940. for ($i = 1;$i <= (int)$chunks;$i++) {
  941. $randomletter = substr(str_shuffle("abcdefghijklmnopqrstuvwxyz"), 0, 1);
  942. file_put_contents('/tmp/part-upload-' . $i, str_repeat($randomletter, 5 * 1024 * 1024));
  943. $this->parts[] = '/tmp/part-upload-' . $i;
  944. }
  945. }
  946. /**
  947. * @Given user :user creates the chunk :id with a size of :size MB
  948. */
  949. public function userCreatesAChunk($user, $id, $size) {
  950. $randomletter = substr(str_shuffle("abcdefghijklmnopqrstuvwxyz"), 0, 1);
  951. file_put_contents('/tmp/part-upload-' . $id, str_repeat($randomletter, (int)$size * 1024 * 1024));
  952. $this->parts[] = '/tmp/part-upload-' . $id;
  953. }
  954. /**
  955. * @Then /^Downloaded content should be the created file$/
  956. */
  957. public function downloadedContentShouldBeTheCreatedFile() {
  958. $content = '';
  959. sort($this->parts);
  960. foreach ($this->parts as $part) {
  961. $content .= file_get_contents($part);
  962. }
  963. Assert::assertEquals($content, (string)$this->response->getBody());
  964. }
  965. /**
  966. * @Then /^the S3 multipart upload was successful with status "([^"]*)"$/
  967. */
  968. public function theSmultipartUploadWasSuccessful($status) {
  969. Assert::assertEquals((int)$status, $this->response->getStatusCode());
  970. }
  971. /**
  972. * @Then /^the upload should fail on object storage$/
  973. */
  974. public function theUploadShouldFailOnObjectStorage() {
  975. $descriptor = [
  976. 0 => ['pipe', 'r'],
  977. 1 => ['pipe', 'w'],
  978. 2 => ['pipe', 'w'],
  979. ];
  980. $process = proc_open('php occ config:system:get objectstore --no-ansi', $descriptor, $pipes, '../../');
  981. $lastCode = proc_close($process);
  982. if ($lastCode === 0) {
  983. $this->theHTTPStatusCodeShouldBe(500);
  984. }
  985. }
  986. }