1
0

Trashbin.php 4.7 KB

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