WebDav.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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. $headers['Range'] = $range;
  156. $client = new GClient();
  157. $options = [];
  158. $options['auth'] = [$token, ""];
  159. $request = $client->createRequest("GET", $fullUrl, $options);
  160. $request->addHeader('Range', $range);
  161. $this->response = $client->send($request);
  162. }
  163. /**
  164. * @When /^Downloading last public shared file inside a folder "([^"]*)" with range "([^"]*)"$/
  165. * @param string $range
  166. */
  167. public function downloadPublicFileInsideAFolderWithRange($path, $range){
  168. $token = $this->lastShareData->data->token;
  169. $fullUrl = substr($this->baseUrl, 0, -4) . "public.php/webdav" . "$path";
  170. $headers['Range'] = $range;
  171. $client = new GClient();
  172. $options = [];
  173. $options['auth'] = [$token, ""];
  174. $request = $client->createRequest("GET", $fullUrl, $options);
  175. $request->addHeader('Range', $range);
  176. $this->response = $client->send($request);
  177. }
  178. /**
  179. * @Then /^Downloaded content should be "([^"]*)"$/
  180. * @param string $content
  181. */
  182. public function downloadedContentShouldBe($content){
  183. PHPUnit_Framework_Assert::assertEquals($content, (string)$this->response->getBody());
  184. }
  185. /**
  186. * @Then /^Downloaded content when downloading file "([^"]*)" with range "([^"]*)" should be "([^"]*)"$/
  187. * @param string $fileSource
  188. * @param string $range
  189. * @param string $content
  190. */
  191. public function downloadedContentWhenDownloadindShouldBe($fileSource, $range, $content){
  192. $this->downloadFileWithRange($fileSource, $range);
  193. $this->downloadedContentShouldBe($content);
  194. }
  195. /**
  196. * @When Downloading file :fileName
  197. * @param string $fileName
  198. */
  199. public function downloadingFile($fileName) {
  200. try {
  201. $this->response = $this->makeDavRequest($this->currentUser, 'GET', $fileName, []);
  202. } catch (\GuzzleHttp\Exception\ClientException $e) {
  203. $this->response = $e->getResponse();
  204. }
  205. }
  206. /**
  207. * @Then The following headers should be set
  208. * @param \Behat\Gherkin\Node\TableNode $table
  209. * @throws \Exception
  210. */
  211. public function theFollowingHeadersShouldBeSet(\Behat\Gherkin\Node\TableNode $table) {
  212. foreach($table->getTable() as $header) {
  213. $headerName = $header[0];
  214. $expectedHeaderValue = $header[1];
  215. $returnedHeader = $this->response->getHeader($headerName);
  216. if($returnedHeader !== $expectedHeaderValue) {
  217. throw new \Exception(
  218. sprintf(
  219. "Expected value '%s' for header '%s', got '%s'",
  220. $expectedHeaderValue,
  221. $headerName,
  222. $returnedHeader
  223. )
  224. );
  225. }
  226. }
  227. }
  228. /**
  229. * @Then Downloaded content should start with :start
  230. * @param int $start
  231. * @throws \Exception
  232. */
  233. public function downloadedContentShouldStartWith($start) {
  234. if(strpos($this->response->getBody()->getContents(), $start) !== 0) {
  235. throw new \Exception(
  236. sprintf(
  237. "Expected '%s', got '%s'",
  238. $start,
  239. $this->response->getBody()->getContents()
  240. )
  241. );
  242. }
  243. }
  244. /**
  245. * @Then /^as "([^"]*)" gets properties of (file|folder|entry) "([^"]*)" with$/
  246. * @param string $user
  247. * @param string $elementType
  248. * @param string $path
  249. * @param \Behat\Gherkin\Node\TableNode|null $propertiesTable
  250. */
  251. public function asGetsPropertiesOfFolderWith($user, $elementType, $path, $propertiesTable) {
  252. $properties = null;
  253. if ($propertiesTable instanceof \Behat\Gherkin\Node\TableNode) {
  254. foreach ($propertiesTable->getRows() as $row) {
  255. $properties[] = $row[0];
  256. }
  257. }
  258. $this->response = $this->listFolder($user, $path, 0, $properties);
  259. }
  260. /**
  261. * @Then /^as "([^"]*)" the (file|folder|entry) "([^"]*)" does not exist$/
  262. * @param string $user
  263. * @param string $entry
  264. * @param string $path
  265. * @param \Behat\Gherkin\Node\TableNode|null $propertiesTable
  266. */
  267. public function asTheFileOrFolderDoesNotExist($user, $entry, $path) {
  268. $client = $this->getSabreClient($user);
  269. $response = $client->request('HEAD', $this->makeSabrePath($user, $path));
  270. if ($response['statusCode'] !== 404) {
  271. throw new \Exception($entry . ' "' . $path . '" expected to not exist (status code ' . $response['statusCode'] . ', expected 404)');
  272. }
  273. return $response;
  274. }
  275. /**
  276. * @Then /^as "([^"]*)" the (file|folder|entry) "([^"]*)" exists$/
  277. * @param string $user
  278. * @param string $entry
  279. * @param string $path
  280. */
  281. public function asTheFileOrFolderExists($user, $entry, $path) {
  282. $this->response = $this->listFolder($user, $path, 0);
  283. }
  284. /**
  285. * @Then the single response should contain a property :key with value :value
  286. * @param string $key
  287. * @param string $expectedValue
  288. * @throws \Exception
  289. */
  290. public function theSingleResponseShouldContainAPropertyWithValue($key, $expectedValue) {
  291. $keys = $this->response;
  292. if (!array_key_exists($key, $keys)) {
  293. throw new \Exception("Cannot find property \"$key\" with \"$expectedValue\"");
  294. }
  295. $value = $keys[$key];
  296. if ($value instanceof ResourceType) {
  297. $value = $value->getValue();
  298. if (empty($value)) {
  299. $value = '';
  300. } else {
  301. $value = $value[0];
  302. }
  303. }
  304. if ($value != $expectedValue) {
  305. throw new \Exception("Property \"$key\" found with value \"$value\", expected \"$expectedValue\"");
  306. }
  307. }
  308. /**
  309. * @Then the response should contain a share-types property with
  310. */
  311. public function theResponseShouldContainAShareTypesPropertyWith($table)
  312. {
  313. $keys = $this->response;
  314. if (!array_key_exists('{http://owncloud.org/ns}share-types', $keys)) {
  315. throw new \Exception("Cannot find property \"{http://owncloud.org/ns}share-types\"");
  316. }
  317. $foundTypes = [];
  318. $data = $keys['{http://owncloud.org/ns}share-types'];
  319. foreach ($data as $item) {
  320. if ($item['name'] !== '{http://owncloud.org/ns}share-type') {
  321. throw new \Exception('Invalid property found: "' . $item['name'] . '"');
  322. }
  323. $foundTypes[] = $item['value'];
  324. }
  325. foreach ($table->getRows() as $row) {
  326. $key = array_search($row[0], $foundTypes);
  327. if ($key === false) {
  328. throw new \Exception('Expected type ' . $row[0] . ' not found');
  329. }
  330. unset($foundTypes[$key]);
  331. }
  332. if ($foundTypes !== []) {
  333. throw new \Exception('Found more share types then specified: ' . $foundTypes);
  334. }
  335. }
  336. /**
  337. * @Then the response should contain an empty property :property
  338. * @param string $property
  339. * @throws \Exception
  340. */
  341. public function theResponseShouldContainAnEmptyProperty($property) {
  342. $properties = $this->response;
  343. if (!array_key_exists($property, $properties)) {
  344. throw new \Exception("Cannot find property \"$property\"");
  345. }
  346. if ($properties[$property] !== null) {
  347. throw new \Exception("Property \"$property\" is not empty");
  348. }
  349. }
  350. /*Returns the elements of a propfind, $folderDepth requires 1 to see elements without children*/
  351. public function listFolder($user, $path, $folderDepth, $properties = null){
  352. $client = $this->getSabreClient($user);
  353. if (!$properties) {
  354. $properties = [
  355. '{DAV:}getetag'
  356. ];
  357. }
  358. $response = $client->propfind($this->makeSabrePath($user, $path), $properties, $folderDepth);
  359. return $response;
  360. }
  361. /* Returns the elements of a report command
  362. * @param string $user
  363. * @param string $path
  364. * @param string $properties properties which needs to be included in the report
  365. * @param string $filterRules filter-rules to choose what needs to appear in the report
  366. */
  367. public function reportFolder($user, $path, $properties, $filterRules){
  368. $client = $this->getSabreClient($user);
  369. $body = '<?xml version="1.0" encoding="utf-8" ?>
  370. <oc:filter-files xmlns:a="DAV:" xmlns:oc="http://owncloud.org/ns" >
  371. <a:prop>
  372. ' . $properties . '
  373. </a:prop>
  374. <oc:filter-rules>
  375. ' . $filterRules . '
  376. </oc:filter-rules>
  377. </oc:filter-files>';
  378. $response = $client->request('REPORT', $this->makeSabrePath($user, $path), $body);
  379. $parsedResponse = $client->parseMultistatus($response['body']);
  380. return $parsedResponse;
  381. }
  382. public function makeSabrePath($user, $path) {
  383. return $this->encodePath($this->getDavFilesPath($user) . $path);
  384. }
  385. public function getSabreClient($user) {
  386. $fullUrl = substr($this->baseUrl, 0, -4);
  387. $settings = array(
  388. 'baseUri' => $fullUrl,
  389. 'userName' => $user,
  390. );
  391. if ($user === 'admin') {
  392. $settings['password'] = $this->adminUser[1];
  393. } else {
  394. $settings['password'] = $this->regularUser;
  395. }
  396. return new SClient($settings);
  397. }
  398. /**
  399. * @Then /^user "([^"]*)" should see following elements$/
  400. * @param string $user
  401. * @param \Behat\Gherkin\Node\TableNode|null $expectedElements
  402. */
  403. public function checkElementList($user, $expectedElements){
  404. $elementList = $this->listFolder($user, '/', 3);
  405. if ($expectedElements instanceof \Behat\Gherkin\Node\TableNode) {
  406. $elementRows = $expectedElements->getRows();
  407. $elementsSimplified = $this->simplifyArray($elementRows);
  408. foreach($elementsSimplified as $expectedElement) {
  409. $webdavPath = "/" . $this->getDavFilesPath($user) . $expectedElement;
  410. if (!array_key_exists($webdavPath,$elementList)){
  411. PHPUnit_Framework_Assert::fail("$webdavPath" . " is not in propfind answer");
  412. }
  413. }
  414. }
  415. }
  416. /**
  417. * @When User :user uploads file :source to :destination
  418. * @param string $user
  419. * @param string $source
  420. * @param string $destination
  421. */
  422. public function userUploadsAFileTo($user, $source, $destination)
  423. {
  424. $file = \GuzzleHttp\Stream\Stream::factory(fopen($source, 'r'));
  425. try {
  426. $this->response = $this->makeDavRequest($user, "PUT", $destination, [], $file);
  427. } catch (\GuzzleHttp\Exception\ServerException $e) {
  428. // 4xx and 5xx responses cause an exception
  429. $this->response = $e->getResponse();
  430. }
  431. }
  432. /**
  433. * @When User :user adds a file of :bytes bytes to :destination
  434. * @param string $user
  435. * @param string $bytes
  436. * @param string $destination
  437. */
  438. public function userAddsAFileTo($user, $bytes, $destination){
  439. $filename = "filespecificSize.txt";
  440. $this->createFileSpecificSize($filename, $bytes);
  441. PHPUnit_Framework_Assert::assertEquals(1, file_exists("work/$filename"));
  442. $this->userUploadsAFileTo($user, "work/$filename", $destination);
  443. $this->removeFile("work/", $filename);
  444. $expectedElements = new \Behat\Gherkin\Node\TableNode([["$destination"]]);
  445. $this->checkElementList($user, $expectedElements);
  446. }
  447. /**
  448. * @When User :user uploads file with content :content to :destination
  449. */
  450. public function userUploadsAFileWithContentTo($user, $content, $destination)
  451. {
  452. $file = \GuzzleHttp\Stream\Stream::factory($content);
  453. try {
  454. $this->response = $this->makeDavRequest($user, "PUT", $destination, [], $file);
  455. } catch (\GuzzleHttp\Exception\ServerException $e) {
  456. // 4xx and 5xx responses cause an exception
  457. $this->response = $e->getResponse();
  458. }
  459. }
  460. /**
  461. * @When /^User "([^"]*)" deletes (file|folder) "([^"]*)"$/
  462. * @param string $user
  463. * @param string $type
  464. * @param string $file
  465. */
  466. public function userDeletesFile($user, $type, $file) {
  467. try {
  468. $this->response = $this->makeDavRequest($user, 'DELETE', $file, []);
  469. } catch (\GuzzleHttp\Exception\ServerException $e) {
  470. // 4xx and 5xx responses cause an exception
  471. $this->response = $e->getResponse();
  472. }
  473. }
  474. /**
  475. * @Given User :user created a folder :destination
  476. * @param string $user
  477. * @param string $destination
  478. */
  479. public function userCreatedAFolder($user, $destination) {
  480. try {
  481. $destination = '/' . ltrim($destination, '/');
  482. $this->response = $this->makeDavRequest($user, "MKCOL", $destination, []);
  483. } catch (\GuzzleHttp\Exception\ServerException $e) {
  484. // 4xx and 5xx responses cause an exception
  485. $this->response = $e->getResponse();
  486. }
  487. }
  488. /**
  489. * @Given user :user uploads chunk file :num of :total with :data to :destination
  490. * @param string $user
  491. * @param int $num
  492. * @param int $total
  493. * @param string $data
  494. * @param string $destination
  495. */
  496. public function userUploadsChunkFileOfWithToWithChecksum($user, $num, $total, $data, $destination)
  497. {
  498. $num -= 1;
  499. $data = \GuzzleHttp\Stream\Stream::factory($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. {
  508. $destination = '/uploads/'.$user.'/'.$id;
  509. $this->makeDavRequest($user, 'MKCOL', $destination, [], null, "uploads");
  510. }
  511. /**
  512. * @Given user :user uploads new chunk file :num with :data to id :id
  513. */
  514. public function userUploadsNewChunkFileOfWithToId($user, $num, $data, $id)
  515. {
  516. $data = \GuzzleHttp\Stream\Stream::factory($data);
  517. $destination = '/uploads/'. $user .'/'. $id .'/' . $num;
  518. $this->makeDavRequest($user, 'PUT', $destination, [], $data, "uploads");
  519. }
  520. /**
  521. * @Given user :user moves new chunk file with id :id to :dest
  522. */
  523. public function userMovesNewChunkFileWithIdToMychunkedfile($user, $id, $dest)
  524. {
  525. $source = '/uploads/' . $user . '/' . $id . '/.file';
  526. $destination = substr($this->baseUrl, 0, -4) . $this->getDavFilesPath($user) . $dest;
  527. $this->makeDavRequest($user, 'MOVE', $source, [
  528. 'Destination' => $destination
  529. ], null, "uploads");
  530. }
  531. /**
  532. * @Given /^Downloading file "([^"]*)" as "([^"]*)"$/
  533. */
  534. public function downloadingFileAs($fileName, $user) {
  535. try {
  536. $this->response = $this->makeDavRequest($user, 'GET', $fileName, []);
  537. } catch (\GuzzleHttp\Exception\ServerException $ex) {
  538. $this->response = $ex->getResponse();
  539. }
  540. }
  541. /**
  542. * URL encodes the given path but keeps the slashes
  543. *
  544. * @param string $path to encode
  545. * @return string encoded path
  546. */
  547. private function encodePath($path) {
  548. // slashes need to stay
  549. return str_replace('%2F', '/', rawurlencode($path));
  550. }
  551. /**
  552. * @When user :user favorites element :path
  553. */
  554. public function userFavoritesElement($user, $path){
  555. $this->response = $this->changeFavStateOfAnElement($user, $path, 1, 0, null);
  556. }
  557. /**
  558. * @When user :user unfavorites element :path
  559. */
  560. public function userUnfavoritesElement($user, $path){
  561. $this->response = $this->changeFavStateOfAnElement($user, $path, 0, 0, null);
  562. }
  563. /*Set the elements of a proppatch, $folderDepth requires 1 to see elements without children*/
  564. public function changeFavStateOfAnElement($user, $path, $favOrUnfav, $folderDepth, $properties = null){
  565. $fullUrl = substr($this->baseUrl, 0, -4);
  566. $settings = array(
  567. 'baseUri' => $fullUrl,
  568. 'userName' => $user,
  569. );
  570. if ($user === 'admin') {
  571. $settings['password'] = $this->adminUser[1];
  572. } else {
  573. $settings['password'] = $this->regularUser;
  574. }
  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. }