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

WebDav.php 35KB

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