CommandLineContext.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. public function __construct($ocPath, $baseUrl) {
  35. $this->ocPath = rtrim($ocPath, '/') . '/';
  36. $this->localBaseUrl = $baseUrl;
  37. $this->remoteBaseUrl = $baseUrl;
  38. }
  39. /**
  40. * @Given Maintenance mode is enabled
  41. */
  42. public function maintenanceModeIsEnabled() {
  43. $this->runOcc(['maintenance:mode', '--on']);
  44. }
  45. /**
  46. * @Then Maintenance mode is disabled
  47. */
  48. public function maintenanceModeIsDisabled() {
  49. $this->runOcc(['maintenance:mode', '--off']);
  50. }
  51. /** @BeforeScenario */
  52. public function gatherContexts(BeforeScenarioScope $scope) {
  53. $environment = $scope->getEnvironment();
  54. // this should really be "WebDavContext" ...
  55. $this->featureContext = $environment->getContext('FeatureContext');
  56. }
  57. private function findLastTransferFolderForUser($sourceUser, $targetUser) {
  58. $foundPaths = [];
  59. $results = $this->featureContext->listFolder($targetUser, '', 1);
  60. foreach ($results as $path => $data) {
  61. $path = rawurldecode($path);
  62. $parts = explode(' ', $path);
  63. if (basename($parts[0]) !== 'transferred') {
  64. continue;
  65. }
  66. if (isset($parts[2]) && $parts[2] === $sourceUser) {
  67. // store timestamp as key
  68. $foundPaths[] = [
  69. 'date' => strtotime(trim($parts[4], '/')),
  70. 'path' => $path,
  71. ];
  72. }
  73. }
  74. if (empty($foundPaths)) {
  75. return null;
  76. }
  77. usort($foundPaths, function ($a, $b) {
  78. return $a['date'] - $b['date'];
  79. });
  80. $davPath = rtrim($this->featureContext->getDavFilesPath($targetUser), '/');
  81. $foundPath = end($foundPaths)['path'];
  82. // strip dav path
  83. return substr($foundPath, strlen($davPath) + 1);
  84. }
  85. /**
  86. * @When /^transferring ownership from "([^"]+)" to "([^"]+)"$/
  87. */
  88. public function transferringOwnership($user1, $user2) {
  89. if ($this->runOcc(['files:transfer-ownership', $user1, $user2]) === 0) {
  90. $this->lastTransferPath = $this->findLastTransferFolderForUser($user1, $user2);
  91. } else {
  92. // failure
  93. $this->lastTransferPath = null;
  94. }
  95. }
  96. /**
  97. * @When /^transferring ownership of path "([^"]+)" from "([^"]+)" to "([^"]+)"$/
  98. */
  99. public function transferringOwnershipPath($path, $user1, $user2) {
  100. $path = '--path=' . $path;
  101. if ($this->runOcc(['files:transfer-ownership', $path, $user1, $user2]) === 0) {
  102. $this->lastTransferPath = $this->findLastTransferFolderForUser($user1, $user2);
  103. } else {
  104. // failure
  105. $this->lastTransferPath = null;
  106. }
  107. }
  108. /**
  109. * @When /^transferring ownership of path "([^"]+)" from "([^"]+)" to "([^"]+)" with received shares$/
  110. */
  111. public function transferringOwnershipPathWithIncomingShares($path, $user1, $user2) {
  112. $path = '--path=' . $path;
  113. if ($this->runOcc(['files:transfer-ownership', $path, $user1, $user2, '--transfer-incoming-shares=1']) === 0) {
  114. $this->lastTransferPath = $this->findLastTransferFolderForUser($user1, $user2);
  115. } else {
  116. // failure
  117. $this->lastTransferPath = null;
  118. }
  119. }
  120. /**
  121. * @When /^using received transfer folder of "([^"]+)" as dav path$/
  122. */
  123. public function usingTransferFolderAsDavPath($user) {
  124. $davPath = $this->featureContext->getDavFilesPath($user);
  125. $davPath = rtrim($davPath, '/') . $this->lastTransferPath;
  126. $this->featureContext->usingDavPath($davPath);
  127. }
  128. /**
  129. * @Then /^transfer folder name contains "([^"]+)"$/
  130. */
  131. public function transferFolderNameContains($text) {
  132. Assert::assertStringContainsString($text, $this->lastTransferPath);
  133. }
  134. }