WebDav.php 35 KB

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