Trashbin.php 5.4 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 DMS\PHPUnitExtensions\ArraySubset\Assert as AssertArraySubset;
  26. use PHPUnit\Framework\Assert;
  27. require __DIR__ . '/../../vendor/autoload.php';
  28. /**
  29. * Trashbin functions
  30. */
  31. trait Trashbin {
  32. // WebDav trait is expected to be used in the class that uses this trait.
  33. /**
  34. * @When User :user empties trashbin
  35. * @param string $user user
  36. */
  37. public function emptyTrashbin($user) {
  38. $client = $this->getSabreClient($user);
  39. $response = $client->request('DELETE', $this->makeSabrePath($user, 'trash', 'trashbin'));
  40. Assert::assertEquals(204, $response['statusCode']);
  41. }
  42. private function findFullTrashname($user, $name) {
  43. $rootListing = $this->listTrashbinFolder($user, '/');
  44. foreach ($rootListing as $href => $rootItem) {
  45. if ($rootItem['{http://nextcloud.org/ns}trashbin-filename'] === $name) {
  46. return basename($href);
  47. }
  48. }
  49. return null;
  50. }
  51. /**
  52. * Get the full /startofpath.dxxxx/rest/of/path from /startofpath/rest/of/path
  53. */
  54. private function getFullTrashPath($user, $path) {
  55. if ($path !== '' && $path !== '/') {
  56. $parts = explode('/', $path);
  57. $fullName = $this->findFullTrashname($user, $parts[1]);
  58. if ($fullName === null) {
  59. Assert::fail("cant find $path in trash");
  60. return '/dummy_full_path_not_found';
  61. }
  62. $parts[1] = $fullName;
  63. $path = implode('/', $parts);
  64. }
  65. return $path;
  66. }
  67. /**
  68. * List trashbin folder
  69. *
  70. * @param string $user user
  71. * @param string $path path
  72. * @return array response
  73. */
  74. public function listTrashbinFolder($user, $path) {
  75. $path = $this->getFullTrashPath($user, $path);
  76. $client = $this->getSabreClient($user);
  77. $results = $client->propfind($this->makeSabrePath($user, 'trash' . $path, 'trashbin'), [
  78. '{http://nextcloud.org/ns}trashbin-filename',
  79. '{http://nextcloud.org/ns}trashbin-original-location',
  80. '{http://nextcloud.org/ns}trashbin-deletion-time'
  81. ], 1);
  82. $results = array_filter($results, function (array $item) {
  83. return isset($item['{http://nextcloud.org/ns}trashbin-filename']);
  84. });
  85. if ($path !== '' && $path !== '/') {
  86. array_shift($results);
  87. }
  88. return $results;
  89. }
  90. /**
  91. * @Then /^user "([^"]*)" in trash folder "([^"]*)" should have the following elements$/
  92. * @param string $user
  93. * @param string $folder
  94. * @param \Behat\Gherkin\Node\TableNode|null $expectedElements
  95. */
  96. public function checkTrashContents($user, $folder, $expectedElements) {
  97. $elementList = $this->listTrashbinFolder($user, $folder);
  98. $trashContent = array_filter(array_map(function (array $item) {
  99. return $item['{http://nextcloud.org/ns}trashbin-filename'];
  100. }, $elementList));
  101. if ($expectedElements instanceof \Behat\Gherkin\Node\TableNode) {
  102. $elementRows = $expectedElements->getRows();
  103. $elementsSimplified = $this->simplifyArray($elementRows);
  104. foreach ($elementsSimplified as $expectedElement) {
  105. $expectedElement = ltrim($expectedElement, '/');
  106. if (array_search($expectedElement, $trashContent) === false) {
  107. Assert::fail("$expectedElement" . " is not in trash listing");
  108. }
  109. }
  110. }
  111. }
  112. /**
  113. * @Then /^as "([^"]*)" the (file|folder) "([^"]*)" exists in trash$/
  114. * @param string $user
  115. * @param string $type
  116. * @param string $file
  117. */
  118. public function checkTrashContains($user, $type, $file) {
  119. $parent = dirname($file);
  120. if ($parent === '.') {
  121. $parent = '/';
  122. }
  123. $name = basename($file);
  124. $elementList = $this->listTrashbinFolder($user, $parent);
  125. $trashContent = array_filter(array_map(function (array $item) {
  126. return $item['{http://nextcloud.org/ns}trashbin-filename'];
  127. }, $elementList));
  128. AssertArraySubset::assertArraySubset([$name], array_values($trashContent));
  129. }
  130. /**
  131. * @Then /^user "([^"]*)" in trash folder "([^"]*)" should have (\d+) elements?$/
  132. * @param string $user
  133. * @param string $folder
  134. * @param \Behat\Gherkin\Node\TableNode|null $expectedElements
  135. */
  136. public function checkTrashSize($user, $folder, $expectedCount) {
  137. $elementList = $this->listTrashbinFolder($user, $folder);
  138. Assert::assertEquals($expectedCount, count($elementList));
  139. }
  140. /**
  141. * @When /^user "([^"]*)" in restores "([^"]*)" from trash$/
  142. * @param string $user
  143. * @param string $file
  144. */
  145. public function restoreFromTrash($user, $file) {
  146. $file = $this->getFullTrashPath($user, $file);
  147. $url = $this->makeSabrePath($user, 'trash' . $file, 'trashbin');
  148. $client = $this->getSabreClient($user);
  149. $response = $client->request('MOVE', $url, null, [
  150. 'Destination' => $this->makeSabrePath($user, 'restore/' . basename($file), 'trashbin'),
  151. ]);
  152. Assert::assertEquals(201, $response['statusCode']);
  153. return;
  154. }
  155. }