Trashbin.php 5.3 KB

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