Trashbin.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * @author Vincent Petry <pvince81@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2017, ownCloud GmbH.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. use GuzzleHttp\Client;
  22. use GuzzleHttp\Message\ResponseInterface;
  23. require __DIR__ . '/../../vendor/autoload.php';
  24. /**
  25. * Trashbin functions
  26. */
  27. trait Trashbin {
  28. /**
  29. * @When User :user empties trashbin
  30. * @param string $user user
  31. */
  32. public function emptyTrashbin($user) {
  33. $this->asAn($user);
  34. $body = new \Behat\Gherkin\Node\TableNode([['allfiles', 'true'], ['dir', '%2F']]);
  35. $this->sendingToWithDirectUrl('POST', "/index.php/apps/files_trashbin/ajax/delete.php", $body);
  36. $this->theHTTPStatusCodeShouldBe('200');
  37. }
  38. /**
  39. * List trashbin folder
  40. *
  41. * @param string $user user
  42. * @param string $path path
  43. * @return array response
  44. */
  45. public function listTrashbinFolder($user, $path){
  46. $this->asAn($user);
  47. $params = '?dir=' . rawurlencode('/' . trim($path, '/'));
  48. $this->sendingToWithDirectUrl('GET', '/index.php/apps/files_trashbin/ajax/list.php' . $params, null);
  49. $this->theHTTPStatusCodeShouldBe('200');
  50. $response = json_decode($this->response->getBody(), true);
  51. return $response['data']['files'];
  52. }
  53. /**
  54. * @Then /^as "([^"]*)" the (file|folder|entry) "([^"]*)" exists in trash$/
  55. * @param string $user
  56. * @param string $entryText
  57. * @param string $path
  58. */
  59. public function asTheFileOrFolderExistsInTrash($user, $entryText, $path) {
  60. $path = trim($path, '/');
  61. $sections = explode('/', $path, 2);
  62. $firstEntry = $this->findFirstTrashedEntry($user, trim($sections[0], '/'));
  63. PHPUnit_Framework_Assert::assertNotNull($firstEntry);
  64. // query was on the main element ?
  65. if (count($sections) === 1) {
  66. // already found, return
  67. return;
  68. }
  69. $subdir = trim(dirname($sections[1]), '/');
  70. if ($subdir !== '' && $subdir !== '.') {
  71. $subdir = $firstEntry . '/' . $subdir;
  72. } else {
  73. $subdir = $firstEntry;
  74. }
  75. $listing = $this->listTrashbinFolder($user, $subdir);
  76. $checkedName = basename($path);
  77. $found = false;
  78. foreach ($listing as $entry) {
  79. if ($entry['name'] === $checkedName) {
  80. $found = true;
  81. break;
  82. }
  83. }
  84. PHPUnit_Framework_Assert::assertTrue($found);
  85. }
  86. /**
  87. * Finds the first trashed entry matching the given name
  88. *
  89. * @param string $name
  90. * @return string|null real entry name with timestamp suffix or null if not found
  91. */
  92. private function findFirstTrashedEntry($user, $name) {
  93. $listing = $this->listTrashbinFolder($user, '/');
  94. foreach ($listing as $entry) {
  95. if ($entry['name'] === $name) {
  96. return $entry['name'] . '.d' . ((int)$entry['mtime'] / 1000);
  97. }
  98. }
  99. return null;
  100. }
  101. }