CommandLineContext.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. require __DIR__ . '/../../vendor/autoload.php';
  8. use Behat\Behat\Hook\Scope\BeforeScenarioScope;
  9. use PHPUnit\Framework\Assert;
  10. class CommandLineContext implements \Behat\Behat\Context\Context {
  11. use CommandLine;
  12. private $lastTransferPath;
  13. private $featureContext;
  14. private $localBaseUrl;
  15. private $remoteBaseUrl;
  16. public function __construct($ocPath, $baseUrl) {
  17. $this->ocPath = rtrim($ocPath, '/') . '/';
  18. $this->localBaseUrl = $baseUrl;
  19. $this->remoteBaseUrl = $baseUrl;
  20. }
  21. /**
  22. * @Given Maintenance mode is enabled
  23. */
  24. public function maintenanceModeIsEnabled() {
  25. $this->runOcc(['maintenance:mode', '--on']);
  26. }
  27. /**
  28. * @Then Maintenance mode is disabled
  29. */
  30. public function maintenanceModeIsDisabled() {
  31. $this->runOcc(['maintenance:mode', '--off']);
  32. }
  33. /** @BeforeScenario */
  34. public function gatherContexts(BeforeScenarioScope $scope) {
  35. $environment = $scope->getEnvironment();
  36. // this should really be "WebDavContext" ...
  37. $this->featureContext = $environment->getContext('FeatureContext');
  38. }
  39. private function findLastTransferFolderForUser($sourceUser, $targetUser) {
  40. $foundPaths = [];
  41. $results = $this->featureContext->listFolder($targetUser, '', 1);
  42. foreach ($results as $path => $data) {
  43. $path = rawurldecode($path);
  44. $parts = explode(' ', $path);
  45. if (basename($parts[0]) !== 'transferred') {
  46. continue;
  47. }
  48. if (isset($parts[2]) && $parts[2] === $sourceUser) {
  49. // store timestamp as key
  50. $foundPaths[] = [
  51. 'date' => strtotime(trim($parts[4], '/')),
  52. 'path' => $path,
  53. ];
  54. }
  55. }
  56. if (empty($foundPaths)) {
  57. return null;
  58. }
  59. usort($foundPaths, function ($a, $b) {
  60. return $a['date'] - $b['date'];
  61. });
  62. $davPath = rtrim($this->featureContext->getDavFilesPath($targetUser), '/');
  63. $foundPath = end($foundPaths)['path'];
  64. // strip dav path
  65. return substr($foundPath, strlen($davPath) + 1);
  66. }
  67. /**
  68. * @When /^transferring ownership from "([^"]+)" to "([^"]+)"$/
  69. */
  70. public function transferringOwnership($user1, $user2) {
  71. if ($this->runOcc(['files:transfer-ownership', $user1, $user2]) === 0) {
  72. $this->lastTransferPath = $this->findLastTransferFolderForUser($user1, $user2);
  73. } else {
  74. // failure
  75. $this->lastTransferPath = null;
  76. }
  77. }
  78. /**
  79. * @When /^transferring ownership of path "([^"]+)" from "([^"]+)" to "([^"]+)"$/
  80. */
  81. public function transferringOwnershipPath($path, $user1, $user2) {
  82. $path = '--path=' . $path;
  83. if ($this->runOcc(['files:transfer-ownership', $path, $user1, $user2]) === 0) {
  84. $this->lastTransferPath = $this->findLastTransferFolderForUser($user1, $user2);
  85. } else {
  86. // failure
  87. $this->lastTransferPath = null;
  88. }
  89. }
  90. /**
  91. * @When /^transferring ownership of path "([^"]+)" from "([^"]+)" to "([^"]+)" with received shares$/
  92. */
  93. public function transferringOwnershipPathWithIncomingShares($path, $user1, $user2) {
  94. $path = '--path=' . $path;
  95. if ($this->runOcc(['files:transfer-ownership', $path, $user1, $user2, '--transfer-incoming-shares=1']) === 0) {
  96. $this->lastTransferPath = $this->findLastTransferFolderForUser($user1, $user2);
  97. } else {
  98. // failure
  99. $this->lastTransferPath = null;
  100. }
  101. }
  102. /**
  103. * @When /^using received transfer folder of "([^"]+)" as dav path$/
  104. */
  105. public function usingTransferFolderAsDavPath($user) {
  106. $davPath = $this->featureContext->getDavFilesPath($user);
  107. $davPath = rtrim($davPath, '/') . $this->lastTransferPath;
  108. $this->featureContext->usingDavPath($davPath);
  109. }
  110. /**
  111. * @Then /^transfer folder name contains "([^"]+)"$/
  112. */
  113. public function transferFolderNameContains($text) {
  114. Assert::assertStringContainsString($text, $this->lastTransferPath);
  115. }
  116. }