WebDav.php 24 KB

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