CommandLineContext.php 3.7 KB

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