|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @author Vincent Petry <[email protected]> |
| 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 | + |
| 22 | +use GuzzleHttp\Client; |
| 23 | +use GuzzleHttp\Message\ResponseInterface; |
| 24 | + |
| 25 | +require __DIR__ . '/../../vendor/autoload.php'; |
| 26 | + |
| 27 | +/** |
| 28 | + * Trashbin functions |
| 29 | + */ |
| 30 | +trait Trashbin { |
| 31 | + |
| 32 | + /** |
| 33 | + * @When User :user empties trashbin |
| 34 | + * @param string $user user |
| 35 | + */ |
| 36 | + public function emptyTrashbin($user) { |
| 37 | + $this->asAn($user); |
| 38 | + $body = new \Behat\Gherkin\Node\TableNode([['allfiles', 'true'], ['dir', '%2F']]); |
| 39 | + $this->sendingToWithDirectUrl('POST', "/index.php/apps/files_trashbin/ajax/delete.php", $body); |
| 40 | + $this->theHTTPStatusCodeShouldBe('200'); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * List trashbin folder |
| 45 | + * |
| 46 | + * @param string $user user |
| 47 | + * @param string $path path |
| 48 | + * @return array response |
| 49 | + */ |
| 50 | + public function listTrashbinFolder($user, $path){ |
| 51 | + $this->asAn($user); |
| 52 | + $params = '?dir=' . rawurlencode('/' . trim($path, '/')); |
| 53 | + $this->sendingToWithDirectUrl('GET', '/index.php/apps/files_trashbin/ajax/list.php' . $params, null); |
| 54 | + $this->theHTTPStatusCodeShouldBe('200'); |
| 55 | + |
| 56 | + $response = json_decode($this->response->getBody(), true); |
| 57 | + |
| 58 | + return $response['data']['files']; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @Then /^as "([^"]*)" the (file|folder|entry) "([^"]*)" exists in trash$/ |
| 63 | + * @param string $user |
| 64 | + * @param string $entryText |
| 65 | + * @param string $path |
| 66 | + */ |
| 67 | + public function asTheFileOrFolderExistsInTrash($user, $entryText, $path) { |
| 68 | + $path = trim($path, '/'); |
| 69 | + $sections = explode('/', $path, 2); |
| 70 | + |
| 71 | + $firstEntry = $this->findFirstTrashedEntry($user, trim($sections[0], '/')); |
| 72 | + |
| 73 | + PHPUnit_Framework_Assert::assertNotNull($firstEntry); |
| 74 | + |
| 75 | + // query was on the main element ? |
| 76 | + if (count($sections) === 1) { |
| 77 | + // already found, return |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + $subdir = trim(dirname($sections[1]), '/'); |
| 82 | + if ($subdir !== '' && $subdir !== '.') { |
| 83 | + $subdir = $firstEntry . '/' . $subdir; |
| 84 | + } else { |
| 85 | + $subdir = $firstEntry; |
| 86 | + } |
| 87 | + |
| 88 | + $listing = $this->listTrashbinFolder($user, $subdir); |
| 89 | + $checkedName = basename($path); |
| 90 | + |
| 91 | + $found = false; |
| 92 | + foreach ($listing as $entry) { |
| 93 | + if ($entry['name'] === $checkedName) { |
| 94 | + $found = true; |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + PHPUnit_Framework_Assert::assertTrue($found); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Finds the first trashed entry matching the given name |
| 104 | + * |
| 105 | + * @param string $name |
| 106 | + * @return string|null real entry name with timestamp suffix or null if not found |
| 107 | + */ |
| 108 | + private function findFirstTrashedEntry($user, $name) { |
| 109 | + $listing = $this->listTrashbinFolder($user, '/'); |
| 110 | + |
| 111 | + foreach ($listing as $entry) { |
| 112 | + if ($entry['name'] === $name) { |
| 113 | + return $entry['name'] . '.d' . ((int)$entry['mtime'] / 1000); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return null; |
| 118 | + } |
| 119 | +} |
| 120 | + |
0 commit comments