CommandLineContext.php 3.9 KB

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