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 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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. /**
  53. * @Given /^using dav path "([^"]*)"$/
  54. */
  55. public function usingDavPath($davPath) {
  56. $this->davPath = $davPath;
  57. }
  58. /**
  59. * @Given /^using old dav path$/
  60. */
  61. public function usingOldDavPath() {
  62. $this->davPath = "remote.php/webdav";
  63. $this->usingOldDavPath = true;
  64. }
  65. /**
  66. * @Given /^using new dav path$/
  67. */
  68. public function usingNewDavPath() {
  69. $this->davPath = "remote.php/dav";
  70. $this->usingOldDavPath = false;
  71. }
  72. public function getDavFilesPath($user) {
  73. if ($this->usingOldDavPath === true) {
  74. return $this->davPath;
  75. } else {
  76. return $this->davPath . '/files/' . $user;
  77. }
  78. }
  79. public function makeDavRequest($user, $method, $path, $headers, $body = null, $type = "files") {
  80. if ($type === "files") {
  81. $fullUrl = substr($this->baseUrl, 0, -4) . $this->getDavFilesPath($user) . "$path";
  82. } elseif ($type === "uploads") {
  83. $fullUrl = substr($this->baseUrl, 0, -4) . $this->davPath . "$path";
  84. } else {
  85. $fullUrl = substr($this->baseUrl, 0, -4) . $this->davPath . '/' . $type . "$path";
  86. }
  87. $client = new GClient();
  88. $options = [
  89. 'headers' => $headers,
  90. 'body' => $body
  91. ];
  92. if ($user === 'admin') {
  93. $options['auth'] = $this->adminUser;
  94. } else {
  95. $options['auth'] = [$user, $this->regularUser];
  96. }
  97. return $client->request($method, $fullUrl, $options);
  98. }
  99. /**
  100. * @Given /^User "([^"]*)" moved (file|folder|entry) "([^"]*)" to "([^"]*)"$/
  101. * @param string $user
  102. * @param string $fileSource
  103. * @param string $fileDestination
  104. */
  105. public function userMovedFile($user, $entry, $fileSource, $fileDestination) {
  106. $fullUrl = substr($this->baseUrl, 0, -4) . $this->getDavFilesPath($user);
  107. $headers['Destination'] = $fullUrl . $fileDestination;
  108. $this->response = $this->makeDavRequest($user, "MOVE", $fileSource, $headers);
  109. Assert::assertEquals(201, $this->response->getStatusCode());
  110. }
  111. /**
  112. * @When /^User "([^"]*)" moves (file|folder|entry) "([^"]*)" to "([^"]*)"$/
  113. * @param string $user
  114. * @param string $fileSource
  115. * @param string $fileDestination
  116. */
  117. public function userMovesFile($user, $entry, $fileSource, $fileDestination) {
  118. $fullUrl = substr($this->baseUrl, 0, -4) . $this->getDavFilesPath($user);
  119. $headers['Destination'] = $fullUrl . $fileDestination;
  120. try {
  121. $this->response = $this->makeDavRequest($user, "MOVE", $fileSource, $headers);
  122. } catch (\GuzzleHttp\Exception\ClientException $e) {
  123. $this->response = $e->getResponse();
  124. }
  125. }
  126. /**
  127. * @When /^User "([^"]*)" copies file "([^"]*)" to "([^"]*)"$/
  128. * @param string $user
  129. * @param string $fileSource
  130. * @param string $fileDestination
  131. */
  132. public function userCopiesFileTo($user, $fileSource, $fileDestination) {
  133. $fullUrl = substr($this->baseUrl, 0, -4) . $this->getDavFilesPath($user);
  134. $headers['Destination'] = $fullUrl . $fileDestination;
  135. try {
  136. $this->response = $this->makeDavRequest($user, 'COPY', $fileSource, $headers);
  137. } catch (\GuzzleHttp\Exception\ClientException $e) {
  138. // 4xx and 5xx responses cause an exception
  139. $this->response = $e->getResponse();
  140. }
  141. }
  142. /**
  143. * @When /^Downloading file "([^"]*)" with range "([^"]*)"$/
  144. * @param string $fileSource
  145. * @param string $range
  146. */
  147. public function downloadFileWithRange($fileSource, $range) {
  148. $headers['Range'] = $range;
  149. $this->response = $this->makeDavRequest($this->currentUser, "GET", $fileSource, $headers);
  150. }
  151. /**
  152. * @When /^Downloading last public shared file with range "([^"]*)"$/
  153. * @param string $range
  154. */
  155. public function downloadPublicFileWithRange($range) {
  156. $token = $this->lastShareData->data->token;
  157. $fullUrl = substr($this->baseUrl, 0, -4) . "public.php/webdav";
  158. $client = new GClient();
  159. $options = [];
  160. $options['auth'] = [$token, ""];
  161. $options['headers'] = [
  162. 'Range' => $range
  163. ];
  164. $this->response = $client->request("GET", $fullUrl, $options);
  165. }
  166. /**
  167. * @When /^Downloading last public shared file inside a folder "([^"]*)" with range "([^"]*)"$/
  168. * @param string $range
  169. */
  170. public function downloadPublicFileInsideAFolderWithRange($path, $range) {
  171. $token = $this->lastShareData->data->token;
  172. $fullUrl = substr($this->baseUrl, 0, -4) . "public.php/webdav" . "$path";
  173. $client = new GClient();
  174. $options = [
  175. 'headers' => [
  176. 'Range' => $range
  177. ]
  178. ];
  179. $options['auth'] = [$token, ""];
  180. $this->response = $client->request("GET", $fullUrl, $options);
  181. }
  182. /**
  183. * @Then /^Downloaded content should be "([^"]*)"$/
  184. * @param string $content
  185. */
  186. public function downloadedContentShouldBe($content) {
  187. Assert::assertEquals($content, (string)$this->response->getBody());
  188. }
  189. /**
  190. * @Then /^Downloaded content when downloading file "([^"]*)" with range "([^"]*)" should be "([^"]*)"$/
  191. * @param string $fileSource
  192. * @param string $range
  193. * @param string $content
  194. */
  195. public function downloadedContentWhenDownloadindShouldBe($fileSource, $range, $content) {
  196. $this->downloadFileWithRange($fileSource, $range);
  197. $this->downloadedContentShouldBe($content);
  198. }
  199. /**
  200. * @When Downloading file :fileName
  201. * @param string $fileName
  202. */
  203. public function downloadingFile($fileName) {
  204. try {
  205. $this->response = $this->makeDavRequest($this->currentUser, 'GET', $fileName, []);
  206. } catch (\GuzzleHttp\Exception\ClientException $e) {
  207. $this->response = $e->getResponse();
  208. }
  209. }
  210. /**
  211. * @Then Downloaded content should start with :start
  212. * @param int $start
  213. * @throws \Exception
  214. */
  215. public function downloadedContentShouldStartWith($start) {
  216. if (strpos($this->response->getBody()->getContents(), $start) !== 0) {
  217. throw new \Exception(
  218. sprintf(
  219. "Expected '%s', got '%s'",
  220. $start,
  221. $this->response->getBody()->getContents()
  222. )
  223. );
  224. }
  225. }
  226. /**
  227. * @Then /^as "([^"]*)" gets properties of (file|folder|entry) "([^"]*)" with$/
  228. * @param string $user
  229. * @param string $elementType
  230. * @param string $path
  231. * @param \Behat\Gherkin\Node\TableNode|null $propertiesTable
  232. */
  233. public function asGetsPropertiesOfFolderWith($user, $elementType, $path, $propertiesTable) {
  234. $properties = null;
  235. if ($propertiesTable instanceof \Behat\Gherkin\Node\TableNode) {
  236. foreach ($propertiesTable->getRows() as $row) {
  237. $properties[] = $row[0];
  238. }
  239. }
  240. $this->response = $this->listFolder($user, $path, 0, $properties);
  241. }
  242. /**
  243. * @Then /^as "([^"]*)" the (file|folder|entry) "([^"]*)" does not exist$/
  244. * @param string $user
  245. * @param string $entry
  246. * @param string $path
  247. * @param \Behat\Gherkin\Node\TableNode|null $propertiesTable
  248. */
  249. public function asTheFileOrFolderDoesNotExist($user, $entry, $path) {
  250. $client = $this->getSabreClient($user);
  251. $response = $client->request('HEAD', $this->makeSabrePath($user, $path));
  252. if ($response['statusCode'] !== 404) {
  253. throw new \Exception($entry . ' "' . $path . '" expected to not exist (status code ' . $response['statusCode'] . ', expected 404)');
  254. }
  255. return $response;
  256. }
  257. /**
  258. * @Then /^as "([^"]*)" the (file|folder|entry) "([^"]*)" exists$/
  259. * @param string $user
  260. * @param string $entry
  261. * @param string $path
  262. */
  263. public function asTheFileOrFolderExists($user, $entry, $path) {
  264. $this->response = $this->listFolder($user, $path, 0);
  265. }
  266. /**
  267. * @Then the single response should contain a property :key with value :value
  268. * @param string $key
  269. * @param string $expectedValue
  270. * @throws \Exception
  271. */
  272. public function theSingleResponseShouldContainAPropertyWithValue($key, $expectedValue) {
  273. $keys = $this->response;
  274. if (!array_key_exists($key, $keys)) {
  275. throw new \Exception("Cannot find property \"$key\" with \"$expectedValue\"");
  276. }
  277. $value = $keys[$key];
  278. if ($value instanceof ResourceType) {
  279. $value = $value->getValue();
  280. if (empty($value)) {
  281. $value = '';
  282. } else {
  283. $value = $value[0];
  284. }
  285. }
  286. if ($value != $expectedValue) {
  287. throw new \Exception("Property \"$key\" found with value \"$value\", expected \"$expectedValue\"");
  288. }
  289. }
  290. /**
  291. * @Then the response should contain a share-types property with
  292. */
  293. public function theResponseShouldContainAShareTypesPropertyWith($table) {
  294. $keys = $this->response;
  295. if (!array_key_exists('{http://owncloud.org/ns}share-types', $keys)) {
  296. throw new \Exception("Cannot find property \"{http://owncloud.org/ns}share-types\"");
  297. }
  298. $foundTypes = [];
  299. $data = $keys['{http://owncloud.org/ns}share-types'];
  300. foreach ($data as $item) {
  301. if ($item['name'] !== '{http://owncloud.org/ns}share-type') {
  302. throw new \Exception('Invalid property found: "' . $item['name'] . '"');
  303. }
  304. $foundTypes[] = $item['value'];
  305. }
  306. foreach ($table->getRows() as $row) {
  307. $key = array_search($row[0], $foundTypes);
  308. if ($key === false) {
  309. throw new \Exception('Expected type ' . $row[0] . ' not found');
  310. }
  311. unset($foundTypes[$key]);
  312. }
  313. if ($foundTypes !== []) {
  314. throw new \Exception('Found more share types then specified: ' . $foundTypes);
  315. }
  316. }
  317. /**
  318. * @Then the response should contain an empty property :property
  319. * @param string $property
  320. * @throws \Exception
  321. */
  322. public function theResponseShouldContainAnEmptyProperty($property) {
  323. $properties = $this->response;
  324. if (!array_key_exists($property, $properties)) {
  325. throw new \Exception("Cannot find property \"$property\"");
  326. }
  327. if ($properties[$property] !== null) {
  328. throw new \Exception("Property \"$property\" is not empty");
  329. }
  330. }
  331. /*Returns the elements of a propfind, $folderDepth requires 1 to see elements without children*/
  332. public function listFolder($user, $path, $folderDepth, $properties = null) {
  333. $client = $this->getSabreClient($user);
  334. if (!$properties) {
  335. $properties = [
  336. '{DAV:}getetag'
  337. ];
  338. }
  339. $response = $client->propfind($this->makeSabrePath($user, $path), $properties, $folderDepth);
  340. return $response;
  341. }
  342. /* Returns the elements of a report command
  343. * @param string $user
  344. * @param string $path
  345. * @param string $properties properties which needs to be included in the report
  346. * @param string $filterRules filter-rules to choose what needs to appear in the report
  347. */
  348. public function reportFolder($user, $path, $properties, $filterRules) {
  349. $client = $this->getSabreClient($user);
  350. $body = '<?xml version="1.0" encoding="utf-8" ?>
  351. <oc:filter-files xmlns:a="DAV:" xmlns:oc="http://owncloud.org/ns" >
  352. <a:prop>
  353. ' . $properties . '
  354. </a:prop>
  355. <oc:filter-rules>
  356. ' . $filterRules . '
  357. </oc:filter-rules>
  358. </oc:filter-files>';
  359. $response = $client->request('REPORT', $this->makeSabrePath($user, $path), $body);
  360. $parsedResponse = $client->parseMultistatus($response['body']);
  361. return $parsedResponse;
  362. }
  363. public function makeSabrePath($user, $path, $type = 'files') {
  364. if ($type === 'files') {
  365. return $this->encodePath($this->getDavFilesPath($user) . $path);
  366. } else {
  367. return $this->encodePath($this->davPath . '/' . $type . '/' . $user . '/' . $path);
  368. }
  369. }
  370. public function getSabreClient($user) {
  371. $fullUrl = substr($this->baseUrl, 0, -4);
  372. $settings = [
  373. 'baseUri' => $fullUrl,
  374. 'userName' => $user,
  375. ];
  376. if ($user === 'admin') {
  377. $settings['password'] = $this->adminUser[1];
  378. } else {
  379. $settings['password'] = $this->regularUser;
  380. }
  381. $settings['authType'] = SClient::AUTH_BASIC;
  382. return new SClient($settings);
  383. }
  384. /**
  385. * @Then /^user "([^"]*)" should see following elements$/
  386. * @param string $user
  387. * @param \Behat\Gherkin\Node\TableNode|null $expectedElements
  388. */
  389. public function checkElementList($user, $expectedElements) {
  390. $elementList = $this->listFolder($user, '/', 3);
  391. if ($expectedElements instanceof \Behat\Gherkin\Node\TableNode) {
  392. $elementRows = $expectedElements->getRows();
  393. $elementsSimplified = $this->simplifyArray($elementRows);
  394. foreach ($elementsSimplified as $expectedElement) {
  395. $webdavPath = "/" . $this->getDavFilesPath($user) . $expectedElement;
  396. if (!array_key_exists($webdavPath, $elementList)) {
  397. Assert::fail("$webdavPath" . " is not in propfind answer");
  398. }
  399. }
  400. }
  401. }
  402. /**
  403. * @When User :user uploads file :source to :destination
  404. * @param string $user
  405. * @param string $source
  406. * @param string $destination
  407. */
  408. public function userUploadsAFileTo($user, $source, $destination) {
  409. $file = \GuzzleHttp\Psr7\stream_for(fopen($source, 'r'));
  410. try {
  411. $this->response = $this->makeDavRequest($user, "PUT", $destination, [], $file);
  412. } catch (\GuzzleHttp\Exception\ServerException $e) {
  413. // 5xx responses cause a server exception
  414. $this->response = $e->getResponse();
  415. } catch (\GuzzleHttp\Exception\ClientException $e) {
  416. // 4xx responses cause a client exception
  417. $this->response = $e->getResponse();
  418. }
  419. }
  420. /**
  421. * @When User :user adds a file of :bytes bytes to :destination
  422. * @param string $user
  423. * @param string $bytes
  424. * @param string $destination
  425. */
  426. public function userAddsAFileTo($user, $bytes, $destination) {
  427. $filename = "filespecificSize.txt";
  428. $this->createFileSpecificSize($filename, $bytes);
  429. Assert::assertEquals(1, file_exists("work/$filename"));
  430. $this->userUploadsAFileTo($user, "work/$filename", $destination);
  431. $this->removeFile("work/", $filename);
  432. $expectedElements = new \Behat\Gherkin\Node\TableNode([["$destination"]]);
  433. $this->checkElementList($user, $expectedElements);
  434. }
  435. /**
  436. * @When User :user uploads file with content :content to :destination
  437. */
  438. public function userUploadsAFileWithContentTo($user, $content, $destination) {
  439. $file = \GuzzleHttp\Psr7\stream_for($content);
  440. try {
  441. $this->response = $this->makeDavRequest($user, "PUT", $destination, [], $file);
  442. } catch (\GuzzleHttp\Exception\ServerException $e) {
  443. // 5xx responses cause a server exception
  444. $this->response = $e->getResponse();
  445. } catch (\GuzzleHttp\Exception\ClientException $e) {
  446. // 4xx responses cause a client exception
  447. $this->response = $e->getResponse();
  448. }
  449. }
  450. /**
  451. * @When /^User "([^"]*)" deletes (file|folder) "([^"]*)"$/
  452. * @param string $user
  453. * @param string $type
  454. * @param string $file
  455. */
  456. public function userDeletesFile($user, $type, $file) {
  457. try {
  458. $this->response = $this->makeDavRequest($user, 'DELETE', $file, []);
  459. } catch (\GuzzleHttp\Exception\ServerException $e) {
  460. // 5xx responses cause a server exception
  461. $this->response = $e->getResponse();
  462. } catch (\GuzzleHttp\Exception\ClientException $e) {
  463. // 4xx responses cause a client exception
  464. $this->response = $e->getResponse();
  465. }
  466. }
  467. /**
  468. * @Given User :user created a folder :destination
  469. * @param string $user
  470. * @param string $destination
  471. */
  472. public function userCreatedAFolder($user, $destination) {
  473. try {
  474. $destination = '/' . ltrim($destination, '/');
  475. $this->response = $this->makeDavRequest($user, "MKCOL", $destination, []);
  476. } catch (\GuzzleHttp\Exception\ServerException $e) {
  477. // 5xx responses cause a server exception
  478. $this->response = $e->getResponse();
  479. } catch (\GuzzleHttp\Exception\ClientException $e) {
  480. // 4xx responses cause a client exception
  481. $this->response = $e->getResponse();
  482. }
  483. }
  484. /**
  485. * @Given user :user uploads chunk file :num of :total with :data to :destination
  486. * @param string $user
  487. * @param int $num
  488. * @param int $total
  489. * @param string $data
  490. * @param string $destination
  491. */
  492. public function userUploadsChunkFileOfWithToWithChecksum($user, $num, $total, $data, $destination) {
  493. $num -= 1;
  494. $data = \GuzzleHttp\Psr7\stream_for($data);
  495. $file = $destination . '-chunking-42-' . $total . '-' . $num;
  496. $this->makeDavRequest($user, 'PUT', $file, ['OC-Chunked' => '1'], $data, "uploads");
  497. }
  498. /**
  499. * @Given user :user uploads bulked files :name1 with :content1 and :name2 with :content2 and :name3 with :content3
  500. * @param string $user
  501. * @param string $name1
  502. * @param string $content1
  503. * @param string $name2
  504. * @param string $content2
  505. * @param string $name3
  506. * @param string $content3
  507. */
  508. public function userUploadsChunkedFiles($user, $name1, $content1, $name2, $content2, $name3, $content3) {
  509. $boundary = "boundary_azertyuiop";
  510. $body = "";
  511. $body .= '--'.$boundary."\r\n";
  512. $body .= "X-File-Path: ".$name1."\r\n";
  513. $body .= "X-File-MD5: f6a6263167c92de8644ac998b3c4e4d1\r\n";
  514. $body .= "Content-Length: ".strlen($content1)."\r\n";
  515. $body .= "\r\n";
  516. $body .= $content1."\r\n";
  517. $body .= '--'.$boundary."\r\n";
  518. $body .= "X-File-Path: ".$name2."\r\n";
  519. $body .= "X-File-MD5: 87c7d4068be07d390a1fffd21bf1e944\r\n";
  520. $body .= "Content-Length: ".strlen($content2)."\r\n";
  521. $body .= "\r\n";
  522. $body .= $content2."\r\n";
  523. $body .= '--'.$boundary."\r\n";
  524. $body .= "X-File-Path: ".$name3."\r\n";
  525. $body .= "X-File-MD5: e86a1cf0678099986a901c79086f5617\r\n";
  526. $body .= "Content-Length: ".strlen($content3)."\r\n";
  527. $body .= "\r\n";
  528. $body .= $content3."\r\n";
  529. $body .= '--'.$boundary."--\r\n";
  530. $stream = fopen('php://temp', 'r+');
  531. fwrite($stream, $body);
  532. rewind($stream);
  533. $client = new GClient();
  534. $options = [
  535. 'auth' => [$user, $this->regularUser],
  536. 'headers' => [
  537. 'Content-Type' => 'multipart/related; boundary='.$boundary,
  538. 'Content-Length' => (string)strlen($body),
  539. ],
  540. 'body' => $body
  541. ];
  542. return $client->request("POST", substr($this->baseUrl, 0, -4) . "remote.php/dav/bulk", $options);
  543. }
  544. /**
  545. * @Given user :user creates a new chunking upload with id :id
  546. */
  547. public function userCreatesANewChunkingUploadWithId($user, $id) {
  548. $destination = '/uploads/' . $user . '/' . $id;
  549. $this->makeDavRequest($user, 'MKCOL', $destination, [], null, "uploads");
  550. }
  551. /**
  552. * @Given user :user uploads new chunk file :num with :data to id :id
  553. */
  554. public function userUploadsNewChunkFileOfWithToId($user, $num, $data, $id) {
  555. $data = \GuzzleHttp\Psr7\stream_for($data);
  556. $destination = '/uploads/' . $user . '/' . $id . '/' . $num;
  557. $this->makeDavRequest($user, 'PUT', $destination, [], $data, "uploads");
  558. }
  559. /**
  560. * @Given user :user moves new chunk file with id :id to :dest
  561. */
  562. public function userMovesNewChunkFileWithIdToMychunkedfile($user, $id, $dest) {
  563. $source = '/uploads/' . $user . '/' . $id . '/.file';
  564. $destination = substr($this->baseUrl, 0, -4) . $this->getDavFilesPath($user) . $dest;
  565. $this->makeDavRequest($user, 'MOVE', $source, [
  566. 'Destination' => $destination
  567. ], null, "uploads");
  568. }
  569. /**
  570. * @Then user :user moves new chunk file with id :id to :dest with size :size
  571. */
  572. public function userMovesNewChunkFileWithIdToMychunkedfileWithSize($user, $id, $dest, $size) {
  573. $source = '/uploads/' . $user . '/' . $id . '/.file';
  574. $destination = substr($this->baseUrl, 0, -4) . $this->getDavFilesPath($user) . $dest;
  575. try {
  576. $this->response = $this->makeDavRequest($user, 'MOVE', $source, [
  577. 'Destination' => $destination,
  578. 'OC-Total-Length' => $size
  579. ], null, "uploads");
  580. } catch (\GuzzleHttp\Exception\BadResponseException $ex) {
  581. $this->response = $ex->getResponse();
  582. }
  583. }
  584. /**
  585. * @Given /^Downloading file "([^"]*)" as "([^"]*)"$/
  586. */
  587. public function downloadingFileAs($fileName, $user) {
  588. try {
  589. $this->response = $this->makeDavRequest($user, 'GET', $fileName, []);
  590. } catch (\GuzzleHttp\Exception\ServerException $e) {
  591. // 5xx responses cause a server exception
  592. $this->response = $e->getResponse();
  593. } catch (\GuzzleHttp\Exception\ClientException $e) {
  594. // 4xx responses cause a client exception
  595. $this->response = $e->getResponse();
  596. }
  597. }
  598. /**
  599. * URL encodes the given path but keeps the slashes
  600. *
  601. * @param string $path to encode
  602. * @return string encoded path
  603. */
  604. private function encodePath($path) {
  605. // slashes need to stay
  606. return str_replace('%2F', '/', rawurlencode($path));
  607. }
  608. /**
  609. * @When user :user favorites element :path
  610. */
  611. public function userFavoritesElement($user, $path) {
  612. $this->response = $this->changeFavStateOfAnElement($user, $path, 1, 0, null);
  613. }
  614. /**
  615. * @When user :user unfavorites element :path
  616. */
  617. public function userUnfavoritesElement($user, $path) {
  618. $this->response = $this->changeFavStateOfAnElement($user, $path, 0, 0, null);
  619. }
  620. /*Set the elements of a proppatch, $folderDepth requires 1 to see elements without children*/
  621. public function changeFavStateOfAnElement($user, $path, $favOrUnfav, $folderDepth, $properties = null) {
  622. $fullUrl = substr($this->baseUrl, 0, -4);
  623. $settings = [
  624. 'baseUri' => $fullUrl,
  625. 'userName' => $user,
  626. ];
  627. if ($user === 'admin') {
  628. $settings['password'] = $this->adminUser[1];
  629. } else {
  630. $settings['password'] = $this->regularUser;
  631. }
  632. $settings['authType'] = SClient::AUTH_BASIC;
  633. $client = new SClient($settings);
  634. if (!$properties) {
  635. $properties = [
  636. '{http://owncloud.org/ns}favorite' => $favOrUnfav
  637. ];
  638. }
  639. $response = $client->proppatch($this->getDavFilesPath($user) . $path, $properties, $folderDepth);
  640. return $response;
  641. }
  642. /**
  643. * @Given user :user stores etag of element :path
  644. */
  645. public function userStoresEtagOfElement($user, $path) {
  646. $propertiesTable = new \Behat\Gherkin\Node\TableNode([['{DAV:}getetag']]);
  647. $this->asGetsPropertiesOfFolderWith($user, 'entry', $path, $propertiesTable);
  648. $pathETAG[$path] = $this->response['{DAV:}getetag'];
  649. $this->storedETAG[$user] = $pathETAG;
  650. }
  651. /**
  652. * @Then etag of element :path of user :user has not changed
  653. */
  654. public function checkIfETAGHasNotChanged($path, $user) {
  655. $propertiesTable = new \Behat\Gherkin\Node\TableNode([['{DAV:}getetag']]);
  656. $this->asGetsPropertiesOfFolderWith($user, 'entry', $path, $propertiesTable);
  657. Assert::assertEquals($this->response['{DAV:}getetag'], $this->storedETAG[$user][$path]);
  658. }
  659. /**
  660. * @Then etag of element :path of user :user has changed
  661. */
  662. public function checkIfETAGHasChanged($path, $user) {
  663. $propertiesTable = new \Behat\Gherkin\Node\TableNode([['{DAV:}getetag']]);
  664. $this->asGetsPropertiesOfFolderWith($user, 'entry', $path, $propertiesTable);
  665. Assert::assertNotEquals($this->response['{DAV:}getetag'], $this->storedETAG[$user][$path]);
  666. }
  667. /**
  668. * @When Connecting to dav endpoint
  669. */
  670. public function connectingToDavEndpoint() {
  671. try {
  672. $this->response = $this->makeDavRequest(null, 'PROPFIND', '', []);
  673. } catch (\GuzzleHttp\Exception\ClientException $e) {
  674. $this->response = $e->getResponse();
  675. }
  676. }
  677. /**
  678. * @Then there are no duplicate headers
  679. */
  680. public function thereAreNoDuplicateHeaders() {
  681. $headers = $this->response->getHeaders();
  682. foreach ($headers as $headerName => $headerValues) {
  683. // if a header has multiple values, they must be different
  684. if (count($headerValues) > 1 && count(array_unique($headerValues)) < count($headerValues)) {
  685. throw new \Exception('Duplicate header found: ' . $headerName);
  686. }
  687. }
  688. }
  689. /**
  690. * @Then /^user "([^"]*)" in folder "([^"]*)" should have favorited the following elements$/
  691. * @param string $user
  692. * @param string $folder
  693. * @param \Behat\Gherkin\Node\TableNode|null $expectedElements
  694. */
  695. public function checkFavoritedElements($user, $folder, $expectedElements) {
  696. $elementList = $this->reportFolder($user,
  697. $folder,
  698. '<oc:favorite/>',
  699. '<oc:favorite>1</oc:favorite>');
  700. if ($expectedElements instanceof \Behat\Gherkin\Node\TableNode) {
  701. $elementRows = $expectedElements->getRows();
  702. $elementsSimplified = $this->simplifyArray($elementRows);
  703. foreach ($elementsSimplified as $expectedElement) {
  704. $webdavPath = "/" . $this->getDavFilesPath($user) . $expectedElement;
  705. if (!array_key_exists($webdavPath, $elementList)) {
  706. Assert::fail("$webdavPath" . " is not in report answer");
  707. }
  708. }
  709. }
  710. }
  711. /**
  712. * @When /^User "([^"]*)" deletes everything from folder "([^"]*)"$/
  713. * @param string $user
  714. * @param string $folder
  715. */
  716. public function userDeletesEverythingInFolder($user, $folder) {
  717. $elementList = $this->listFolder($user, $folder, 1);
  718. $elementListKeys = array_keys($elementList);
  719. array_shift($elementListKeys);
  720. $davPrefix = "/" . $this->getDavFilesPath($user);
  721. foreach ($elementListKeys as $element) {
  722. if (substr($element, 0, strlen($davPrefix)) == $davPrefix) {
  723. $element = substr($element, strlen($davPrefix));
  724. }
  725. $this->userDeletesFile($user, "element", $element);
  726. }
  727. }
  728. /**
  729. * @param string $user
  730. * @param string $path
  731. * @return int
  732. */
  733. private function getFileIdForPath($user, $path) {
  734. $propertiesTable = new \Behat\Gherkin\Node\TableNode([["{http://owncloud.org/ns}fileid"]]);
  735. $this->asGetsPropertiesOfFolderWith($user, 'file', $path, $propertiesTable);
  736. return (int)$this->response['{http://owncloud.org/ns}fileid'];
  737. }
  738. /**
  739. * @Given /^User "([^"]*)" stores id of file "([^"]*)"$/
  740. * @param string $user
  741. * @param string $path
  742. */
  743. public function userStoresFileIdForPath($user, $path) {
  744. $this->storedFileID = $this->getFileIdForPath($user, $path);
  745. }
  746. /**
  747. * @Given /^User "([^"]*)" checks id of file "([^"]*)"$/
  748. * @param string $user
  749. * @param string $path
  750. */
  751. public function userChecksFileIdForPath($user, $path) {
  752. $currentFileID = $this->getFileIdForPath($user, $path);
  753. Assert::assertEquals($currentFileID, $this->storedFileID);
  754. }
  755. }