1
0

ExternalStorage.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. use Behat\Gherkin\Node\TableNode;
  7. use PHPUnit\Framework\Assert;
  8. require __DIR__ . '/../../vendor/autoload.php';
  9. trait ExternalStorage {
  10. private array $storageIds = [];
  11. private array $lastExternalStorageData;
  12. /**
  13. * @AfterScenario
  14. **/
  15. public function deleteCreatedStorages(): void {
  16. foreach ($this->storageIds as $storageId) {
  17. $this->deleteStorage($storageId);
  18. }
  19. $this->storageIds = [];
  20. }
  21. private function deleteStorage(string $storageId): void {
  22. // Based on "runOcc" from CommandLine trait
  23. $args = ['files_external:delete', '--yes', $storageId];
  24. $args = array_map(function ($arg) {
  25. return escapeshellarg($arg);
  26. }, $args);
  27. $args[] = '--no-ansi --no-warnings';
  28. $args = implode(' ', $args);
  29. $descriptor = [
  30. 0 => ['pipe', 'r'],
  31. 1 => ['pipe', 'w'],
  32. 2 => ['pipe', 'w'],
  33. ];
  34. $process = proc_open('php console.php ' . $args, $descriptor, $pipes, $ocPath = '../..');
  35. $lastStdOut = stream_get_contents($pipes[1]);
  36. proc_close($process);
  37. }
  38. /**
  39. * @When logged in user creates external global storage
  40. *
  41. * @param TableNode $fields
  42. */
  43. public function loggedInUserCreatesExternalGlobalStorage(TableNode $fields): void {
  44. $this->sendJsonWithRequestToken('POST', '/index.php/apps/files_external/globalstorages', $fields);
  45. $this->theHTTPStatusCodeShouldBe('201');
  46. $this->lastExternalStorageData = json_decode($this->response->getBody(), $asAssociativeArray = true);
  47. $this->storageIds[] = $this->lastExternalStorageData['id'];
  48. }
  49. /**
  50. * @When logged in user updates last external userglobal storage
  51. *
  52. * @param TableNode $fields
  53. */
  54. public function loggedInUserUpdatesLastExternalUserglobalStorage(TableNode $fields): void {
  55. $this->sendJsonWithRequestToken('PUT', '/index.php/apps/files_external/userglobalstorages/' . $this->lastExternalStorageData['id'], $fields);
  56. $this->theHTTPStatusCodeShouldBe('200');
  57. $this->lastExternalStorageData = json_decode($this->response->getBody(), $asAssociativeArray = true);
  58. }
  59. /**
  60. * @Then fields of last external storage match with
  61. *
  62. * @param TableNode $fields
  63. */
  64. public function fieldsOfLastExternalStorageMatchWith(TableNode $fields): void {
  65. foreach ($fields->getRowsHash() as $expectedField => $expectedValue) {
  66. if (!array_key_exists($expectedField, $this->lastExternalStorageData)) {
  67. Assert::fail("$expectedField was not found in response");
  68. }
  69. Assert::assertEquals($expectedValue, $this->lastExternalStorageData[$expectedField], "Field '$expectedField' does not match ({$this->lastExternalStorageData[$expectedField]})");
  70. }
  71. }
  72. private function sendJsonWithRequestToken(string $method, string $url, TableNode $fields): void {
  73. $isFirstField = true;
  74. $fieldsAsJsonString = '{';
  75. foreach ($fields->getRowsHash() as $key => $value) {
  76. $fieldsAsJsonString .= ($isFirstField ? '' : ',') . '"' . $key . '":' . $value;
  77. $isFirstField = false;
  78. }
  79. $fieldsAsJsonString .= '}';
  80. $body = [
  81. 'headers' => [
  82. 'Content-Type' => 'application/json',
  83. ],
  84. 'body' => $fieldsAsJsonString,
  85. ];
  86. $this->sendingAToWithRequesttoken($method, $url, $body);
  87. }
  88. }