WebDav.php 23 KB

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