WebDav.php 24 KB

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