WebDav.php 24 KB

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