FilesDropContext.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. use Behat\Behat\Context\Context;
  7. use Behat\Behat\Context\SnippetAcceptingContext;
  8. use GuzzleHttp\Client;
  9. require __DIR__ . '/../../vendor/autoload.php';
  10. class FilesDropContext implements Context, SnippetAcceptingContext {
  11. use WebDav;
  12. /**
  13. * @When Dropping file :path with :content
  14. */
  15. public function droppingFileWith($path, $content, $nickName = null) {
  16. $client = new Client();
  17. $options = [];
  18. if (count($this->lastShareData->data->element) > 0) {
  19. $token = $this->lastShareData->data[0]->token;
  20. } else {
  21. $token = $this->lastShareData->data[0]->token;
  22. }
  23. $base = substr($this->baseUrl, 0, -4);
  24. $fullUrl = str_replace('//', '/', $base . "/public.php/dav/files/$token/$path");
  25. $options['headers'] = [
  26. 'X-REQUESTED-WITH' => 'XMLHttpRequest'
  27. ];
  28. if ($nickName) {
  29. $options['headers']['X-NC-NICKNAME'] = $nickName;
  30. }
  31. $options['body'] = \GuzzleHttp\Psr7\Utils::streamFor($content);
  32. try {
  33. $this->response = $client->request('PUT', $fullUrl, $options);
  34. } catch (\GuzzleHttp\Exception\ClientException $e) {
  35. $this->response = $e->getResponse();
  36. }
  37. }
  38. /**
  39. * @When Dropping file :path with :content as :nickName
  40. */
  41. public function droppingFileWithAs($path, $content, $nickName) {
  42. $this->droppingFileWith($path, $content, $nickName);
  43. }
  44. /**
  45. * @When Creating folder :folder in drop
  46. */
  47. public function creatingFolderInDrop($folder) {
  48. $client = new Client();
  49. $options = [];
  50. if (count($this->lastShareData->data->element) > 0) {
  51. $token = $this->lastShareData->data[0]->token;
  52. } else {
  53. $token = $this->lastShareData->data[0]->token;
  54. }
  55. $base = substr($this->baseUrl, 0, -4);
  56. $fullUrl = str_replace('//', '/', $base . "/public.php/dav/files/$token/$folder");
  57. $options['headers'] = [
  58. 'X-REQUESTED-WITH' => 'XMLHttpRequest'
  59. ];
  60. try {
  61. $this->response = $client->request('MKCOL', $fullUrl, $options);
  62. } catch (\GuzzleHttp\Exception\ClientException $e) {
  63. $this->response = $e->getResponse();
  64. }
  65. }
  66. }