Trashbin.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017, ownCloud GmbH.
  4. *
  5. * @author Vincent Petry <pvince81@owncloud.com>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. use PHPUnit\Framework\Assert;
  23. require __DIR__ . '/../../vendor/autoload.php';
  24. /**
  25. * Trashbin functions
  26. */
  27. trait Trashbin {
  28. use WebDav;
  29. /**
  30. * @When User :user empties trashbin
  31. * @param string $user user
  32. */
  33. public function emptyTrashbin($user) {
  34. $client = $this->getSabreClient($user);
  35. $response = $client->request('DELETE', $this->makeSabrePath($user, 'trash', 'trashbin'));
  36. Assert::assertEquals(204, $response['statusCode']);
  37. }
  38. private function findFullTrashname($user, $name) {
  39. $rootListing = $this->listTrashbinFolder($user, '/');
  40. foreach ($rootListing as $href => $rootItem) {
  41. if ($rootItem['{http://nextcloud.org/ns}trashbin-filename'] === $name) {
  42. return basename($href);
  43. }
  44. }
  45. return null;
  46. }
  47. /**
  48. * Get the full /startofpath.dxxxx/rest/of/path from /startofpath/rest/of/path
  49. */
  50. private function getFullTrashPath($user, $path) {
  51. if ($path !== '' && $path !== '/') {
  52. $parts = explode('/', $path);
  53. $fullName = $this->findFullTrashname($user, $parts[1]);
  54. if ($fullName === null) {
  55. Assert::fail("cant find $path in trash");
  56. return '/dummy_full_path_not_found';
  57. }
  58. $parts[1] = $fullName;
  59. $path = implode('/', $parts);
  60. }
  61. return $path;
  62. }
  63. /**
  64. * List trashbin folder
  65. *
  66. * @param string $user user
  67. * @param string $path path
  68. * @return array response
  69. */
  70. public function listTrashbinFolder($user, $path) {
  71. $path = $this->getFullTrashPath($user, $path);
  72. $client = $this->getSabreClient($user);
  73. $results = $client->propfind($this->makeSabrePath($user, 'trash' . $path, 'trashbin'), [
  74. '{http://nextcloud.org/ns}trashbin-filename',
  75. '{http://nextcloud.org/ns}trashbin-original-location',
  76. '{http://nextcloud.org/ns}trashbin-deletion-time'
  77. ], 1);
  78. $results = array_filter($results, function (array $item) {
  79. return isset($item['{http://nextcloud.org/ns}trashbin-filename']);
  80. });
  81. if ($path !== '' && $path !== '/') {
  82. array_shift($results);
  83. }
  84. return $results;
  85. }
  86. /**
  87. * @Then /^user "([^"]*)" in trash folder "([^"]*)" should have the following elements$/
  88. * @param string $user
  89. * @param string $folder
  90. * @param \Behat\Gherkin\Node\TableNode|null $expectedElements
  91. */
  92. public function checkTrashContents($user, $folder, $expectedElements) {
  93. $elementList = $this->listTrashbinFolder($user, $folder);
  94. $trashContent = array_filter(array_map(function (array $item) {
  95. return $item['{http://nextcloud.org/ns}trashbin-filename'];
  96. }, $elementList));
  97. if ($expectedElements instanceof \Behat\Gherkin\Node\TableNode) {
  98. $elementRows = $expectedElements->getRows();
  99. $elementsSimplified = $this->simplifyArray($elementRows);
  100. foreach ($elementsSimplified as $expectedElement) {
  101. $expectedElement = ltrim($expectedElement, '/');
  102. if (array_search($expectedElement, $trashContent) === false) {
  103. Assert::fail("$expectedElement" . " is not in trash listing");
  104. }
  105. }
  106. }
  107. }
  108. /**
  109. * @Then /^as "([^"]*)" the (file|folder) "([^"]*)" exists in trash$/
  110. * @param string $user
  111. * @param string $type
  112. * @param string $file
  113. */
  114. public function checkTrashContains($user, $type, $file) {
  115. $parent = dirname($file);
  116. if ($parent === '.') {
  117. $parent = '/';
  118. }
  119. $name = basename($file);
  120. $elementList = $this->listTrashbinFolder($user, $parent);
  121. $trashContent = array_filter(array_map(function (array $item) {
  122. return $item['{http://nextcloud.org/ns}trashbin-filename'];
  123. }, $elementList));
  124. Assert::assertArraySubset([$name], array_values($trashContent));
  125. }
  126. /**
  127. * @Then /^user "([^"]*)" in trash folder "([^"]*)" should have (\d+) elements?$/
  128. * @param string $user
  129. * @param string $folder
  130. * @param \Behat\Gherkin\Node\TableNode|null $expectedElements
  131. */
  132. public function checkTrashSize($user, $folder, $expectedCount) {
  133. $elementList = $this->listTrashbinFolder($user, $folder);
  134. Assert::assertEquals($expectedCount, count($elementList));
  135. }
  136. /**
  137. * @When /^user "([^"]*)" in restores "([^"]*)" from trash$/
  138. * @param string $user
  139. * @param string $file
  140. */
  141. public function restoreFromTrash($user, $file) {
  142. $file = $this->getFullTrashPath($user, $file);
  143. $url = $this->makeSabrePath($user, 'trash' . $file, 'trashbin');
  144. $client = $this->getSabreClient($user);
  145. $response = $client->request('MOVE', $url, null, [
  146. 'Destination' => $this->makeSabrePath($user, 'restore/' . basename($file), 'trashbin'),
  147. ]);
  148. Assert::assertEquals(201, $response['statusCode']);
  149. return;
  150. }
  151. }