CommandLineContext.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Stefan Weil <sw@weilnetz.de>
  9. * @author Sujith H <sharidasan@owncloud.com>
  10. * @author Vincent Petry <vincent@nextcloud.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. require __DIR__ . '/../../vendor/autoload.php';
  28. use Behat\Behat\Hook\Scope\BeforeScenarioScope;
  29. use PHPUnit\Framework\Assert;
  30. class CommandLineContext implements \Behat\Behat\Context\Context {
  31. use CommandLine;
  32. private $lastTransferPath;
  33. private $featureContext;
  34. private $localBaseUrl;
  35. private $remoteBaseUrl;
  36. public function __construct($ocPath, $baseUrl) {
  37. $this->ocPath = rtrim($ocPath, '/') . '/';
  38. $this->localBaseUrl = $baseUrl;
  39. $this->remoteBaseUrl = $baseUrl;
  40. }
  41. /**
  42. * @Given Maintenance mode is enabled
  43. */
  44. public function maintenanceModeIsEnabled() {
  45. $this->runOcc(['maintenance:mode', '--on']);
  46. }
  47. /**
  48. * @Then Maintenance mode is disabled
  49. */
  50. public function maintenanceModeIsDisabled() {
  51. $this->runOcc(['maintenance:mode', '--off']);
  52. }
  53. /** @BeforeScenario */
  54. public function gatherContexts(BeforeScenarioScope $scope) {
  55. $environment = $scope->getEnvironment();
  56. // this should really be "WebDavContext" ...
  57. $this->featureContext = $environment->getContext('FeatureContext');
  58. }
  59. private function findLastTransferFolderForUser($sourceUser, $targetUser) {
  60. $foundPaths = [];
  61. $results = $this->featureContext->listFolder($targetUser, '', 1);
  62. foreach ($results as $path => $data) {
  63. $path = rawurldecode($path);
  64. $parts = explode(' ', $path);
  65. if (basename($parts[0]) !== 'transferred') {
  66. continue;
  67. }
  68. if (isset($parts[2]) && $parts[2] === $sourceUser) {
  69. // store timestamp as key
  70. $foundPaths[] = [
  71. 'date' => strtotime(trim($parts[4], '/')),
  72. 'path' => $path,
  73. ];
  74. }
  75. }
  76. if (empty($foundPaths)) {
  77. return null;
  78. }
  79. usort($foundPaths, function ($a, $b) {
  80. return $a['date'] - $b['date'];
  81. });
  82. $davPath = rtrim($this->featureContext->getDavFilesPath($targetUser), '/');
  83. $foundPath = end($foundPaths)['path'];
  84. // strip dav path
  85. return substr($foundPath, strlen($davPath) + 1);
  86. }
  87. /**
  88. * @When /^transferring ownership from "([^"]+)" to "([^"]+)"$/
  89. */
  90. public function transferringOwnership($user1, $user2) {
  91. if ($this->runOcc(['files:transfer-ownership', $user1, $user2]) === 0) {
  92. $this->lastTransferPath = $this->findLastTransferFolderForUser($user1, $user2);
  93. } else {
  94. // failure
  95. $this->lastTransferPath = null;
  96. }
  97. }
  98. /**
  99. * @When /^transferring ownership of path "([^"]+)" from "([^"]+)" to "([^"]+)"$/
  100. */
  101. public function transferringOwnershipPath($path, $user1, $user2) {
  102. $path = '--path=' . $path;
  103. if ($this->runOcc(['files:transfer-ownership', $path, $user1, $user2]) === 0) {
  104. $this->lastTransferPath = $this->findLastTransferFolderForUser($user1, $user2);
  105. } else {
  106. // failure
  107. $this->lastTransferPath = null;
  108. }
  109. }
  110. /**
  111. * @When /^transferring ownership of path "([^"]+)" from "([^"]+)" to "([^"]+)" with received shares$/
  112. */
  113. public function transferringOwnershipPathWithIncomingShares($path, $user1, $user2) {
  114. $path = '--path=' . $path;
  115. if ($this->runOcc(['files:transfer-ownership', $path, $user1, $user2, '--transfer-incoming-shares=1']) === 0) {
  116. $this->lastTransferPath = $this->findLastTransferFolderForUser($user1, $user2);
  117. } else {
  118. // failure
  119. $this->lastTransferPath = null;
  120. }
  121. }
  122. /**
  123. * @When /^using received transfer folder of "([^"]+)" as dav path$/
  124. */
  125. public function usingTransferFolderAsDavPath($user) {
  126. $davPath = $this->featureContext->getDavFilesPath($user);
  127. $davPath = rtrim($davPath, '/') . $this->lastTransferPath;
  128. $this->featureContext->usingDavPath($davPath);
  129. }
  130. /**
  131. * @Then /^transfer folder name contains "([^"]+)"$/
  132. */
  133. public function transferFolderNameContains($text) {
  134. Assert::assertStringContainsString($text, $this->lastTransferPath);
  135. }
  136. }